1 /* $Id: passwddb.c,v 1.16 2007-01-15 15:10:26 adam Exp $
2 Copyright (C) 1995-2007
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->des = xstrdup (des);
97 pe->encrypt_flag = encrypt_flag;
98 pe->next = db->entries;
105 void passwd_db_close(Passwd_db db)
107 struct passwd_entry *pe = db->entries;
110 struct passwd_entry *pe_next = pe->next;
120 void passwd_db_show(Passwd_db db)
122 struct passwd_entry *pe;
123 for (pe = db->entries; pe; pe = pe->next)
124 yaz_log (YLOG_LOG,"%s:%s", pe->name, pe->des);
127 int passwd_db_auth(Passwd_db db, const char *user, const char *pass)
129 struct passwd_entry *pe;
130 for (pe = db->entries; pe; pe = pe->next)
131 if (user && !strcmp (user, pe->name))
135 if (pe->encrypt_flag)
139 if (strlen (pe->des) < 3)
144 if (pe->des[0] != '$') /* Not MD5? (assume DES) */
146 if (strlen(pass) > 8) /* maximum key length is 8 */
149 des_try = crypt (pass, pe->des);
151 if (strcmp (des_try, pe->des))
159 if (strcmp (pe->des, pass))
165 int passwd_db_file_crypt(Passwd_db db, const char *fname)
168 return passwd_db_file_int(db, fname, 1);
174 int passwd_db_file_plain(Passwd_db db, const char *fname)
176 return passwd_db_file_int(db, fname, 0);
182 * indent-tabs-mode: nil
184 * vim: shiftwidth=4 tabstop=8 expandtab