]> arthur.barton.de Git - netatalk.git/blob - bin/afppasswd/afppasswd.c
Fixes for stupid paper bag compile-time problems with conditionals
[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
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <ctype.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/param.h>
33 #include <fcntl.h>
34 #include <pwd.h>
35
36 #include <netatalk/endian.h>
37
38 #ifdef UAM_RNDNUM
39 #include <des.h>
40
41 #ifdef USE_CRACKLIB
42 #include <crack.h>
43 #endif
44
45 #define OPT_ISROOT  (1 << 0)
46 #define OPT_CREATE  (1 << 1)
47 #define OPT_FORCE   (1 << 2)
48 #define OPT_ADDUSER (1 << 3)
49 #define OPT_NOCRACK (1 << 4)
50
51 #define PASSWD_ILLEGAL '*'
52
53 #define FORMAT  ":****************:****************:********\n"
54 #define FORMAT_LEN 44
55 #define OPTIONS "cafun:p:"
56 #define UID_START 100
57
58 #define HEXPASSWDLEN 16
59 #define PASSWDLEN 8
60
61 static char buf[MAXPATHLEN + 1];
62
63 /* if newpwd is null, convert buf from hex to binary. if newpwd isn't
64  * null, convert newpwd to hex and save it in buf. */
65 #define unhex(x)  (isdigit(x) ? (x) - '0' : toupper(x) + 10 - 'A')
66 static void convert_passwd(char *buf, char *newpwd, const int keyfd)
67 {
68   u_int8_t key[HEXPASSWDLEN];
69   Key_schedule schedule;
70   int i, j;
71
72   if (!newpwd) {
73     /* convert to binary */
74     for (i = j = 0; i < sizeof(key); i += 2, j++)
75       buf[j] = (unhex(buf[i]) << 4) | unhex(buf[i + 1]);
76     if (j <= DES_KEY_SZ)
77       memset(buf + j, 0, sizeof(key) - j);
78   }
79
80   if (keyfd > -1) {
81     lseek(keyfd, 0, SEEK_SET);
82     read(keyfd, key, sizeof(key));
83     /* convert to binary */
84     for (i = j = 0; i < sizeof(key); i += 2, j++)
85       key[j] = (unhex(key[i]) << 4) | unhex(key[i + 1]);
86     if (j <= DES_KEY_SZ)
87       memset(key + j, 0, sizeof(key) - j);
88     key_sched((C_Block *) key, schedule);
89     memset(key, 0, sizeof(key));   
90     if (newpwd) {
91         ecb_encrypt((C_Block *) newpwd, (C_Block *) newpwd, schedule,
92                     DES_ENCRYPT);
93     } else {
94       /* decrypt the password */
95       ecb_encrypt((C_Block *) buf, (C_Block *) buf, schedule, DES_DECRYPT);
96     }
97     memset(schedule, 0, sizeof(schedule));      
98   }
99
100   if (newpwd) {
101     const unsigned char hextable[] = "0123456789ABCDEF";
102
103     /* convert to hex */
104     for (i = j = 0; i < DES_KEY_SZ; i++, j += 2) {
105       buf[j] = hextable[(newpwd[i] & 0xF0) >> 4];
106       buf[j + 1] = hextable[newpwd[i] & 0x0F];
107     }
108   }
109 }
110
111 /* this matches the code in uam_randnum.c */
112 static int update_passwd(const char *path, const char *name, int flags)
113 {
114   char password[PASSWDLEN + 1], *p, *passwd;
115   FILE *fp;
116   off_t pos;
117   int keyfd = -1, err = 0;
118
119   if ((fp = fopen(path, "r+")) == NULL) {
120     fprintf(stderr, "afppasswd: can't open %s\n", path);
121     return -1;
122   }
123
124   /* open the key file if it exists */
125   strcpy(buf, path);
126   if (strlen(path) < sizeof(buf) - 5) {
127     strcat(buf, ".key");
128     keyfd = open(buf, O_RDONLY);
129   } 
130
131   pos = ftell(fp);
132   memset(buf, 0, sizeof(buf));
133   while (fgets(buf, sizeof(buf), fp)) {
134     if ((p = strchr(buf, ':'))) {
135       /* check for a match */
136       if (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
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: %m\n", path);
240       err = -1;
241       break;
242     }
243   }
244   endpwent();
245   close(fd);
246
247   return err;
248 }
249
250
251 int main(int argc, char **argv)
252 {
253   struct stat st;
254   int flags;
255   uid_t uid_min = UID_START, uid;
256   char *path = _PATH_AFPDPWFILE;
257   int i, err = 0;
258
259   extern char *optarg;
260   extern int optind, opterr;
261
262   flags = ((uid = getuid()) == 0) ? OPT_ISROOT : 0;
263
264   if (((flags & OPT_ISROOT) == 0) && (argc > 1)) {
265     fprintf(stderr, "Usage: afppasswd [-acfn] [-u minuid] [-p path] [username]\n");
266     fprintf(stderr, "  -a        add a new user\n");
267     fprintf(stderr, "  -c        create and initialize password file or specific user\n");
268     fprintf(stderr, "  -f        force an action\n");
269 #ifdef USE_CRACKLIB
270     fprintf(stderr, "  -n        disable cracklib checking of passwords\n");
271 #endif
272     fprintf(stderr, "  -u uid    minimum uid to use, defaults to 100\n");
273     fprintf(stderr, "  -p path   path to afppasswd file\n");
274     return -1;
275   }
276
277   while ((i = getopt(argc, argv, OPTIONS)) != EOF) {
278     switch (i) {
279     case 'c': /* create and initialize password file or specific user */
280       flags |= OPT_CREATE;
281       break;
282     case 'a': /* add a new user */
283       flags |= OPT_ADDUSER;
284     case 'f': /* force an action */
285       flags |= OPT_FORCE;
286       break;
287     case 'u':  /* minimum uid to use. default is 100 */
288       uid_min = atoi(optarg);
289       break;
290 #ifdef USE_CRACKLIB
291     case 'n': /* disable CRACKLIB check */
292       flags |= OPT_NOCRACK;
293       break;
294 #endif
295     case 'p': /* path to afppasswd file */
296       path = optarg;
297       break;
298     default:
299       err++;
300       break;
301     }
302   }
303
304   if (err || (optind + ((flags & OPT_CREATE) ? 0 : 
305                         (flags & OPT_ISROOT)) != argc)) {
306 #ifdef USE_CRACKLIB
307     fprintf(stderr, "Usage: afppasswd [-acfn] [-u minuid] [-p path] [username]\n");
308 #else
309     fprintf(stderr, "Usage: afppasswd [-acf] [-u minuid] [-p path] [username]\n");
310 #endif
311     return -1;
312   }
313
314   i = stat(path, &st);
315   if (flags & OPT_CREATE) {
316     if ((flags & OPT_ISROOT) == 0) {
317       fprintf(stderr, "afppasswd: only root can create the password file.\n");
318       return -1;
319     }
320
321     if (!i && ((flags & OPT_FORCE) == 0)) {
322       fprintf(stderr, "afppasswd: password file already exists.\n");
323       return -1;
324     }
325     return create_file(path, uid_min);
326
327   } else {
328     struct passwd *pwd = NULL;
329
330     if (i < 0) {
331       fprintf(stderr, "afppasswd: %s doesn't exist.\n", path);
332       return -1;
333     }
334     
335     /* if we're root, we need to specify the username */
336     pwd = (flags & OPT_ISROOT) ? getpwnam(argv[optind]) : getpwuid(uid);
337     if (pwd)
338       return update_passwd(path, pwd->pw_name, flags);
339
340     fprintf(stderr, "afppasswd: can't get password entry.\n");
341     return -1;
342   }
343 }
344 #else
345
346 main(int argc, char **argv)
347 {
348   fprintf(stderr, "afppasswd is only useful if you're using centralized passwords\n");
349   fprintf(stderr, "for the Random Number authentication methods.\n");
350   return -1;
351 }
352 #endif
353