Bump copyright year
[idzebra-moved-to-github.git] / util / passwddb.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 2004-2013 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17
18 */
19
20
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
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 <assert.h>
36 #include <yaz/log.h>
37 #include <yaz/xmalloc.h>
38
39 #include <passwddb.h>
40
41 struct passwd_entry {
42     int encrypt_flag;
43     char *name;
44     char *des;
45     struct passwd_entry *next;
46 };
47
48 struct passwd_db {
49     struct passwd_entry *entries;
50 };
51
52 Passwd_db passwd_db_open (void)
53 {
54     struct passwd_db *p = (struct passwd_db *) xmalloc (sizeof(*p));
55     p->entries = 0;
56     return p;
57 }
58
59 static int get_entry (const char **p, char *dst, int max)
60 {
61     int i = 0;
62     while ((*p)[i] != ':' && (*p)[i])
63         i++;
64     if (i >= max)
65         i = max-1;
66     if (i)
67         memcpy (dst, *p, i);
68     dst[i] = '\0';
69     *p += i;
70     if (*p)
71         (*p)++;
72     return i;
73 }
74
75 static int passwd_db_file_int(Passwd_db db, const char *fname,
76                               int encrypt_flag)
77 {
78     FILE *f;
79     char buf[1024];
80     f = fopen (fname, "r");
81     if (!f)
82         return -1;
83     while (fgets (buf, sizeof(buf)-1, f))
84     {
85         struct passwd_entry *pe;
86         char name[128];
87         char des[128];
88         char *p;
89         const char *cp = buf;
90         if ((p = strchr (buf, '\n')))
91             *p = '\0';
92         get_entry (&cp, name, 128);
93         get_entry (&cp, des, 128);
94
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;
100         db->entries = pe;
101     }
102     fclose (f);
103     return 0;
104 }
105
106 void passwd_db_close(Passwd_db db)
107 {
108     struct passwd_entry *pe = db->entries;
109     while (pe)
110     {
111         struct passwd_entry *pe_next = pe->next;
112
113         xfree (pe->name);
114         xfree (pe->des);
115         xfree (pe);
116         pe = pe_next;
117     }
118     xfree (db);
119 }
120
121 void passwd_db_show(Passwd_db db)
122 {
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);
126 }
127
128 int passwd_db_auth(Passwd_db db, const char *user, const char *pass)
129 {
130     struct passwd_entry *pe;
131
132     assert(db);
133     for (pe = db->entries; pe; pe = pe->next)
134         if (user && !strcmp (user, pe->name))
135             break;
136     if (!pe)
137         return -1;
138     if (!pass)
139         return -2;
140     if (pe->encrypt_flag)
141     {
142 #if HAVE_CRYPT_H
143         const char *des_try;
144         assert(pe->des);
145         if (strlen (pe->des) < 3)
146             return -3;
147
148         if (pe->des[0] != '$') /* Not MD5? (assume DES) */
149         {
150             if (strlen(pass) > 8) /* maximum key length is 8 */
151                 return -2;
152         }
153         des_try = crypt (pass, pe->des);
154
155         assert(des_try);
156         if (strcmp (des_try, pe->des))
157             return -2;
158 #else
159         return -2;
160 #endif
161     }
162     else
163     {
164         assert(pass);
165         assert(pe->des);
166         if (strcmp (pe->des, pass))
167             return -2;
168     }
169     return 0;
170 }
171
172 int passwd_db_file_crypt(Passwd_db db, const char *fname)
173 {
174 #if HAVE_CRYPT_H
175     return passwd_db_file_int(db, fname, 1);
176 #else
177     return -1;
178 #endif
179 }
180
181 int passwd_db_file_plain(Passwd_db db, const char *fname)
182 {
183     return passwd_db_file_int(db, fname, 0);
184 }
185
186 /*
187  * Local variables:
188  * c-basic-offset: 4
189  * c-file-style: "Stroustrup"
190  * indent-tabs-mode: nil
191  * End:
192  * vim: shiftwidth=4 tabstop=8 expandtab
193  */
194