]> arthur.barton.de Git - netatalk.git/blob - bin/afppasswd/afppasswd.c
Merge 2-2
[netatalk.git] / bin / afppasswd / afppasswd.c
1 /* 
2  * Copyright 1999 (c) Adrian Sun (asun@u.washington.edu)
3  * All Rights Reserved. See COPYRIGHT.
4  *
5  * format of the password file:
6  * name:****************:****************:********
7  *      password         last login date  failed usage count
8  *
9  * ***'s are illegal. they're just place holders for hex values. hex
10  * values that represent actual numbers are in network byte order.
11  *
12  * last login date is currently a 4-byte number representing seconds
13  * since 1970 (UTC). there's enough space to extend it to 8 bytes.
14  *
15  * the last two fields aren't currently used by the randnum uams.
16  *
17  * root syntax: afppasswd [-c] [-a] [-p path] [-f] [username]
18  * user syntax: afppasswd 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif /* HAVE_CONFIG_H */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <ctype.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/param.h>
34 #include <fcntl.h>
35 #include <pwd.h>
36 #include <arpa/inet.h>
37
38 #include <des.h>
39
40 #ifdef USE_CRACKLIB
41 #include <crack.h>
42 #endif /* USE_CRACKLIB */
43
44 #define OPT_ISROOT  (1 << 0)
45 #define OPT_CREATE  (1 << 1)
46 #define OPT_FORCE   (1 << 2)
47 #define OPT_ADDUSER (1 << 3)
48 #define OPT_NOCRACK (1 << 4)
49
50 #define PASSWD_ILLEGAL '*'
51
52 #define FORMAT  ":****************:****************:********\n"
53 #define FORMAT_LEN 44
54 #define OPTIONS "cafnu:p:"
55 #define UID_START 100
56
57 #define HEXPASSWDLEN 16
58 #define PASSWDLEN 8
59
60 static char buf[MAXPATHLEN + 1];
61
62 /* if newpwd is null, convert buf from hex to binary. if newpwd isn't
63  * null, convert newpwd to hex and save it in buf. */
64 #define unhex(x)  (isdigit(x) ? (x) - '0' : toupper(x) + 10 - 'A')
65 static void convert_passwd(char *buf, char *newpwd, const int keyfd)
66 {
67   uint8_t key[HEXPASSWDLEN];
68   Key_schedule schedule;
69   unsigned int i, j;
70
71   if (!newpwd) {
72     /* convert to binary */
73     for (i = j = 0; i < sizeof(key); i += 2, j++)
74       buf[j] = (unhex(buf[i]) << 4) | unhex(buf[i + 1]);
75     if (j <= DES_KEY_SZ)
76       memset(buf + j, 0, sizeof(key) - j);
77   }
78
79   if (keyfd > -1) {
80     lseek(keyfd, 0, SEEK_SET);
81     read(keyfd, key, sizeof(key));
82     /* convert to binary */
83     for (i = j = 0; i < sizeof(key); i += 2, j++)
84       key[j] = (unhex(key[i]) << 4) | unhex(key[i + 1]);
85     if (j <= DES_KEY_SZ)
86       memset(key + j, 0, sizeof(key) - j);
87     key_sched((C_Block *) key, schedule);
88     memset(key, 0, sizeof(key));   
89     if (newpwd) {
90         ecb_encrypt((C_Block *) newpwd, (C_Block *) newpwd, schedule,
91                     DES_ENCRYPT);
92     } else {
93       /* decrypt the password */
94       ecb_encrypt((C_Block *) buf, (C_Block *) buf, schedule, DES_DECRYPT);
95     }
96     memset(&schedule, 0, sizeof(schedule));      
97   }
98
99   if (newpwd) {
100     const unsigned char hextable[] = "0123456789ABCDEF";
101
102     /* convert to hex */
103     for (i = j = 0; i < DES_KEY_SZ; i++, j += 2) {
104       buf[j] = hextable[(newpwd[i] & 0xF0) >> 4];
105       buf[j + 1] = hextable[newpwd[i] & 0x0F];
106     }
107   }
108 }
109
110 /* this matches the code in uam_randnum.c */
111 static int update_passwd(const char *path, const char *name, int flags)
112 {
113   char password[PASSWDLEN + 1], *p, *passwd;
114   FILE *fp;
115   off_t pos;
116   int keyfd = -1, err = 0;
117
118   if ((fp = fopen(path, "r+")) == NULL) {
119     fprintf(stderr, "afppasswd: can't open %s\n", path);
120     return -1;
121   }
122
123   /* open the key file if it exists */
124   strcpy(buf, path);
125   if (strlen(path) < sizeof(buf) - 5) {
126     strcat(buf, ".key");
127     keyfd = open(buf, O_RDONLY);
128   } 
129
130   pos = ftell(fp);
131   memset(buf, 0, sizeof(buf));
132   while (fgets(buf, sizeof(buf), fp)) {
133     if ((p = strchr(buf, ':'))) {
134       /* check for a match */
135       if (strlen(name) == (p - buf) && 
136           strncmp(buf, name, p - buf) == 0) {
137         p++;
138         if (!(flags & OPT_ISROOT) && (*p == PASSWD_ILLEGAL)) {
139           fprintf(stderr, "Your password is disabled. Please see your administrator.\n");
140           break;
141         }
142         goto found_entry;
143       }
144     }
145     pos = ftell(fp);
146     memset(buf, 0, sizeof(buf));
147   }
148
149   if (flags & OPT_ADDUSER) {
150     strcpy(buf, name);
151     strcat(buf, FORMAT);
152     p = strchr(buf, ':') + 1;
153     fwrite(buf, strlen(buf), 1, fp);
154   } else {
155     fprintf(stderr, "afppasswd: can't find %s in %s\n", name, path);
156     err = -1;
157     goto update_done;
158   }
159
160 found_entry:
161   /* need to verify against old password */
162   if ((flags & OPT_ISROOT) == 0) {
163     passwd = getpass("Enter OLD AFP password: ");
164     convert_passwd(p, NULL, keyfd);
165     if (strncmp(passwd, p, PASSWDLEN)) {
166       fprintf(stderr, "afppasswd: invalid password.\n");
167       err = -1;
168       goto update_done;
169     }
170   }
171
172   /* new password */
173   passwd = getpass("Enter NEW AFP password: ");
174   memcpy(password, passwd, sizeof(password));
175   password[PASSWDLEN] = '\0';
176 #ifdef USE_CRACKLIB
177   if (!(flags & OPT_NOCRACK)) {
178     if (passwd = FascistCheck(password, _PATH_CRACKLIB)) { 
179         fprintf(stderr, "Error: %s\n", passwd);
180         err = -1;
181         goto update_done;
182     } 
183   }
184 #endif /* USE_CRACKLIB */
185
186   passwd = getpass("Enter NEW AFP password again: ");
187   if (strcmp(passwd, password) == 0) {
188      struct flock lock;
189      int fd = fileno(fp);
190
191      convert_passwd(p, password, keyfd);
192      lock.l_type = F_WRLCK;
193      lock.l_start = pos;
194      lock.l_len = 1;
195      lock.l_whence = SEEK_SET;
196      fseek(fp, pos, SEEK_SET);
197      fcntl(fd, F_SETLKW, &lock);
198      fwrite(buf, p - buf + HEXPASSWDLEN, 1, fp);
199      lock.l_type = F_UNLCK;
200      fcntl(fd, F_SETLK, &lock);
201      printf("afppasswd: updated password.\n");
202
203   } else {
204     fprintf(stderr, "afppasswd: passwords don't match!\n");
205     err = -1;
206   }
207
208 update_done:
209   if (keyfd > -1)
210     close(keyfd);
211   fclose(fp);
212   return err;
213 }
214
215
216 /* creates a file with all the password entries */
217 static int create_file(const char *path, uid_t minuid)
218 {
219   struct passwd *pwd;
220   int fd, len, err = 0;
221
222   
223   if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0600)) < 0) {
224     fprintf(stderr, "afppasswd: can't create %s\n", path);
225     return -1;
226   }
227
228   setpwent();
229   while ((pwd = getpwent())) {
230     if (pwd->pw_uid < minuid)
231       continue;
232     /* a little paranoia */
233     if (strlen(pwd->pw_name) + FORMAT_LEN > sizeof(buf) - 1)
234       continue;
235     strcpy(buf, pwd->pw_name);
236     strcat(buf, FORMAT);
237     len = strlen(buf);
238     if (write(fd, buf, len) != len) {
239       fprintf(stderr, "afppasswd: problem writing to %s: %s\n", path,
240               strerror(errno));
241       err = -1;
242       break;
243     }
244   }
245   endpwent();
246   close(fd);
247
248   return err;
249 }
250
251
252 int main(int argc, char **argv)
253 {
254   struct stat st;
255   int flags;
256   uid_t uid_min = UID_START, uid;
257   char *path = _PATH_AFPDPWFILE;
258   int i, err = 0;
259
260   extern char *optarg;
261   extern int optind;
262
263   flags = ((uid = getuid()) == 0) ? OPT_ISROOT : 0;
264
265   if (((flags & OPT_ISROOT) == 0) && (argc > 1)) {
266     fprintf(stderr, "afppasswd (Netatalk %s)\n", VERSION);
267     fprintf(stderr, "Usage: afppasswd [-acfn] [-u minuid] [-p path] [username]\n");
268     fprintf(stderr, "  -a        add a new user\n");
269     fprintf(stderr, "  -c        create and initialize password file or specific user\n");
270     fprintf(stderr, "  -f        force an action\n");
271 #ifdef USE_CRACKLIB
272     fprintf(stderr, "  -n        disable cracklib checking of passwords\n");
273 #endif /* USE_CRACKLIB */
274     fprintf(stderr, "  -u uid    minimum uid to use, defaults to 100\n");
275     fprintf(stderr, "  -p path   path to afppasswd file\n");
276     return -1;
277   }
278
279   while ((i = getopt(argc, argv, OPTIONS)) != EOF) {
280     switch (i) {
281     case 'c': /* create and initialize password file or specific user */
282       flags |= OPT_CREATE;
283       break;
284     case 'a': /* add a new user */
285       flags |= OPT_ADDUSER;
286       break;
287     case 'f': /* force an action */
288       flags |= OPT_FORCE;
289       break;
290     case 'u':  /* minimum uid to use. default is 100 */
291       uid_min = atoi(optarg);
292       break;
293 #ifdef USE_CRACKLIB
294     case 'n': /* disable CRACKLIB check */
295       flags |= OPT_NOCRACK;
296       break;
297 #endif /* USE_CRACKLIB */
298     case 'p': /* path to afppasswd file */
299       path = optarg;
300       break;
301     default:
302       err++;
303       break;
304     }
305   }
306   
307   if (err || (optind + ((flags & OPT_CREATE) ? 0 : 
308                         (flags & OPT_ISROOT)) != argc)) {
309 #ifdef USE_CRACKLIB
310     fprintf(stderr, "Usage: afppasswd [-acfn] [-u minuid] [-p path] [username]\n");
311 #else /* USE_CRACKLIB */
312     fprintf(stderr, "Usage: afppasswd [-acf] [-u minuid] [-p path] [username]\n");
313 #endif /* USE_CRACKLIB */
314     fprintf(stderr, "  -a        add a new user\n");
315     fprintf(stderr, "  -c        create and initialize password file or specific user\n");
316     fprintf(stderr, "  -f        force an action\n");
317 #ifdef USE_CRACKLIB
318     fprintf(stderr, "  -n        disable cracklib checking of passwords\n");
319 #endif /* USE_CRACKLIB */
320     fprintf(stderr, "  -u uid    minimum uid to use, defaults to 100\n");
321     fprintf(stderr, "  -p path   path to afppasswd file\n");
322     return -1;
323   }
324
325   i = stat(path, &st);
326   if (flags & OPT_CREATE) {
327     if ((flags & OPT_ISROOT) == 0) {
328       fprintf(stderr, "afppasswd: only root can create the password file.\n");
329       return -1;
330     }
331
332     if (!i && ((flags & OPT_FORCE) == 0)) {
333       fprintf(stderr, "afppasswd: password file already exists.\n");
334       return -1;
335     }
336     return create_file(path, uid_min);
337
338   } else {
339     struct passwd *pwd = NULL;
340
341     if (i < 0) {
342       fprintf(stderr, "afppasswd: %s doesn't exist.\n", path);
343       return -1;
344     }
345     
346     /* if we're root, we need to specify the username */
347     pwd = (flags & OPT_ISROOT) ? getpwnam(argv[optind]) : getpwuid(uid);
348     if (pwd)
349       return update_passwd(path, pwd->pw_name, flags);
350
351     fprintf(stderr, "afppasswd: can't get password entry.\n");
352     return -1;
353   }
354 }