1 /* $Id: passwddb.c,v 1.7.2.3 2006-08-14 10:39:24 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <yaz/xmalloc.h>
44 struct passwd_entry *next;
48 struct passwd_entry *entries;
51 Passwd_db passwd_db_open (void)
53 struct passwd_db *p = (struct passwd_db *) xmalloc (sizeof(*p));
58 static int get_entry (const char **p, char *dst, int max)
61 while ((*p)[i] != ':' && (*p)[i])
74 static int passwd_db_file_int(Passwd_db db, const char *fname,
79 f = fopen (fname, "r");
82 while (fgets (buf, sizeof(buf)-1, f))
84 struct passwd_entry *pe;
89 if ((p = strchr (buf, '\n')))
91 get_entry (&cp, name, 128);
92 get_entry (&cp, des, 128);
94 pe = (struct passwd_entry *) xmalloc (sizeof(*pe));
95 pe->name = xstrdup (name);
96 pe->encrypt_flag = encrypt_flag;
97 pe->des = xstrdup (des);
98 pe->next = db->entries;
106 void passwd_db_close (Passwd_db db)
108 struct passwd_entry *pe = db->entries;
111 struct passwd_entry *pe_next = pe->next;
121 void passwd_db_show (Passwd_db db)
123 struct passwd_entry *pe;
124 for (pe = db->entries; pe; pe = pe->next)
125 logf (LOG_LOG,"%s:%s", pe->name, pe->des);
128 int passwd_db_auth (Passwd_db db, const char *user, const char *pass)
130 struct passwd_entry *pe;
131 for (pe = db->entries; pe; pe = pe->next)
132 if (user && !strcmp (user, pe->name))
136 if (pe->encrypt_flag)
141 if (strlen (pe->des) < 3)
145 memcpy (salt, pe->des, 2);
147 des_try = crypt (pass, salt);
148 if (strcmp (des_try, pe->des))
156 if (strcmp (pe->des, pass))
162 int passwd_db_file_crypt(Passwd_db db, const char *fname)
165 return passwd_db_file_int(db, fname, 1);
171 int passwd_db_file_plain(Passwd_db db, const char *fname)
173 return passwd_db_file_int(db, fname, 0);