1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2010 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
34 #include <yaz/xmalloc.h>
42 struct passwd_entry *next;
46 struct passwd_entry *entries;
49 Passwd_db passwd_db_open (void)
51 struct passwd_db *p = (struct passwd_db *) xmalloc (sizeof(*p));
56 static int get_entry (const char **p, char *dst, int max)
59 while ((*p)[i] != ':' && (*p)[i])
72 static int passwd_db_file_int(Passwd_db db, const char *fname,
77 f = fopen (fname, "r");
80 while (fgets (buf, sizeof(buf)-1, f))
82 struct passwd_entry *pe;
87 if ((p = strchr (buf, '\n')))
89 get_entry (&cp, name, 128);
90 get_entry (&cp, des, 128);
92 pe = (struct passwd_entry *) xmalloc (sizeof(*pe));
93 pe->name = xstrdup (name);
94 pe->des = xstrdup (des);
95 pe->encrypt_flag = encrypt_flag;
96 pe->next = db->entries;
103 void passwd_db_close(Passwd_db db)
105 struct passwd_entry *pe = db->entries;
108 struct passwd_entry *pe_next = pe->next;
118 void passwd_db_show(Passwd_db db)
120 struct passwd_entry *pe;
121 for (pe = db->entries; pe; pe = pe->next)
122 yaz_log (YLOG_LOG,"%s:%s", pe->name, pe->des);
125 int passwd_db_auth(Passwd_db db, const char *user, const char *pass)
127 struct passwd_entry *pe;
130 for (pe = db->entries; pe; pe = pe->next)
131 if (user && !strcmp (user, pe->name))
137 if (pe->encrypt_flag)
142 if (strlen (pe->des) < 3)
145 if (pe->des[0] != '$') /* Not MD5? (assume DES) */
147 if (strlen(pass) > 8) /* maximum key length is 8 */
150 des_try = crypt (pass, pe->des);
153 if (strcmp (des_try, pe->des))
163 if (strcmp (pe->des, pass))
169 int passwd_db_file_crypt(Passwd_db db, const char *fname)
172 return passwd_db_file_int(db, fname, 1);
178 int passwd_db_file_plain(Passwd_db db, const char *fname)
180 return passwd_db_file_int(db, fname, 0);
186 * c-file-style: "Stroustrup"
187 * indent-tabs-mode: nil
189 * vim: shiftwidth=4 tabstop=8 expandtab