1 /* $Id: passwddb.c,v 1.13 2006-05-10 08:13:46 adam Exp $
2 Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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)
140 if (strlen (pe->des) < 3)
144 memcpy (salt, pe->des, 2);
146 des_try = crypt (pass, salt);
147 if (strcmp (des_try, pe->des))
155 if (strcmp (pe->des, pass))
161 int passwd_db_file_crypt(Passwd_db db, const char *fname)
164 return passwd_db_file_int(db, fname, 1);
170 int passwd_db_file_plain(Passwd_db db, const char *fname)
172 return passwd_db_file_int(db, fname, 0);
178 * indent-tabs-mode: nil
180 * vim: shiftwidth=4 tabstop=8 expandtab