Fixed bug #685: Optimize xelm/melm matching. Indexing the Koha collection
[idzebra-moved-to-github.git] / util / passwddb.c
1 /* $Id: passwddb.c,v 1.7.2.3 2006-08-14 10:39:24 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 #ifdef WIN32
25 #else
26 #include <unistd.h>
27 #endif
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     char *name;
42     char *des;
43     int encrypt_flag;
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->encrypt_flag = encrypt_flag;
97         pe->des = xstrdup (des);
98         pe->next = db->entries;
99         db->entries = pe;
100     }
101     fclose (f);
102     return 0;
103 }
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         logf (LOG_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     for (pe = db->entries; pe; pe = pe->next)
132         if (user && !strcmp (user, pe->name))
133             break;
134     if (!pe)
135         return -1;
136     if (pe->encrypt_flag)
137     {
138 #if HAVE_CRYPT_H
139         char salt[3];
140         const char *des_try;
141         if (strlen (pe->des) < 3)
142             return -3;
143         if (!pass)
144             return -2;
145         memcpy (salt, pe->des, 2);
146         salt[2] = '\0'; 
147         des_try = crypt (pass, salt);
148         if (strcmp (des_try, pe->des))
149             return -2;
150 #else
151         return -2;
152 #endif
153     }
154     else
155     {
156         if (strcmp (pe->des, pass))
157             return -2;
158     }
159     return 0;   
160 }
161
162 int passwd_db_file_crypt(Passwd_db db, const char *fname)
163 {
164 #if HAVE_CRYPT_H
165     return passwd_db_file_int(db, fname, 1);
166 #else
167     return -1;
168 #endif
169 }
170
171 int passwd_db_file_plain(Passwd_db db, const char *fname)
172 {
173     return passwd_db_file_int(db, fname, 0);
174 }
175