cbe1485a998390ba0b2141400ffdfabc5b13d383
[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.4  1999-02-02 14:51:39  adam
8  * Updated WIN32 code specific sections. Changed header.
9  *
10  * Revision 1.3  1998/06/25 19:16:32  adam
11  * Minor changes to Visual C++ project.
12  *
13  * Revision 1.2  1998/06/25 09:55:51  adam
14  * Minor changes - fixex headers.
15  *
16  */
17 #ifdef WIN32
18 #else
19 #include <unistd.h>
20 #endif
21 #include <string.h>
22 #include <stdio.h>
23
24 #ifndef USE_CRYPT
25 #define USE_CRYPT 0
26 #endif
27
28 #if USE_CRYPT
29 #include <crypt.h>
30 #endif
31
32 #include <log.h>
33 #include <xmalloc.h>
34
35 #include <passwddb.h>
36
37 struct passwd_entry {
38         char *name;
39         char *des;
40         struct passwd_entry *next;
41 };
42
43 struct passwd_db {
44         struct passwd_entry *entries;
45 };
46
47 Passwd_db passwd_db_open (void)
48 {
49         struct passwd_db *p = xmalloc (sizeof(*p));
50         p->entries = 0;
51         return p;
52 }
53
54 static int get_entry (const char **p, char *dst, int max)
55 {       
56         int i = 0;
57         while ((*p)[i] != ':' && (*p)[i])
58                 i++;
59         if (i >= max)
60                 i = max-1;
61         if (i)
62                 memcpy (dst, *p, i);
63         dst[i] = '\0';
64         *p += i;
65         if (*p)
66                 (*p)++;
67         return i;
68 }
69
70 int passwd_db_file (Passwd_db db, const char *fname)
71 {
72         FILE *f;
73         char buf[1024];
74         f = fopen (fname, "r");
75         if (!f)
76                 return -1;
77         while (fgets (buf, sizeof(buf)-1, f))
78         {
79                 struct passwd_entry *pe;
80                 char name[128];
81                 char des[128];
82                 char *p;
83                 const char *cp = buf;
84                 if ((p = strchr (buf, '\n')))
85                         *p = '\0';
86                 get_entry (&cp, name, 128);
87                 get_entry (&cp, des, 128);
88
89                 pe = xmalloc (sizeof(*pe));
90                 pe->name = xstrdup (name);
91                 pe->des = xstrdup (des);
92                 pe->next = db->entries;
93                 db->entries = pe;
94         }
95         fclose (f);
96         return 0;
97 }
98
99 void passwd_db_close (Passwd_db db)
100 {
101         struct passwd_entry *pe = db->entries;
102         while (pe)
103         {
104                 struct passwd_entry *pe_next = pe->next;
105         
106                 xfree (pe->name);
107                 xfree (pe->des);
108                 xfree (pe);
109                 pe = pe_next;
110         }
111         xfree (db);
112 }
113
114 void passwd_db_show (Passwd_db db)
115 {
116         struct passwd_entry *pe;
117         for (pe = db->entries; pe; pe = pe->next)
118                 logf (LOG_LOG,"%s:%s", pe->name, pe->des);
119 }
120
121 int passwd_db_auth (Passwd_db db, const char *user, const char *pass)
122 {
123         struct passwd_entry *pe;
124 #if USE_CRYPT
125         char salt[3];
126         const char *des_try;
127 #endif
128         for (pe = db->entries; pe; pe = pe->next)
129                 if (user && !strcmp (user, pe->name))
130                         break;
131         if (!pe)
132                 return -1;
133 #if USE_CRYPT
134         if (strlen (pe->des) < 3)
135                 return -3;
136         if (!pass)
137             return -2;
138         memcpy (salt, pe->des, 2);
139         salt[2] = '\0'; 
140         des_try = crypt (pass, salt);
141         if (strcmp (des_try, pe->des))
142                 return -2;
143 #else
144         if (strcmp (pe->des, pass))
145                 return -2;
146 #endif
147         return 0;       
148 }
149