2 * Copyright 1999 (c) Adrian Sun (asun@u.washington.edu)
3 * All Rights Reserved. See COPYRIGHT.
5 * format of the password file:
6 * name:****************:****************:********
7 * password last login date failed usage count
9 * ***'s are illegal. they're just place holders for hex values. hex
10 * values that represent actual numbers are in network byte order.
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.
15 * the last two fields aren't currently used by the randnum uams.
17 * root syntax: afppasswd [-c] [-a] [-p path] [-f] [username]
18 * user syntax: afppasswd
30 #include <sys/types.h>
32 #include <sys/param.h>
36 #include <netatalk/endian.h>
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)
51 #define PASSWD_ILLEGAL '*'
53 #define FORMAT ":****************:****************:********\n"
55 #define OPTIONS "cafun:p:"
58 #define HEXPASSWDLEN 16
61 static char buf[MAXPATHLEN + 1];
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)
68 u_int8_t key[HEXPASSWDLEN];
69 Key_schedule schedule;
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]);
77 memset(buf + j, 0, sizeof(key) - j);
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);
88 key_sched((C_Block *) key, schedule);
89 memset(key, 0, sizeof(key));
91 ecb_encrypt((C_Block *) newpwd, (C_Block *) newpwd, schedule,
94 /* decrypt the password */
95 ecb_encrypt((C_Block *) buf, (C_Block *) buf, schedule, DES_DECRYPT);
97 memset(schedule, 0, sizeof(schedule));
101 const unsigned char hextable[] = "0123456789ABCDEF";
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];
111 /* this matches the code in uam_randnum.c */
112 static int update_passwd(const char *path, const char *name, int flags)
114 char password[PASSWDLEN + 1], *p, *passwd;
117 int keyfd = -1, err = 0;
119 if ((fp = fopen(path, "r+")) == NULL) {
120 fprintf(stderr, "afppasswd: can't open %s\n", path);
124 /* open the key file if it exists */
126 if (strlen(path) < sizeof(buf) - 5) {
128 keyfd = open(buf, O_RDONLY);
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) {
138 if (!(flags & OPT_ISROOT) && (*p == PASSWD_ILLEGAL)) {
139 fprintf(stderr, "Your password is disabled. Please see your administrator.\n");
146 memset(buf, 0, sizeof(buf));
149 if (flags & OPT_ADDUSER) {
152 p = strchr(buf, ':') + 1;
153 fwrite(buf, strlen(buf), 1, fp);
155 fprintf(stderr, "afppasswd: can't find %s in %s\n", name, path);
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");
173 passwd = getpass("Enter NEW AFP password: ");
174 memcpy(password, passwd, sizeof(password));
175 password[PASSWDLEN] = '\0';
177 if (!(flags & OPT_NOCRACK)) {
178 if (passwd = FascistCheck(password, _PATH_CRACKLIB)) {
179 fprintf(stderr, "Error: %s\n", passwd);
186 passwd = getpass("Enter NEW AFP password again: ");
187 if (strcmp(passwd, password) == 0) {
191 convert_passwd(p, password, keyfd);
192 lock.l_type = F_WRLCK;
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");
204 fprintf(stderr, "afppasswd: passwords don't match!\n");
216 /* creates a file with all the password entries */
217 static int create_file(const char *path, uid_t minuid)
220 int fd, len, err = 0;
223 if ((fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0600)) < 0) {
224 fprintf(stderr, "afppasswd: can't create %s\n", path);
229 while ((pwd = getpwent())) {
230 if (pwd->pw_uid < minuid)
232 /* a little paranoia */
233 if (strlen(pwd->pw_name) + FORMAT_LEN > sizeof(buf) - 1)
235 strcpy(buf, pwd->pw_name);
238 if (write(fd, buf, len) != len) {
239 fprintf(stderr, "afppasswd: problem writing to %s: %m\n", path);
251 int main(int argc, char **argv)
255 uid_t uid_min = UID_START, uid;
256 char *path = _PATH_AFPDPWFILE;
260 extern int optind, opterr;
262 flags = ((uid = getuid()) == 0) ? OPT_ISROOT : 0;
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");
270 fprintf(stderr, " -n disable cracklib checking of passwords\n");
272 fprintf(stderr, " -u uid minimum uid to use, defaults to 100\n");
273 fprintf(stderr, " -p path path to afppasswd file\n");
277 while ((i = getopt(argc, argv, OPTIONS)) != EOF) {
279 case 'c': /* create and initialize password file or specific user */
282 case 'a': /* add a new user */
283 flags |= OPT_ADDUSER;
284 case 'f': /* force an action */
287 case 'u': /* minimum uid to use. default is 100 */
288 uid_min = atoi(optarg);
291 case 'n': /* disable CRACKLIB check */
292 flags |= OPT_NOCRACK;
295 case 'p': /* path to afppasswd file */
304 if (err || (optind + ((flags & OPT_CREATE) ? 0 :
305 (flags & OPT_ISROOT)) != argc)) {
307 fprintf(stderr, "Usage: afppasswd [-acfn] [-u minuid] [-p path] [username]\n");
309 fprintf(stderr, "Usage: afppasswd [-acf] [-u minuid] [-p path] [username]\n");
315 if (flags & OPT_CREATE) {
316 if ((flags & OPT_ISROOT) == 0) {
317 fprintf(stderr, "afppasswd: only root can create the password file.\n");
321 if (!i && ((flags & OPT_FORCE) == 0)) {
322 fprintf(stderr, "afppasswd: password file already exists.\n");
325 return create_file(path, uid_min);
328 struct passwd *pwd = NULL;
331 fprintf(stderr, "afppasswd: %s doesn't exist.\n", path);
335 /* if we're root, we need to specify the username */
336 pwd = (flags & OPT_ISROOT) ? getpwnam(argv[optind]) : getpwuid(uid);
338 return update_passwd(path, pwd->pw_name, flags);
340 fprintf(stderr, "afppasswd: can't get password entry.\n");
346 main(int argc, char **argv)
348 fprintf(stderr, "afppasswd is only useful if you're using centralized passwords\n");
349 fprintf(stderr, "for the Random Number authentication methods.\n");