2 * $Id: afppasswd.c,v 1.19 2005-04-28 20:49:19 bfernhomberg Exp $
4 * Copyright 1999 (c) Adrian Sun (asun@u.washington.edu)
5 * All Rights Reserved. See COPYRIGHT.
7 * format of the password file:
8 * name:****************:****************:********
9 * password last login date failed usage count
11 * ***'s are illegal. they're just place holders for hex values. hex
12 * values that represent actual numbers are in network byte order.
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.
17 * the last two fields aren't currently used by the randnum uams.
19 * root syntax: afppasswd [-c] [-a] [-p path] [-f] [username]
20 * user syntax: afppasswd
25 #endif /* HAVE_CONFIG_H */
33 #endif /* HAVE_UNISTD_H */
35 #include <sys/types.h>
37 #include <sys/param.h>
40 #endif /* HAVE_FCNTL_H */
43 #include <netatalk/endian.h>
49 #endif /* USE_CRACKLIB */
51 #define OPT_ISROOT (1 << 0)
52 #define OPT_CREATE (1 << 1)
53 #define OPT_FORCE (1 << 2)
54 #define OPT_ADDUSER (1 << 3)
55 #define OPT_NOCRACK (1 << 4)
57 #define PASSWD_ILLEGAL '*'
59 #define FORMAT ":****************:****************:********\n"
61 #define OPTIONS "cafnu:p:"
64 #define HEXPASSWDLEN 16
67 static char buf[MAXPATHLEN + 1];
69 /* if newpwd is null, convert buf from hex to binary. if newpwd isn't
70 * null, convert newpwd to hex and save it in buf. */
71 #define unhex(x) (isdigit(x) ? (x) - '0' : toupper(x) + 10 - 'A')
72 static void convert_passwd(char *buf, char *newpwd, const int keyfd)
74 u_int8_t key[HEXPASSWDLEN];
75 Key_schedule schedule;
79 /* convert to binary */
80 for (i = j = 0; i < sizeof(key); i += 2, j++)
81 buf[j] = (unhex(buf[i]) << 4) | unhex(buf[i + 1]);
83 memset(buf + j, 0, sizeof(key) - j);
87 lseek(keyfd, 0, SEEK_SET);
88 read(keyfd, key, sizeof(key));
89 /* convert to binary */
90 for (i = j = 0; i < sizeof(key); i += 2, j++)
91 key[j] = (unhex(key[i]) << 4) | unhex(key[i + 1]);
93 memset(key + j, 0, sizeof(key) - j);
94 key_sched((C_Block *) key, schedule);
95 memset(key, 0, sizeof(key));
97 ecb_encrypt((C_Block *) newpwd, (C_Block *) newpwd, schedule,
100 /* decrypt the password */
101 ecb_encrypt((C_Block *) buf, (C_Block *) buf, schedule, DES_DECRYPT);
103 memset(&schedule, 0, sizeof(schedule));
107 const unsigned char hextable[] = "0123456789ABCDEF";
110 for (i = j = 0; i < DES_KEY_SZ; i++, j += 2) {
111 buf[j] = hextable[(newpwd[i] & 0xF0) >> 4];
112 buf[j + 1] = hextable[newpwd[i] & 0x0F];
117 /* this matches the code in uam_randnum.c */
118 static int update_passwd(const char *path, const char *name, int flags)
120 char password[PASSWDLEN + 1], *p, *passwd;
123 int keyfd = -1, err = 0;
125 if ((fp = fopen(path, "r+")) == NULL) {
126 fprintf(stderr, "afppasswd: can't open %s\n", path);
130 /* open the key file if it exists */
132 if (strlen(path) < sizeof(buf) - 5) {
134 keyfd = open(buf, O_RDONLY);
138 memset(buf, 0, sizeof(buf));
139 while (fgets(buf, sizeof(buf), fp)) {
140 if ((p = strchr(buf, ':'))) {
141 /* check for a match */
142 if (strlen(name) == (p - buf) &&
143 strncmp(buf, name, p - buf) == 0) {
145 if (!(flags & OPT_ISROOT) && (*p == PASSWD_ILLEGAL)) {
146 fprintf(stderr, "Your password is disabled. Please see your administrator.\n");
153 memset(buf, 0, sizeof(buf));
156 if (flags & OPT_ADDUSER) {
159 p = strchr(buf, ':') + 1;
160 fwrite(buf, strlen(buf), 1, fp);
162 fprintf(stderr, "afppasswd: can't find %s in %s\n", name, path);
168 /* need to verify against old password */
169 if ((flags & OPT_ISROOT) == 0) {
170 passwd = getpass("Enter OLD AFP password: ");
171 convert_passwd(p, NULL, keyfd);
172 if (strncmp(passwd, p, PASSWDLEN)) {
173 fprintf(stderr, "afppasswd: invalid password.\n");
180 passwd = getpass("Enter NEW AFP password: ");
181 memcpy(password, passwd, sizeof(password));
182 password[PASSWDLEN] = '\0';
184 if (!(flags & OPT_NOCRACK)) {
185 if (passwd = FascistCheck(password, _PATH_CRACKLIB)) {
186 fprintf(stderr, "Error: %s\n", passwd);
191 #endif /* USE_CRACKLIB */
193 passwd = getpass("Enter NEW AFP password again: ");
194 if (strcmp(passwd, password) == 0) {
198 convert_passwd(p, password, keyfd);
199 lock.l_type = F_WRLCK;
202 lock.l_whence = SEEK_SET;
203 fseek(fp, pos, SEEK_SET);
204 fcntl(fd, F_SETLKW, &lock);
205 fwrite(buf, p - buf + HEXPASSWDLEN, 1, fp);
206 lock.l_type = F_UNLCK;
207 fcntl(fd, F_SETLK, &lock);
208 printf("afppasswd: updated password.\n");
211 fprintf(stderr, "afppasswd: passwords don't match!\n");
223 /* creates a file with all the password entries */
224 static int create_file(const char *path, uid_t minuid)
227 int fd, len, err = 0;
230 if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0600)) < 0) {
231 fprintf(stderr, "afppasswd: can't create %s\n", path);
236 while ((pwd = getpwent())) {
237 if (pwd->pw_uid < minuid)
239 /* a little paranoia */
240 if (strlen(pwd->pw_name) + FORMAT_LEN > sizeof(buf) - 1)
242 strcpy(buf, pwd->pw_name);
245 if (write(fd, buf, len) != len) {
246 fprintf(stderr, "afppasswd: problem writing to %s: %s\n", path,
259 int main(int argc, char **argv)
263 uid_t uid_min = UID_START, uid;
264 char *path = _PATH_AFPDPWFILE;
270 flags = ((uid = getuid()) == 0) ? OPT_ISROOT : 0;
272 if (((flags & OPT_ISROOT) == 0) && (argc > 1)) {
273 fprintf(stderr, "Usage: afppasswd [-acfn] [-u minuid] [-p path] [username]\n");
274 fprintf(stderr, " -a add a new user\n");
275 fprintf(stderr, " -c create and initialize password file or specific user\n");
276 fprintf(stderr, " -f force an action\n");
278 fprintf(stderr, " -n disable cracklib checking of passwords\n");
279 #endif /* USE_CRACKLIB */
280 fprintf(stderr, " -u uid minimum uid to use, defaults to 100\n");
281 fprintf(stderr, " -p path path to afppasswd file\n");
285 while ((i = getopt(argc, argv, OPTIONS)) != EOF) {
287 case 'c': /* create and initialize password file or specific user */
290 case 'a': /* add a new user */
291 flags |= OPT_ADDUSER;
293 case 'f': /* force an action */
296 case 'u': /* minimum uid to use. default is 100 */
297 uid_min = atoi(optarg);
300 case 'n': /* disable CRACKLIB check */
301 flags |= OPT_NOCRACK;
303 #endif /* USE_CRACKLIB */
304 case 'p': /* path to afppasswd file */
313 if (err || (optind + ((flags & OPT_CREATE) ? 0 :
314 (flags & OPT_ISROOT)) != argc)) {
316 fprintf(stderr, "Usage: afppasswd [-acfn] [-u minuid] [-p path] [username]\n");
317 #else /* USE_CRACKLIB */
318 fprintf(stderr, "Usage: afppasswd [-acf] [-u minuid] [-p path] [username]\n");
319 #endif /* USE_CRACKLIB */
320 fprintf(stderr, " -a add a new user\n");
321 fprintf(stderr, " -c create and initialize password file or specific user\n");
322 fprintf(stderr, " -f force an action\n");
324 fprintf(stderr, " -n disable cracklib checking of passwords\n");
325 #endif /* USE_CRACKLIB */
326 fprintf(stderr, " -u uid minimum uid to use, defaults to 100\n");
327 fprintf(stderr, " -p path path to afppasswd file\n");
332 if (flags & OPT_CREATE) {
333 if ((flags & OPT_ISROOT) == 0) {
334 fprintf(stderr, "afppasswd: only root can create the password file.\n");
338 if (!i && ((flags & OPT_FORCE) == 0)) {
339 fprintf(stderr, "afppasswd: password file already exists.\n");
342 return create_file(path, uid_min);
345 struct passwd *pwd = NULL;
348 fprintf(stderr, "afppasswd: %s doesn't exist.\n", path);
352 /* if we're root, we need to specify the username */
353 pwd = (flags & OPT_ISROOT) ? getpwnam(argv[optind]) : getpwuid(uid);
355 return update_passwd(path, pwd->pw_name, flags);
357 fprintf(stderr, "afppasswd: can't get password entry.\n");