pw_user.c revision 63572
11558Srgrimes/*- 21558Srgrimes * Copyright (C) 1996 31558Srgrimes * David L. Nugent. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 141558Srgrimes * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 151558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 161558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 171558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 181558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 191558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 201558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 211558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 221558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 231558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 241558Srgrimes * SUCH DAMAGE. 251558Srgrimes * 261558Srgrimes */ 271558Srgrimes 281558Srgrimes#ifndef lint 291558Srgrimesstatic const char rcsid[] = 30114589Sobrien "$FreeBSD: head/usr.sbin/pw/pw_user.c 63572 2000-07-20 00:09:29Z davidn $"; 311558Srgrimes#endif /* not lint */ 327585Sbde 331558Srgrimes#include <ctype.h> 341558Srgrimes#include <err.h> 351558Srgrimes#include <fcntl.h> 361558Srgrimes#include <sys/param.h> 371558Srgrimes#include <dirent.h> 3841477Sjulian#include <paths.h> 39114589Sobrien#include <termios.h> 4041477Sjulian#include <sys/types.h> 4193103Smarkm#include <sys/time.h> 4293103Smarkm#include <sys/resource.h> 4393103Smarkm#include <unistd.h> 441558Srgrimes#include <utmp.h> 4555275Speter#if defined(USE_MD5RAND) 4675557Smckusick#include <md5.h> 471558Srgrimes#endif 481558Srgrimes#include "pw.h" 4940918Smjacob#include "bitmap.h" 5086514Siedowse 51172236Srodrigc#if (MAXLOGNAME-1) > UT_NAMESIZE 5298542Smckusick#define LOGNAMESIZE UT_NAMESIZE 5323675Speter#else 541558Srgrimes#define LOGNAMESIZE (MAXLOGNAME-1) 551558Srgrimes#endif 5623675Speter 5723675Speterstatic int randinit; 5855725Speterstatic char locked_str[] = "*LOCKED*"; 591558Srgrimes 60120901Smckusickstatic int print_user(struct passwd * pwd, int pretty, int v7); 61172236Srodrigcstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 6255725Speterstatic uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 63101037Smuxstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 6486514Siedowsestatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 6523675Speterstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 661558Srgrimesstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 671558Srgrimesstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 6892839Simpstatic char *shell_path(char const * path, char *shells[], char *sh); 69100935Sphkstatic void rmat(uid_t uid); 7092839Simpstatic void rmskey(char const * name); 71171800Spjd 7292839Simp/*- 7323675Speter * -C config configuration file 747585Sbde * -q quiet operation 7592839Simp * -n name login name 761558Srgrimes * -u uid user id 771558Srgrimes * -c comment user name/comment 7841474Sjulian * -d directory home directory 79126345Sscottl * -e date account expiry date 8066861Sadrian * -p date password expiry date 811558Srgrimes * -g grp primary group 821558Srgrimes * -G grp1,grp2 additional groups 8366861Sadrian * -m [ -k dir ] create and set up home 84188110Smckusick * -s shell name of login shell 85188110Smckusick * -o duplicate uid ok 861558Srgrimes * -L class user class 871558Srgrimes * -l name new login name 8866861Sadrian * -h fd password filehandle 891558Srgrimes * -F force print or add 901558Srgrimes * Setting defaults: 911558Srgrimes * -D set user defaults 921558Srgrimes * -b dir default home root dir 9374556Smckusick * -e period default expiry period 9474556Smckusick * -p period default password change period 9574556Smckusick * -g group default group 9674556Smckusick * -G grp1,grp2.. default additional groups 971558Srgrimes * -L class default login class 9866861Sadrian * -k dir default home skeleton 991558Srgrimes * -s shell default shell 10098542Smckusick * -w method default password method 10198542Smckusick */ 10298542Smckusick 1031558Srgrimesint 1048871Srgrimespw_user(struct userconf * cnf, int mode, struct cargs * args) 1051558Srgrimes{ 1061558Srgrimes int rc, edited = 0; 1071558Srgrimes char *p = NULL; 1081558Srgrimes char *passtmp; 1092153Sdg struct carg *a_name; 11066861Sadrian struct carg *a_uid; 1112153Sdg struct carg *arg; 1122153Sdg struct passwd *pwd = NULL; 11375927Smckusick struct group *grp; 11475927Smckusick struct stat st; 11575927Smckusick char line[_PASSWORD_LEN+1]; 11675927Smckusick 1171558Srgrimes static struct passwd fakeuser = 1181558Srgrimes { 1191558Srgrimes NULL, 12023675Speter "*", 1211558Srgrimes -1, 1221558Srgrimes -1, 1231558Srgrimes 0, 1241558Srgrimes "", 1251558Srgrimes "User &", 1261558Srgrimes "/nonexistent", 1271558Srgrimes "/bin/sh", 1281558Srgrimes 0 12966861Sadrian#if defined(__FreeBSD__) 13066861Sadrian ,0 131187931Sobrien#endif 132187931Sobrien }; 133187931Sobrien 134187931Sobrien 13566861Sadrian /* 13666861Sadrian * With M_NEXT, we only need to return the 137188110Smckusick * next uid to stdout 138188110Smckusick */ 139188110Smckusick if (mode == M_NEXT) 140188110Smckusick { 1411558Srgrimes uid_t next = pw_uidpolicy(cnf, args); 1421558Srgrimes if (getarg(args, 'q')) 1431558Srgrimes return next; 1441558Srgrimes printf("%ld:", (long)next); 1451558Srgrimes pw_group(cnf, mode, args); 1461558Srgrimes return EXIT_SUCCESS; 14766861Sadrian } 1481558Srgrimes 1491558Srgrimes /* 1501558Srgrimes * We can do all of the common legwork here 1511558Srgrimes */ 15266861Sadrian 15366861Sadrian if ((arg = getarg(args, 'b')) != NULL) { 15466861Sadrian cnf->home = arg->val; 15566861Sadrian } 1561558Srgrimes 1571558Srgrimes /* 158187931Sobrien * If we'll need to use it or we're updating it, 1591558Srgrimes * then create the base home directory if necessary 16070050Siedowse */ 161126345Sscottl if (arg != NULL || getarg(args, 'm') != NULL) { 162126345Sscottl int l = strlen(cnf->home); 163126345Sscottl 164126345Sscottl if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 165126345Sscottl cnf->home[--l] = '\0'; 166126345Sscottl 167126345Sscottl if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 168126345Sscottl errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 16941474Sjulian 17041474Sjulian if (stat(cnf->home, &st) == -1) { 171102231Strhodes char dbuf[MAXPATHLEN]; 17241474Sjulian 17341474Sjulian /* 17441474Sjulian * This is a kludge especially for Joerg :) 17541474Sjulian * If the home directory would be created in the root partition, then 17641474Sjulian * we really create it under /usr which is likely to have more space. 17766861Sadrian * But we create a symlink from cnf->home -> "/usr" -> cnf->home 17886514Siedowse */ 17941474Sjulian if (strchr(cnf->home+1, '/') == NULL) { 1801558Srgrimes strcpy(dbuf, "/usr"); 18166861Sadrian strncat(dbuf, cnf->home, MAXPATHLEN-5); 1821558Srgrimes if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) { 1831558Srgrimes chown(dbuf, 0, 0); 1841558Srgrimes symlink(dbuf, cnf->home); 18523675Speter } 186100935Sphk /* If this falls, fall back to old method */ 1871558Srgrimes } 1881558Srgrimes p = strncpy(dbuf, cnf->home, sizeof dbuf); 1891558Srgrimes dbuf[MAXPATHLEN-1] = '\0'; 1901558Srgrimes if (stat(dbuf, &st) == -1) { 1911558Srgrimes while ((p = strchr(++p, '/')) != NULL) { 1921558Srgrimes *p = '\0'; 19323675Speter if (stat(dbuf, &st) == -1) { 1941558Srgrimes if (mkdir(dbuf, 0755) == -1) 1951558Srgrimes goto direrr; 1961558Srgrimes chown(dbuf, 0, 0); 1971558Srgrimes } else if (!S_ISDIR(st.st_mode)) 198102231Strhodes errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 1991558Srgrimes *p = '/'; 2001558Srgrimes } 20123675Speter } 20292839Simp if (stat(dbuf, &st) == -1) { 2031558Srgrimes if (mkdir(dbuf, 0755) == -1) { 20498542Smckusick direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 2051558Srgrimes } 20674556Smckusick chown(dbuf, 0, 0); 207120901Smckusick } 208120901Smckusick } else if (!S_ISDIR(st.st_mode)) 20998542Smckusick errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 210172236Srodrigc } 211172236Srodrigc 212172236Srodrigc if ((arg = getarg(args, 'e')) != NULL) 213171800Spjd cnf->expire_days = atoi(arg->val); 21498542Smckusick 215101037Smux if ((arg = getarg(args, 'y')) != NULL) 2161558Srgrimes cnf->nispasswd = arg->val; 217172236Srodrigc 218172236Srodrigc if ((arg = getarg(args, 'p')) != NULL && arg->val) 219172236Srodrigc cnf->password_days = atoi(arg->val); 220172236Srodrigc 2211558Srgrimes if ((arg = getarg(args, 'g')) != NULL) { 222187931Sobrien p = arg->val; 2231558Srgrimes if ((grp = GETGRNAM(p)) == NULL) { 22475927Smckusick if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 22575927Smckusick errx(EX_NOUSER, "group `%s' does not exist", p); 226102231Strhodes } 22775927Smckusick cnf->default_group = newstr(grp->gr_name); 22875927Smckusick } 22975927Smckusick if ((arg = getarg(args, 'L')) != NULL) 23075927Smckusick cnf->default_class = pw_checkname((u_char *)arg->val, 0); 23175927Smckusick 23275927Smckusick if ((arg = getarg(args, 'G')) != NULL && arg->val) { 23375927Smckusick int i = 0; 23475927Smckusick 23575927Smckusick for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 23675927Smckusick if ((grp = GETGRNAM(p)) == NULL) { 23775927Smckusick if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 23875927Smckusick errx(EX_NOUSER, "group `%s' does not exist", p); 23975927Smckusick } 24075557Smckusick if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 24175927Smckusick cnf->groups[i++] = newstr(grp->gr_name); 24275927Smckusick } 24375927Smckusick while (i < cnf->numgroups) 24475927Smckusick cnf->groups[i++] = NULL; 245207141Sjeff } 246207141Sjeff 247207141Sjeff if ((arg = getarg(args, 'k')) != NULL) { 24875927Smckusick if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 24975927Smckusick errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 25075927Smckusick } 25175927Smckusick 25275927Smckusick if ((arg = getarg(args, 's')) != NULL) 25375927Smckusick cnf->shell_default = arg->val; 25475927Smckusick 25575927Smckusick if (mode == M_ADD && getarg(args, 'D')) { 25675927Smckusick if (getarg(args, 'n') != NULL) 25775927Smckusick errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 258187931Sobrien if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 259163845Spjd if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 260163845Spjd cnf->min_uid = 1000; 261163845Spjd if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 262163845Spjd cnf->max_uid = 32000; 263163845Spjd } 264163845Spjd if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 265163845Spjd if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 266163845Spjd cnf->min_gid = 1000; 267163845Spjd if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 268163845Spjd cnf->max_gid = 32000; 269163845Spjd } 270163845Spjd if ((arg = getarg(args, 'w')) != NULL) 271163845Spjd cnf->default_password = boolean_val(arg->val, cnf->default_password); 272163845Spjd 273163845Spjd arg = getarg(args, 'C'); 274171800Spjd if (write_userconfig(arg ? arg->val : NULL)) 275171800Spjd return EXIT_SUCCESS; 276171800Spjd warn("config update"); 277163845Spjd return EX_IOERR; 278163845Spjd } 279163845Spjd 280163845Spjd if (mode == M_PRINT && getarg(args, 'a')) { 281163845Spjd int pretty = getarg(args, 'P') != NULL; 282163845Spjd int v7 = getarg(args, '7') != NULL; 28374556Smckusick 28474556Smckusick SETPWENT(); 285102231Strhodes while ((pwd = GETPWENT()) != NULL) 28674556Smckusick print_user(pwd, pretty, v7); 28774556Smckusick ENDPWENT(); 28874556Smckusick return EXIT_SUCCESS; 28974556Smckusick } 29074556Smckusick 29174556Smckusick if ((a_name = getarg(args, 'n')) != NULL) 29274556Smckusick pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0)); 29375557Smckusick a_uid = getarg(args, 'u'); 29474556Smckusick 29574556Smckusick if (a_uid == NULL) { 29675557Smckusick if (a_name == NULL) 29775557Smckusick errx(EX_DATAERR, "user name or id required"); 29874556Smckusick 29974556Smckusick /* 30075557Smckusick * Determine whether 'n' switch is name or uid - we don't 30175557Smckusick * really don't really care which we have, but we need to 30275557Smckusick * know. 303207141Sjeff */ 30475557Smckusick if (mode != M_ADD && pwd == NULL 30575557Smckusick && strspn(a_name->val, "0123456789") == strlen(a_name->val) 30675557Smckusick && atoi(a_name->val) > 0) { /* Assume uid */ 30775557Smckusick (a_uid = a_name)->ch = 'u'; 30875557Smckusick a_name = NULL; 309187931Sobrien } 31075557Smckusick } 311102231Strhodes 31275557Smckusick /* 31375557Smckusick * Update, delete & print require that the user exists 314118302Sru */ 31575557Smckusick if (mode == M_UPDATE || mode == M_DELETE || 31675557Smckusick mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 31775557Smckusick 31875557Smckusick if (a_name == NULL && pwd == NULL) /* Try harder */ 31975557Smckusick pwd = GETPWUID(atoi(a_uid->val)); 32075557Smckusick 32175557Smckusick if (pwd == NULL) { 322120901Smckusick if (mode == M_PRINT && getarg(args, 'F')) { 32374556Smckusick fakeuser.pw_name = a_name ? a_name->val : "nouser"; 324120901Smckusick fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 325120901Smckusick return print_user(&fakeuser, 326120901Smckusick getarg(args, 'P') != NULL, 327120901Smckusick getarg(args, '7') != NULL); 328120901Smckusick } 329120901Smckusick if (a_name == NULL) 330120901Smckusick errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 331120901Smckusick errx(EX_NOUSER, "no such user `%s'", a_name->val); 332120901Smckusick } 333120901Smckusick 334120901Smckusick if (a_name == NULL) /* May be needed later */ 335120901Smckusick a_name = addarg(args, 'n', newstr(pwd->pw_name)); 336120901Smckusick 337120901Smckusick /* 338120901Smckusick * The M_LOCK and M_UNLOCK functions simply add or remove 339120901Smckusick * a "*LOCKED*" prefix from in front of the password to 340120901Smckusick * prevent it decoding correctly, and therefore prevents 341120901Smckusick * access. Of course, this only prevents access via 342120901Smckusick * password authentication (not ssh, kerberos or any 343120901Smckusick * other method that does not use the UNIX password) but 344120901Smckusick * that is a known limitation. 345120901Smckusick */ 346120901Smckusick 347120901Smckusick if (mode == M_LOCK) { 348120901Smckusick if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 349120901Smckusick errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 350172236Srodrigc passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str)); 351172236Srodrigc if (passtmp == NULL) /* disaster */ 352172236Srodrigc errx(EX_UNAVAILABLE, "out of memory"); 353172236Srodrigc strcpy(passtmp, locked_str); 354172236Srodrigc strcat(passtmp, pwd->pw_passwd); 355172236Srodrigc pwd->pw_passwd = passtmp; 356172236Srodrigc edited = 1; 357182027Srodrigc } else if (mode == M_UNLOCK) { 358182027Srodrigc if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 359172236Srodrigc errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 360186471Sobrien pwd->pw_passwd += sizeof(locked_str)-1; 36174556Smckusick edited = 1; 36274556Smckusick } else if (mode == M_DELETE) { 36374556Smckusick /* 364172236Srodrigc * Handle deletions now 365172236Srodrigc */ 36674556Smckusick char file[MAXPATHLEN]; 36774556Smckusick char home[MAXPATHLEN]; 36874556Smckusick uid_t uid = pwd->pw_uid; 36974556Smckusick 37074556Smckusick if (strcmp(pwd->pw_name, "root") == 0) 37174556Smckusick errx(EX_DATAERR, "cannot remove user 'root'"); 37274556Smckusick 37323675Speter if (!PWALTDIR()) { 37423675Speter /* 3751558Srgrimes * Remove skey record from /etc/skeykeys 3761558Srgrimes */ 37741477Sjulian 37823675Speter rmskey(pwd->pw_name); 37975557Smckusick 38086514Siedowse /* 38186514Siedowse * Remove crontabs 382100935Sphk */ 383100935Sphk sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 384100935Sphk if (access(file, F_OK) == 0) { 38541477Sjulian sprintf(file, "crontab -u %s -r", pwd->pw_name); 38631904Sbde system(file); 3872153Sdg } 388207141Sjeff } 389207141Sjeff /* 390207141Sjeff * Save these for later, since contents of pwd may be 391207141Sjeff * invalidated by deletion 392207141Sjeff */ 393207141Sjeff sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 394207141Sjeff strncpy(home, pwd->pw_dir, sizeof home); 395207141Sjeff home[sizeof home - 1] = '\0'; 396207141Sjeff 397207141Sjeff rc = delpwent(pwd); 398207141Sjeff if (rc == -1) 399207141Sjeff err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 400207141Sjeff else if (rc != 0) { 401207141Sjeff warn("passwd update"); 402207141Sjeff return EX_IOERR; 403207141Sjeff } 404207141Sjeff 405207141Sjeff if (cnf->nispasswd && *cnf->nispasswd=='/') { 406207141Sjeff rc = delnispwent(cnf->nispasswd, a_name->val); 407207141Sjeff if (rc == -1) 40855275Speter warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 40955275Speter else if (rc != 0) 41034266Sjulian warn("WARNING: NIS passwd update"); 41134266Sjulian /* non-fatal */ 41234266Sjulian } 41334266Sjulian 41434266Sjulian editgroups(a_name->val, NULL); 4151558Srgrimes 4161558Srgrimes pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 4171558Srgrimes 4181558Srgrimes if (!PWALTDIR()) { 41974556Smckusick /* 420102231Strhodes * Remove mail file 4211558Srgrimes */ 4221558Srgrimes remove(file); 4231558Srgrimes 4241558Srgrimes /* 4251558Srgrimes * Remove at jobs 4261558Srgrimes */ 4271558Srgrimes if (getpwuid(uid) == NULL) 4281558Srgrimes rmat(uid); 42934266Sjulian 430201708Smckusick /* 431201708Smckusick * Remove home directory and contents 432201708Smckusick */ 433201708Smckusick if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 4341558Srgrimes if (stat(home, &st) != -1) { 4351558Srgrimes rm_r(home, uid); 4361558Srgrimes pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 4371558Srgrimes a_name->val, (long) uid, home, 4381558Srgrimes stat(home, &st) == -1 ? "" : "not completely "); 4391558Srgrimes } 4401558Srgrimes } 4411558Srgrimes } 4421558Srgrimes return EXIT_SUCCESS; 4431558Srgrimes } else if (mode == M_PRINT) 4441558Srgrimes return print_user(pwd, 4451558Srgrimes getarg(args, 'P') != NULL, 4461558Srgrimes getarg(args, '7') != NULL); 4471558Srgrimes 4481558Srgrimes /* 4491558Srgrimes * The rest is edit code 4501558Srgrimes */ 4511558Srgrimes if ((arg = getarg(args, 'l')) != NULL) { 4521558Srgrimes if (strcmp(pwd->pw_name, "root") == 0) 4531558Srgrimes errx(EX_DATAERR, "can't rename `root' account"); 4541558Srgrimes pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 4551558Srgrimes edited = 1; 4561558Srgrimes } 4571558Srgrimes 4581558Srgrimes if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { 4591558Srgrimes pwd->pw_uid = (uid_t) atol(arg->val); 4601558Srgrimes edited = 1; 4611558Srgrimes if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 4621558Srgrimes errx(EX_DATAERR, "can't change uid of `root' account"); 4631558Srgrimes if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 4641558Srgrimes warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 4651558Srgrimes } 4661558Srgrimes 4671558Srgrimes if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 4681558Srgrimes gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 4691558Srgrimes if (newgid != pwd->pw_gid) { 4701558Srgrimes edited = 1; 47174556Smckusick pwd->pw_gid = newgid; 47274556Smckusick } 47374556Smckusick } 47474556Smckusick 47574556Smckusick if ((arg = getarg(args, 'p')) != NULL) { 47674556Smckusick if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 47774556Smckusick if (pwd->pw_change != 0) { 47874556Smckusick pwd->pw_change = 0; 479100935Sphk edited = 1; 480100935Sphk } 48174556Smckusick } 482101037Smux else { 483101037Smux time_t now = time(NULL); 484101037Smux time_t expire = parse_date(now, arg->val); 485101037Smux 486101037Smux if (now == expire) 487101037Smux errx(EX_DATAERR, "invalid password change date `%s'", arg->val); 4881558Srgrimes if (pwd->pw_change != expire) { 48974556Smckusick pwd->pw_change = expire; 49074556Smckusick edited = 1; 49174556Smckusick } 492100935Sphk } 4931558Srgrimes } 4941558Srgrimes 4951558Srgrimes if ((arg = getarg(args, 'e')) != NULL) { 496100935Sphk if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 4971558Srgrimes if (pwd->pw_expire != 0) { 4981558Srgrimes pwd->pw_expire = 0; 4991558Srgrimes edited = 1; 5001558Srgrimes } 5011558Srgrimes } 5021558Srgrimes else { 5032179Sdg time_t now = time(NULL); 50441474Sjulian time_t expire = parse_date(now, arg->val); 5051558Srgrimes 5061558Srgrimes if (now == expire) 5071558Srgrimes errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val); 5088871Srgrimes if (pwd->pw_expire != expire) { 5091558Srgrimes pwd->pw_expire = expire; 5101558Srgrimes edited = 1; 5111558Srgrimes } 512163845Spjd } 51398542Smckusick } 51498542Smckusick 5151558Srgrimes if ((arg = getarg(args, 's')) != NULL) { 51634266Sjulian char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 51734266Sjulian if (shell == NULL) 51855275Speter shell = ""; 51955725Speter if (strcmp(shell, pwd->pw_shell) != 0) { 520102231Strhodes pwd->pw_shell = shell; 52155725Speter edited = 1; 52274556Smckusick } 52355275Speter } 52434266Sjulian 52541474Sjulian if (getarg(args, 'L')) { 52641474Sjulian if (cnf->default_class == NULL) 52741474Sjulian cnf->default_class = ""; 52841474Sjulian if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 52941474Sjulian pwd->pw_class = cnf->default_class; 53041474Sjulian edited = 1; 53141474Sjulian } 5321558Srgrimes } 53318808Sguido 53418808Sguido if ((arg = getarg(args, 'd')) != NULL) { 535171800Spjd edited = strcmp(pwd->pw_dir, arg->val) != 0; 53641474Sjulian if (stat(pwd->pw_dir = arg->val, &st) == -1) { 53741474Sjulian if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 5381558Srgrimes warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 5391558Srgrimes } else if (!S_ISDIR(st.st_mode)) 5401558Srgrimes warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 5411558Srgrimes } 5421558Srgrimes 5431558Srgrimes if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) { 5441558Srgrimes pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 54555275Speter edited = 1; 546171800Spjd } 547171800Spjd 548171800Spjd } else { 549172236Srodrigc 550172236Srodrigc /* 551172236Srodrigc * Add code 552171800Spjd */ 553171800Spjd 554171800Spjd if (a_name == NULL) /* Required */ 555172236Srodrigc errx(EX_DATAERR, "login name required"); 556172236Srodrigc else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ 557172236Srodrigc errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 558172236Srodrigc 559171800Spjd /* 560171800Spjd * Now, set up defaults for a new user 561171800Spjd */ 562171800Spjd pwd = &fakeuser; 563171800Spjd pwd->pw_name = a_name->val; 564171800Spjd pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 565172236Srodrigc pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 566172236Srodrigc pwd->pw_uid = pw_uidpolicy(cnf, args); 567172236Srodrigc pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 568172236Srodrigc pwd->pw_change = pw_pwdpolicy(cnf, args); 569172236Srodrigc pwd->pw_expire = pw_exppolicy(cnf, args); 570172236Srodrigc pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 571172236Srodrigc pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 572176822Srodrigc edited = 1; 573182027Srodrigc 574177905Srodrigc if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 575177905Srodrigc warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 576177905Srodrigc } 577177905Srodrigc 578176822Srodrigc /* 579186471Sobrien * Shared add/edit code 580171800Spjd */ 581171800Spjd if ((arg = getarg(args, 'c')) != NULL) { 582172236Srodrigc char *gecos = pw_checkname((u_char *)arg->val, 1); 583172236Srodrigc if (strcmp(pwd->pw_gecos, gecos) != 0) { 584171800Spjd pwd->pw_gecos = gecos; 585171800Spjd edited = 1; 586171800Spjd } 587171800Spjd } 588171800Spjd 58955275Speter if ((arg = getarg(args, 'h')) != NULL) { 59075927Smckusick if (strcmp(arg->val, "-") == 0) { 59155275Speter if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 59255275Speter pwd->pw_passwd = "*"; /* No access */ 59392839Simp edited = 1; 59455275Speter } 59555725Speter } else { 59655725Speter int fd = atoi(arg->val); 597100935Sphk int b; 59875927Smckusick int istty = isatty(fd); 59975927Smckusick struct termios t; 60055275Speter 60175927Smckusick if (istty) { 60255275Speter if (tcgetattr(fd, &t) == -1) 60375927Smckusick istty = 0; 60475927Smckusick else { 60575927Smckusick struct termios n = t; 60675927Smckusick 60755275Speter /* Disable echo */ 60855275Speter n.c_lflag &= ~(ECHO); 60975927Smckusick tcsetattr(fd, TCSANOW, &n); 610100935Sphk printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 611100935Sphk fflush(stdout); 61255725Speter } 613100935Sphk } 61475927Smckusick b = read(fd, line, sizeof(line) - 1); 61555725Speter if (istty) { /* Restore state */ 61675927Smckusick tcsetattr(fd, TCSANOW, &t); 61775927Smckusick fputc('\n', stdout); 61875927Smckusick fflush(stdout); 61975927Smckusick } 62075927Smckusick if (b < 0) { 621100935Sphk warn("-h file descriptor"); 62255725Speter return EX_IOERR; 62375927Smckusick } 62455275Speter line[b] = '\0'; 62575927Smckusick if ((p = strpbrk(line, " \t\r\n")) != NULL) 62675927Smckusick *p = '\0'; 62755275Speter if (!*line) 62866861Sadrian errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 62966861Sadrian pwd->pw_passwd = pw_pwcrypt(line); 63092839Simp edited = 1; 63166861Sadrian } 63266861Sadrian } 633188110Smckusick 634141611Sru /* 63593103Smarkm * Special case: -N only displays & exits 63666861Sadrian */ 63766861Sadrian if (getarg(args, 'N') != NULL) 638 return print_user(pwd, 639 getarg(args, 'P') != NULL, 640 getarg(args, '7') != NULL); 641 642 if (mode == M_ADD) { 643 edited = 1; /* Always */ 644 rc = addpwent(pwd); 645 if (rc == -1) { 646 warnx("user '%s' already exists", pwd->pw_name); 647 return EX_IOERR; 648 } else if (rc != 0) { 649 warn("passwd file update"); 650 return EX_IOERR; 651 } 652 if (cnf->nispasswd && *cnf->nispasswd=='/') { 653 rc = addnispwent(cnf->nispasswd, pwd); 654 if (rc == -1) 655 warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 656 else 657 warn("NIS passwd update"); 658 /* NOTE: we treat NIS-only update errors as non-fatal */ 659 } 660 } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 661 if (edited) { /* Only updated this if required */ 662 rc = chgpwent(a_name->val, pwd); 663 if (rc == -1) { 664 warnx("user '%s' does not exist (NIS?)", pwd->pw_name); 665 return EX_IOERR; 666 } else if (rc != 0) { 667 warn("passwd file update"); 668 return EX_IOERR; 669 } 670 if ( cnf->nispasswd && *cnf->nispasswd=='/') { 671 rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); 672 if (rc == -1) 673 warn("User '%s' not found in NIS passwd", pwd->pw_name); 674 else 675 warn("NIS passwd update"); 676 /* NOTE: NIS-only update errors are not fatal */ 677 } 678 } 679 } 680 681 /* 682 * Ok, user is created or changed - now edit group file 683 */ 684 685 if (mode == M_ADD || getarg(args, 'G') != NULL) 686 editgroups(pwd->pw_name, cnf->groups); 687 688 /* go get a current version of pwd */ 689 pwd = GETPWNAM(a_name->val); 690 if (pwd == NULL) { 691 /* This will fail when we rename, so special case that */ 692 if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { 693 a_name->val = arg->val; /* update new name */ 694 pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ 695 } 696 } 697 if (pwd == NULL) /* can't go on without this */ 698 errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 699 700 grp = GETGRGID(pwd->pw_gid); 701 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 702 pwd->pw_name, (long) pwd->pw_uid, 703 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 704 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 705 706 /* 707 * If adding, let's touch and chown the user's mail file. This is not 708 * strictly necessary under BSD with a 0755 maildir but it also 709 * doesn't hurt anything to create the empty mailfile 710 */ 711 if (mode == M_ADD) { 712 FILE *fp; 713 714 if (!PWALTDIR()) { 715 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 716 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 717 * mtime */ 718 chown(line, pwd->pw_uid, pwd->pw_gid); 719 720 /* 721 * Send mail to the new user as well, if we are asked to 722 */ 723 if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 724 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 725 726 if (pfp == NULL) 727 warn("sendmail"); 728 else { 729 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 730 while (fgets(line, sizeof(line), fp) != NULL) { 731 /* Do substitutions? */ 732 fputs(line, pfp); 733 } 734 pclose(pfp); 735 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 736 pwd->pw_name, (long) pwd->pw_uid); 737 } 738 fclose(fp); 739 } 740 } 741 } 742 743 /* 744 * Finally, let's create and populate the user's home directory. Note 745 * that this also `works' for editing users if -m is used, but 746 * existing files will *not* be overwritten. 747 */ 748 if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 749 copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 750 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 751 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 752 } 753 754 return EXIT_SUCCESS; 755} 756 757 758static uid_t 759pw_uidpolicy(struct userconf * cnf, struct cargs * args) 760{ 761 struct passwd *pwd; 762 uid_t uid = (uid_t) - 1; 763 struct carg *a_uid = getarg(args, 'u'); 764 765 /* 766 * Check the given uid, if any 767 */ 768 if (a_uid != NULL) { 769 uid = (uid_t) atol(a_uid->val); 770 771 if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL) 772 errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 773 } else { 774 struct bitmap bm; 775 776 /* 777 * We need to allocate the next available uid under one of 778 * two policies a) Grab the first unused uid b) Grab the 779 * highest possible unused uid 780 */ 781 if (cnf->min_uid >= cnf->max_uid) { /* Sanity 782 * claus^H^H^H^Hheck */ 783 cnf->min_uid = 1000; 784 cnf->max_uid = 32000; 785 } 786 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 787 788 /* 789 * Now, let's fill the bitmap from the password file 790 */ 791 SETPWENT(); 792 while ((pwd = GETPWENT()) != NULL) 793 if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 794 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 795 ENDPWENT(); 796 797 /* 798 * Then apply the policy, with fallback to reuse if necessary 799 */ 800 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 801 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 802 803 /* 804 * Another sanity check 805 */ 806 if (uid < cnf->min_uid || uid > cnf->max_uid) 807 errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 808 bm_dealloc(&bm); 809 } 810 return uid; 811} 812 813 814static uid_t 815pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 816{ 817 struct group *grp; 818 gid_t gid = (uid_t) - 1; 819 struct carg *a_gid = getarg(args, 'g'); 820 821 /* 822 * If no arg given, see if default can help out 823 */ 824 if (a_gid == NULL && cnf->default_group && *cnf->default_group) 825 a_gid = addarg(args, 'g', cnf->default_group); 826 827 /* 828 * Check the given gid, if any 829 */ 830 SETGRENT(); 831 if (a_gid != NULL) { 832 if ((grp = GETGRNAM(a_gid->val)) == NULL) { 833 gid = (gid_t) atol(a_gid->val); 834 if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 835 errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 836 } 837 gid = grp->gr_gid; 838 } else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) { 839 gid = grp->gr_gid; /* Already created? Use it anyway... */ 840 } else { 841 struct cargs grpargs; 842 char tmp[32]; 843 844 LIST_INIT(&grpargs); 845 addarg(&grpargs, 'n', nam); 846 847 /* 848 * We need to auto-create a group with the user's name. We 849 * can send all the appropriate output to our sister routine 850 * bit first see if we can create a group with gid==uid so we 851 * can keep the user and group ids in sync. We purposely do 852 * NOT check the gid range if we can force the sync. If the 853 * user's name dups an existing group, then the group add 854 * function will happily handle that case for us and exit. 855 */ 856 if (GETGRGID(prefer) == NULL) { 857 sprintf(tmp, "%lu", (unsigned long) prefer); 858 addarg(&grpargs, 'g', tmp); 859 } 860 if (getarg(args, 'N')) 861 { 862 addarg(&grpargs, 'N', NULL); 863 addarg(&grpargs, 'q', NULL); 864 gid = pw_group(cnf, M_NEXT, &grpargs); 865 } 866 else 867 { 868 pw_group(cnf, M_ADD, &grpargs); 869 if ((grp = GETGRNAM(nam)) != NULL) 870 gid = grp->gr_gid; 871 } 872 a_gid = grpargs.lh_first; 873 while (a_gid != NULL) { 874 struct carg *t = a_gid->list.le_next; 875 LIST_REMOVE(a_gid, list); 876 a_gid = t; 877 } 878 } 879 ENDGRENT(); 880 return gid; 881} 882 883 884static time_t 885pw_pwdpolicy(struct userconf * cnf, struct cargs * args) 886{ 887 time_t result = 0; 888 time_t now = time(NULL); 889 struct carg *arg = getarg(args, 'p'); 890 891 if (arg != NULL) { 892 if ((result = parse_date(now, arg->val)) == now) 893 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 894 } else if (cnf->password_days > 0) 895 result = now + ((long) cnf->password_days * 86400L); 896 return result; 897} 898 899 900static time_t 901pw_exppolicy(struct userconf * cnf, struct cargs * args) 902{ 903 time_t result = 0; 904 time_t now = time(NULL); 905 struct carg *arg = getarg(args, 'e'); 906 907 if (arg != NULL) { 908 if ((result = parse_date(now, arg->val)) == now) 909 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 910 } else if (cnf->expire_days > 0) 911 result = now + ((long) cnf->expire_days * 86400L); 912 return result; 913} 914 915 916static char * 917pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 918{ 919 struct carg *arg = getarg(args, 'd'); 920 921 if (arg) 922 return arg->val; 923 else { 924 static char home[128]; 925 926 if (cnf->home == NULL || *cnf->home == '\0') 927 errx(EX_CONFIG, "no base home directory set"); 928 sprintf(home, "%s/%s", cnf->home, user); 929 return home; 930 } 931} 932 933static char * 934shell_path(char const * path, char *shells[], char *sh) 935{ 936 if (sh != NULL && (*sh == '/' || *sh == '\0')) 937 return sh; /* specified full path or forced none */ 938 else { 939 char *p; 940 char paths[_UC_MAXLINE]; 941 942 /* 943 * We need to search paths 944 */ 945 strncpy(paths, path, sizeof paths); 946 paths[sizeof paths - 1] = '\0'; 947 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 948 int i; 949 static char shellpath[256]; 950 951 if (sh != NULL) { 952 sprintf(shellpath, "%s/%s", p, sh); 953 if (access(shellpath, X_OK) == 0) 954 return shellpath; 955 } else 956 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 957 sprintf(shellpath, "%s/%s", p, shells[i]); 958 if (access(shellpath, X_OK) == 0) 959 return shellpath; 960 } 961 } 962 if (sh == NULL) 963 errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 964 errx(EX_CONFIG, "no default shell available or defined"); 965 return NULL; 966 } 967} 968 969 970static char * 971pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 972{ 973 char *sh = newshell; 974 struct carg *arg = getarg(args, 's'); 975 976 if (newshell == NULL && arg != NULL) 977 sh = arg->val; 978 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 979} 980 981static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 982 983char * 984pw_pwcrypt(char *password) 985{ 986 int i; 987 char salt[12]; 988 989 static char buf[256]; 990 991 /* 992 * Calculate a salt value 993 */ 994 if (!randinit) { 995 randinit = 1; 996#ifdef __FreeBSD__ 997 srandomdev(); 998#else 999 srandom((unsigned long) (time(NULL) ^ getpid())); 1000#endif 1001 } 1002 for (i = 0; i < 8; i++) 1003 salt[i] = chars[random() % 63]; 1004 salt[i] = '\0'; 1005 1006 return strcpy(buf, crypt(password, salt)); 1007} 1008 1009#if defined(USE_MD5RAND) 1010u_char * 1011pw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 1012{ 1013 int i; 1014 for (i=0;i<len;i+=16) { 1015 u_char ubuf[16]; 1016 1017 MD5_CTX md5_ctx; 1018 struct timeval tv, tvo; 1019 struct rusage ru; 1020 int n=0; 1021 int t; 1022 1023 MD5Init (&md5_ctx); 1024 t=getpid(); 1025 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 1026 t=getppid(); 1027 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 1028 gettimeofday (&tvo, NULL); 1029 do { 1030 getrusage (RUSAGE_SELF, &ru); 1031 MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 1032 gettimeofday (&tv, NULL); 1033 MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 1034 } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 1035 MD5Final (ubuf, &md5_ctx); 1036 memcpy(buf+i, ubuf, MIN(16, len-n)); 1037 } 1038 return buf; 1039} 1040 1041#else /* Portable version */ 1042 1043static u_char * 1044pw_getrand(u_char *buf, int len) 1045{ 1046 int i; 1047 1048 for (i = 0; i < len; i++) { 1049 unsigned long val = random(); 1050 /* Use all bits in the random value */ 1051 buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 1052 } 1053 return buf; 1054} 1055 1056#endif 1057 1058static char * 1059pw_password(struct userconf * cnf, struct cargs * args, char const * user) 1060{ 1061 int i, l; 1062 char pwbuf[32]; 1063 u_char rndbuf[sizeof pwbuf]; 1064 1065 switch (cnf->default_password) { 1066 case -1: /* Random password */ 1067 if (!randinit) { 1068 randinit = 1; 1069#ifdef __FreeBSD__ 1070 srandomdev(); 1071#else 1072 srandom((unsigned long) (time(NULL) ^ getpid())); 1073#endif 1074 } 1075 l = (random() % 8 + 8); /* 8 - 16 chars */ 1076 pw_getrand(rndbuf, l); 1077 for (i = 0; i < l; i++) 1078 pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)]; 1079 pwbuf[i] = '\0'; 1080 1081 /* 1082 * We give this information back to the user 1083 */ 1084 if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) { 1085 if (isatty(STDOUT_FILENO)) 1086 printf("Password for '%s' is: ", user); 1087 printf("%s\n", pwbuf); 1088 fflush(stdout); 1089 } 1090 break; 1091 1092 case -2: /* No password at all! */ 1093 return ""; 1094 1095 case 0: /* No login - default */ 1096 default: 1097 return "*"; 1098 1099 case 1: /* user's name */ 1100 strncpy(pwbuf, user, sizeof pwbuf); 1101 pwbuf[sizeof pwbuf - 1] = '\0'; 1102 break; 1103 } 1104 return pw_pwcrypt(pwbuf); 1105} 1106 1107 1108static int 1109print_user(struct passwd * pwd, int pretty, int v7) 1110{ 1111 if (!pretty) { 1112 char buf[_UC_MAXLINE]; 1113 1114 fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD); 1115 fputs(buf, stdout); 1116 } else { 1117 int j; 1118 char *p; 1119 struct group *grp = GETGRGID(pwd->pw_gid); 1120 char uname[60] = "User &", office[60] = "[None]", 1121 wphone[60] = "[None]", hphone[60] = "[None]"; 1122 char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 1123 struct tm * tptr; 1124 1125 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1126 strncpy(uname, p, sizeof uname); 1127 uname[sizeof uname - 1] = '\0'; 1128 if ((p = strtok(NULL, ",")) != NULL) { 1129 strncpy(office, p, sizeof office); 1130 office[sizeof office - 1] = '\0'; 1131 if ((p = strtok(NULL, ",")) != NULL) { 1132 strncpy(wphone, p, sizeof wphone); 1133 wphone[sizeof wphone - 1] = '\0'; 1134 if ((p = strtok(NULL, "")) != NULL) { 1135 strncpy(hphone, p, sizeof hphone); 1136 hphone[sizeof hphone - 1] = '\0'; 1137 } 1138 } 1139 } 1140 } 1141 /* 1142 * Handle '&' in gecos field 1143 */ 1144 if ((p = strchr(uname, '&')) != NULL) { 1145 int l = strlen(pwd->pw_name); 1146 int m = strlen(p); 1147 1148 memmove(p + l, p + 1, m); 1149 memmove(p, pwd->pw_name, l); 1150 *p = (char) toupper((unsigned char)*p); 1151 } 1152 if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 1153 strftime(acexpire, sizeof acexpire, "%a %Ef %Y %X", tptr); 1154 if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 1155 strftime(pwexpire, sizeof pwexpire, "%a %Ef %Y %X", tptr); 1156 printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 1157 " Full Name: %s\n" 1158 " Home: %-26.26s Class: %s\n" 1159 " Shell: %-26.26s Office: %s\n" 1160 "Work Phone: %-26.26s Home Phone: %s\n" 1161 "Acc Expire: %-26.26s Pwd Expire: %s\n", 1162 pwd->pw_name, (long) pwd->pw_uid, 1163 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 1164 uname, pwd->pw_dir, pwd->pw_class, 1165 pwd->pw_shell, office, wphone, hphone, 1166 acexpire, pwexpire); 1167 SETGRENT(); 1168 j = 0; 1169 while ((grp=GETGRENT()) != NULL) 1170 { 1171 int i = 0; 1172 while (grp->gr_mem[i] != NULL) 1173 { 1174 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1175 { 1176 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1177 break; 1178 } 1179 ++i; 1180 } 1181 } 1182 ENDGRENT(); 1183 printf("%s", j ? "\n" : ""); 1184 } 1185 return EXIT_SUCCESS; 1186} 1187 1188char * 1189pw_checkname(u_char *name, int gecos) 1190{ 1191 int l = 0; 1192 char const *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1193 1194 while (name[l]) { 1195 if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 || 1196 (!gecos && l==0 && name[l] == '-') || /* leading '-' */ 1197 (!gecos && name[l] & 0x80)) /* 8-bit */ 1198 errx(EX_DATAERR, (name[l] >= ' ' && name[l] < 127) 1199 ? "invalid character `%c' in field" 1200 : "invalid character 0x%02x in field", 1201 name[l]); 1202 ++l; 1203 } 1204 if (!gecos && l > LOGNAMESIZE) 1205 errx(EX_DATAERR, "name too long `%s'", name); 1206 return (char *)name; 1207} 1208 1209 1210static void 1211rmat(uid_t uid) 1212{ 1213 DIR *d = opendir("/var/at/jobs"); 1214 1215 if (d != NULL) { 1216 struct dirent *e; 1217 1218 while ((e = readdir(d)) != NULL) { 1219 struct stat st; 1220 1221 if (strncmp(e->d_name, ".lock", 5) != 0 && 1222 stat(e->d_name, &st) == 0 && 1223 !S_ISDIR(st.st_mode) && 1224 st.st_uid == uid) { 1225 char tmp[MAXPATHLEN]; 1226 1227 sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 1228 system(tmp); 1229 } 1230 } 1231 closedir(d); 1232 } 1233} 1234 1235static void 1236rmskey(char const * name) 1237{ 1238 static const char etcskey[] = "/etc/skeykeys"; 1239 FILE *fp = fopen(etcskey, "r+"); 1240 1241 if (fp != NULL) { 1242 char tmp[1024]; 1243 off_t atofs = 0; 1244 int length = strlen(name); 1245 1246 while (fgets(tmp, sizeof tmp, fp) != NULL) { 1247 if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 1248 if (fseek(fp, atofs, SEEK_SET) == 0) { 1249 fwrite("#", 1, 1, fp); /* Comment username out */ 1250 } 1251 break; 1252 } 1253 atofs = ftell(fp); 1254 } 1255 /* 1256 * If we got an error of any sort, don't update! 1257 */ 1258 fclose(fp); 1259 } 1260} 1261 1262