Update copyright year + FSF address
[idzebra-moved-to-github.git] / util / passwddb.c
1 /* $Id: passwddb.c,v 1.14 2006-08-14 10:40:34 adam Exp $
2    Copyright (C) 1995-2006
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20
21 */
22
23
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27
28 #include <string.h>
29 #include <stdio.h>
30
31 #if HAVE_CRYPT_H
32 #include <crypt.h>
33 #endif
34
35 #include <yaz/log.h>
36 #include <yaz/xmalloc.h>
37
38 #include <passwddb.h>
39
40 struct passwd_entry {
41     int encrypt_flag;
42     char *name;
43     char *des;
44     struct passwd_entry *next;
45 };
46
47 struct passwd_db {
48     struct passwd_entry *entries;
49 };
50
51 Passwd_db passwd_db_open (void)
52 {
53     struct passwd_db *p = (struct passwd_db *) xmalloc (sizeof(*p));
54     p->entries = 0;
55     return p;
56 }
57
58 static int get_entry (const char **p, char *dst, int max)
59 {       
60     int i = 0;
61     while ((*p)[i] != ':' && (*p)[i])
62         i++;
63     if (i >= max)
64         i = max-1;
65     if (i)
66         memcpy (dst, *p, i);
67     dst[i] = '\0';
68     *p += i;
69     if (*p)
70         (*p)++;
71     return i;
72 }
73
74 static int passwd_db_file_int(Passwd_db db, const char *fname,
75                               int encrypt_flag)
76 {
77     FILE *f;
78     char buf[1024];
79     f = fopen (fname, "r");
80     if (!f)
81         return -1;
82     while (fgets (buf, sizeof(buf)-1, f))
83     {
84         struct passwd_entry *pe;
85         char name[128];
86         char des[128];
87         char *p;
88         const char *cp = buf;
89         if ((p = strchr (buf, '\n')))
90             *p = '\0';
91         get_entry (&cp, name, 128);
92         get_entry (&cp, des, 128);
93
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;
99         db->entries = pe;
100     }
101     fclose (f);
102     return 0;
103 }
104
105 void passwd_db_close(Passwd_db db)
106 {
107     struct passwd_entry *pe = db->entries;
108     while (pe)
109     {
110         struct passwd_entry *pe_next = pe->next;
111         
112         xfree (pe->name);
113         xfree (pe->des);
114         xfree (pe);
115         pe = pe_next;
116     }
117     xfree (db);
118 }
119
120 void passwd_db_show(Passwd_db db)
121 {
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);
125 }
126
127 int passwd_db_auth(Passwd_db db, const char *user, const char *pass)
128 {
129     struct passwd_entry *pe;
130     for (pe = db->entries; pe; pe = pe->next)
131         if (user && !strcmp (user, pe->name))
132             break;
133     if (!pe)
134         return -1;
135     if (pe->encrypt_flag)
136     {
137 #if HAVE_CRYPT_H
138         char salt[3];
139         const char *des_try;
140         if (strlen (pe->des) < 3)
141             return -3;
142         if (!pass)
143             return -2;
144         memcpy (salt, pe->des, 2);
145         salt[2] = '\0'; 
146         des_try = crypt (pass, salt);
147         if (strcmp (des_try, pe->des))
148             return -2;
149 #else
150         return -2;
151 #endif
152     }
153     else
154     {
155         if (strcmp (pe->des, pass))
156             return -2;
157     }
158     return 0;   
159 }
160
161 int passwd_db_file_crypt(Passwd_db db, const char *fname)
162 {
163 #if HAVE_CRYPT_H
164     return passwd_db_file_int(db, fname, 1);
165 #else
166     return -1;
167 #endif
168 }
169
170 int passwd_db_file_plain(Passwd_db db, const char *fname)
171 {
172     return passwd_db_file_int(db, fname, 0);
173 }
174
175 /*
176  * Local variables:
177  * c-basic-offset: 4
178  * indent-tabs-mode: nil
179  * End:
180  * vim: shiftwidth=4 tabstop=8 expandtab
181  */
182