Improved installation. Updated for inclusion of YAZ header files.
[idzebra-moved-to-github.git] / util / passwddb.c
1 /*
2  * Copyright (C) 1998-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: passwddb.c,v $
7  * Revision 1.6  1999-11-30 13:48:04  adam
8  * Improved installation. Updated for inclusion of YAZ header files.
9  *
10  * Revision 1.5  1999/05/26 07:49:14  adam
11  * C++ compilation.
12  *
13  * Revision 1.4  1999/02/02 14:51:39  adam
14  * Updated WIN32 code specific sections. Changed header.
15  *
16  * Revision 1.3  1998/06/25 19:16:32  adam
17  * Minor changes to Visual C++ project.
18  *
19  * Revision 1.2  1998/06/25 09:55:51  adam
20  * Minor changes - fixex headers.
21  *
22  */
23 #ifdef WIN32
24 #else
25 #include <unistd.h>
26 #endif
27 #include <string.h>
28 #include <stdio.h>
29
30 #ifndef USE_CRYPT
31 #define USE_CRYPT 0
32 #endif
33
34 #if USE_CRYPT
35 #include <crypt.h>
36 #endif
37
38 #include <yaz/log.h>
39 #include <yaz/xmalloc.h>
40
41 #include <passwddb.h>
42
43 struct passwd_entry {
44         char *name;
45         char *des;
46         struct passwd_entry *next;
47 };
48
49 struct passwd_db {
50         struct passwd_entry *entries;
51 };
52
53 Passwd_db passwd_db_open (void)
54 {
55         struct passwd_db *p = (struct passwd_db *) xmalloc (sizeof(*p));
56         p->entries = 0;
57         return p;
58 }
59
60 static int get_entry (const char **p, char *dst, int max)
61 {       
62         int i = 0;
63         while ((*p)[i] != ':' && (*p)[i])
64                 i++;
65         if (i >= max)
66                 i = max-1;
67         if (i)
68                 memcpy (dst, *p, i);
69         dst[i] = '\0';
70         *p += i;
71         if (*p)
72                 (*p)++;
73         return i;
74 }
75
76 int passwd_db_file (Passwd_db db, const char *fname)
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->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                 logf (LOG_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 #if USE_CRYPT
131         char salt[3];
132         const char *des_try;
133 #endif
134         for (pe = db->entries; pe; pe = pe->next)
135                 if (user && !strcmp (user, pe->name))
136                         break;
137         if (!pe)
138                 return -1;
139 #if USE_CRYPT
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         if (strcmp (pe->des, pass))
151                 return -2;
152 #endif
153         return 0;       
154 }
155