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