source: trunk/minix/commands/httpd/pass.c@ 21

Last change on this file since 21 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 3.7 KB
RevLine 
[9]1/* pass.c
2 *
3 * This file is part of httpd.
4 *
5 * 07/07/1996 Initial Release Michael Temari <Michael@TemWare.Com>
6 * 12/29/2002 Michael Temari <Michael@TemWare.Com>
7 *
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
20
21#define STD_PASSWD_FILE "/etc/passwd"
22
23#include "pass.h"
24
25static char buffer[1024];
26static char *pwduser;
27static char *pwdpass;
28static char *pwde[4];
29
30_PROTOTYPE(static int getuser, (char *pwdfile, char *user));
31
32static int getuser(pwdfile, user)
33char *pwdfile;
34char *user;
35{
36FILE *fp;
37char *p;
38int i;
39
40 if((fp = fopen(pwdfile, "r")) == (FILE *)NULL)
41 return(-1);
42
43 for(i = 0; i < 4; i ++) pwde[i] = "";
44
45 while(1) {
46 if(fgets(buffer, sizeof(buffer), fp) == (char *)NULL) {
47 fclose(fp);
48 return(-1);
49 }
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);
63 }
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;
72 }
73 }
74 fclose(fp);
75
76 return(0);
77 }
78}
79
80int passfile(pwdfile)
81char *pwdfile;
82{
83FILE *fp;
84
85 if(!strcmp(pwdfile, STD_PASSWD_FILE))
86 return(0);
87
88 if((fp = fopen(pwdfile, "r")) == (FILE *)NULL)
89 return(-1);
90
91 fclose(fp);
92
93 return(0);
94}
95
96int passuser(pwdfile, user)
97char *pwdfile;
98char *user;
99{
100 if(!strcmp(pwdfile, STD_PASSWD_FILE))
101 if(getpwnam(user) == (struct passwd *)NULL)
102 return(-1);
103 else
104 return(0);
105
106 return(getuser(pwdfile, user));
107}
108
109int passnone(pwdfile, user)
110char *pwdfile;
111char *user;
112{
113struct passwd *pwd;
114
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);
123
124 if(getuser(pwdfile, user))
125 return(-1);
126
127 if(!strcmp(pwdpass, crypt("", pwdpass)))
128 return(-1);
129 else
130 return(0);
131}
132
133int passpass(pwdfile, user, pass)
134char *pwdfile;
135char *user;
136char *pass;
137{
138struct passwd *pwd;
139
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);
148
149 if(getuser(pwdfile, user))
150 return(-1);
151
152 if(strcmp(pwdpass, crypt(pass, pwdpass)))
153 return(-1);
154 else
155 return(0);
156}
157
158int passadd(pwdfile, user, pass, e1, e2, e3, e4)
159char *pwdfile;
160char *user;
161char *pass;
162char *e1;
163char *e2;
164char *e3;
165char *e4;
166{
167FILE *fp;
168time_t salt;
169char sl[2];
170int cn;
171char *ee1;
172char *ee2;
173char *ee3;
174char *ee4;
175
176
177 if(pwdfile == (char *)NULL ||
178 user == (char *)NULL ||
179 pass == (char *)NULL)
180 return(PASS_ERROR);
181
182 if(!strcmp(pwdfile, STD_PASSWD_FILE))
183 return(PASS_ERROR);
184
185 if(!getuser(pwdfile, user))
186 return(PASS_USEREXISTS);
187
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;
194 }
195
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;
200
201 /* XXX need to add locking mechanics to add new user */
202
203 if((fp = fopen(pwdfile, "a")) == (FILE *)NULL)
204 return(PASS_ERROR);
205
206 fprintf(fp, "%s:%s:%s:%s:%s:%s\n", user, crypt(pass, sl), ee1, ee2, ee3, ee4);
207
208 fclose(fp);
209
210 /* XXX need to add unlocking mechanics to add new user */
211
212 return(PASS_GOOD);
213}
Note: See TracBrowser for help on using the repository browser.