Added lance entry to drivers.conf.
[minix3-old.git] / commands / httpd / pass.c
blobc7011322ec6bad42fa82d56b690b1e7f22a97186
1 /* pass.c
3 * This file is part of httpd.
5 * 07/07/1996 Initial Release Michael Temari <Michael@TemWare.Com>
6 * 12/29/2002 Michael Temari <Michael@TemWare.Com>
8 */
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <time.h>
16 #include <pwd.h>
17 #ifdef _MINIX
18 #include <minix/minlib.h>
19 #endif
21 #define STD_PASSWD_FILE "/etc/passwd"
23 #include "pass.h"
25 static char buffer[1024];
26 static char *pwduser;
27 static char *pwdpass;
28 static char *pwde[4];
30 _PROTOTYPE(static int getuser, (char *pwdfile, char *user));
32 static int getuser(pwdfile, user)
33 char *pwdfile;
34 char *user;
36 FILE *fp;
37 char *p;
38 int i;
40 if((fp = fopen(pwdfile, "r")) == (FILE *)NULL)
41 return(-1);
43 for(i = 0; i < 4; i ++) pwde[i] = "";
45 while(1) {
46 if(fgets(buffer, sizeof(buffer), fp) == (char *)NULL) {
47 fclose(fp);
48 return(-1);
50 p = buffer;
51 pwduser = p;
52 while(*p && *p != ':') p++;
53 if(*p != ':') continue;
54 *p++ = '\0';
55 if(strcmp(pwduser, user)) continue;
56 pwdpass = p;
57 while(*p && *p != ':' && *p != '\r' && *p != '\n') p++;
58 if(*p == ':')
59 *p++ = '\0';
60 else {
61 if(*p) *p = '\0';
62 fclose(fp);
64 for(i = 0; i < 4; i++) {
65 pwde[i] = p;
66 while(*p && *p != ':' && *p != '\r' && *p != '\n') p++;
67 if(*p == ':')
68 *p++ = '\0';
69 else {
70 if(*p) *p = '\0';
71 break;
74 fclose(fp);
76 return(0);
80 int passfile(pwdfile)
81 char *pwdfile;
83 FILE *fp;
85 if(!strcmp(pwdfile, STD_PASSWD_FILE))
86 return(0);
88 if((fp = fopen(pwdfile, "r")) == (FILE *)NULL)
89 return(-1);
91 fclose(fp);
93 return(0);
96 int passuser(pwdfile, user)
97 char *pwdfile;
98 char *user;
100 if(!strcmp(pwdfile, STD_PASSWD_FILE))
101 if(getpwnam(user) == (struct passwd *)NULL)
102 return(-1);
103 else
104 return(0);
106 return(getuser(pwdfile, user));
109 int passnone(pwdfile, user)
110 char *pwdfile;
111 char *user;
113 struct passwd *pwd;
115 if(!strcmp(pwdfile, STD_PASSWD_FILE))
116 if((pwd = getpwnam(user)) == (struct passwd *)NULL)
117 return(-1);
118 else
119 if(!strcmp(pwd->pw_passwd, crypt("", pwd->pw_passwd)))
120 return(-1);
121 else
122 return(0);
124 if(getuser(pwdfile, user))
125 return(-1);
127 if(!strcmp(pwdpass, crypt("", pwdpass)))
128 return(-1);
129 else
130 return(0);
133 int passpass(pwdfile, user, pass)
134 char *pwdfile;
135 char *user;
136 char *pass;
138 struct passwd *pwd;
140 if(!strcmp(pwdfile, STD_PASSWD_FILE))
141 if((pwd = getpwnam(user)) == (struct passwd *)NULL)
142 return(-1);
143 else
144 if(strcmp(pwd->pw_passwd, crypt(pass, pwd->pw_passwd)))
145 return(-1);
146 else
147 return(0);
149 if(getuser(pwdfile, user))
150 return(-1);
152 if(strcmp(pwdpass, crypt(pass, pwdpass)))
153 return(-1);
154 else
155 return(0);
158 int passadd(pwdfile, user, pass, e1, e2, e3, e4)
159 char *pwdfile;
160 char *user;
161 char *pass;
162 char *e1;
163 char *e2;
164 char *e3;
165 char *e4;
167 FILE *fp;
168 time_t salt;
169 char sl[2];
170 int cn;
171 char *ee1;
172 char *ee2;
173 char *ee3;
174 char *ee4;
177 if(pwdfile == (char *)NULL ||
178 user == (char *)NULL ||
179 pass == (char *)NULL)
180 return(PASS_ERROR);
182 if(!strcmp(pwdfile, STD_PASSWD_FILE))
183 return(PASS_ERROR);
185 if(!getuser(pwdfile, user))
186 return(PASS_USEREXISTS);
188 time(&salt);
189 sl[0] = (salt & 077) + '.';
190 sl[1] = ((salt >> 6) & 077) + '.';
191 for (cn = 0; cn < 2; cn++) {
192 if (sl[cn] > '9') sl[cn] += 7;
193 if (sl[cn] > 'Z') sl[cn] += 6;
196 if(e1 == (char *)NULL) ee1 = ""; else ee1 = e1;
197 if(e2 == (char *)NULL) ee2 = ""; else ee2 = e2;
198 if(e3 == (char *)NULL) ee3 = ""; else ee3 = e3;
199 if(e4 == (char *)NULL) ee4 = ""; else ee4 = e4;
201 /* XXX need to add locking mechanics to add new user */
203 if((fp = fopen(pwdfile, "a")) == (FILE *)NULL)
204 return(PASS_ERROR);
206 fprintf(fp, "%s:%s:%s:%s:%s:%s\n", user, crypt(pass, sl), ee1, ee2, ee3, ee4);
208 fclose(fp);
210 /* XXX need to add unlocking mechanics to add new user */
212 return(PASS_GOOD);