pw_user.c revision 167919
1288522Sgrehan/*- 2288522Sgrehan * Copyright (C) 1996 3288522Sgrehan * David L. Nugent. All rights reserved. 4288522Sgrehan * 5288522Sgrehan * Redistribution and use in source and binary forms, with or without 6288522Sgrehan * modification, are permitted provided that the following conditions 7288522Sgrehan * are met: 8288522Sgrehan * 1. Redistributions of source code must retain the above copyright 9288522Sgrehan * notice, this list of conditions and the following disclaimer. 10288522Sgrehan * 2. Redistributions in binary form must reproduce the above copyright 11288522Sgrehan * notice, this list of conditions and the following disclaimer in the 12288522Sgrehan * documentation and/or other materials provided with the distribution. 13288522Sgrehan * 14288522Sgrehan * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 15288522Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16288522Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17288522Sgrehan * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 18288522Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19288522Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20288522Sgrehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21288522Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22288522Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23288522Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24288522Sgrehan * SUCH DAMAGE. 25288522Sgrehan * 26288522Sgrehan */ 27288522Sgrehan 28288522Sgrehan#ifndef lint 29288522Sgrehanstatic const char rcsid[] = 30288522Sgrehan "$FreeBSD: head/usr.sbin/pw/pw_user.c 167919 2007-03-26 23:29:20Z le $"; 31288522Sgrehan#endif /* not lint */ 32288522Sgrehan 33288522Sgrehan#include <ctype.h> 34288522Sgrehan#include <err.h> 35288522Sgrehan#include <fcntl.h> 36288522Sgrehan#include <sys/param.h> 37288522Sgrehan#include <dirent.h> 38288522Sgrehan#include <paths.h> 39288522Sgrehan#include <termios.h> 40288522Sgrehan#include <sys/types.h> 41288522Sgrehan#include <sys/time.h> 42288522Sgrehan#include <sys/resource.h> 43288522Sgrehan#include <unistd.h> 44288522Sgrehan#include <utmp.h> 45288522Sgrehan#include <login_cap.h> 46288522Sgrehan#if defined(USE_MD5RAND) 47288522Sgrehan#include <md5.h> 48288522Sgrehan#endif 49288522Sgrehan#include "pw.h" 50288522Sgrehan#include "bitmap.h" 51288522Sgrehan 52288522Sgrehan#if (MAXLOGNAME-1) > UT_NAMESIZE 53288522Sgrehan#define LOGNAMESIZE UT_NAMESIZE 54288522Sgrehan#else 55288522Sgrehan#define LOGNAMESIZE (MAXLOGNAME-1) 56288522Sgrehan#endif 57288522Sgrehan 58288522Sgrehanstatic char locked_str[] = "*LOCKED*"; 59288522Sgrehan 60288522Sgrehanstatic int print_user(struct passwd * pwd, int pretty, int v7); 61288522Sgrehanstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 62288522Sgrehanstatic uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 63288522Sgrehanstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 64288522Sgrehanstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 65288522Sgrehanstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 66288522Sgrehanstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 67288522Sgrehanstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 68288522Sgrehanstatic char *shell_path(char const * path, char *shells[], char *sh); 69288522Sgrehanstatic void rmat(uid_t uid); 70288522Sgrehanstatic void rmopie(char const * name); 71288522Sgrehan 72288522Sgrehan/*- 73288522Sgrehan * -C config configuration file 74288522Sgrehan * -q quiet operation 75288522Sgrehan * -n name login name 76288522Sgrehan * -u uid user id 77288522Sgrehan * -c comment user name/comment 78288522Sgrehan * -d directory home directory 79288522Sgrehan * -e date account expiry date 80288522Sgrehan * -p date password expiry date 81288522Sgrehan * -g grp primary group 82288522Sgrehan * -G grp1,grp2 additional groups 83288522Sgrehan * -m [ -k dir ] create and set up home 84288522Sgrehan * -s shell name of login shell 85288522Sgrehan * -o duplicate uid ok 86288522Sgrehan * -L class user class 87288522Sgrehan * -l name new login name 88288522Sgrehan * -h fd password filehandle 89288522Sgrehan * -H fd encrypted password filehandle 90288522Sgrehan * -F force print or add 91288522Sgrehan * Setting defaults: 92288522Sgrehan * -D set user defaults 93288522Sgrehan * -b dir default home root dir 94288522Sgrehan * -e period default expiry period 95288522Sgrehan * -p period default password change period 96288522Sgrehan * -g group default group 97288522Sgrehan * -G grp1,grp2.. default additional groups 98288522Sgrehan * -L class default login class 99288522Sgrehan * -k dir default home skeleton 100288522Sgrehan * -s shell default shell 101288522Sgrehan * -w method default password method 102288522Sgrehan */ 103288522Sgrehan 104288522Sgrehanint 105288522Sgrehanpw_user(struct userconf * cnf, int mode, struct cargs * args) 106288522Sgrehan{ 107288522Sgrehan int rc, edited = 0; 108288522Sgrehan char *p = NULL; 109288522Sgrehan char *passtmp; 110288522Sgrehan struct carg *a_name; 111288522Sgrehan struct carg *a_uid; 112288522Sgrehan struct carg *arg; 113288522Sgrehan struct passwd *pwd = NULL; 114288522Sgrehan struct group *grp; 115288522Sgrehan struct stat st; 116288522Sgrehan char line[_PASSWORD_LEN+1]; 117288522Sgrehan FILE *fp; 118288522Sgrehan mode_t dmode; 119288522Sgrehan char *dmode_c; 120288522Sgrehan void *set = NULL; 121288522Sgrehan 122288522Sgrehan static struct passwd fakeuser = 123288522Sgrehan { 124288522Sgrehan NULL, 125288522Sgrehan "*", 126288522Sgrehan -1, 127288522Sgrehan -1, 128288522Sgrehan 0, 129288522Sgrehan "", 130288522Sgrehan "User &", 131288522Sgrehan "/nonexistent", 132288522Sgrehan "/bin/sh", 133288522Sgrehan 0 134288522Sgrehan#if defined(__FreeBSD__) 135288522Sgrehan ,0 136288522Sgrehan#endif 137288522Sgrehan }; 138288522Sgrehan 139288522Sgrehan 140288522Sgrehan /* 141288522Sgrehan * With M_NEXT, we only need to return the 142288522Sgrehan * next uid to stdout 143288522Sgrehan */ 144288522Sgrehan if (mode == M_NEXT) 145288522Sgrehan { 146288522Sgrehan uid_t next = pw_uidpolicy(cnf, args); 147288522Sgrehan if (getarg(args, 'q')) 148288522Sgrehan return next; 149288522Sgrehan printf("%ld:", (long)next); 150288522Sgrehan pw_group(cnf, mode, args); 151288522Sgrehan return EXIT_SUCCESS; 152288522Sgrehan } 153288522Sgrehan 154288522Sgrehan /* 155288522Sgrehan * We can do all of the common legwork here 156288522Sgrehan */ 157288522Sgrehan 158288522Sgrehan if ((arg = getarg(args, 'b')) != NULL) { 159288522Sgrehan cnf->home = arg->val; 160288522Sgrehan } 161288522Sgrehan 162288522Sgrehan if ((arg = getarg(args, 'M')) != NULL) { 163288522Sgrehan dmode_c = arg->val; 164288522Sgrehan if ((set = setmode(dmode_c)) == NULL) 165288522Sgrehan errx(EX_DATAERR, "invalid directory creation mode '%s'", 166288522Sgrehan dmode_c); 167288522Sgrehan dmode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO); 168288522Sgrehan free(set); 169288522Sgrehan } else 170288522Sgrehan dmode = S_IRWXU | S_IRWXG | S_IRWXO; 171288522Sgrehan 172288522Sgrehan /* 173288522Sgrehan * If we'll need to use it or we're updating it, 174288522Sgrehan * then create the base home directory if necessary 175288522Sgrehan */ 176288522Sgrehan if (arg != NULL || getarg(args, 'm') != NULL) { 177288522Sgrehan int l = strlen(cnf->home); 178288522Sgrehan 179288522Sgrehan if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 180288522Sgrehan cnf->home[--l] = '\0'; 181288522Sgrehan 182288522Sgrehan if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 183288522Sgrehan errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 184288522Sgrehan 185288522Sgrehan if (stat(cnf->home, &st) == -1) { 186288522Sgrehan char dbuf[MAXPATHLEN]; 187288522Sgrehan 188288522Sgrehan /* 189288522Sgrehan * This is a kludge especially for Joerg :) 190288522Sgrehan * If the home directory would be created in the root partition, then 191288522Sgrehan * we really create it under /usr which is likely to have more space. 192288522Sgrehan * But we create a symlink from cnf->home -> "/usr" -> cnf->home 193288522Sgrehan */ 194288522Sgrehan if (strchr(cnf->home+1, '/') == NULL) { 195288522Sgrehan strcpy(dbuf, "/usr"); 196288522Sgrehan strncat(dbuf, cnf->home, MAXPATHLEN-5); 197288522Sgrehan if (mkdir(dbuf, dmode) != -1 || errno == EEXIST) { 198288522Sgrehan chown(dbuf, 0, 0); 199288522Sgrehan /* 200288522Sgrehan * Skip first "/" and create symlink: 201288522Sgrehan * /home -> usr/home 202288522Sgrehan */ 203288522Sgrehan symlink(dbuf+1, cnf->home); 204288522Sgrehan } 205288522Sgrehan /* If this falls, fall back to old method */ 206288522Sgrehan } 207288522Sgrehan strlcpy(dbuf, cnf->home, sizeof(dbuf)); 208288522Sgrehan p = dbuf; 209288522Sgrehan if (stat(dbuf, &st) == -1) { 210288522Sgrehan while ((p = strchr(++p, '/')) != NULL) { 211288522Sgrehan *p = '\0'; 212288522Sgrehan if (stat(dbuf, &st) == -1) { 213288522Sgrehan if (mkdir(dbuf, dmode) == -1) 214288522Sgrehan goto direrr; 215288522Sgrehan chown(dbuf, 0, 0); 216288522Sgrehan } else if (!S_ISDIR(st.st_mode)) 217288522Sgrehan errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 218288522Sgrehan *p = '/'; 219288522Sgrehan } 220288522Sgrehan } 221288522Sgrehan if (stat(dbuf, &st) == -1) { 222288522Sgrehan if (mkdir(dbuf, dmode) == -1) { 223288522Sgrehan direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 224288522Sgrehan } 225288522Sgrehan chown(dbuf, 0, 0); 226288522Sgrehan } 227288522Sgrehan } else if (!S_ISDIR(st.st_mode)) 228288522Sgrehan errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 229288522Sgrehan } 230288522Sgrehan 231288522Sgrehan if ((arg = getarg(args, 'e')) != NULL) 232288522Sgrehan cnf->expire_days = atoi(arg->val); 233288522Sgrehan 234288522Sgrehan if ((arg = getarg(args, 'y')) != NULL) 235288522Sgrehan cnf->nispasswd = arg->val; 236288522Sgrehan 237288522Sgrehan if ((arg = getarg(args, 'p')) != NULL && arg->val) 238288522Sgrehan cnf->password_days = atoi(arg->val); 239288522Sgrehan 240288522Sgrehan if ((arg = getarg(args, 'g')) != NULL) { 241288522Sgrehan if (!*(p = arg->val)) /* Handle empty group list specially */ 242288522Sgrehan cnf->default_group = ""; 243288522Sgrehan else { 244288522Sgrehan if ((grp = GETGRNAM(p)) == NULL) { 245288522Sgrehan if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 246288522Sgrehan errx(EX_NOUSER, "group `%s' does not exist", p); 247288522Sgrehan } 248288522Sgrehan cnf->default_group = newstr(grp->gr_name); 249288522Sgrehan } 250288522Sgrehan } 251288522Sgrehan if ((arg = getarg(args, 'L')) != NULL) 252288522Sgrehan cnf->default_class = pw_checkname((u_char *)arg->val, 0); 253288522Sgrehan 254288522Sgrehan if ((arg = getarg(args, 'G')) != NULL && arg->val) { 255288522Sgrehan int i = 0; 256288522Sgrehan 257288522Sgrehan for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 258288522Sgrehan if ((grp = GETGRNAM(p)) == NULL) { 259288522Sgrehan if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 260288522Sgrehan errx(EX_NOUSER, "group `%s' does not exist", p); 261288522Sgrehan } 262288522Sgrehan if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 263288522Sgrehan cnf->groups[i++] = newstr(grp->gr_name); 264288522Sgrehan } 265288522Sgrehan while (i < cnf->numgroups) 266288522Sgrehan cnf->groups[i++] = NULL; 267288522Sgrehan } 268288522Sgrehan 269288522Sgrehan if ((arg = getarg(args, 'k')) != NULL) { 270288522Sgrehan if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 271288522Sgrehan errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 272288522Sgrehan } 273288522Sgrehan 274288522Sgrehan if ((arg = getarg(args, 's')) != NULL) 275288522Sgrehan cnf->shell_default = arg->val; 276288522Sgrehan 277288522Sgrehan if ((arg = getarg(args, 'w')) != NULL) 278288522Sgrehan cnf->default_password = boolean_val(arg->val, cnf->default_password); 279288522Sgrehan if (mode == M_ADD && getarg(args, 'D')) { 280288522Sgrehan if (getarg(args, 'n') != NULL) 281288522Sgrehan errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 282288522Sgrehan if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 283288522Sgrehan if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 284288522Sgrehan cnf->min_uid = 1000; 285288522Sgrehan if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 286288522Sgrehan cnf->max_uid = 32000; 287288522Sgrehan } 288288522Sgrehan if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 289288522Sgrehan if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 290288522Sgrehan cnf->min_gid = 1000; 291288522Sgrehan if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 292288522Sgrehan cnf->max_gid = 32000; 293288522Sgrehan } 294288522Sgrehan 295288522Sgrehan arg = getarg(args, 'C'); 296288522Sgrehan if (write_userconfig(arg ? arg->val : NULL)) 297288522Sgrehan return EXIT_SUCCESS; 298288522Sgrehan warn("config update"); 299288522Sgrehan return EX_IOERR; 300288522Sgrehan } 301288522Sgrehan 302288522Sgrehan if (mode == M_PRINT && getarg(args, 'a')) { 303288522Sgrehan int pretty = getarg(args, 'P') != NULL; 304288522Sgrehan int v7 = getarg(args, '7') != NULL; 305288522Sgrehan 306288522Sgrehan SETPWENT(); 307288522Sgrehan while ((pwd = GETPWENT()) != NULL) 308288522Sgrehan print_user(pwd, pretty, v7); 309288522Sgrehan ENDPWENT(); 310288522Sgrehan return EXIT_SUCCESS; 311288522Sgrehan } 312288522Sgrehan 313288522Sgrehan if ((a_name = getarg(args, 'n')) != NULL) 314288522Sgrehan pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0)); 315288522Sgrehan a_uid = getarg(args, 'u'); 316288522Sgrehan 317288522Sgrehan if (a_uid == NULL) { 318288522Sgrehan if (a_name == NULL) 319288522Sgrehan errx(EX_DATAERR, "user name or id required"); 320288522Sgrehan 321288522Sgrehan /* 322288522Sgrehan * Determine whether 'n' switch is name or uid - we don't 323288522Sgrehan * really don't really care which we have, but we need to 324288522Sgrehan * know. 325288522Sgrehan */ 326288522Sgrehan if (mode != M_ADD && pwd == NULL 327288522Sgrehan && strspn(a_name->val, "0123456789") == strlen(a_name->val) 328288522Sgrehan && atoi(a_name->val) > 0) { /* Assume uid */ 329288522Sgrehan (a_uid = a_name)->ch = 'u'; 330288522Sgrehan a_name = NULL; 331288522Sgrehan } 332288522Sgrehan } 333288522Sgrehan 334288522Sgrehan /* 335288522Sgrehan * Update, delete & print require that the user exists 336288522Sgrehan */ 337288522Sgrehan if (mode == M_UPDATE || mode == M_DELETE || 338288522Sgrehan mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 339288522Sgrehan 340288522Sgrehan if (a_name == NULL && pwd == NULL) /* Try harder */ 341288522Sgrehan pwd = GETPWUID(atoi(a_uid->val)); 342288522Sgrehan 343288522Sgrehan if (pwd == NULL) { 344288522Sgrehan if (mode == M_PRINT && getarg(args, 'F')) { 345288522Sgrehan fakeuser.pw_name = a_name ? a_name->val : "nouser"; 346288522Sgrehan fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 347288522Sgrehan return print_user(&fakeuser, 348288522Sgrehan getarg(args, 'P') != NULL, 349288522Sgrehan getarg(args, '7') != NULL); 350288522Sgrehan } 351298454Saraujo if (a_name == NULL) 352288522Sgrehan errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 353288522Sgrehan errx(EX_NOUSER, "no such user `%s'", a_name->val); 354288522Sgrehan } 355288522Sgrehan 356288522Sgrehan if (a_name == NULL) /* May be needed later */ 357288522Sgrehan a_name = addarg(args, 'n', newstr(pwd->pw_name)); 358288522Sgrehan 359288522Sgrehan /* 360288522Sgrehan * The M_LOCK and M_UNLOCK functions simply add or remove 361288522Sgrehan * a "*LOCKED*" prefix from in front of the password to 362288522Sgrehan * prevent it decoding correctly, and therefore prevents 363288522Sgrehan * access. Of course, this only prevents access via 364288522Sgrehan * password authentication (not ssh, kerberos or any 365288522Sgrehan * other method that does not use the UNIX password) but 366288522Sgrehan * that is a known limitation. 367288522Sgrehan */ 368288522Sgrehan 369288522Sgrehan if (mode == M_LOCK) { 370288522Sgrehan if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 371288522Sgrehan errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 372288522Sgrehan passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str)); 373288522Sgrehan if (passtmp == NULL) /* disaster */ 374288522Sgrehan errx(EX_UNAVAILABLE, "out of memory"); 375288522Sgrehan strcpy(passtmp, locked_str); 376288522Sgrehan strcat(passtmp, pwd->pw_passwd); 377288522Sgrehan pwd->pw_passwd = passtmp; 378288522Sgrehan edited = 1; 379288522Sgrehan } else if (mode == M_UNLOCK) { 380288522Sgrehan if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 381288522Sgrehan errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 382288522Sgrehan pwd->pw_passwd += sizeof(locked_str)-1; 383288522Sgrehan edited = 1; 384288522Sgrehan } else if (mode == M_DELETE) { 385288522Sgrehan /* 386288522Sgrehan * Handle deletions now 387288522Sgrehan */ 388288522Sgrehan char file[MAXPATHLEN]; 389288522Sgrehan char home[MAXPATHLEN]; 390288522Sgrehan uid_t uid = pwd->pw_uid; 391288522Sgrehan 392288522Sgrehan if (strcmp(pwd->pw_name, "root") == 0) 393288522Sgrehan errx(EX_DATAERR, "cannot remove user 'root'"); 394288522Sgrehan 395288522Sgrehan if (!PWALTDIR()) { 396288522Sgrehan /* 397288522Sgrehan * Remove opie record from /etc/opiekeys 398288522Sgrehan */ 399288522Sgrehan 400288522Sgrehan rmopie(pwd->pw_name); 401288522Sgrehan 402288522Sgrehan /* 403288522Sgrehan * Remove crontabs 404288522Sgrehan */ 405288522Sgrehan sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 406288522Sgrehan if (access(file, F_OK) == 0) { 407288522Sgrehan sprintf(file, "crontab -u %s -r", pwd->pw_name); 408288522Sgrehan system(file); 409288522Sgrehan } 410288522Sgrehan } 411288522Sgrehan /* 412288522Sgrehan * Save these for later, since contents of pwd may be 413288522Sgrehan * invalidated by deletion 414288522Sgrehan */ 415288522Sgrehan sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 416288522Sgrehan strlcpy(home, pwd->pw_dir, sizeof(home)); 417288522Sgrehan 418288522Sgrehan rc = delpwent(pwd); 419288522Sgrehan if (rc == -1) 420288522Sgrehan err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 421288522Sgrehan else if (rc != 0) { 422288522Sgrehan warn("passwd update"); 423288522Sgrehan return EX_IOERR; 424288522Sgrehan } 425288522Sgrehan 426288522Sgrehan if (cnf->nispasswd && *cnf->nispasswd=='/') { 427288522Sgrehan rc = delnispwent(cnf->nispasswd, a_name->val); 428288522Sgrehan if (rc == -1) 429288522Sgrehan warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 430288522Sgrehan else if (rc != 0) 431288522Sgrehan warn("WARNING: NIS passwd update"); 432288522Sgrehan /* non-fatal */ 433288522Sgrehan } 434288522Sgrehan 435288522Sgrehan editgroups(a_name->val, NULL); 436288522Sgrehan 437288522Sgrehan pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 438288522Sgrehan 439288522Sgrehan if (!PWALTDIR()) { 440288522Sgrehan /* 441288522Sgrehan * Remove mail file 442288522Sgrehan */ 443288522Sgrehan remove(file); 444288522Sgrehan 445288522Sgrehan /* 446288522Sgrehan * Remove at jobs 447288522Sgrehan */ 448288522Sgrehan if (getpwuid(uid) == NULL) 449288522Sgrehan rmat(uid); 450288522Sgrehan 451288522Sgrehan /* 452288522Sgrehan * Remove home directory and contents 453288522Sgrehan */ 454288522Sgrehan if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 455288522Sgrehan if (stat(home, &st) != -1) { 456288522Sgrehan rm_r(home, uid); 457288522Sgrehan pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 458288522Sgrehan a_name->val, (long) uid, home, 459288522Sgrehan stat(home, &st) == -1 ? "" : "not completely "); 460288522Sgrehan } 461288522Sgrehan } 462288522Sgrehan } 463288522Sgrehan return EXIT_SUCCESS; 464288522Sgrehan } else if (mode == M_PRINT) 465288522Sgrehan return print_user(pwd, 466288522Sgrehan getarg(args, 'P') != NULL, 467288522Sgrehan getarg(args, '7') != NULL); 468288522Sgrehan 469288522Sgrehan /* 470288522Sgrehan * The rest is edit code 471288522Sgrehan */ 472288522Sgrehan if ((arg = getarg(args, 'l')) != NULL) { 473288522Sgrehan if (strcmp(pwd->pw_name, "root") == 0) 474288522Sgrehan errx(EX_DATAERR, "can't rename `root' account"); 475288522Sgrehan pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 476288522Sgrehan edited = 1; 477288522Sgrehan } 478288522Sgrehan 479288522Sgrehan if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { 480288522Sgrehan pwd->pw_uid = (uid_t) atol(arg->val); 481288522Sgrehan edited = 1; 482288522Sgrehan if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 483288522Sgrehan errx(EX_DATAERR, "can't change uid of `root' account"); 484288522Sgrehan if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 485288522Sgrehan warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 486288522Sgrehan } 487288522Sgrehan 488288522Sgrehan if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 489288522Sgrehan gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 490288522Sgrehan if (newgid != pwd->pw_gid) { 491288522Sgrehan edited = 1; 492288522Sgrehan pwd->pw_gid = newgid; 493288522Sgrehan } 494288522Sgrehan } 495288522Sgrehan 496288522Sgrehan if ((arg = getarg(args, 'p')) != NULL) { 497288522Sgrehan if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 498288522Sgrehan if (pwd->pw_change != 0) { 499288522Sgrehan pwd->pw_change = 0; 500288522Sgrehan edited = 1; 501288522Sgrehan } 502288522Sgrehan } 503288522Sgrehan else { 504288522Sgrehan time_t now = time(NULL); 505288522Sgrehan time_t expire = parse_date(now, arg->val); 506288522Sgrehan 507288522Sgrehan if (now == expire) 508288522Sgrehan errx(EX_DATAERR, "invalid password change date `%s'", arg->val); 509288522Sgrehan if (pwd->pw_change != expire) { 510288522Sgrehan pwd->pw_change = expire; 511288522Sgrehan edited = 1; 512288522Sgrehan } 513288522Sgrehan } 514288522Sgrehan } 515288522Sgrehan 516288522Sgrehan if ((arg = getarg(args, 'e')) != NULL) { 517288522Sgrehan if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 518288522Sgrehan if (pwd->pw_expire != 0) { 519288522Sgrehan pwd->pw_expire = 0; 520288522Sgrehan edited = 1; 521288522Sgrehan } 522288522Sgrehan } 523288522Sgrehan else { 524288522Sgrehan time_t now = time(NULL); 525288522Sgrehan time_t expire = parse_date(now, arg->val); 526288522Sgrehan 527288522Sgrehan if (now == expire) 528288522Sgrehan errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val); 529288522Sgrehan if (pwd->pw_expire != expire) { 530288522Sgrehan pwd->pw_expire = expire; 531288522Sgrehan edited = 1; 532288522Sgrehan } 533288522Sgrehan } 534288522Sgrehan } 535288522Sgrehan 536288522Sgrehan if ((arg = getarg(args, 's')) != NULL) { 537288522Sgrehan char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 538288522Sgrehan if (shell == NULL) 539288524Sgrehan shell = ""; 540288522Sgrehan if (strcmp(shell, pwd->pw_shell) != 0) { 541288522Sgrehan pwd->pw_shell = shell; 542288522Sgrehan edited = 1; 543288522Sgrehan } 544288522Sgrehan } 545288522Sgrehan 546288522Sgrehan if (getarg(args, 'L')) { 547288522Sgrehan if (cnf->default_class == NULL) 548288522Sgrehan cnf->default_class = ""; 549288522Sgrehan if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 550 pwd->pw_class = cnf->default_class; 551 edited = 1; 552 } 553 } 554 555 if ((arg = getarg(args, 'd')) != NULL) { 556 if (strcmp(pwd->pw_dir, arg->val)) 557 edited = 1; 558 if (stat(pwd->pw_dir = arg->val, &st) == -1) { 559 if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 560 warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 561 } else if (!S_ISDIR(st.st_mode)) 562 warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 563 } 564 565 if ((arg = getarg(args, 'w')) != NULL && 566 getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) { 567 login_cap_t *lc; 568 569 lc = login_getpwclass(pwd); 570 if (lc == NULL || 571 login_setcryptfmt(lc, "md5", NULL) == NULL) 572 warn("setting crypt(3) format"); 573 login_close(lc); 574 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 575 edited = 1; 576 } 577 578 } else { 579 login_cap_t *lc; 580 581 /* 582 * Add code 583 */ 584 585 if (a_name == NULL) /* Required */ 586 errx(EX_DATAERR, "login name required"); 587 else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ 588 errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 589 590 /* 591 * Now, set up defaults for a new user 592 */ 593 pwd = &fakeuser; 594 pwd->pw_name = a_name->val; 595 pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 596 pwd->pw_uid = pw_uidpolicy(cnf, args); 597 pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 598 pwd->pw_change = pw_pwdpolicy(cnf, args); 599 pwd->pw_expire = pw_exppolicy(cnf, args); 600 pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 601 pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 602 lc = login_getpwclass(pwd); 603 if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL) 604 warn("setting crypt(3) format"); 605 login_close(lc); 606 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 607 edited = 1; 608 609 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 610 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 611 } 612 613 /* 614 * Shared add/edit code 615 */ 616 if ((arg = getarg(args, 'c')) != NULL) { 617 char *gecos = pw_checkname((u_char *)arg->val, 1); 618 if (strcmp(pwd->pw_gecos, gecos) != 0) { 619 pwd->pw_gecos = gecos; 620 edited = 1; 621 } 622 } 623 624 if ((arg = getarg(args, 'h')) != NULL || 625 (arg = getarg(args, 'H')) != NULL) { 626 if (strcmp(arg->val, "-") == 0) { 627 if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 628 pwd->pw_passwd = "*"; /* No access */ 629 edited = 1; 630 } 631 } else { 632 int fd = atoi(arg->val); 633 int precrypt = (arg->ch == 'H'); 634 int b; 635 int istty = isatty(fd); 636 struct termios t; 637 login_cap_t *lc; 638 639 if (istty) { 640 if (tcgetattr(fd, &t) == -1) 641 istty = 0; 642 else { 643 struct termios n = t; 644 645 /* Disable echo */ 646 n.c_lflag &= ~(ECHO); 647 tcsetattr(fd, TCSANOW, &n); 648 printf("%s%spassword for user %s:", 649 (mode == M_UPDATE) ? "new " : "", 650 precrypt ? "encrypted " : "", 651 pwd->pw_name); 652 fflush(stdout); 653 } 654 } 655 b = read(fd, line, sizeof(line) - 1); 656 if (istty) { /* Restore state */ 657 tcsetattr(fd, TCSANOW, &t); 658 fputc('\n', stdout); 659 fflush(stdout); 660 } 661 if (b < 0) { 662 warn("-%c file descriptor", precrypt ? 'H' : 663 'h'); 664 return EX_IOERR; 665 } 666 line[b] = '\0'; 667 if ((p = strpbrk(line, " \t\r\n")) != NULL) 668 *p = '\0'; 669 if (!*line) 670 errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 671 if (precrypt) { 672 if (strchr(line, ':') != NULL) 673 return EX_DATAERR; 674 pwd->pw_passwd = line; 675 } else { 676 lc = login_getpwclass(pwd); 677 if (lc == NULL || 678 login_setcryptfmt(lc, "md5", NULL) == NULL) 679 warn("setting crypt(3) format"); 680 login_close(lc); 681 pwd->pw_passwd = pw_pwcrypt(line); 682 } 683 edited = 1; 684 } 685 } 686 687 /* 688 * Special case: -N only displays & exits 689 */ 690 if (getarg(args, 'N') != NULL) 691 return print_user(pwd, 692 getarg(args, 'P') != NULL, 693 getarg(args, '7') != NULL); 694 695 if (mode == M_ADD) { 696 edited = 1; /* Always */ 697 rc = addpwent(pwd); 698 if (rc == -1) { 699 warnx("user '%s' already exists", pwd->pw_name); 700 return EX_IOERR; 701 } else if (rc != 0) { 702 warn("passwd file update"); 703 return EX_IOERR; 704 } 705 if (cnf->nispasswd && *cnf->nispasswd=='/') { 706 rc = addnispwent(cnf->nispasswd, pwd); 707 if (rc == -1) 708 warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 709 else 710 warn("NIS passwd update"); 711 /* NOTE: we treat NIS-only update errors as non-fatal */ 712 } 713 } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 714 if (edited) { /* Only updated this if required */ 715 rc = chgpwent(a_name->val, pwd); 716 if (rc == -1) { 717 warnx("user '%s' does not exist (NIS?)", pwd->pw_name); 718 return EX_IOERR; 719 } else if (rc != 0) { 720 warn("passwd file update"); 721 return EX_IOERR; 722 } 723 if ( cnf->nispasswd && *cnf->nispasswd=='/') { 724 rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); 725 if (rc == -1) 726 warn("User '%s' not found in NIS passwd", pwd->pw_name); 727 else 728 warn("NIS passwd update"); 729 /* NOTE: NIS-only update errors are not fatal */ 730 } 731 } 732 } 733 734 /* 735 * Ok, user is created or changed - now edit group file 736 */ 737 738 if (mode == M_ADD || getarg(args, 'G') != NULL) 739 editgroups(pwd->pw_name, cnf->groups); 740 741 /* go get a current version of pwd */ 742 pwd = GETPWNAM(a_name->val); 743 if (pwd == NULL) { 744 /* This will fail when we rename, so special case that */ 745 if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { 746 a_name->val = arg->val; /* update new name */ 747 pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ 748 } 749 } 750 if (pwd == NULL) /* can't go on without this */ 751 errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 752 753 grp = GETGRGID(pwd->pw_gid); 754 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s", 755 pwd->pw_name, (long) pwd->pw_uid, 756 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 757 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 758 759 /* 760 * If adding, let's touch and chown the user's mail file. This is not 761 * strictly necessary under BSD with a 0755 maildir but it also 762 * doesn't hurt anything to create the empty mailfile 763 */ 764 if (mode == M_ADD) { 765 if (!PWALTDIR()) { 766 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 767 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 768 * mtime */ 769 chown(line, pwd->pw_uid, pwd->pw_gid); 770 } 771 } 772 773 /* 774 * Let's create and populate the user's home directory. Note 775 * that this also `works' for editing users if -m is used, but 776 * existing files will *not* be overwritten. 777 */ 778 if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 779 copymkdir(pwd->pw_dir, cnf->dotdir, dmode, pwd->pw_uid, pwd->pw_gid); 780 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 781 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 782 } 783 784 785 /* 786 * Finally, send mail to the new user as well, if we are asked to 787 */ 788 if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 789 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 790 791 if (pfp == NULL) 792 warn("sendmail"); 793 else { 794 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 795 while (fgets(line, sizeof(line), fp) != NULL) { 796 /* Do substitutions? */ 797 fputs(line, pfp); 798 } 799 pclose(pfp); 800 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 801 pwd->pw_name, (long) pwd->pw_uid); 802 } 803 fclose(fp); 804 } 805 806 return EXIT_SUCCESS; 807} 808 809 810static uid_t 811pw_uidpolicy(struct userconf * cnf, struct cargs * args) 812{ 813 struct passwd *pwd; 814 uid_t uid = (uid_t) - 1; 815 struct carg *a_uid = getarg(args, 'u'); 816 817 /* 818 * Check the given uid, if any 819 */ 820 if (a_uid != NULL) { 821 uid = (uid_t) atol(a_uid->val); 822 823 if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL) 824 errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 825 } else { 826 struct bitmap bm; 827 828 /* 829 * We need to allocate the next available uid under one of 830 * two policies a) Grab the first unused uid b) Grab the 831 * highest possible unused uid 832 */ 833 if (cnf->min_uid >= cnf->max_uid) { /* Sanity 834 * claus^H^H^H^Hheck */ 835 cnf->min_uid = 1000; 836 cnf->max_uid = 32000; 837 } 838 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 839 840 /* 841 * Now, let's fill the bitmap from the password file 842 */ 843 SETPWENT(); 844 while ((pwd = GETPWENT()) != NULL) 845 if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 846 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 847 ENDPWENT(); 848 849 /* 850 * Then apply the policy, with fallback to reuse if necessary 851 */ 852 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 853 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 854 855 /* 856 * Another sanity check 857 */ 858 if (uid < cnf->min_uid || uid > cnf->max_uid) 859 errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 860 bm_dealloc(&bm); 861 } 862 return uid; 863} 864 865 866static uid_t 867pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 868{ 869 struct group *grp; 870 gid_t gid = (uid_t) - 1; 871 struct carg *a_gid = getarg(args, 'g'); 872 873 /* 874 * If no arg given, see if default can help out 875 */ 876 if (a_gid == NULL && cnf->default_group && *cnf->default_group) 877 a_gid = addarg(args, 'g', cnf->default_group); 878 879 /* 880 * Check the given gid, if any 881 */ 882 SETGRENT(); 883 if (a_gid != NULL) { 884 if ((grp = GETGRNAM(a_gid->val)) == NULL) { 885 gid = (gid_t) atol(a_gid->val); 886 if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 887 errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 888 } 889 gid = grp->gr_gid; 890 } else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) { 891 gid = grp->gr_gid; /* Already created? Use it anyway... */ 892 } else { 893 struct cargs grpargs; 894 char tmp[32]; 895 896 LIST_INIT(&grpargs); 897 addarg(&grpargs, 'n', nam); 898 899 /* 900 * We need to auto-create a group with the user's name. We 901 * can send all the appropriate output to our sister routine 902 * bit first see if we can create a group with gid==uid so we 903 * can keep the user and group ids in sync. We purposely do 904 * NOT check the gid range if we can force the sync. If the 905 * user's name dups an existing group, then the group add 906 * function will happily handle that case for us and exit. 907 */ 908 if (GETGRGID(prefer) == NULL) { 909 sprintf(tmp, "%lu", (unsigned long) prefer); 910 addarg(&grpargs, 'g', tmp); 911 } 912 if (getarg(args, 'N')) 913 { 914 addarg(&grpargs, 'N', NULL); 915 addarg(&grpargs, 'q', NULL); 916 gid = pw_group(cnf, M_NEXT, &grpargs); 917 } 918 else 919 { 920 pw_group(cnf, M_ADD, &grpargs); 921 if ((grp = GETGRNAM(nam)) != NULL) 922 gid = grp->gr_gid; 923 } 924 a_gid = LIST_FIRST(&grpargs); 925 while (a_gid != NULL) { 926 struct carg *t = LIST_NEXT(a_gid, list); 927 LIST_REMOVE(a_gid, list); 928 a_gid = t; 929 } 930 } 931 ENDGRENT(); 932 return gid; 933} 934 935 936static time_t 937pw_pwdpolicy(struct userconf * cnf, struct cargs * args) 938{ 939 time_t result = 0; 940 time_t now = time(NULL); 941 struct carg *arg = getarg(args, 'p'); 942 943 if (arg != NULL) { 944 if ((result = parse_date(now, arg->val)) == now) 945 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 946 } else if (cnf->password_days > 0) 947 result = now + ((long) cnf->password_days * 86400L); 948 return result; 949} 950 951 952static time_t 953pw_exppolicy(struct userconf * cnf, struct cargs * args) 954{ 955 time_t result = 0; 956 time_t now = time(NULL); 957 struct carg *arg = getarg(args, 'e'); 958 959 if (arg != NULL) { 960 if ((result = parse_date(now, arg->val)) == now) 961 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 962 } else if (cnf->expire_days > 0) 963 result = now + ((long) cnf->expire_days * 86400L); 964 return result; 965} 966 967 968static char * 969pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 970{ 971 struct carg *arg = getarg(args, 'd'); 972 973 if (arg) 974 return arg->val; 975 else { 976 static char home[128]; 977 978 if (cnf->home == NULL || *cnf->home == '\0') 979 errx(EX_CONFIG, "no base home directory set"); 980 sprintf(home, "%s/%s", cnf->home, user); 981 return home; 982 } 983} 984 985static char * 986shell_path(char const * path, char *shells[], char *sh) 987{ 988 if (sh != NULL && (*sh == '/' || *sh == '\0')) 989 return sh; /* specified full path or forced none */ 990 else { 991 char *p; 992 char paths[_UC_MAXLINE]; 993 994 /* 995 * We need to search paths 996 */ 997 strlcpy(paths, path, sizeof(paths)); 998 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 999 int i; 1000 static char shellpath[256]; 1001 1002 if (sh != NULL) { 1003 sprintf(shellpath, "%s/%s", p, sh); 1004 if (access(shellpath, X_OK) == 0) 1005 return shellpath; 1006 } else 1007 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 1008 sprintf(shellpath, "%s/%s", p, shells[i]); 1009 if (access(shellpath, X_OK) == 0) 1010 return shellpath; 1011 } 1012 } 1013 if (sh == NULL) 1014 errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 1015 errx(EX_CONFIG, "no default shell available or defined"); 1016 return NULL; 1017 } 1018} 1019 1020 1021static char * 1022pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 1023{ 1024 char *sh = newshell; 1025 struct carg *arg = getarg(args, 's'); 1026 1027 if (newshell == NULL && arg != NULL) 1028 sh = arg->val; 1029 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 1030} 1031 1032static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 1033 1034char * 1035pw_pwcrypt(char *password) 1036{ 1037 int i; 1038 char salt[12]; 1039 1040 static char buf[256]; 1041 1042 /* 1043 * Calculate a salt value 1044 */ 1045 for (i = 0; i < 8; i++) 1046 salt[i] = chars[arc4random() % 63]; 1047 salt[i] = '\0'; 1048 1049 return strcpy(buf, crypt(password, salt)); 1050} 1051 1052#if defined(USE_MD5RAND) 1053u_char * 1054pw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 1055{ 1056 int i; 1057 for (i=0;i<len;i+=16) { 1058 u_char ubuf[16]; 1059 1060 MD5_CTX md5_ctx; 1061 struct timeval tv, tvo; 1062 struct rusage ru; 1063 int n=0; 1064 int t; 1065 1066 MD5Init (&md5_ctx); 1067 t=getpid(); 1068 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 1069 t=getppid(); 1070 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 1071 gettimeofday (&tvo, NULL); 1072 do { 1073 getrusage (RUSAGE_SELF, &ru); 1074 MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 1075 gettimeofday (&tv, NULL); 1076 MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 1077 } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 1078 MD5Final (ubuf, &md5_ctx); 1079 memcpy(buf+i, ubuf, MIN(16, len-i)); 1080 } 1081 return buf; 1082} 1083 1084#else /* Portable version */ 1085 1086static u_char * 1087pw_getrand(u_char *buf, int len) 1088{ 1089 int i; 1090 1091 for (i = 0; i < len; i++) { 1092 unsigned long val = arc4random(); 1093 /* Use all bits in the random value */ 1094 buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 1095 } 1096 return buf; 1097} 1098 1099#endif 1100 1101static char * 1102pw_password(struct userconf * cnf, struct cargs * args, char const * user) 1103{ 1104 int i, l; 1105 char pwbuf[32]; 1106 u_char rndbuf[sizeof pwbuf]; 1107 1108 switch (cnf->default_password) { 1109 case -1: /* Random password */ 1110 l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 1111 pw_getrand(rndbuf, l); 1112 for (i = 0; i < l; i++) 1113 pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)]; 1114 pwbuf[i] = '\0'; 1115 1116 /* 1117 * We give this information back to the user 1118 */ 1119 if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL && 1120 getarg(args, 'N') == NULL) { 1121 if (isatty(STDOUT_FILENO)) 1122 printf("Password for '%s' is: ", user); 1123 printf("%s\n", pwbuf); 1124 fflush(stdout); 1125 } 1126 break; 1127 1128 case -2: /* No password at all! */ 1129 return ""; 1130 1131 case 0: /* No login - default */ 1132 default: 1133 return "*"; 1134 1135 case 1: /* user's name */ 1136 strlcpy(pwbuf, user, sizeof(pwbuf)); 1137 break; 1138 } 1139 return pw_pwcrypt(pwbuf); 1140} 1141 1142 1143static int 1144print_user(struct passwd * pwd, int pretty, int v7) 1145{ 1146 if (!pretty) { 1147 char buf[_UC_MAXLINE]; 1148 1149 fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD); 1150 fputs(buf, stdout); 1151 } else { 1152 int j; 1153 char *p; 1154 struct group *grp = GETGRGID(pwd->pw_gid); 1155 char uname[60] = "User &", office[60] = "[None]", 1156 wphone[60] = "[None]", hphone[60] = "[None]"; 1157 char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 1158 struct tm * tptr; 1159 1160 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1161 strlcpy(uname, p, sizeof(uname)); 1162 if ((p = strtok(NULL, ",")) != NULL) { 1163 strlcpy(office, p, sizeof(office)); 1164 if ((p = strtok(NULL, ",")) != NULL) { 1165 strlcpy(wphone, p, sizeof(wphone)); 1166 if ((p = strtok(NULL, "")) != NULL) { 1167 strlcpy(hphone, p, 1168 sizeof(hphone)); 1169 } 1170 } 1171 } 1172 } 1173 /* 1174 * Handle '&' in gecos field 1175 */ 1176 if ((p = strchr(uname, '&')) != NULL) { 1177 int l = strlen(pwd->pw_name); 1178 int m = strlen(p); 1179 1180 memmove(p + l, p + 1, m); 1181 memmove(p, pwd->pw_name, l); 1182 *p = (char) toupper((unsigned char)*p); 1183 } 1184 if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 1185 strftime(acexpire, sizeof acexpire, "%c", tptr); 1186 if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 1187 strftime(pwexpire, sizeof pwexpire, "%c", tptr); 1188 printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 1189 " Full Name: %s\n" 1190 " Home: %-26.26s Class: %s\n" 1191 " Shell: %-26.26s Office: %s\n" 1192 "Work Phone: %-26.26s Home Phone: %s\n" 1193 "Acc Expire: %-26.26s Pwd Expire: %s\n", 1194 pwd->pw_name, (long) pwd->pw_uid, 1195 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 1196 uname, pwd->pw_dir, pwd->pw_class, 1197 pwd->pw_shell, office, wphone, hphone, 1198 acexpire, pwexpire); 1199 SETGRENT(); 1200 j = 0; 1201 while ((grp=GETGRENT()) != NULL) 1202 { 1203 int i = 0; 1204 while (grp->gr_mem[i] != NULL) 1205 { 1206 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1207 { 1208 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1209 break; 1210 } 1211 ++i; 1212 } 1213 } 1214 ENDGRENT(); 1215 printf("%s", j ? "\n" : ""); 1216 } 1217 return EXIT_SUCCESS; 1218} 1219 1220char * 1221pw_checkname(u_char *name, int gecos) 1222{ 1223 char showch[8]; 1224 u_char const *badchars, *ch, *showtype; 1225 int reject; 1226 1227 ch = name; 1228 reject = 0; 1229 if (gecos) { 1230 /* See if the name is valid as a gecos (comment) field. */ 1231 badchars = ":!@"; 1232 showtype = "gecos field"; 1233 } else { 1234 /* See if the name is valid as a userid or group. */ 1235 badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1236 showtype = "userid/group name"; 1237 /* Userids and groups can not have a leading '-'. */ 1238 if (*ch == '-') 1239 reject = 1; 1240 } 1241 if (!reject) { 1242 while (*ch) { 1243 if (strchr(badchars, *ch) != NULL || *ch < ' ' || 1244 *ch == 127) { 1245 reject = 1; 1246 break; 1247 } 1248 /* 8-bit characters are only allowed in GECOS fields */ 1249 if (!gecos && (*ch & 0x80)) { 1250 reject = 1; 1251 break; 1252 } 1253 ch++; 1254 } 1255 } 1256 /* 1257 * A `$' is allowed as the final character for userids and groups, 1258 * mainly for the benefit of samba. 1259 */ 1260 if (reject && !gecos) { 1261 if (*ch == '$' && *(ch + 1) == '\0') { 1262 reject = 0; 1263 ch++; 1264 } 1265 } 1266 if (reject) { 1267 snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127) 1268 ? "`%c'" : "0x%02x", *ch); 1269 errx(EX_DATAERR, "invalid character %s at position %d in %s", 1270 showch, (ch - name), showtype); 1271 } 1272 if (!gecos && (ch - name) > LOGNAMESIZE) 1273 errx(EX_DATAERR, "name too long `%s' (max is %d)", name, 1274 LOGNAMESIZE); 1275 return (char *)name; 1276} 1277 1278 1279static void 1280rmat(uid_t uid) 1281{ 1282 DIR *d = opendir("/var/at/jobs"); 1283 1284 if (d != NULL) { 1285 struct dirent *e; 1286 1287 while ((e = readdir(d)) != NULL) { 1288 struct stat st; 1289 1290 if (strncmp(e->d_name, ".lock", 5) != 0 && 1291 stat(e->d_name, &st) == 0 && 1292 !S_ISDIR(st.st_mode) && 1293 st.st_uid == uid) { 1294 char tmp[MAXPATHLEN]; 1295 1296 sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 1297 system(tmp); 1298 } 1299 } 1300 closedir(d); 1301 } 1302} 1303 1304static void 1305rmopie(char const * name) 1306{ 1307 static const char etcopie[] = "/etc/opiekeys"; 1308 FILE *fp = fopen(etcopie, "r+"); 1309 1310 if (fp != NULL) { 1311 char tmp[1024]; 1312 off_t atofs = 0; 1313 int length = strlen(name); 1314 1315 while (fgets(tmp, sizeof tmp, fp) != NULL) { 1316 if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 1317 if (fseek(fp, atofs, SEEK_SET) == 0) { 1318 fwrite("#", 1, 1, fp); /* Comment username out */ 1319 } 1320 break; 1321 } 1322 atofs = ftell(fp); 1323 } 1324 /* 1325 * If we got an error of any sort, don't update! 1326 */ 1327 fclose(fp); 1328 } 1329} 1330 1331