2 * $Id: afppasswd.c,v 1.16 2003-06-07 03:07:27 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 */
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 /* FIXME: preparation for i18n */
74 #define unhex(x) (isdigit(x) ? (x) - '0' : toupper(x) + 10 - 'A')
76 static u_int8_t *retrieve_key(int keyfd)
78 static u_int8_t key[HEXPASSWDLEN];
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]);
87 memset(key + j, 0, sizeof(key) - j);
92 static void retrieve_passwd(char *buf, int keyfd)
94 DES_key_schedule schedule;
98 /* convert to binary */
99 for (i = j = 0; i < HEXPASSWDLEN; i += 2, j++)
100 buf[j] = (unhex(buf[i]) << 4) | unhex(buf[i + 1]);
102 memset(buf + j, 0, HEXPASSWDLEN - j);
104 key = retrieve_key(keyfd);
105 DES_key_sched((DES_cblock *) key, &schedule);
107 /* decrypt the password */
108 DES_ecb_encrypt((DES_cblock *) buf, (DES_cblock *) buf,
109 &schedule, DES_DECRYPT);
111 memset(key, 0, HEXPASSWDLEN);
112 memset(&schedule, 0, sizeof(schedule));
115 static void set_passwd(char *buf, char *newpwd, const int keyfd)
117 static const unsigned char hextable[] = "0123456789ABCDEF";
118 DES_key_schedule schedule;
122 key = retrieve_key(keyfd);
123 DES_key_sched((DES_cblock *) key, &schedule);
125 DES_ecb_encrypt((DES_cblock *) newpwd, (DES_cblock *) newpwd,
126 &schedule, DES_ENCRYPT);
128 memset(&schedule, 0, sizeof(schedule));
131 for (i = j = 0; i < DES_KEY_SZ; i++, j += 2) {
132 buf[j] = hextable[(newpwd[i] & 0xF0) >> 4];
133 buf[j + 1] = hextable[newpwd[i] & 0x0F];
137 /* this matches the code in uam_randnum.c */
138 static int update_passwd(const char *path, const char *name, int flags)
140 char password[PASSWDLEN + 1], *p, *passwd;
144 int keyfd = -1, err = 0;
146 if ((fp = fopen(path, "r+")) == NULL) {
147 fprintf(stderr, _("Can't open password file %s: %s.\n"), path, strerror(errno));
151 /* open the key file if it exists */
153 if (strlen(path) < sizeof(buf) - 5) {
155 keyfd = open(buf, O_RDONLY);
159 memset(buf, 0, sizeof(buf));
160 while (fgets(buf, sizeof(buf), fp)) {
161 if ((p = strchr(buf, ':'))) {
162 /* check for a match */
163 if (strncmp(buf, name, p - buf) == 0) {
165 if (!(flags & OPT_ISROOT) && (*p == PASSWD_ILLEGAL)) {
166 fprintf(stderr, _("Your password is disabled. Please see your administrator.\n"));
174 memset(buf, 0, sizeof(buf));
178 if ((flags & OPT_ISROOT) && (flags & OPT_ADDUSER)) {
181 p = strchr(buf, ':') + 1;
182 fwrite(buf, strlen(buf), 1, fp);
184 fprintf(stderr, _("Can't find user %s in %s\n"), name, path);
190 /* need to verify against old password */
191 if ((flags & OPT_ISROOT) == 0) {
192 passwd = getpass(_("Enter OLD AFP password: "));
193 retrieve_passwd(p, keyfd);
194 if (strncmp(passwd, p, PASSWDLEN)) {
195 memset(passwd, 0, strlen(passwd));
196 fprintf(stderr, _("Wrong password.\n"));
200 memset(passwd, 0, strlen(passwd));
204 passwd = getpass(_("Enter NEW AFP password: "));
205 memcpy(password, passwd, sizeof(password));
206 memset(passwd, 0, strlen(passwd));
207 password[PASSWDLEN] = '\0';
209 if (!(flags & OPT_NOCRACK)) {
211 if (str = FascistCheck(password, _PATH_CRACKLIB)) {
212 memset(password, 0, PASSWDLEN);
213 fprintf(stderr, _("Error: %s\n"), str);
218 #endif /* USE_CRACKLIB */
220 passwd = getpass(_("Enter NEW AFP password again: "));
221 if (strcmp(passwd, password) == 0) {
225 memset(passwd, 0, strlen(passwd));
227 set_passwd(p, password, keyfd);
228 lock.l_type = F_WRLCK;
231 lock.l_whence = SEEK_SET;
232 fseek(fp, pos, SEEK_SET);
233 fcntl(fd, F_SETLKW, &lock);
234 fwrite(buf, p - buf + HEXPASSWDLEN, 1, fp);
235 lock.l_type = F_UNLCK;
236 fcntl(fd, F_SETLK, &lock);
237 printf(_("Updated password.\n"));
238 memset(password, 0, PASSWDLEN);
241 memset(passwd, 0, strlen(passwd));
242 memset(password, 0, PASSWDLEN);
243 fprintf(stderr, _("Passwords don't match!\n"));
255 /* creates a file with all the password entries */
256 static int create_file(const char *path, uid_t minuid)
259 int fd, len, err = 0;
262 if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0600)) < 0) {
263 fprintf(stderr, _("Can't create password file %s: %s\n"), path, strerror(errno));
268 while ((pwd = getpwent())) {
269 if (pwd->pw_uid < minuid)
271 /* a little paranoia */
272 if (strlen(pwd->pw_name) + FORMAT_LEN > sizeof(buf) - 1)
274 strcpy(buf, pwd->pw_name);
277 if (write(fd, buf, len) != len) {
278 fprintf(stderr, _("Problem writing to password file %s: %s\n"),
279 path, strerror(errno));
291 static void usage(void)
294 fprintf(stderr, _("Usage: afppasswd [-acfn] [-u minuid] [-p path] [username]\n"));
295 #else /* USE_CRACKLIB */
296 fprintf(stderr, _("Usage: afppasswd [-acf] [-u minuid] [-p path] [username]\n"));
297 #endif /* USE_CRACKLIB */
298 fprintf(stderr, _(" -a add a new user\n"));
299 fprintf(stderr, _(" -c create and initialize password file or specific user\n"));
300 fprintf(stderr, _(" -f force an action\n"));
302 fprintf(stderr, _(" -n disable cracklib checking of passwords\n"));
303 #endif /* USE_CRACKLIB */
304 fprintf(stderr, _(" -u uid minimum uid to use, defaults to 100\n"));
305 fprintf(stderr, _(" -p path path to afppasswd file\n"));
309 int main(int argc, char **argv)
313 uid_t uid_min = UID_START, uid;
314 char *path = _PATH_AFPDPWFILE;
320 flags = ((uid = getuid()) == 0) ? OPT_ISROOT : 0;
322 if (((flags & OPT_ISROOT) == 0) && (argc > 1)) {
327 while ((i = getopt(argc, argv, OPTIONS)) != EOF) {
329 case 'c': /* create and initialize password file or specific user */
332 case 'a': /* add a new user */
333 flags |= OPT_ADDUSER;
335 case 'f': /* force an action */
338 case 'u': /* minimum uid to use. default is 100 */
339 uid_min = atoi(optarg);
342 case 'n': /* disable CRACKLIB check */
343 flags |= OPT_NOCRACK;
345 #endif /* USE_CRACKLIB */
346 case 'p': /* path to afppasswd file */
355 if (err || (optind + ((flags & OPT_CREATE) ? 0 :
356 (flags & OPT_ISROOT)) != argc)) {
361 if (flags & OPT_CREATE) {
362 if ((flags & OPT_ISROOT) == 0) {
363 fprintf(stderr, _("Only root can create the password file.\n"));
367 if (!stat(path, &st) && ((flags & OPT_FORCE) == 0)) {
368 fprintf(stderr, _("Password file already exists.\n"));
371 return create_file(path, uid_min);
374 struct passwd *pwd = NULL;
376 if (stat(path, &st) < 0) {
377 fprintf(stderr, _("Password file %s doesn't exist.\n"), path);
381 /* if we're root, we need to specify the username */
382 pwd = (flags & OPT_ISROOT) ? getpwnam(argv[optind]) : getpwuid(uid);
384 return update_passwd(path, pwd->pw_name, flags);
386 fprintf(stderr, _("Can't get password entry.\n"));