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