char pwbuf[32];
switch (cnf->default_password) {
- case -1: /* Random password */
+ case P_NONE: /* No password at all! */
+ return "";
+ case P_RANDOM: /* Random password */
l = (arc4random() % 8 + 8); /* 8 - 16 chars */
for (i = 0; i < l; i++)
pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
fflush(stdout);
}
break;
-
- case -2: /* No password at all! */
- return "";
-
- case 0: /* No login - default */
- default:
- return "*";
-
- case 1: /* user's name */
+ case P_YES: /* user's name */
strlcpy(pwbuf, user, sizeof(pwbuf));
break;
+ case P_NO: /* No login - default */
+ /* FALLTHROUGH */
+ default:
+ return "*";
}
return pw_pwcrypt(pwbuf);
}
return (m);
}
+static long
+validate_expire(char *str, int opt)
+{
+ if (!numerics(str))
+ errx(EX_DATAERR, "-%c argument must be numeric "
+ "when setting defaults: %s", (char)opt, str);
+ return strtol(str, NULL, 0);
+}
+
static void
mix_config(struct userconf *cmdcnf, struct userconf *cfg)
{
- if (cmdcnf->default_password == 0)
+ if (cmdcnf->default_password < 0)
cmdcnf->default_password = cfg->default_password;
if (cmdcnf->reuse_uids == 0)
cmdcnf->reuse_uids = cfg->reuse_uids;
cmdcnf->min_gid = cfg->min_gid;
if (cmdcnf->max_gid == 0)
cmdcnf->max_gid = cfg->max_gid;
- if (cmdcnf->expire_days == 0)
+ if (cmdcnf->expire_days < 0)
cmdcnf->expire_days = cfg->expire_days;
- if (cmdcnf->password_days == 0)
+ if (cmdcnf->password_days < 0)
cmdcnf->password_days = cfg->password_days;
}
if ((cmdcnf = calloc(1, sizeof(struct userconf))) == NULL)
err(EXIT_FAILURE, "calloc()");
+ cmdcnf->default_password = cmdcnf->expire_days = cmdcnf->password_days = -1;
+ now = time(NULL);
+
if (arg1 != NULL) {
if (arg1[strspn(arg1, "0123456789")] == '\0')
id = pw_checkid(arg1, UID_MAX);
homedir = optarg;
break;
case 'e':
- now = time(NULL);
- cmdcnf->expire_days = parse_date(now, optarg);
+ if (genconf)
+ cmdcnf->expire_days = validate_expire(optarg, ch);
+ else
+ cmdcnf->expire_days = parse_date(now, optarg);
break;
case 'p':
- now = time(NULL);
- cmdcnf->password_days = parse_date(now, optarg);
+ if (genconf)
+ cmdcnf->password_days = validate_expire(optarg, ch);
+ else
+ cmdcnf->password_days = parse_date(now, optarg);
break;
case 'g':
validate_grname(cmdcnf, optarg);
pwd->pw_uid = pw_uidpolicy(cmdcnf, id);
pwd->pw_gid = pw_gidpolicy(cnf, grname, pwd->pw_name,
(gid_t) pwd->pw_uid, dryrun);
- pwd->pw_change = cmdcnf->password_days;
- pwd->pw_expire = cmdcnf->expire_days;
+
+ if (cmdcnf->password_days > 0)
+ pwd->pw_change = now + cmdcnf->password_days * 86400L;
+ if (cmdcnf->expire_days > 0)
+ pwd->pw_expire = now + cmdcnf->expire_days * 86400L;
+
pwd->pw_dir = pw_homepolicy(cmdcnf, homedir, pwd->pw_name);
pwd->pw_shell = pw_shellpolicy(cmdcnf);
lc = login_getpwclass(pwd);
class = nispasswd = NULL;
quiet = createhome = pretty = dryrun = nis = precrypted = false;
edited = false;
+ now = time(NULL);
if (arg1 != NULL) {
if (arg1[strspn(arg1, "0123456789")] == '\0')
homedir = optarg;
break;
case 'e':
- now = time(NULL);
expire_days = parse_date(now, optarg);
break;
case 'p':
- now = time(NULL);
password_days = parse_date(now, optarg);
break;
case 'g':
}
}
- if (password_days >= 0 && pwd->pw_change != password_days) {
- pwd->pw_change = password_days;
+
+ if (password_days >= 0) {
+ pwd->pw_change = now + password_days * 86400L;
edited = true;
}
- if (expire_days >= 0 && pwd->pw_expire != expire_days) {
- pwd->pw_expire = expire_days;
+ if (expire_days >= 0) {
+ pwd->pw_expire = now + expire_days * 86400L;
edited = true;
}