pw_user.c revision 286152
1204076Spjd/*- 2204076Spjd * Copyright (C) 1996 3211877Spjd * David L. Nugent. All rights reserved. 4204076Spjd * 5204076Spjd * Redistribution and use in source and binary forms, with or without 6204076Spjd * modification, are permitted provided that the following conditions 7204076Spjd * are met: 8204076Spjd * 1. Redistributions of source code must retain the above copyright 9204076Spjd * notice, this list of conditions and the following disclaimer. 10204076Spjd * 2. Redistributions in binary form must reproduce the above copyright 11204076Spjd * notice, this list of conditions and the following disclaimer in the 12204076Spjd * documentation and/or other materials provided with the distribution. 13204076Spjd * 14204076Spjd * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 15204076Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16204076Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17204076Spjd * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 18204076Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19204076Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20204076Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21204076Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22204076Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23204076Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24204076Spjd * SUCH DAMAGE. 25204076Spjd * 26204076Spjd */ 27204076Spjd 28204076Spjd#ifndef lint 29204076Spjdstatic const char rcsid[] = 30204076Spjd "$FreeBSD: head/usr.sbin/pw/pw_user.c 286152 2015-08-01 10:25:55Z bapt $"; 31204076Spjd#endif /* not lint */ 32204076Spjd 33204076Spjd#include <ctype.h> 34204076Spjd#include <err.h> 35204076Spjd#include <fcntl.h> 36204076Spjd#include <inttypes.h> 37204076Spjd#include <sys/param.h> 38204076Spjd#include <dirent.h> 39204076Spjd#include <paths.h> 40204076Spjd#include <termios.h> 41204076Spjd#include <sys/types.h> 42204076Spjd#include <sys/time.h> 43204076Spjd#include <sys/resource.h> 44204076Spjd#include <login_cap.h> 45213009Spjd#include <pwd.h> 46204076Spjd#include <grp.h> 47204076Spjd#include <libutil.h> 48204076Spjd#include "pw.h" 49204076Spjd#include "bitmap.h" 50204076Spjd 51204076Spjd#define LOGNAMESIZE (MAXLOGNAME-1) 52204076Spjd 53204076Spjdstatic char locked_str[] = "*LOCKED*"; 54204076Spjd 55204076Spjdstatic int pw_userdel(char *name, long id); 56204076Spjdstatic int print_user(struct passwd * pwd); 57212038Spjdstatic uid_t pw_uidpolicy(struct userconf * cnf, long id); 58204076Spjdstatic uid_t pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer); 59204076Spjdstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 60204076Spjdstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 61211977Spjdstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 62204076Spjdstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 63204076Spjdstatic char *pw_password(struct userconf * cnf, char const * user); 64204076Spjdstatic char *shell_path(char const * path, char *shells[], char *sh); 65204076Spjdstatic void rmat(uid_t uid); 66204076Spjdstatic void rmopie(char const * name); 67204076Spjd 68219864Spjdstatic void 69219864Spjdcreate_and_populate_homedir(struct passwd *pwd) 70204076Spjd{ 71204076Spjd struct userconf *cnf = conf.userconf; 72204076Spjd const char *skeldir; 73204076Spjd int skelfd = -1; 74204076Spjd 75204076Spjd skeldir = cnf->dotdir; 76204076Spjd 77204076Spjd if (skeldir != NULL && *skeldir != '\0') { 78211984Spjd if (*skeldir == '/') 79211984Spjd skeldir++; 80204076Spjd skelfd = openat(conf.rootfd, skeldir, O_DIRECTORY|O_CLOEXEC); 81204076Spjd } 82204076Spjd 83204076Spjd copymkdir(conf.rootfd, pwd->pw_dir, skelfd, cnf->homemode, pwd->pw_uid, 84204076Spjd pwd->pw_gid, 0); 85204076Spjd pw_log(cnf, M_ADD, W_USER, "%s(%ju) home %s made", pwd->pw_name, 86204076Spjd (uintmax_t)pwd->pw_uid, pwd->pw_dir); 87204076Spjd} 88204076Spjd 89204076Spjdstatic int 90204076Spjdset_passwd(struct passwd *pwd, bool update) 91204076Spjd{ 92204076Spjd int b, istty; 93204076Spjd struct termios t, n; 94204076Spjd login_cap_t *lc; 95204076Spjd char line[_PASSWORD_LEN+1]; 96204076Spjd char *p; 97204076Spjd 98204076Spjd if (conf.fd == '-') { 99204076Spjd if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 100204076Spjd pwd->pw_passwd = "*"; /* No access */ 101204076Spjd return (1); 102204076Spjd } 103204076Spjd return (0); 104204076Spjd } 105204076Spjd 106204076Spjd if ((istty = isatty(conf.fd))) { 107204076Spjd if (tcgetattr(conf.fd, &t) == -1) 108204076Spjd istty = 0; 109204076Spjd else { 110211877Spjd n = t; 111211877Spjd n.c_lflag &= ~(ECHO); 112211877Spjd tcsetattr(conf.fd, TCSANOW, &n); 113211877Spjd printf("%s%spassword for user %s:", 114211877Spjd update ? "new " : "", 115211877Spjd conf.precrypted ? "encrypted " : "", 116211877Spjd pwd->pw_name); 117211877Spjd fflush(stdout); 118211877Spjd } 119211877Spjd } 120211877Spjd b = read(conf.fd, line, sizeof(line) - 1); 121211877Spjd if (istty) { /* Restore state */ 122211877Spjd tcsetattr(conf.fd, TCSANOW, &t); 123211877Spjd fputc('\n', stdout); 124211877Spjd fflush(stdout); 125211877Spjd } 126211877Spjd 127211877Spjd if (b < 0) 128211877Spjd err(EX_IOERR, "-%c file descriptor", 129211877Spjd conf.precrypted ? 'H' : 'h'); 130204076Spjd line[b] = '\0'; 131204076Spjd if ((p = strpbrk(line, "\r\n")) != NULL) 132204076Spjd *p = '\0'; 133204076Spjd if (!*line) 134204076Spjd errx(EX_DATAERR, "empty password read on file descriptor %d", 135204076Spjd conf.fd); 136204076Spjd if (conf.precrypted) { 137204076Spjd if (strchr(line, ':') != NULL) 138204076Spjd errx(EX_DATAERR, "bad encrypted password"); 139204076Spjd pwd->pw_passwd = line; 140204076Spjd } else { 141204076Spjd lc = login_getpwclass(pwd); 142204076Spjd if (lc == NULL || 143204076Spjd login_setcryptfmt(lc, "sha512", NULL) == NULL) 144204076Spjd warn("setting crypt(3) format"); 145204076Spjd login_close(lc); 146204076Spjd pwd->pw_passwd = pw_pwcrypt(line); 147204076Spjd } 148204076Spjd return (1); 149204076Spjd} 150204076Spjd 151204076Spjdint 152204076Spjdpw_usernext(struct userconf *cnf, bool quiet) 153204076Spjd{ 154204076Spjd uid_t next = pw_uidpolicy(cnf, -1); 155210879Spjd 156210879Spjd if (quiet) 157210879Spjd return (next); 158204076Spjd 159204076Spjd printf("%ju:", (uintmax_t)next); 160204076Spjd pw_groupnext(cnf, quiet); 161204076Spjd 162210879Spjd return (EXIT_SUCCESS); 163210879Spjd} 164210879Spjd 165204076Spjdstatic int 166204076Spjdpw_usershow(char *name, long id, struct passwd *fakeuser) 167204076Spjd{ 168204076Spjd struct passwd *pwd = NULL; 169204076Spjd 170204076Spjd if (id < 0 && name == NULL && !conf.all) 171204076Spjd errx(EX_DATAERR, "username or id or '-a' required"); 172204076Spjd 173204076Spjd if (conf.all) { 174204076Spjd SETPWENT(); 175204076Spjd while ((pwd = GETPWENT()) != NULL) 176204076Spjd print_user(pwd); 177204076Spjd ENDPWENT(); 178204076Spjd return (EXIT_SUCCESS); 179204076Spjd } 180204076Spjd 181204076Spjd pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 182204076Spjd if (pwd == NULL) { 183204076Spjd if (conf.force) { 184204076Spjd pwd = fakeuser; 185204076Spjd } else { 186223181Strociny if (name == NULL) 187220271Spjd errx(EX_NOUSER, "no such uid `%ld'", id); 188220271Spjd errx(EX_NOUSER, "no such user `%s'", name); 189220271Spjd } 190223181Strociny } 191220271Spjd 192204076Spjd return (print_user(pwd)); 193204076Spjd} 194204076Spjd 195204076Spjdstatic void 196204076Spjdperform_chgpwent(const char *name, struct passwd *pwd) 197204076Spjd{ 198204076Spjd int rc; 199204076Spjd 200204076Spjd rc = chgpwent(name, pwd); 201204076Spjd if (rc == -1) 202204076Spjd errx(EX_IOERR, "user '%s' does not exist (NIS?)", pwd->pw_name); 203204076Spjd else if (rc != 0) 204204076Spjd err(EX_IOERR, "passwd file update"); 205204076Spjd 206204076Spjd if (conf.userconf->nispasswd && *conf.userconf->nispasswd == '/') { 207204076Spjd rc = chgnispwent(conf.userconf->nispasswd, name, pwd); 208204076Spjd if (rc == -1) 209204076Spjd warn("User '%s' not found in NIS passwd", pwd->pw_name); 210204076Spjd else if (rc != 0) 211204076Spjd warn("NIS passwd update"); 212204076Spjd /* NOTE: NIS-only update errors are not fatal */ 213204076Spjd } 214204076Spjd} 215204076Spjd 216204076Spjd/* 217204076Spjd * The M_LOCK and M_UNLOCK functions simply add or remove 218204076Spjd * a "*LOCKED*" prefix from in front of the password to 219204076Spjd * prevent it decoding correctly, and therefore prevents 220204076Spjd * access. Of course, this only prevents access via 221204076Spjd * password authentication (not ssh, kerberos or any 222204076Spjd * other method that does not use the UNIX password) but 223204076Spjd * that is a known limitation. 224204076Spjd */ 225204076Spjdstatic int 226204076Spjdpw_userlock(char *name, long id, int mode) 227204076Spjd{ 228204076Spjd struct passwd *pwd = NULL; 229204076Spjd char *passtmp = NULL; 230204076Spjd bool locked = false; 231204076Spjd 232204076Spjd if (id < 0 && name == NULL) 233204076Spjd errx(EX_DATAERR, "username or id required"); 234204076Spjd 235204076Spjd pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 236204076Spjd if (pwd == NULL) { 237204076Spjd if (name == NULL) 238204076Spjd errx(EX_NOUSER, "no such uid `%ld'", id); 239204076Spjd errx(EX_NOUSER, "no such user `%s'", name); 240204076Spjd } 241204076Spjd 242204076Spjd if (name == NULL) 243204076Spjd name = pwd->pw_name; 244204076Spjd 245204076Spjd if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str) -1) == 0) 246204076Spjd locked = true; 247204076Spjd if (mode == M_LOCK && locked) 248204076Spjd errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 249204076Spjd if (mode == M_UNLOCK && !locked) 250214284Spjd errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 251214284Spjd 252214284Spjd if (mode == M_LOCK) { 253214284Spjd asprintf(&passtmp, "%s%s", locked_str, pwd->pw_passwd); 254204076Spjd if (passtmp == NULL) /* disaster */ 255218138Spjd errx(EX_UNAVAILABLE, "out of memory"); 256204076Spjd pwd->pw_passwd = passtmp; 257204076Spjd } else { 258204076Spjd pwd->pw_passwd += sizeof(locked_str)-1; 259214284Spjd } 260214284Spjd 261214284Spjd perform_chgpwent(name, pwd); 262214284Spjd free(passtmp); 263214284Spjd 264214284Spjd return (EXIT_SUCCESS); 265214284Spjd} 266220865Spjd 267204076Spjd/*- 268219830Spjd * -C config configuration file 269219830Spjd * -q quiet operation 270219830Spjd * -n name login name 271219830Spjd * -u uid user id 272219830Spjd * -c comment user name/comment 273219830Spjd * -d directory home directory 274219830Spjd * -e date account expiry date 275219830Spjd * -p date password expiry date 276219830Spjd * -g grp primary group 277219830Spjd * -G grp1,grp2 additional groups 278219830Spjd * -m [ -k dir ] create and set up home 279219830Spjd * -s shell name of login shell 280219831Spjd * -o duplicate uid ok 281219830Spjd * -L class user class 282204076Spjd * -l name new login name 283226842Spjd * -h fd password filehandle 284204076Spjd * -H fd encrypted password filehandle 285204076Spjd * -F force print or add 286226842Spjd * Setting defaults: 287204076Spjd * -D set user defaults 288204076Spjd * -b dir default home root dir 289226842Spjd * -e period default expiry period 290204076Spjd * -p period default password change period 291204076Spjd * -g group default group 292204076Spjd * -G grp1,grp2.. default additional groups 293204076Spjd * -L class default login class 294204076Spjd * -k dir default home skeleton 295204076Spjd * -s shell default shell 296204076Spjd * -w method default password method 297204076Spjd */ 298204076Spjd 299204076Spjdint 300204076Spjdpw_user(int mode, char *name, long id, struct cargs * args) 301204076Spjd{ 302204076Spjd int rc, edited = 0; 303204076Spjd char *p = NULL; 304204076Spjd struct carg *arg; 305204076Spjd struct passwd *pwd = NULL; 306204076Spjd struct group *grp; 307204076Spjd struct stat st; 308204076Spjd struct userconf *cnf; 309204076Spjd char line[_PASSWORD_LEN+1]; 310204076Spjd char path[MAXPATHLEN]; 311204076Spjd FILE *fp; 312204076Spjd char *dmode_c; 313204076Spjd void *set = NULL; 314204076Spjd int valid_type = _PWF_FILES; 315204076Spjd 316204076Spjd static struct passwd fakeuser = 317204076Spjd { 318204076Spjd "nouser", 319204076Spjd "*", 320204076Spjd -1, 321204076Spjd -1, 322204076Spjd 0, 323204076Spjd "", 324204076Spjd "User &", 325204076Spjd "/nonexistent", 326204076Spjd "/bin/sh", 327204076Spjd 0 328204076Spjd#if defined(__FreeBSD__) 329218138Spjd ,0 330204076Spjd#endif 331204076Spjd }; 332204076Spjd 333204076Spjd cnf = conf.userconf; 334204076Spjd 335204076Spjd if (mode == M_NEXT) 336204076Spjd return (pw_usernext(cnf, conf.quiet)); 337204076Spjd 338204076Spjd if (mode == M_PRINT) 339204076Spjd return (pw_usershow(name, id, &fakeuser)); 340204076Spjd 341204076Spjd if (mode == M_DELETE) 342204076Spjd return (pw_userdel(name, id)); 343204076Spjd 344204076Spjd if (mode == M_LOCK || mode == M_UNLOCK) 345204076Spjd return (pw_userlock(name, id, mode)); 346204076Spjd 347204076Spjd /* 348220007Spjd * We can do all of the common legwork here 349204076Spjd */ 350214276Spjd 351204076Spjd if ((arg = getarg(args, 'b')) != NULL) { 352204076Spjd cnf->home = arg->val; 353214275Spjd } 354214275Spjd 355209182Spjd if ((arg = getarg(args, 'M')) != NULL) { 356223181Strociny dmode_c = arg->val; 357220271Spjd if ((set = setmode(dmode_c)) == NULL) 358220271Spjd errx(EX_DATAERR, "invalid directory creation mode '%s'", 359220271Spjd dmode_c); 360223181Strociny cnf->homemode = getmode(set, _DEF_DIRMODE); 361204076Spjd free(set); 362204076Spjd } 363204076Spjd 364212038Spjd /* 365204076Spjd * If we'll need to use it or we're updating it, 366204076Spjd * then create the base home directory if necessary 367204076Spjd */ 368204076Spjd if (arg != NULL || getarg(args, 'm') != NULL) { 369204076Spjd int l = strlen(cnf->home); 370204076Spjd 371204076Spjd if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 372213009Spjd cnf->home[--l] = '\0'; 373204076Spjd 374204076Spjd if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 375219482Strociny errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 376204076Spjd 377204076Spjd if (stat(cnf->home, &st) == -1) { 378204076Spjd char dbuf[MAXPATHLEN]; 379204076Spjd 380219818Spjd /* 381204076Spjd * This is a kludge especially for Joerg :) 382204076Spjd * If the home directory would be created in the root partition, then 383204076Spjd * we really create it under /usr which is likely to have more space. 384204076Spjd * But we create a symlink from cnf->home -> "/usr" -> cnf->home 385212038Spjd */ 386212038Spjd if (strchr(cnf->home+1, '/') == NULL) { 387212038Spjd snprintf(dbuf, MAXPATHLEN, "/usr%s", cnf->home); 388219818Spjd if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) { 389212038Spjd chown(dbuf, 0, 0); 390212038Spjd /* 391212038Spjd * Skip first "/" and create symlink: 392212038Spjd * /home -> usr/home 393204076Spjd */ 394204076Spjd symlink(dbuf+1, cnf->home); 395204076Spjd } 396204076Spjd /* If this falls, fall back to old method */ 397204076Spjd } 398204076Spjd strlcpy(dbuf, cnf->home, sizeof(dbuf)); 399204076Spjd p = dbuf; 400204076Spjd if (stat(dbuf, &st) == -1) { 401204076Spjd while ((p = strchr(p + 1, '/')) != NULL) { 402204076Spjd *p = '\0'; 403204076Spjd if (stat(dbuf, &st) == -1) { 404204076Spjd if (mkdir(dbuf, _DEF_DIRMODE) == -1) 405204076Spjd err(EX_OSFILE, "mkdir '%s'", dbuf); 406212038Spjd chown(dbuf, 0, 0); 407212038Spjd } else if (!S_ISDIR(st.st_mode)) 408218043Spjd errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 409218043Spjd *p = '/'; 410204076Spjd } 411204076Spjd } 412204076Spjd if (stat(dbuf, &st) == -1) { 413211977Spjd if (mkdir(dbuf, _DEF_DIRMODE) == -1) 414211984Spjd err(EX_OSFILE, "mkdir '%s'", dbuf); 415218043Spjd chown(dbuf, 0, 0); 416219482Strociny } 417211984Spjd } else if (!S_ISDIR(st.st_mode)) 418218043Spjd errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 419218043Spjd } 420218043Spjd 421218043Spjd if (conf.expire_days > 0) 422218043Spjd cnf->expire_days = conf.expire_days; 423204076Spjd 424218045Spjd if ((arg = getarg(args, 'y')) != NULL) 425218045Spjd cnf->nispasswd = arg->val; 426218043Spjd 427219482Strociny if (conf.password_days > 0) 428218043Spjd cnf->password_days = conf.password_days; 429220005Spjd 430204076Spjd if ((arg = getarg(args, 'g')) != NULL) { 431213009Spjd if (!*(p = arg->val)) /* Handle empty group list specially */ 432213009Spjd cnf->default_group = ""; 433210880Spjd else { 434207371Spjd if ((grp = GETGRNAM(p)) == NULL) { 435219721Strociny if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 436207371Spjd errx(EX_NOUSER, "group `%s' does not exist", p); 437207371Spjd } 438207371Spjd cnf->default_group = newstr(grp->gr_name); 439207371Spjd } 440204076Spjd } 441213007Spjd if ((arg = getarg(args, 'L')) != NULL) 442213007Spjd cnf->default_class = pw_checkname(arg->val, 0); 443221899Spjd 444218049Spjd if ((arg = getarg(args, 'G')) != NULL && arg->val) { 445218214Spjd for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 446218049Spjd if ((grp = GETGRNAM(p)) == NULL) { 447213007Spjd if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 448213007Spjd errx(EX_NOUSER, "group `%s' does not exist", p); 449213007Spjd } 450213007Spjd sl_add(cnf->groups, newstr(grp->gr_name)); 451213007Spjd } 452213007Spjd } 453213007Spjd 454213007Spjd if ((arg = getarg(args, 'k')) != NULL) { 455213007Spjd char *tmp = cnf->dotdir = arg->val; 456218138Spjd if (*tmp == '/') 457213007Spjd tmp++; 458204076Spjd if ((fstatat(conf.rootfd, tmp, &st, 0) == -1) || 459212038Spjd !S_ISDIR(st.st_mode)) 460204076Spjd errx(EX_OSFILE, "skeleton `%s' is not a directory or " 461204076Spjd "does not exist", cnf->dotdir); 462218138Spjd } 463204076Spjd 464218138Spjd if ((arg = getarg(args, 's')) != NULL) 465213007Spjd cnf->shell_default = arg->val; 466204076Spjd 467204076Spjd if ((arg = getarg(args, 'w')) != NULL) 468204076Spjd cnf->default_password = boolean_val(arg->val, cnf->default_password); 469204076Spjd if (mode == M_ADD && getarg(args, 'D')) { 470204076Spjd if (name != NULL) 471204076Spjd errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 472204076Spjd if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 473204076Spjd if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 474204076Spjd cnf->min_uid = 1000; 475204076Spjd if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 476204076Spjd cnf->max_uid = 32000; 477204076Spjd } 478204076Spjd if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 479204076Spjd if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 480204076Spjd cnf->min_gid = 1000; 481204076Spjd if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 482204076Spjd cnf->max_gid = 32000; 483204076Spjd } 484204076Spjd 485204076Spjd if (write_userconfig(conf.config)) 486204076Spjd return (EXIT_SUCCESS); 487204076Spjd err(EX_IOERR, "config udpate"); 488204076Spjd } 489204076Spjd 490204076Spjd if (name != NULL) 491204076Spjd pwd = GETPWNAM(pw_checkname(name, 0)); 492204076Spjd 493204076Spjd if (id < 0 && name == NULL) 494204076Spjd errx(EX_DATAERR, "user name or id required"); 495204076Spjd 496204076Spjd /* 497204076Spjd * Update require that the user exists 498211882Spjd */ 499211882Spjd if (mode == M_UPDATE) { 500211882Spjd 501204076Spjd if (name == NULL && pwd == NULL) /* Try harder */ 502204076Spjd pwd = GETPWUID(id); 503204076Spjd 504204076Spjd if (pwd == NULL) { 505204076Spjd if (name == NULL) 506204076Spjd errx(EX_NOUSER, "no such uid `%ld'", id); 507204076Spjd errx(EX_NOUSER, "no such user `%s'", name); 508204076Spjd } 509204076Spjd 510204076Spjd if (conf.userconf->nispasswd && *conf.userconf->nispasswd == '/') 511204076Spjd valid_type = _PWF_NIS; 512204076Spjd 513204076Spjd if (PWF._altdir == PWF_REGULAR && 514204076Spjd ((pwd->pw_fields & _PWF_SOURCE) != valid_type)) 515204076Spjd errx(EX_NOUSER, "no such %s user `%s'", 516204076Spjd valid_type == _PWF_FILES ? "local" : "NIS" , name); 517204076Spjd 518204076Spjd if (name == NULL) 519204076Spjd name = pwd->pw_name; 520204076Spjd 521222164Spjd /* 522211882Spjd * The rest is edit code 523211882Spjd */ 524204076Spjd if (conf.newname != NULL) { 525204076Spjd if (strcmp(pwd->pw_name, "root") == 0) 526204076Spjd errx(EX_DATAERR, "can't rename `root' account"); 527204076Spjd pwd->pw_name = pw_checkname(conf.newname, 0); 528204076Spjd edited = 1; 529204076Spjd } 530204076Spjd 531204076Spjd if (id > 0 && isdigit((unsigned char)*arg->val)) { 532204076Spjd pwd->pw_uid = (uid_t)id; 533204076Spjd edited = 1; 534204076Spjd if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 535204076Spjd errx(EX_DATAERR, "can't change uid of `root' account"); 536204076Spjd if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 537204076Spjd warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 538204076Spjd } 539204076Spjd 540204076Spjd if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 541204076Spjd gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 542204076Spjd if (newgid != pwd->pw_gid) { 543204076Spjd edited = 1; 544204076Spjd pwd->pw_gid = newgid; 545204076Spjd } 546204076Spjd } 547204076Spjd 548204076Spjd if ((arg = getarg(args, 'p')) != NULL) { 549204076Spjd if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 550204076Spjd if (pwd->pw_change != 0) { 551204076Spjd pwd->pw_change = 0; 552204076Spjd edited = 1; 553204076Spjd } 554204076Spjd } 555204076Spjd else { 556204076Spjd time_t now = time(NULL); 557204076Spjd time_t expire = parse_date(now, arg->val); 558204076Spjd 559204076Spjd if (pwd->pw_change != expire) { 560204076Spjd pwd->pw_change = expire; 561204076Spjd edited = 1; 562204076Spjd } 563204076Spjd } 564204076Spjd } 565204076Spjd 566204076Spjd if ((arg = getarg(args, 'e')) != NULL) { 567204076Spjd if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 568204076Spjd if (pwd->pw_expire != 0) { 569204076Spjd pwd->pw_expire = 0; 570204076Spjd edited = 1; 571204076Spjd } 572204076Spjd } 573204076Spjd else { 574204076Spjd time_t now = time(NULL); 575204076Spjd time_t expire = parse_date(now, arg->val); 576204076Spjd 577204076Spjd if (pwd->pw_expire != expire) { 578204076Spjd pwd->pw_expire = expire; 579204076Spjd edited = 1; 580204076Spjd } 581204076Spjd } 582212899Spjd } 583211984Spjd 584211984Spjd if ((arg = getarg(args, 's')) != NULL) { 585211984Spjd char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 586211984Spjd if (shell == NULL) 587218138Spjd shell = ""; 588211984Spjd if (strcmp(shell, pwd->pw_shell) != 0) { 589211984Spjd pwd->pw_shell = shell; 590211984Spjd edited = 1; 591212038Spjd } 592211984Spjd } 593211984Spjd 594211984Spjd if (getarg(args, 'L')) { 595204076Spjd if (cnf->default_class == NULL) 596204076Spjd cnf->default_class = ""; 597204076Spjd if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 598204076Spjd pwd->pw_class = cnf->default_class; 599204076Spjd edited = 1; 600204076Spjd } 601204076Spjd } 602204076Spjd 603204076Spjd if ((arg = getarg(args, 'd')) != NULL) { 604204076Spjd if (strcmp(pwd->pw_dir, arg->val)) 605204076Spjd edited = 1; 606211877Spjd if (stat(pwd->pw_dir = arg->val, &st) == -1) { 607204076Spjd if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 608204076Spjd warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 609211984Spjd } else if (!S_ISDIR(st.st_mode)) 610204076Spjd warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 611204076Spjd } 612211877Spjd 613211877Spjd if ((arg = getarg(args, 'w')) != NULL && conf.fd == -1) { 614211877Spjd login_cap_t *lc; 615211877Spjd 616211877Spjd lc = login_getpwclass(pwd); 617211877Spjd if (lc == NULL || 618211877Spjd login_setcryptfmt(lc, "sha512", NULL) == NULL) 619222228Spjd warn("setting crypt(3) format"); 620222228Spjd login_close(lc); 621222228Spjd pwd->pw_passwd = pw_password(cnf, pwd->pw_name); 622222228Spjd edited = 1; 623222228Spjd } 624222228Spjd 625222228Spjd } else { 626222228Spjd login_cap_t *lc; 627222228Spjd 628222228Spjd /* 629222228Spjd * Add code 630222228Spjd */ 631222228Spjd 632222228Spjd if (name == NULL) /* Required */ 633204076Spjd errx(EX_DATAERR, "login name required"); 634204076Spjd else if ((pwd = GETPWNAM(name)) != NULL) /* Exists */ 635211882Spjd errx(EX_DATAERR, "login name `%s' already exists", name); 636211882Spjd 637211882Spjd /* 638211882Spjd * Now, set up defaults for a new user 639211882Spjd */ 640211882Spjd pwd = &fakeuser; 641211882Spjd pwd->pw_name = name; 642211882Spjd pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 643204076Spjd pwd->pw_uid = pw_uidpolicy(cnf, id); 644204076Spjd pwd->pw_gid = pw_gidpolicy(args, pwd->pw_name, (gid_t) pwd->pw_uid); 645211984Spjd pwd->pw_change = pw_pwdpolicy(cnf, args); 646212051Spjd pwd->pw_expire = pw_exppolicy(cnf, args); 647204076Spjd pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 648204076Spjd pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 649204076Spjd lc = login_getpwclass(pwd); 650204076Spjd if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL) 651211877Spjd warn("setting crypt(3) format"); 652204076Spjd login_close(lc); 653204076Spjd pwd->pw_passwd = pw_password(cnf, pwd->pw_name); 654204076Spjd edited = 1; 655204076Spjd 656204076Spjd if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 657204076Spjd warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 658204076Spjd } 659204076Spjd 660204076Spjd /* 661204076Spjd * Shared add/edit code 662204076Spjd */ 663204076Spjd if (conf.gecos != NULL) { 664204076Spjd if (strcmp(pwd->pw_gecos, conf.gecos) != 0) { 665204076Spjd pwd->pw_gecos = conf.gecos; 666204076Spjd edited = 1; 667225832Spjd } 668204076Spjd } 669204076Spjd 670204076Spjd if (conf.fd != -1) 671204076Spjd edited = set_passwd(pwd, mode == M_UPDATE); 672204076Spjd 673211877Spjd /* 674204076Spjd * Special case: -N only displays & exits 675204076Spjd */ 676204076Spjd if (conf.dryrun) 677204076Spjd return print_user(pwd); 678204076Spjd 679204076Spjd if (mode == M_ADD) { 680204076Spjd edited = 1; /* Always */ 681204076Spjd rc = addpwent(pwd); 682204076Spjd if (rc == -1) 683204076Spjd errx(EX_IOERR, "user '%s' already exists", 684204076Spjd pwd->pw_name); 685204076Spjd else if (rc != 0) 686204076Spjd err(EX_IOERR, "passwd file update"); 687204076Spjd if (cnf->nispasswd && *cnf->nispasswd=='/') { 688204076Spjd rc = addnispwent(cnf->nispasswd, pwd); 689204076Spjd if (rc == -1) 690204076Spjd warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 691204076Spjd else if (rc != 0) 692204076Spjd warn("NIS passwd update"); 693204076Spjd /* NOTE: we treat NIS-only update errors as non-fatal */ 694204076Spjd } 695204076Spjd } else if (mode == M_UPDATE && edited) /* Only updated this if required */ 696204076Spjd perform_chgpwent(name, pwd); 697204076Spjd 698204076Spjd /* 699204076Spjd * Ok, user is created or changed - now edit group file 700204076Spjd */ 701204076Spjd 702225831Spjd if (mode == M_ADD || getarg(args, 'G') != NULL) { 703204076Spjd int j; 704204076Spjd size_t i; 705225832Spjd /* First remove the user from all group */ 706204076Spjd SETGRENT(); 707204076Spjd while ((grp = GETGRENT()) != NULL) { 708204076Spjd char group[MAXLOGNAME]; 709204076Spjd if (grp->gr_mem == NULL) 710204076Spjd continue; 711204076Spjd for (i = 0; grp->gr_mem[i] != NULL; i++) { 712204076Spjd if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0) 713204076Spjd continue; 714204076Spjd for (j = i; grp->gr_mem[j] != NULL ; j++) 715204076Spjd grp->gr_mem[j] = grp->gr_mem[j+1]; 716204076Spjd strlcpy(group, grp->gr_name, MAXLOGNAME); 717204076Spjd chggrent(group, grp); 718204076Spjd } 719204076Spjd } 720204076Spjd ENDGRENT(); 721204076Spjd 722204076Spjd /* now add to group where needed */ 723204076Spjd for (i = 0; i < cnf->groups->sl_cur; i++) { 724204076Spjd grp = GETGRNAM(cnf->groups->sl_str[i]); 725204076Spjd grp = gr_add(grp, pwd->pw_name); 726204076Spjd /* 727204076Spjd * grp can only be NULL in 2 cases: 728204076Spjd * - the new member is already a member 729204076Spjd * - a problem with memory occurs 730204076Spjd * in both cases we want to skip now. 731204076Spjd */ 732204076Spjd if (grp == NULL) 733204076Spjd continue; 734204076Spjd chggrent(grp->gr_name, grp); 735204076Spjd free(grp); 736204076Spjd } 737204076Spjd } 738204076Spjd 739204076Spjd 740225832Spjd /* go get a current version of pwd */ 741225832Spjd pwd = GETPWNAM(name); 742225832Spjd if (pwd == NULL) { 743225832Spjd /* This will fail when we rename, so special case that */ 744225832Spjd if (mode == M_UPDATE && conf.newname != NULL) { 745225832Spjd name = conf.newname; /* update new name */ 746204076Spjd pwd = GETPWNAM(name); /* refetch renamed rec */ 747225832Spjd } 748225832Spjd } 749225832Spjd if (pwd == NULL) /* can't go on without this */ 750204076Spjd errx(EX_NOUSER, "user '%s' disappeared during update", name); 751225832Spjd 752204076Spjd grp = GETGRGID(pwd->pw_gid); 753225832Spjd pw_log(cnf, mode, W_USER, "%s(%ju):%s(%ju):%s:%s:%s", 754204076Spjd pwd->pw_name, (uintmax_t)pwd->pw_uid, 755204076Spjd grp ? grp->gr_name : "unknown", (uintmax_t)(grp ? grp->gr_gid : (uid_t)-1), 756225832Spjd pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 757204076Spjd 758204076Spjd /* 759204076Spjd * If adding, let's touch and chown the user's mail file. This is not 760204076Spjd * strictly necessary under BSD with a 0755 maildir but it also 761204076Spjd * doesn't hurt anything to create the empty mailfile 762211877Spjd */ 763204076Spjd if (mode == M_ADD) { 764204076Spjd if (PWALTDIR() != PWF_ALT) { 765204076Spjd snprintf(path, sizeof(path), "%s/%s", _PATH_MAILDIR, 766204076Spjd pwd->pw_name); 767204076Spjd close(openat(conf.rootfd, path +1, O_RDWR | O_CREAT, 768204076Spjd 0600)); /* Preserve contents & mtime */ 769204076Spjd fchownat(conf.rootfd, path + 1, pwd->pw_uid, 770204076Spjd pwd->pw_gid, AT_SYMLINK_NOFOLLOW); 771204076Spjd } 772204076Spjd } 773204076Spjd 774204076Spjd /* 775204076Spjd * Let's create and populate the user's home directory. Note 776204076Spjd * that this also `works' for editing users if -m is used, but 777204076Spjd * existing files will *not* be overwritten. 778204076Spjd */ 779204076Spjd if (PWALTDIR() != PWF_ALT && getarg(args, 'm') != NULL && pwd->pw_dir && 780204076Spjd *pwd->pw_dir == '/' && pwd->pw_dir[1]) 781204076Spjd create_and_populate_homedir(pwd); 782211877Spjd 783204076Spjd /* 784204076Spjd * Finally, send mail to the new user as well, if we are asked to 785204076Spjd */ 786204076Spjd if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 787204076Spjd FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 788204076Spjd 789204076Spjd if (pfp == NULL) 790204076Spjd warn("sendmail"); 791204076Spjd else { 792204076Spjd fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 793204076Spjd while (fgets(line, sizeof(line), fp) != NULL) { 794204076Spjd /* Do substitutions? */ 795204076Spjd fputs(line, pfp); 796204076Spjd } 797204076Spjd pclose(pfp); 798204076Spjd pw_log(cnf, mode, W_USER, "%s(%ju) new user mail sent", 799204076Spjd pwd->pw_name, (uintmax_t)pwd->pw_uid); 800204076Spjd } 801204076Spjd fclose(fp); 802204076Spjd } 803204076Spjd 804204076Spjd return EXIT_SUCCESS; 805225782Spjd} 806225782Spjd 807204076Spjd 808204076Spjdstatic uid_t 809204076Spjdpw_uidpolicy(struct userconf * cnf, long id) 810204076Spjd{ 811204076Spjd struct passwd *pwd; 812211984Spjd uid_t uid = (uid_t) - 1; 813204076Spjd 814204076Spjd /* 815209185Spjd * Check the given uid, if any 816204076Spjd */ 817204076Spjd if (id >= 0) { 818204076Spjd uid = (uid_t) id; 819211877Spjd 820204076Spjd if ((pwd = GETPWUID(uid)) != NULL && conf.checkduplicate) 821204076Spjd errx(EX_DATAERR, "uid `%ju' has already been allocated", 822204076Spjd (uintmax_t)pwd->pw_uid); 823204076Spjd } else { 824 struct bitmap bm; 825 826 /* 827 * We need to allocate the next available uid under one of 828 * two policies a) Grab the first unused uid b) Grab the 829 * highest possible unused uid 830 */ 831 if (cnf->min_uid >= cnf->max_uid) { /* Sanity 832 * claus^H^H^H^Hheck */ 833 cnf->min_uid = 1000; 834 cnf->max_uid = 32000; 835 } 836 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 837 838 /* 839 * Now, let's fill the bitmap from the password file 840 */ 841 SETPWENT(); 842 while ((pwd = GETPWENT()) != NULL) 843 if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 844 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 845 ENDPWENT(); 846 847 /* 848 * Then apply the policy, with fallback to reuse if necessary 849 */ 850 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 851 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 852 853 /* 854 * Another sanity check 855 */ 856 if (uid < cnf->min_uid || uid > cnf->max_uid) 857 errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 858 bm_dealloc(&bm); 859 } 860 return uid; 861} 862 863 864static uid_t 865pw_gidpolicy(struct cargs * args, char *nam, gid_t prefer) 866{ 867 struct group *grp; 868 gid_t gid = (uid_t) - 1; 869 struct carg *a_gid = getarg(args, 'g'); 870 struct userconf *cnf = conf.userconf; 871 872 /* 873 * If no arg given, see if default can help out 874 */ 875 if (a_gid == NULL && cnf->default_group && *cnf->default_group) 876 a_gid = addarg(args, 'g', cnf->default_group); 877 878 /* 879 * Check the given gid, if any 880 */ 881 SETGRENT(); 882 if (a_gid != NULL) { 883 if ((grp = GETGRNAM(a_gid->val)) == NULL) { 884 gid = (gid_t) atol(a_gid->val); 885 if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 886 errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 887 } 888 gid = grp->gr_gid; 889 } else if ((grp = GETGRNAM(nam)) != NULL && 890 (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) { 891 gid = grp->gr_gid; /* Already created? Use it anyway... */ 892 } else { 893 gid_t grid = -1; 894 895 /* 896 * We need to auto-create a group with the user's name. We 897 * can send all the appropriate output to our sister routine 898 * bit first see if we can create a group with gid==uid so we 899 * can keep the user and group ids in sync. We purposely do 900 * NOT check the gid range if we can force the sync. If the 901 * user's name dups an existing group, then the group add 902 * function will happily handle that case for us and exit. 903 */ 904 if (GETGRGID(prefer) == NULL) 905 grid = prefer; 906 if (conf.dryrun) { 907 gid = pw_groupnext(cnf, true); 908 } else { 909 pw_group(M_ADD, nam, grid, NULL); 910 if ((grp = GETGRNAM(nam)) != NULL) 911 gid = grp->gr_gid; 912 } 913 } 914 ENDGRENT(); 915 return gid; 916} 917 918 919static time_t 920pw_pwdpolicy(struct userconf * cnf, struct cargs * args) 921{ 922 time_t result = 0; 923 time_t now = time(NULL); 924 struct carg *arg = getarg(args, 'p'); 925 926 if (arg != NULL) { 927 if ((result = parse_date(now, arg->val)) == now) 928 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 929 } else if (cnf->password_days > 0) 930 result = now + ((long) cnf->password_days * 86400L); 931 return result; 932} 933 934 935static time_t 936pw_exppolicy(struct userconf * cnf, struct cargs * args) 937{ 938 time_t result = 0; 939 time_t now = time(NULL); 940 struct carg *arg = getarg(args, 'e'); 941 942 if (arg != NULL) { 943 if ((result = parse_date(now, arg->val)) == now) 944 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 945 } else if (cnf->expire_days > 0) 946 result = now + ((long) cnf->expire_days * 86400L); 947 return result; 948} 949 950 951static char * 952pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 953{ 954 struct carg *arg = getarg(args, 'd'); 955 static char home[128]; 956 957 if (arg) 958 return (arg->val); 959 960 if (cnf->home == NULL || *cnf->home == '\0') 961 errx(EX_CONFIG, "no base home directory set"); 962 snprintf(home, sizeof(home), "%s/%s", cnf->home, user); 963 964 return (home); 965} 966 967static char * 968shell_path(char const * path, char *shells[], char *sh) 969{ 970 if (sh != NULL && (*sh == '/' || *sh == '\0')) 971 return sh; /* specified full path or forced none */ 972 else { 973 char *p; 974 char paths[_UC_MAXLINE]; 975 976 /* 977 * We need to search paths 978 */ 979 strlcpy(paths, path, sizeof(paths)); 980 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 981 int i; 982 static char shellpath[256]; 983 984 if (sh != NULL) { 985 snprintf(shellpath, sizeof(shellpath), "%s/%s", p, sh); 986 if (access(shellpath, X_OK) == 0) 987 return shellpath; 988 } else 989 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 990 snprintf(shellpath, sizeof(shellpath), "%s/%s", p, shells[i]); 991 if (access(shellpath, X_OK) == 0) 992 return shellpath; 993 } 994 } 995 if (sh == NULL) 996 errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 997 errx(EX_CONFIG, "no default shell available or defined"); 998 return NULL; 999 } 1000} 1001 1002 1003static char * 1004pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 1005{ 1006 char *sh = newshell; 1007 struct carg *arg = getarg(args, 's'); 1008 1009 if (newshell == NULL && arg != NULL) 1010 sh = arg->val; 1011 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 1012} 1013 1014#define SALTSIZE 32 1015 1016static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./"; 1017 1018char * 1019pw_pwcrypt(char *password) 1020{ 1021 int i; 1022 char salt[SALTSIZE + 1]; 1023 char *cryptpw; 1024 1025 static char buf[256]; 1026 1027 /* 1028 * Calculate a salt value 1029 */ 1030 for (i = 0; i < SALTSIZE; i++) 1031 salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)]; 1032 salt[SALTSIZE] = '\0'; 1033 1034 cryptpw = crypt(password, salt); 1035 if (cryptpw == NULL) 1036 errx(EX_CONFIG, "crypt(3) failure"); 1037 return strcpy(buf, cryptpw); 1038} 1039 1040 1041static char * 1042pw_password(struct userconf * cnf, char const * user) 1043{ 1044 int i, l; 1045 char pwbuf[32]; 1046 1047 switch (cnf->default_password) { 1048 case -1: /* Random password */ 1049 l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 1050 for (i = 0; i < l; i++) 1051 pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)]; 1052 pwbuf[i] = '\0'; 1053 1054 /* 1055 * We give this information back to the user 1056 */ 1057 if (conf.fd == -1 && !conf.dryrun) { 1058 if (isatty(STDOUT_FILENO)) 1059 printf("Password for '%s' is: ", user); 1060 printf("%s\n", pwbuf); 1061 fflush(stdout); 1062 } 1063 break; 1064 1065 case -2: /* No password at all! */ 1066 return ""; 1067 1068 case 0: /* No login - default */ 1069 default: 1070 return "*"; 1071 1072 case 1: /* user's name */ 1073 strlcpy(pwbuf, user, sizeof(pwbuf)); 1074 break; 1075 } 1076 return pw_pwcrypt(pwbuf); 1077} 1078 1079static int 1080pw_userdel(char *name, long id) 1081{ 1082 struct passwd *pwd = NULL; 1083 char file[MAXPATHLEN]; 1084 char home[MAXPATHLEN]; 1085 uid_t uid; 1086 struct group *gr, *grp; 1087 char grname[LOGNAMESIZE]; 1088 int rc; 1089 struct stat st; 1090 int valid_type = _PWF_FILES; 1091 1092 if (id < 0 && name == NULL) 1093 errx(EX_DATAERR, "username or id required"); 1094 1095 pwd = (name != NULL) ? GETPWNAM(pw_checkname(name, 0)) : GETPWUID(id); 1096 if (pwd == NULL) { 1097 if (name == NULL) 1098 errx(EX_NOUSER, "no such uid `%ld'", id); 1099 errx(EX_NOUSER, "no such user `%s'", name); 1100 } 1101 1102 if (conf.userconf->nispasswd && *conf.userconf->nispasswd == '/') 1103 valid_type = _PWF_NIS; 1104 1105 if (PWF._altdir == PWF_REGULAR && 1106 ((pwd->pw_fields & _PWF_SOURCE) != valid_type)) 1107 errx(EX_NOUSER, "no such %s user `%s'", 1108 valid_type == _PWF_FILES ? "local" : "NIS" , name); 1109 1110 uid = pwd->pw_uid; 1111 if (name == NULL) 1112 name = pwd->pw_name; 1113 1114 if (strcmp(pwd->pw_name, "root") == 0) 1115 errx(EX_DATAERR, "cannot remove user 'root'"); 1116 1117 /* Remove opie record from /etc/opiekeys */ 1118 1119 if (PWALTDIR() != PWF_ALT) 1120 rmopie(pwd->pw_name); 1121 1122 if (!PWALTDIR()) { 1123 /* Remove crontabs */ 1124 snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name); 1125 if (access(file, F_OK) == 0) { 1126 snprintf(file, sizeof(file), "crontab -u %s -r", pwd->pw_name); 1127 system(file); 1128 } 1129 } 1130 /* 1131 * Save these for later, since contents of pwd may be 1132 * invalidated by deletion 1133 */ 1134 snprintf(file, sizeof(file), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 1135 strlcpy(home, pwd->pw_dir, sizeof(home)); 1136 gr = GETGRGID(pwd->pw_gid); 1137 if (gr != NULL) 1138 strlcpy(grname, gr->gr_name, LOGNAMESIZE); 1139 else 1140 grname[0] = '\0'; 1141 1142 rc = delpwent(pwd); 1143 if (rc == -1) 1144 err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 1145 else if (rc != 0) 1146 err(EX_IOERR, "passwd update"); 1147 1148 if (conf.userconf->nispasswd && *conf.userconf->nispasswd=='/') { 1149 rc = delnispwent(conf.userconf->nispasswd, name); 1150 if (rc == -1) 1151 warnx("WARNING: user '%s' does not exist in NIS passwd", 1152 pwd->pw_name); 1153 else if (rc != 0) 1154 warn("WARNING: NIS passwd update"); 1155 /* non-fatal */ 1156 } 1157 1158 grp = GETGRNAM(name); 1159 if (grp != NULL && 1160 (grp->gr_mem == NULL || *grp->gr_mem == NULL) && 1161 strcmp(name, grname) == 0) 1162 delgrent(GETGRNAM(name)); 1163 SETGRENT(); 1164 while ((grp = GETGRENT()) != NULL) { 1165 int i, j; 1166 char group[MAXLOGNAME]; 1167 if (grp->gr_mem == NULL) 1168 continue; 1169 1170 for (i = 0; grp->gr_mem[i] != NULL; i++) { 1171 if (strcmp(grp->gr_mem[i], name) != 0) 1172 continue; 1173 1174 for (j = i; grp->gr_mem[j] != NULL; j++) 1175 grp->gr_mem[j] = grp->gr_mem[j+1]; 1176 strlcpy(group, grp->gr_name, MAXLOGNAME); 1177 chggrent(group, grp); 1178 } 1179 } 1180 ENDGRENT(); 1181 1182 pw_log(conf.userconf, M_DELETE, W_USER, "%s(%ju) account removed", name, 1183 (uintmax_t)uid); 1184 1185 /* Remove mail file */ 1186 if (PWALTDIR() != PWF_ALT) 1187 unlinkat(conf.rootfd, file + 1, 0); 1188 1189 /* Remove at jobs */ 1190 if (!PWALTDIR() && getpwuid(uid) == NULL) 1191 rmat(uid); 1192 1193 /* Remove home directory and contents */ 1194 if (PWALTDIR() != PWF_ALT && conf.deletehome && *home == '/' && 1195 getpwuid(uid) == NULL && 1196 fstatat(conf.rootfd, home + 1, &st, 0) != -1) { 1197 rm_r(conf.rootfd, home, uid); 1198 pw_log(conf.userconf, M_DELETE, W_USER, "%s(%ju) home '%s' %s" 1199 "removed", name, (uintmax_t)uid, home, 1200 fstatat(conf.rootfd, home + 1, &st, 0) == -1 ? "" : "not " 1201 "completely "); 1202 } 1203 1204 return (EXIT_SUCCESS); 1205} 1206 1207static int 1208print_user(struct passwd * pwd) 1209{ 1210 if (!conf.pretty) { 1211 char *buf; 1212 1213 buf = conf.v7 ? pw_make_v7(pwd) : pw_make(pwd); 1214 printf("%s\n", buf); 1215 free(buf); 1216 } else { 1217 int j; 1218 char *p; 1219 struct group *grp = GETGRGID(pwd->pw_gid); 1220 char uname[60] = "User &", office[60] = "[None]", 1221 wphone[60] = "[None]", hphone[60] = "[None]"; 1222 char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 1223 struct tm * tptr; 1224 1225 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1226 strlcpy(uname, p, sizeof(uname)); 1227 if ((p = strtok(NULL, ",")) != NULL) { 1228 strlcpy(office, p, sizeof(office)); 1229 if ((p = strtok(NULL, ",")) != NULL) { 1230 strlcpy(wphone, p, sizeof(wphone)); 1231 if ((p = strtok(NULL, "")) != NULL) { 1232 strlcpy(hphone, p, 1233 sizeof(hphone)); 1234 } 1235 } 1236 } 1237 } 1238 /* 1239 * Handle '&' in gecos field 1240 */ 1241 if ((p = strchr(uname, '&')) != NULL) { 1242 int l = strlen(pwd->pw_name); 1243 int m = strlen(p); 1244 1245 memmove(p + l, p + 1, m); 1246 memmove(p, pwd->pw_name, l); 1247 *p = (char) toupper((unsigned char)*p); 1248 } 1249 if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 1250 strftime(acexpire, sizeof acexpire, "%c", tptr); 1251 if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 1252 strftime(pwexpire, sizeof pwexpire, "%c", tptr); 1253 printf("Login Name: %-15s #%-12ju Group: %-15s #%ju\n" 1254 " Full Name: %s\n" 1255 " Home: %-26.26s Class: %s\n" 1256 " Shell: %-26.26s Office: %s\n" 1257 "Work Phone: %-26.26s Home Phone: %s\n" 1258 "Acc Expire: %-26.26s Pwd Expire: %s\n", 1259 pwd->pw_name, (uintmax_t)pwd->pw_uid, 1260 grp ? grp->gr_name : "(invalid)", (uintmax_t)pwd->pw_gid, 1261 uname, pwd->pw_dir, pwd->pw_class, 1262 pwd->pw_shell, office, wphone, hphone, 1263 acexpire, pwexpire); 1264 SETGRENT(); 1265 j = 0; 1266 while ((grp=GETGRENT()) != NULL) 1267 { 1268 int i = 0; 1269 if (grp->gr_mem != NULL) { 1270 while (grp->gr_mem[i] != NULL) 1271 { 1272 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1273 { 1274 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1275 break; 1276 } 1277 ++i; 1278 } 1279 } 1280 } 1281 ENDGRENT(); 1282 printf("%s", j ? "\n" : ""); 1283 } 1284 return EXIT_SUCCESS; 1285} 1286 1287char * 1288pw_checkname(char *name, int gecos) 1289{ 1290 char showch[8]; 1291 const char *badchars, *ch, *showtype; 1292 int reject; 1293 1294 ch = name; 1295 reject = 0; 1296 if (gecos) { 1297 /* See if the name is valid as a gecos (comment) field. */ 1298 badchars = ":!@"; 1299 showtype = "gecos field"; 1300 } else { 1301 /* See if the name is valid as a userid or group. */ 1302 badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1303 showtype = "userid/group name"; 1304 /* Userids and groups can not have a leading '-'. */ 1305 if (*ch == '-') 1306 reject = 1; 1307 } 1308 if (!reject) { 1309 while (*ch) { 1310 if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1311 *ch == 127) { 1312 reject = 1; 1313 break; 1314 } 1315 /* 8-bit characters are only allowed in GECOS fields */ 1316 if (!gecos && (*ch & 0x80)) { 1317 reject = 1; 1318 break; 1319 } 1320 ch++; 1321 } 1322 } 1323 /* 1324 * A `$' is allowed as the final character for userids and groups, 1325 * mainly for the benefit of samba. 1326 */ 1327 if (reject && !gecos) { 1328 if (*ch == '$' && *(ch + 1) == '\0') { 1329 reject = 0; 1330 ch++; 1331 } 1332 } 1333 if (reject) { 1334 snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1335 ? "`%c'" : "0x%02x", *ch); 1336 errx(EX_DATAERR, "invalid character %s at position %td in %s", 1337 showch, (ch - name), showtype); 1338 } 1339 if (!gecos && (ch - name) > LOGNAMESIZE) 1340 errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1341 LOGNAMESIZE); 1342 1343 return (name); 1344} 1345 1346 1347static void 1348rmat(uid_t uid) 1349{ 1350 DIR *d = opendir("/var/at/jobs"); 1351 1352 if (d != NULL) { 1353 struct dirent *e; 1354 1355 while ((e = readdir(d)) != NULL) { 1356 struct stat st; 1357 1358 if (strncmp(e->d_name, ".lock", 5) != 0 && 1359 stat(e->d_name, &st) == 0 && 1360 !S_ISDIR(st.st_mode) && 1361 st.st_uid == uid) { 1362 char tmp[MAXPATHLEN]; 1363 1364 snprintf(tmp, sizeof(tmp), "/usr/bin/atrm %s", e->d_name); 1365 system(tmp); 1366 } 1367 } 1368 closedir(d); 1369 } 1370} 1371 1372static void 1373rmopie(char const * name) 1374{ 1375 char tmp[1014]; 1376 FILE *fp; 1377 int fd; 1378 size_t len; 1379 off_t atofs = 0; 1380 1381 if ((fd = openat(conf.rootfd, "etc/opiekeys", O_RDWR)) == -1) 1382 return; 1383 1384 fp = fdopen(fd, "r+"); 1385 len = strlen(name); 1386 1387 while (fgets(tmp, sizeof(tmp), fp) != NULL) { 1388 if (strncmp(name, tmp, len) == 0 && tmp[len]==' ') { 1389 /* Comment username out */ 1390 if (fseek(fp, atofs, SEEK_SET) == 0) 1391 fwrite("#", 1, 1, fp); 1392 break; 1393 } 1394 atofs = ftell(fp); 1395 } 1396 /* 1397 * If we got an error of any sort, don't update! 1398 */ 1399 fclose(fp); 1400} 1401