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