| 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 |
|
|---|
| 25 | static char buffer[1024];
|
|---|
| 26 | static char *pwduser;
|
|---|
| 27 | static char *pwdpass;
|
|---|
| 28 | static char *pwde[4];
|
|---|
| 29 |
|
|---|
| 30 | _PROTOTYPE(static int getuser, (char *pwdfile, char *user));
|
|---|
| 31 |
|
|---|
| 32 | static int getuser(pwdfile, user)
|
|---|
| 33 | char *pwdfile;
|
|---|
| 34 | char *user;
|
|---|
| 35 | {
|
|---|
| 36 | FILE *fp;
|
|---|
| 37 | char *p;
|
|---|
| 38 | int 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 |
|
|---|
| 80 | int passfile(pwdfile)
|
|---|
| 81 | char *pwdfile;
|
|---|
| 82 | {
|
|---|
| 83 | FILE *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 |
|
|---|
| 96 | int passuser(pwdfile, user)
|
|---|
| 97 | char *pwdfile;
|
|---|
| 98 | char *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 |
|
|---|
| 109 | int passnone(pwdfile, user)
|
|---|
| 110 | char *pwdfile;
|
|---|
| 111 | char *user;
|
|---|
| 112 | {
|
|---|
| 113 | struct 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 |
|
|---|
| 133 | int passpass(pwdfile, user, pass)
|
|---|
| 134 | char *pwdfile;
|
|---|
| 135 | char *user;
|
|---|
| 136 | char *pass;
|
|---|
| 137 | {
|
|---|
| 138 | struct 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 |
|
|---|
| 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;
|
|---|
| 166 | {
|
|---|
| 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;
|
|---|
| 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 | }
|
|---|