1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37 #include <yaz/xmalloc.h>
45 struct passwd_entry *next;
49 struct passwd_entry *entries;
52 Passwd_db passwd_db_open (void)
54 struct passwd_db *p = (struct passwd_db *) xmalloc (sizeof(*p));
59 static int get_entry (const char **p, char *dst, int max)
62 while ((*p)[i] != ':' && (*p)[i])
75 static int passwd_db_file_int(Passwd_db db, const char *fname,
80 f = fopen (fname, "r");
83 while (fgets (buf, sizeof(buf)-1, f))
85 struct passwd_entry *pe;
90 if ((p = strchr (buf, '\n')))
92 get_entry (&cp, name, 128);
93 get_entry (&cp, des, 128);
95 pe = (struct passwd_entry *) xmalloc (sizeof(*pe));
96 pe->name = xstrdup (name);
97 pe->des = xstrdup (des);
98 pe->encrypt_flag = encrypt_flag;
99 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 yaz_log (YLOG_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;
133 for (pe = db->entries; pe; pe = pe->next)
134 if (user && !strcmp (user, pe->name))
140 if (pe->encrypt_flag)
145 if (strlen (pe->des) < 3)
148 if (pe->des[0] != '$') /* Not MD5? (assume DES) */
150 if (strlen(pass) > 8) /* maximum key length is 8 */
153 des_try = crypt (pass, pe->des);
156 if (strcmp (des_try, pe->des))
166 if (strcmp (pe->des, pass))
172 int passwd_db_file_crypt(Passwd_db db, const char *fname)
175 return passwd_db_file_int(db, fname, 1);
181 int passwd_db_file_plain(Passwd_db db, const char *fname)
183 return passwd_db_file_int(db, fname, 0);
189 * c-file-style: "Stroustrup"
190 * indent-tabs-mode: nil
192 * vim: shiftwidth=4 tabstop=8 expandtab