2 * $Id: afppasswd.c,v 1.17 2003-06-09 02:51:34 srittau 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 */
34 #endif /* HAVE_UNISTD_H */
36 #include <sys/types.h>
38 #include <sys/param.h>
41 #endif /* HAVE_FCNTL_H */
44 #include <netatalk/endian.h>
50 #endif /* USE_CRACKLIB */
52 #define OPT_ISROOT (1 << 0)
53 #define OPT_CREATE (1 << 1)
54 #define OPT_FORCE (1 << 2)
55 #define OPT_ADDUSER (1 << 3)
56 #define OPT_NOCRACK (1 << 4)
58 #define PASSWD_ILLEGAL '*'
60 #define FORMAT ":****************:****************:********\n"
62 #define OPTIONS "cafnu:p:"
65 #define PASSWDLEN DES_KEY_SZ
66 #define HEXPASSWDLEN (PASSWDLEN * 2)
68 static char buf[MAXPATHLEN + 1];
70 /* FIXME: preparation for i18n */
75 /* SRC and DST must not overlap. */
76 static void hexify(u_int8_t *dst, const u_int8_t *src)
80 for (i = j = 0; i < DES_KEY_SZ; i++, j += 2) {
81 static const unsigned char hextable[] = "0123456789ABCDEF";
83 dst[j] = hextable[(src[i] & 0xF0) >> 4];
84 dst[j + 1] = hextable[(src[i] & 0x0F)];
88 /* SRC and DST can overlap. */
89 #define unhex(x) (isdigit(x) ? (x) - '0' : toupper(x) + 10 - 'A')
90 static void unhexify(u_int8_t *dst, const u_int8_t *src)
94 /* convert to binary */
95 for (i = j = 0; i < HEXPASSWDLEN; i += 2, j++)
96 dst[j] = (unhex(src[i]) << 4) | unhex(src[i + 1]);
98 memset(dst + j, 0, HEXPASSWDLEN - j);
101 static u_int8_t *retrieve_key(int keyfd)
103 static u_int8_t key[HEXPASSWDLEN];
105 if (lseek(keyfd, 0, SEEK_SET) == -1)
107 if (read(keyfd, key, sizeof(key)) < sizeof(key))
115 static int decrypt_passwd(u_int8_t *dst, const u_int8_t *src, int keyfd)
120 key = retrieve_key(keyfd);
125 DES_key_schedule schedule;
127 DES_set_key_unchecked((DES_cblock *) key, &schedule);
128 DES_ecb_encrypt((DES_cblock *) src, (DES_cblock *) dst,
129 &schedule, DES_DECRYPT);
131 memset(&schedule, 0, sizeof(schedule));
134 memset(key, 0, HEXPASSWDLEN);
139 static int encrypt_passwd(u_int8_t *dst, const u_int8_t *src, int keyfd)
144 key = retrieve_key(keyfd);
149 DES_key_schedule schedule;
151 DES_set_key_unchecked((DES_cblock *) key, &schedule);
152 DES_ecb_encrypt((DES_cblock *) src, (DES_cblock *) dst,
153 &schedule, DES_ENCRYPT);
155 memset(&schedule, 0, sizeof(schedule));
161 /* this matches the code in uam_randnum.c */
162 static int update_passwd(const char *path, const char *name, int flags)
164 char password[PASSWDLEN + 1], *p, *passwd;
168 int keyfd = -1, err = 0;
170 if ((fp = fopen(path, "r+")) == NULL) {
171 fprintf(stderr, _("Can't open password file %s: %s.\n"), path, strerror(errno));
175 /* open the key file if it exists */
177 if (strlen(path) < sizeof(buf) - 5) {
179 keyfd = open(buf, O_RDONLY);
180 if (keyfd == -1 && errno != ENOENT) {
181 fprintf(stderr, _("Can't open key file %s: %s\n"), buf, strerror(errno));
188 memset(buf, 0, sizeof(buf));
189 while (fgets(buf, sizeof(buf), fp)) {
190 if ((p = strchr(buf, ':'))) {
191 /* check for a match */
192 if (strncmp(buf, name, p - buf) == 0) {
194 if (!(flags & OPT_ISROOT) && (*p == PASSWD_ILLEGAL)) {
195 fprintf(stderr, _("Your password is disabled. Please see your administrator.\n"));
203 memset(buf, 0, sizeof(buf));
207 if ((flags & OPT_ISROOT) && (flags & OPT_ADDUSER)) {
210 p = strchr(buf, ':') + 1;
211 fwrite(buf, strlen(buf), 1, fp);
213 fprintf(stderr, _("Can't find user %s in %s\n"), name, path);
219 /* need to verify against old password */
220 if ((flags & OPT_ISROOT) == 0) {
221 passwd = getpass(_("Enter OLD AFP password: "));
223 err = decrypt_passwd(p, p, keyfd);
225 memset(passwd, 0, strlen(passwd));
228 if (strncmp(passwd, p, PASSWDLEN)) {
229 memset(passwd, 0, strlen(passwd));
230 fprintf(stderr, _("Wrong password.\n"));
234 memset(passwd, 0, strlen(passwd));
238 passwd = getpass(_("Enter NEW AFP password: "));
239 memcpy(password, passwd, sizeof(password));
240 memset(passwd, 0, strlen(passwd));
241 password[PASSWDLEN] = '\0';
243 if (!(flags & OPT_NOCRACK)) {
245 if (str = FascistCheck(password, _PATH_CRACKLIB)) {
246 memset(password, 0, PASSWDLEN);
247 fprintf(stderr, _("Error: %s\n"), str);
252 #endif /* USE_CRACKLIB */
254 passwd = getpass(_("Enter NEW AFP password again: "));
255 if (strcmp(passwd, password) == 0) {
259 memset(passwd, 0, strlen(passwd));
261 err = encrypt_passwd(password, password, keyfd);
266 lock.l_type = F_WRLCK;
269 lock.l_whence = SEEK_SET;
270 fseek(fp, pos, SEEK_SET);
271 fcntl(fd, F_SETLKW, &lock);
272 fwrite(buf, p - buf + HEXPASSWDLEN, 1, fp);
273 lock.l_type = F_UNLCK;
274 fcntl(fd, F_SETLK, &lock);
275 printf(_("Updated password.\n"));
276 memset(password, 0, PASSWDLEN);
279 memset(passwd, 0, strlen(passwd));
280 memset(password, 0, PASSWDLEN);
281 fprintf(stderr, _("Passwords don't match!\n"));
293 /* creates a file with all the password entries */
294 static int create_file(const char *path, uid_t minuid)
297 int fd, len, err = 0;
300 if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0600)) < 0) {
301 fprintf(stderr, _("Can't create password file %s: %s\n"), path, strerror(errno));
306 while ((pwd = getpwent())) {
307 if (pwd->pw_uid < minuid)
309 /* a little paranoia */
310 if (strlen(pwd->pw_name) + FORMAT_LEN > sizeof(buf) - 1)
312 strcpy(buf, pwd->pw_name);
315 if (write(fd, buf, len) != len) {
316 fprintf(stderr, _("Problem writing to password file %s: %s\n"),
317 path, strerror(errno));
329 static void usage(void)
332 fprintf(stderr, _("Usage: afppasswd [-acfn] [-u minuid] [-p path] [username]\n"));
333 #else /* USE_CRACKLIB */
334 fprintf(stderr, _("Usage: afppasswd [-acf] [-u minuid] [-p path] [username]\n"));
335 #endif /* USE_CRACKLIB */
336 fprintf(stderr, _(" -a add a new user\n"));
337 fprintf(stderr, _(" -c create and initialize password file or specific user\n"));
338 fprintf(stderr, _(" -f force an action\n"));
340 fprintf(stderr, _(" -n disable cracklib checking of passwords\n"));
341 #endif /* USE_CRACKLIB */
342 fprintf(stderr, _(" -u uid minimum uid to use, defaults to 100\n"));
343 fprintf(stderr, _(" -p path path to afppasswd file\n"));
347 int main(int argc, char **argv)
351 uid_t uid_min = UID_START, uid;
352 char *path = _PATH_AFPDPWFILE;
358 flags = ((uid = getuid()) == 0) ? OPT_ISROOT : 0;
360 if (((flags & OPT_ISROOT) == 0) && (argc > 1)) {
365 while ((i = getopt(argc, argv, OPTIONS)) != EOF) {
367 case 'c': /* create and initialize password file or specific user */
370 case 'a': /* add a new user */
371 flags |= OPT_ADDUSER;
373 case 'f': /* force an action */
376 case 'u': /* minimum uid to use. default is 100 */
377 uid_min = atoi(optarg);
380 case 'n': /* disable CRACKLIB check */
381 flags |= OPT_NOCRACK;
383 #endif /* USE_CRACKLIB */
384 case 'p': /* path to afppasswd file */
393 if (err || (optind + ((flags & OPT_CREATE) ? 0 :
394 (flags & OPT_ISROOT)) != argc)) {
399 if (flags & OPT_CREATE) {
400 if ((flags & OPT_ISROOT) == 0) {
401 fprintf(stderr, _("Only root can create the password file.\n"));
405 if (!stat(path, &st) && ((flags & OPT_FORCE) == 0)) {
406 fprintf(stderr, _("Password file already exists.\n"));
409 return create_file(path, uid_min);
412 struct passwd *pwd = NULL;
414 if (stat(path, &st) < 0) {
415 fprintf(stderr, _("Password file %s doesn't exist.\n"), path);
419 /* if we're root, we need to specify the username */
420 pwd = (flags & OPT_ISROOT) ? getpwnam(argv[optind]) : getpwuid(uid);
422 return update_passwd(path, pwd->pw_name, flags);
424 fprintf(stderr, _("Can't get password entry.\n"));