pw_user.c revision 73563
166131Swpaul/*- 266131Swpaul * Copyright (C) 1996 366131Swpaul * David L. Nugent. All rights reserved. 466131Swpaul * 566131Swpaul * Redistribution and use in source and binary forms, with or without 666131Swpaul * modification, are permitted provided that the following conditions 766131Swpaul * are met: 866131Swpaul * 1. Redistributions of source code must retain the above copyright 966131Swpaul * notice, this list of conditions and the following disclaimer. 1066131Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1166131Swpaul * notice, this list of conditions and the following disclaimer in the 1266131Swpaul * documentation and/or other materials provided with the distribution. 1366131Swpaul * 1466131Swpaul * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND 1566131Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1666131Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1766131Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE 1866131Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1966131Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2066131Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2166131Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2266131Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2366131Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2466131Swpaul * SUCH DAMAGE. 2566131Swpaul * 2666131Swpaul */ 2766131Swpaul 2866131Swpaul#ifndef lint 2966131Swpaulstatic const char rcsid[] = 3066131Swpaul "$FreeBSD: head/usr.sbin/pw/pw_user.c 73563 2001-03-05 02:15:38Z kris $"; 3166131Swpaul#endif /* not lint */ 3266131Swpaul 3366131Swpaul#include <ctype.h> 34155157Smarius#include <err.h> 3566131Swpaul#include <fcntl.h> 3666131Swpaul#include <sys/param.h> 3766131Swpaul#include <dirent.h> 3866131Swpaul#include <paths.h> 39135141Sbrueffer#include <termios.h> 4066131Swpaul#include <sys/types.h> 41151046Strhodes#include <sys/time.h> 42151046Strhodes#include <sys/resource.h> 43148220Strhodes#include <unistd.h> 44148145Strhodes#include <utmp.h> 4566131Swpaul#include <login_cap.h> 4666131Swpaul#if defined(USE_MD5RAND) 47148145Strhodes#include <md5.h> 48148145Strhodes#endif 49151046Strhodes#include "pw.h" 50151046Strhodes#include "bitmap.h" 51148145Strhodes 52148145Strhodes#if (MAXLOGNAME-1) > UT_NAMESIZE 53148145Strhodes#define LOGNAMESIZE UT_NAMESIZE 54148145Strhodes#else 5566131Swpaul#define LOGNAMESIZE (MAXLOGNAME-1) 5666131Swpaul#endif 5766131Swpaul 58135141Sbruefferstatic char locked_str[] = "*LOCKED*"; 5966690Swpaul 60135141Sbruefferstatic int print_user(struct passwd * pwd, int pretty, int v7); 61155157Smariusstatic uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args); 6266131Swpaulstatic uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer); 63135141Sbruefferstatic time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args); 6466131Swpaulstatic time_t pw_exppolicy(struct userconf * cnf, struct cargs * args); 6566196Swpaulstatic char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user); 6666196Swpaulstatic char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell); 6766196Swpaulstatic char *pw_password(struct userconf * cnf, struct cargs * args, char const * user); 6866196Swpaulstatic char *shell_path(char const * path, char *shells[], char *sh); 6966131Swpaulstatic void rmat(uid_t uid); 7066131Swpaulstatic void rmskey(char const * name); 7166131Swpaul 7266131Swpaul/*- 7366131Swpaul * -C config configuration file 7466131Swpaul * -q quiet operation 7566131Swpaul * -n name login name 7666131Swpaul * -u uid user id 7766131Swpaul * -c comment user name/comment 7866131Swpaul * -d directory home directory 7966131Swpaul * -e date account expiry date 8066131Swpaul * -p date password expiry date 8166131Swpaul * -g grp primary group 8266131Swpaul * -G grp1,grp2 additional groups 8366131Swpaul * -m [ -k dir ] create and set up home 8466131Swpaul * -s shell name of login shell 8566131Swpaul * -o duplicate uid ok 8666131Swpaul * -L class user class 8766131Swpaul * -l name new login name 8866131Swpaul * -h fd password filehandle 8966131Swpaul * -F force print or add 9066131Swpaul * Setting defaults: 91135141Sbrueffer * -D set user defaults 9266131Swpaul * -b dir default home root dir 9366131Swpaul * -e period default expiry period 9466131Swpaul * -p period default password change period 9566131Swpaul * -g group default group 9666131Swpaul * -G grp1,grp2.. default additional groups 9766131Swpaul * -L class default login class 9866131Swpaul * -k dir default home skeleton 9966131Swpaul * -s shell default shell 10066131Swpaul * -w method default password method 10166131Swpaul */ 10266131Swpaul 10366131Swpaulint 10466131Swpaulpw_user(struct userconf * cnf, int mode, struct cargs * args) 10566131Swpaul{ 10666131Swpaul int rc, edited = 0; 107235317Sgjb char *p = NULL; 10866131Swpaul char *passtmp; 10966131Swpaul struct carg *a_name; 11066131Swpaul struct carg *a_uid; 11166131Swpaul struct carg *arg; 11266131Swpaul struct passwd *pwd = NULL; 11366131Swpaul struct group *grp; 114135141Sbrueffer struct stat st; 115135141Sbrueffer char line[_PASSWORD_LEN+1]; 116135141Sbrueffer 117135141Sbrueffer static struct passwd fakeuser = 118135141Sbrueffer { 119135141Sbrueffer NULL, 120135141Sbrueffer "*", 121135141Sbrueffer -1, 122135141Sbrueffer -1, 123135141Sbrueffer 0, 124135141Sbrueffer "", 125135141Sbrueffer "User &", 126135141Sbrueffer "/nonexistent", 127135141Sbrueffer "/bin/sh", 128135141Sbrueffer 0 129135141Sbrueffer#if defined(__FreeBSD__) 130155157Smarius ,0 131135141Sbrueffer#endif 132135141Sbrueffer }; 133135141Sbrueffer 13466131Swpaul 13566131Swpaul /* 13666131Swpaul * With M_NEXT, we only need to return the 13766131Swpaul * next uid to stdout 13866131Swpaul */ 13966131Swpaul if (mode == M_NEXT) 14066131Swpaul { 14166131Swpaul uid_t next = pw_uidpolicy(cnf, args); 142131530Sru if (getarg(args, 'q')) 14366131Swpaul return next; 14466131Swpaul printf("%ld:", (long)next); 14566131Swpaul pw_group(cnf, mode, args); 14666131Swpaul return EXIT_SUCCESS; 14789610Smpp } 14866131Swpaul 14966131Swpaul /* 15066131Swpaul * We can do all of the common legwork here 15166131Swpaul */ 15266131Swpaul 15366131Swpaul if ((arg = getarg(args, 'b')) != NULL) { 15466131Swpaul cnf->home = arg->val; 15566131Swpaul } 15666131Swpaul 15766131Swpaul /* 15866131Swpaul * If we'll need to use it or we're updating it, 15966131Swpaul * then create the base home directory if necessary 16066131Swpaul */ 16166131Swpaul if (arg != NULL || getarg(args, 'm') != NULL) { 16266131Swpaul int l = strlen(cnf->home); 16366131Swpaul 16466131Swpaul if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */ 16566131Swpaul cnf->home[--l] = '\0'; 16666131Swpaul 16766131Swpaul if (l < 2 || *cnf->home != '/') /* Check for absolute path name */ 16866131Swpaul errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home); 16966131Swpaul 17066131Swpaul if (stat(cnf->home, &st) == -1) { 17166131Swpaul char dbuf[MAXPATHLEN]; 17266131Swpaul 17366131Swpaul /* 174110946Strhodes * This is a kludge especially for Joerg :) 17579727Sschweikh * If the home directory would be created in the root partition, then 17666131Swpaul * we really create it under /usr which is likely to have more space. 17766131Swpaul * But we create a symlink from cnf->home -> "/usr" -> cnf->home 17866131Swpaul */ 17966131Swpaul if (strchr(cnf->home+1, '/') == NULL) { 180202386Sru strcpy(dbuf, "/usr"); 18166131Swpaul strncat(dbuf, cnf->home, MAXPATHLEN-5); 18266131Swpaul if (mkdir(dbuf, 0755) != -1 || errno == EEXIST) { 18366131Swpaul chown(dbuf, 0, 0); 18466131Swpaul symlink(dbuf, cnf->home); 18566131Swpaul } 18680898Ssheldonh /* If this falls, fall back to old method */ 18766131Swpaul } 18866131Swpaul p = strncpy(dbuf, cnf->home, sizeof dbuf); 18966131Swpaul dbuf[MAXPATHLEN-1] = '\0'; 19066131Swpaul if (stat(dbuf, &st) == -1) { 19166131Swpaul while ((p = strchr(++p, '/')) != NULL) { 192 *p = '\0'; 193 if (stat(dbuf, &st) == -1) { 194 if (mkdir(dbuf, 0755) == -1) 195 goto direrr; 196 chown(dbuf, 0, 0); 197 } else if (!S_ISDIR(st.st_mode)) 198 errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf); 199 *p = '/'; 200 } 201 } 202 if (stat(dbuf, &st) == -1) { 203 if (mkdir(dbuf, 0755) == -1) { 204 direrr: err(EX_OSFILE, "mkdir '%s'", dbuf); 205 } 206 chown(dbuf, 0, 0); 207 } 208 } else if (!S_ISDIR(st.st_mode)) 209 errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home); 210 } 211 212 if ((arg = getarg(args, 'e')) != NULL) 213 cnf->expire_days = atoi(arg->val); 214 215 if ((arg = getarg(args, 'y')) != NULL) 216 cnf->nispasswd = arg->val; 217 218 if ((arg = getarg(args, 'p')) != NULL && arg->val) 219 cnf->password_days = atoi(arg->val); 220 221 if ((arg = getarg(args, 'g')) != NULL) { 222 if (!*(p = arg->val)) /* Handle empty group list specially */ 223 cnf->default_group = ""; 224 else { 225 if ((grp = GETGRNAM(p)) == NULL) { 226 if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 227 errx(EX_NOUSER, "group `%s' does not exist", p); 228 } 229 cnf->default_group = newstr(grp->gr_name); 230 } 231 } 232 if ((arg = getarg(args, 'L')) != NULL) 233 cnf->default_class = pw_checkname((u_char *)arg->val, 0); 234 235 if ((arg = getarg(args, 'G')) != NULL && arg->val) { 236 int i = 0; 237 238 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) { 239 if ((grp = GETGRNAM(p)) == NULL) { 240 if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL) 241 errx(EX_NOUSER, "group `%s' does not exist", p); 242 } 243 if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1) 244 cnf->groups[i++] = newstr(grp->gr_name); 245 } 246 while (i < cnf->numgroups) 247 cnf->groups[i++] = NULL; 248 } 249 250 if ((arg = getarg(args, 'k')) != NULL) { 251 if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode)) 252 errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir); 253 } 254 255 if ((arg = getarg(args, 's')) != NULL) 256 cnf->shell_default = arg->val; 257 258 if ((arg = getarg(args, 'w')) != NULL) 259 cnf->default_password = boolean_val(arg->val, cnf->default_password); 260 if (mode == M_ADD && getarg(args, 'D')) { 261 if (getarg(args, 'n') != NULL) 262 errx(EX_DATAERR, "can't combine `-D' with `-n name'"); 263 if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 264 if ((cnf->min_uid = (uid_t) atoi(p)) == 0) 265 cnf->min_uid = 1000; 266 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid) 267 cnf->max_uid = 32000; 268 } 269 if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) { 270 if ((cnf->min_gid = (gid_t) atoi(p)) == 0) 271 cnf->min_gid = 1000; 272 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid) 273 cnf->max_gid = 32000; 274 } 275 276 arg = getarg(args, 'C'); 277 if (write_userconfig(arg ? arg->val : NULL)) 278 return EXIT_SUCCESS; 279 warn("config update"); 280 return EX_IOERR; 281 } 282 283 if (mode == M_PRINT && getarg(args, 'a')) { 284 int pretty = getarg(args, 'P') != NULL; 285 int v7 = getarg(args, '7') != NULL; 286 287 SETPWENT(); 288 while ((pwd = GETPWENT()) != NULL) 289 print_user(pwd, pretty, v7); 290 ENDPWENT(); 291 return EXIT_SUCCESS; 292 } 293 294 if ((a_name = getarg(args, 'n')) != NULL) 295 pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0)); 296 a_uid = getarg(args, 'u'); 297 298 if (a_uid == NULL) { 299 if (a_name == NULL) 300 errx(EX_DATAERR, "user name or id required"); 301 302 /* 303 * Determine whether 'n' switch is name or uid - we don't 304 * really don't really care which we have, but we need to 305 * know. 306 */ 307 if (mode != M_ADD && pwd == NULL 308 && strspn(a_name->val, "0123456789") == strlen(a_name->val) 309 && atoi(a_name->val) > 0) { /* Assume uid */ 310 (a_uid = a_name)->ch = 'u'; 311 a_name = NULL; 312 } 313 } 314 315 /* 316 * Update, delete & print require that the user exists 317 */ 318 if (mode == M_UPDATE || mode == M_DELETE || 319 mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) { 320 321 if (a_name == NULL && pwd == NULL) /* Try harder */ 322 pwd = GETPWUID(atoi(a_uid->val)); 323 324 if (pwd == NULL) { 325 if (mode == M_PRINT && getarg(args, 'F')) { 326 fakeuser.pw_name = a_name ? a_name->val : "nouser"; 327 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1; 328 return print_user(&fakeuser, 329 getarg(args, 'P') != NULL, 330 getarg(args, '7') != NULL); 331 } 332 if (a_name == NULL) 333 errx(EX_NOUSER, "no such uid `%s'", a_uid->val); 334 errx(EX_NOUSER, "no such user `%s'", a_name->val); 335 } 336 337 if (a_name == NULL) /* May be needed later */ 338 a_name = addarg(args, 'n', newstr(pwd->pw_name)); 339 340 /* 341 * The M_LOCK and M_UNLOCK functions simply add or remove 342 * a "*LOCKED*" prefix from in front of the password to 343 * prevent it decoding correctly, and therefore prevents 344 * access. Of course, this only prevents access via 345 * password authentication (not ssh, kerberos or any 346 * other method that does not use the UNIX password) but 347 * that is a known limitation. 348 */ 349 350 if (mode == M_LOCK) { 351 if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0) 352 errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name); 353 passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str)); 354 if (passtmp == NULL) /* disaster */ 355 errx(EX_UNAVAILABLE, "out of memory"); 356 strcpy(passtmp, locked_str); 357 strcat(passtmp, pwd->pw_passwd); 358 pwd->pw_passwd = passtmp; 359 edited = 1; 360 } else if (mode == M_UNLOCK) { 361 if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0) 362 errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name); 363 pwd->pw_passwd += sizeof(locked_str)-1; 364 edited = 1; 365 } else if (mode == M_DELETE) { 366 /* 367 * Handle deletions now 368 */ 369 char file[MAXPATHLEN]; 370 char home[MAXPATHLEN]; 371 uid_t uid = pwd->pw_uid; 372 373 if (strcmp(pwd->pw_name, "root") == 0) 374 errx(EX_DATAERR, "cannot remove user 'root'"); 375 376 if (!PWALTDIR()) { 377 /* 378 * Remove skey record from /etc/skeykeys 379 */ 380 381 rmskey(pwd->pw_name); 382 383 /* 384 * Remove crontabs 385 */ 386 sprintf(file, "/var/cron/tabs/%s", pwd->pw_name); 387 if (access(file, F_OK) == 0) { 388 sprintf(file, "crontab -u %s -r", pwd->pw_name); 389 system(file); 390 } 391 } 392 /* 393 * Save these for later, since contents of pwd may be 394 * invalidated by deletion 395 */ 396 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 397 strncpy(home, pwd->pw_dir, sizeof home); 398 home[sizeof home - 1] = '\0'; 399 400 rc = delpwent(pwd); 401 if (rc == -1) 402 err(EX_IOERR, "user '%s' does not exist", pwd->pw_name); 403 else if (rc != 0) { 404 warn("passwd update"); 405 return EX_IOERR; 406 } 407 408 if (cnf->nispasswd && *cnf->nispasswd=='/') { 409 rc = delnispwent(cnf->nispasswd, a_name->val); 410 if (rc == -1) 411 warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name); 412 else if (rc != 0) 413 warn("WARNING: NIS passwd update"); 414 /* non-fatal */ 415 } 416 417 editgroups(a_name->val, NULL); 418 419 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid); 420 421 if (!PWALTDIR()) { 422 /* 423 * Remove mail file 424 */ 425 remove(file); 426 427 /* 428 * Remove at jobs 429 */ 430 if (getpwuid(uid) == NULL) 431 rmat(uid); 432 433 /* 434 * Remove home directory and contents 435 */ 436 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) { 437 if (stat(home, &st) != -1) { 438 rm_r(home, uid); 439 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved", 440 a_name->val, (long) uid, home, 441 stat(home, &st) == -1 ? "" : "not completely "); 442 } 443 } 444 } 445 return EXIT_SUCCESS; 446 } else if (mode == M_PRINT) 447 return print_user(pwd, 448 getarg(args, 'P') != NULL, 449 getarg(args, '7') != NULL); 450 451 /* 452 * The rest is edit code 453 */ 454 if ((arg = getarg(args, 'l')) != NULL) { 455 if (strcmp(pwd->pw_name, "root") == 0) 456 errx(EX_DATAERR, "can't rename `root' account"); 457 pwd->pw_name = pw_checkname((u_char *)arg->val, 0); 458 edited = 1; 459 } 460 461 if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) { 462 pwd->pw_uid = (uid_t) atol(arg->val); 463 edited = 1; 464 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0) 465 errx(EX_DATAERR, "can't change uid of `root' account"); 466 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 467 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name); 468 } 469 470 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */ 471 gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid; 472 if (newgid != pwd->pw_gid) { 473 edited = 1; 474 pwd->pw_gid = newgid; 475 } 476 } 477 478 if ((arg = getarg(args, 'p')) != NULL) { 479 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 480 if (pwd->pw_change != 0) { 481 pwd->pw_change = 0; 482 edited = 1; 483 } 484 } 485 else { 486 time_t now = time(NULL); 487 time_t expire = parse_date(now, arg->val); 488 489 if (now == expire) 490 errx(EX_DATAERR, "invalid password change date `%s'", arg->val); 491 if (pwd->pw_change != expire) { 492 pwd->pw_change = expire; 493 edited = 1; 494 } 495 } 496 } 497 498 if ((arg = getarg(args, 'e')) != NULL) { 499 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) { 500 if (pwd->pw_expire != 0) { 501 pwd->pw_expire = 0; 502 edited = 1; 503 } 504 } 505 else { 506 time_t now = time(NULL); 507 time_t expire = parse_date(now, arg->val); 508 509 if (now == expire) 510 errx(EX_DATAERR, "invalid account expiry date `%s'", arg->val); 511 if (pwd->pw_expire != expire) { 512 pwd->pw_expire = expire; 513 edited = 1; 514 } 515 } 516 } 517 518 if ((arg = getarg(args, 's')) != NULL) { 519 char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val); 520 if (shell == NULL) 521 shell = ""; 522 if (strcmp(shell, pwd->pw_shell) != 0) { 523 pwd->pw_shell = shell; 524 edited = 1; 525 } 526 } 527 528 if (getarg(args, 'L')) { 529 if (cnf->default_class == NULL) 530 cnf->default_class = ""; 531 if (strcmp(pwd->pw_class, cnf->default_class) != 0) { 532 pwd->pw_class = cnf->default_class; 533 edited = 1; 534 } 535 } 536 537 if ((arg = getarg(args, 'd')) != NULL) { 538 edited = strcmp(pwd->pw_dir, arg->val) != 0; 539 if (stat(pwd->pw_dir = arg->val, &st) == -1) { 540 if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0) 541 warnx("WARNING: home `%s' does not exist", pwd->pw_dir); 542 } else if (!S_ISDIR(st.st_mode)) 543 warnx("WARNING: home `%s' is not a directory", pwd->pw_dir); 544 } 545 546 if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL) { 547 login_cap_t *lc; 548 549 lc = login_getpwclass(pwd); 550 if (lc == NULL || 551 login_setcryptfmt(lc, "md5", NULL) == NULL) 552 warn("setting crypt(3) format"); 553 login_close(lc); 554 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 555 edited = 1; 556 } 557 558 } else { 559 login_cap_t *lc; 560 561 /* 562 * Add code 563 */ 564 565 if (a_name == NULL) /* Required */ 566 errx(EX_DATAERR, "login name required"); 567 else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */ 568 errx(EX_DATAERR, "login name `%s' already exists", a_name->val); 569 570 /* 571 * Now, set up defaults for a new user 572 */ 573 pwd = &fakeuser; 574 pwd->pw_name = a_name->val; 575 pwd->pw_class = cnf->default_class ? cnf->default_class : ""; 576 pwd->pw_uid = pw_uidpolicy(cnf, args); 577 pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid); 578 pwd->pw_change = pw_pwdpolicy(cnf, args); 579 pwd->pw_expire = pw_exppolicy(cnf, args); 580 pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name); 581 pwd->pw_shell = pw_shellpolicy(cnf, args, NULL); 582 lc = login_getpwclass(pwd); 583 if (lc == NULL || login_setcryptfmt(lc, "md5", NULL) == NULL) 584 warn("setting crypt(3) format"); 585 login_close(lc); 586 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name); 587 edited = 1; 588 589 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0) 590 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name); 591 } 592 593 /* 594 * Shared add/edit code 595 */ 596 if ((arg = getarg(args, 'c')) != NULL) { 597 char *gecos = pw_checkname((u_char *)arg->val, 1); 598 if (strcmp(pwd->pw_gecos, gecos) != 0) { 599 pwd->pw_gecos = gecos; 600 edited = 1; 601 } 602 } 603 604 if ((arg = getarg(args, 'h')) != NULL) { 605 if (strcmp(arg->val, "-") == 0) { 606 if (!pwd->pw_passwd || *pwd->pw_passwd != '*') { 607 pwd->pw_passwd = "*"; /* No access */ 608 edited = 1; 609 } 610 } else { 611 int fd = atoi(arg->val); 612 int b; 613 int istty = isatty(fd); 614 struct termios t; 615 login_cap_t *lc; 616 617 if (istty) { 618 if (tcgetattr(fd, &t) == -1) 619 istty = 0; 620 else { 621 struct termios n = t; 622 623 /* Disable echo */ 624 n.c_lflag &= ~(ECHO); 625 tcsetattr(fd, TCSANOW, &n); 626 printf("%sassword for user %s:", (mode == M_UPDATE) ? "New p" : "P", pwd->pw_name); 627 fflush(stdout); 628 } 629 } 630 b = read(fd, line, sizeof(line) - 1); 631 if (istty) { /* Restore state */ 632 tcsetattr(fd, TCSANOW, &t); 633 fputc('\n', stdout); 634 fflush(stdout); 635 } 636 if (b < 0) { 637 warn("-h file descriptor"); 638 return EX_IOERR; 639 } 640 line[b] = '\0'; 641 if ((p = strpbrk(line, " \t\r\n")) != NULL) 642 *p = '\0'; 643 if (!*line) 644 errx(EX_DATAERR, "empty password read on file descriptor %d", fd); 645 lc = login_getpwclass(pwd); 646 if (lc == NULL || 647 login_setcryptfmt(lc, "md5", NULL) == NULL) 648 warn("setting crypt(3) format"); 649 login_close(lc); 650 pwd->pw_passwd = pw_pwcrypt(line); 651 edited = 1; 652 } 653 } 654 655 /* 656 * Special case: -N only displays & exits 657 */ 658 if (getarg(args, 'N') != NULL) 659 return print_user(pwd, 660 getarg(args, 'P') != NULL, 661 getarg(args, '7') != NULL); 662 663 if (mode == M_ADD) { 664 edited = 1; /* Always */ 665 rc = addpwent(pwd); 666 if (rc == -1) { 667 warnx("user '%s' already exists", pwd->pw_name); 668 return EX_IOERR; 669 } else if (rc != 0) { 670 warn("passwd file update"); 671 return EX_IOERR; 672 } 673 if (cnf->nispasswd && *cnf->nispasswd=='/') { 674 rc = addnispwent(cnf->nispasswd, pwd); 675 if (rc == -1) 676 warnx("User '%s' already exists in NIS passwd", pwd->pw_name); 677 else 678 warn("NIS passwd update"); 679 /* NOTE: we treat NIS-only update errors as non-fatal */ 680 } 681 } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) { 682 if (edited) { /* Only updated this if required */ 683 rc = chgpwent(a_name->val, pwd); 684 if (rc == -1) { 685 warnx("user '%s' does not exist (NIS?)", pwd->pw_name); 686 return EX_IOERR; 687 } else if (rc != 0) { 688 warn("passwd file update"); 689 return EX_IOERR; 690 } 691 if ( cnf->nispasswd && *cnf->nispasswd=='/') { 692 rc = chgnispwent(cnf->nispasswd, a_name->val, pwd); 693 if (rc == -1) 694 warn("User '%s' not found in NIS passwd", pwd->pw_name); 695 else 696 warn("NIS passwd update"); 697 /* NOTE: NIS-only update errors are not fatal */ 698 } 699 } 700 } 701 702 /* 703 * Ok, user is created or changed - now edit group file 704 */ 705 706 if (mode == M_ADD || getarg(args, 'G') != NULL) 707 editgroups(pwd->pw_name, cnf->groups); 708 709 /* go get a current version of pwd */ 710 pwd = GETPWNAM(a_name->val); 711 if (pwd == NULL) { 712 /* This will fail when we rename, so special case that */ 713 if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) { 714 a_name->val = arg->val; /* update new name */ 715 pwd = GETPWNAM(a_name->val); /* refetch renamed rec */ 716 } 717 } 718 if (pwd == NULL) /* can't go on without this */ 719 errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val); 720 721 grp = GETGRGID(pwd->pw_gid); 722 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s", 723 pwd->pw_name, (long) pwd->pw_uid, 724 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1), 725 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell); 726 727 /* 728 * If adding, let's touch and chown the user's mail file. This is not 729 * strictly necessary under BSD with a 0755 maildir but it also 730 * doesn't hurt anything to create the empty mailfile 731 */ 732 if (mode == M_ADD) { 733 FILE *fp; 734 735 if (!PWALTDIR()) { 736 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 737 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents & 738 * mtime */ 739 chown(line, pwd->pw_uid, pwd->pw_gid); 740 741 /* 742 * Send mail to the new user as well, if we are asked to 743 */ 744 if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) { 745 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w"); 746 747 if (pfp == NULL) 748 warn("sendmail"); 749 else { 750 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name); 751 while (fgets(line, sizeof(line), fp) != NULL) { 752 /* Do substitutions? */ 753 fputs(line, pfp); 754 } 755 pclose(pfp); 756 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent", 757 pwd->pw_name, (long) pwd->pw_uid); 758 } 759 fclose(fp); 760 } 761 } 762 } 763 764 /* 765 * Finally, let's create and populate the user's home directory. Note 766 * that this also `works' for editing users if -m is used, but 767 * existing files will *not* be overwritten. 768 */ 769 if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) { 770 copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid); 771 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made", 772 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir); 773 } 774 775 return EXIT_SUCCESS; 776} 777 778 779static uid_t 780pw_uidpolicy(struct userconf * cnf, struct cargs * args) 781{ 782 struct passwd *pwd; 783 uid_t uid = (uid_t) - 1; 784 struct carg *a_uid = getarg(args, 'u'); 785 786 /* 787 * Check the given uid, if any 788 */ 789 if (a_uid != NULL) { 790 uid = (uid_t) atol(a_uid->val); 791 792 if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL) 793 errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid); 794 } else { 795 struct bitmap bm; 796 797 /* 798 * We need to allocate the next available uid under one of 799 * two policies a) Grab the first unused uid b) Grab the 800 * highest possible unused uid 801 */ 802 if (cnf->min_uid >= cnf->max_uid) { /* Sanity 803 * claus^H^H^H^Hheck */ 804 cnf->min_uid = 1000; 805 cnf->max_uid = 32000; 806 } 807 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1); 808 809 /* 810 * Now, let's fill the bitmap from the password file 811 */ 812 SETPWENT(); 813 while ((pwd = GETPWENT()) != NULL) 814 if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid) 815 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid); 816 ENDPWENT(); 817 818 /* 819 * Then apply the policy, with fallback to reuse if necessary 820 */ 821 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid) 822 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid); 823 824 /* 825 * Another sanity check 826 */ 827 if (uid < cnf->min_uid || uid > cnf->max_uid) 828 errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used"); 829 bm_dealloc(&bm); 830 } 831 return uid; 832} 833 834 835static uid_t 836pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer) 837{ 838 struct group *grp; 839 gid_t gid = (uid_t) - 1; 840 struct carg *a_gid = getarg(args, 'g'); 841 842 /* 843 * If no arg given, see if default can help out 844 */ 845 if (a_gid == NULL && cnf->default_group && *cnf->default_group) 846 a_gid = addarg(args, 'g', cnf->default_group); 847 848 /* 849 * Check the given gid, if any 850 */ 851 SETGRENT(); 852 if (a_gid != NULL) { 853 if ((grp = GETGRNAM(a_gid->val)) == NULL) { 854 gid = (gid_t) atol(a_gid->val); 855 if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL) 856 errx(EX_NOUSER, "group `%s' is not defined", a_gid->val); 857 } 858 gid = grp->gr_gid; 859 } else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) { 860 gid = grp->gr_gid; /* Already created? Use it anyway... */ 861 } else { 862 struct cargs grpargs; 863 char tmp[32]; 864 865 LIST_INIT(&grpargs); 866 addarg(&grpargs, 'n', nam); 867 868 /* 869 * We need to auto-create a group with the user's name. We 870 * can send all the appropriate output to our sister routine 871 * bit first see if we can create a group with gid==uid so we 872 * can keep the user and group ids in sync. We purposely do 873 * NOT check the gid range if we can force the sync. If the 874 * user's name dups an existing group, then the group add 875 * function will happily handle that case for us and exit. 876 */ 877 if (GETGRGID(prefer) == NULL) { 878 sprintf(tmp, "%lu", (unsigned long) prefer); 879 addarg(&grpargs, 'g', tmp); 880 } 881 if (getarg(args, 'N')) 882 { 883 addarg(&grpargs, 'N', NULL); 884 addarg(&grpargs, 'q', NULL); 885 gid = pw_group(cnf, M_NEXT, &grpargs); 886 } 887 else 888 { 889 pw_group(cnf, M_ADD, &grpargs); 890 if ((grp = GETGRNAM(nam)) != NULL) 891 gid = grp->gr_gid; 892 } 893 a_gid = LIST_FIRST(&grpargs); 894 while (a_gid != NULL) { 895 struct carg *t = LIST_NEXT(a_gid, list); 896 LIST_REMOVE(a_gid, list); 897 a_gid = t; 898 } 899 } 900 ENDGRENT(); 901 return gid; 902} 903 904 905static time_t 906pw_pwdpolicy(struct userconf * cnf, struct cargs * args) 907{ 908 time_t result = 0; 909 time_t now = time(NULL); 910 struct carg *arg = getarg(args, 'p'); 911 912 if (arg != NULL) { 913 if ((result = parse_date(now, arg->val)) == now) 914 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 915 } else if (cnf->password_days > 0) 916 result = now + ((long) cnf->password_days * 86400L); 917 return result; 918} 919 920 921static time_t 922pw_exppolicy(struct userconf * cnf, struct cargs * args) 923{ 924 time_t result = 0; 925 time_t now = time(NULL); 926 struct carg *arg = getarg(args, 'e'); 927 928 if (arg != NULL) { 929 if ((result = parse_date(now, arg->val)) == now) 930 errx(EX_DATAERR, "invalid date/time `%s'", arg->val); 931 } else if (cnf->expire_days > 0) 932 result = now + ((long) cnf->expire_days * 86400L); 933 return result; 934} 935 936 937static char * 938pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user) 939{ 940 struct carg *arg = getarg(args, 'd'); 941 942 if (arg) 943 return arg->val; 944 else { 945 static char home[128]; 946 947 if (cnf->home == NULL || *cnf->home == '\0') 948 errx(EX_CONFIG, "no base home directory set"); 949 sprintf(home, "%s/%s", cnf->home, user); 950 return home; 951 } 952} 953 954static char * 955shell_path(char const * path, char *shells[], char *sh) 956{ 957 if (sh != NULL && (*sh == '/' || *sh == '\0')) 958 return sh; /* specified full path or forced none */ 959 else { 960 char *p; 961 char paths[_UC_MAXLINE]; 962 963 /* 964 * We need to search paths 965 */ 966 strncpy(paths, path, sizeof paths); 967 paths[sizeof paths - 1] = '\0'; 968 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) { 969 int i; 970 static char shellpath[256]; 971 972 if (sh != NULL) { 973 sprintf(shellpath, "%s/%s", p, sh); 974 if (access(shellpath, X_OK) == 0) 975 return shellpath; 976 } else 977 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) { 978 sprintf(shellpath, "%s/%s", p, shells[i]); 979 if (access(shellpath, X_OK) == 0) 980 return shellpath; 981 } 982 } 983 if (sh == NULL) 984 errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh); 985 errx(EX_CONFIG, "no default shell available or defined"); 986 return NULL; 987 } 988} 989 990 991static char * 992pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell) 993{ 994 char *sh = newshell; 995 struct carg *arg = getarg(args, 's'); 996 997 if (newshell == NULL && arg != NULL) 998 sh = arg->val; 999 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default); 1000} 1001 1002static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."; 1003 1004char * 1005pw_pwcrypt(char *password) 1006{ 1007 int i; 1008 char salt[12]; 1009 1010 static char buf[256]; 1011 1012 /* 1013 * Calculate a salt value 1014 */ 1015 for (i = 0; i < 8; i++) 1016 salt[i] = chars[arc4random() % 63]; 1017 salt[i] = '\0'; 1018 1019 return strcpy(buf, crypt(password, salt)); 1020} 1021 1022#if defined(USE_MD5RAND) 1023u_char * 1024pw_getrand(u_char *buf, int len) /* cryptographically secure rng */ 1025{ 1026 int i; 1027 for (i=0;i<len;i+=16) { 1028 u_char ubuf[16]; 1029 1030 MD5_CTX md5_ctx; 1031 struct timeval tv, tvo; 1032 struct rusage ru; 1033 int n=0; 1034 int t; 1035 1036 MD5Init (&md5_ctx); 1037 t=getpid(); 1038 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 1039 t=getppid(); 1040 MD5Update (&md5_ctx, (u_char*)&t, sizeof t); 1041 gettimeofday (&tvo, NULL); 1042 do { 1043 getrusage (RUSAGE_SELF, &ru); 1044 MD5Update (&md5_ctx, (u_char*)&ru, sizeof ru); 1045 gettimeofday (&tv, NULL); 1046 MD5Update (&md5_ctx, (u_char*)&tv, sizeof tv); 1047 } while (n++<20 || tv.tv_usec-tvo.tv_usec<100*1000); 1048 MD5Final (ubuf, &md5_ctx); 1049 memcpy(buf+i, ubuf, MIN(16, len-n)); 1050 } 1051 return buf; 1052} 1053 1054#else /* Portable version */ 1055 1056static u_char * 1057pw_getrand(u_char *buf, int len) 1058{ 1059 int i; 1060 1061 for (i = 0; i < len; i++) { 1062 unsigned long val = random(); 1063 /* Use all bits in the random value */ 1064 buf[i]=(u_char)((val >> 24) ^ (val >> 16) ^ (val >> 8) ^ val); 1065 } 1066 return buf; 1067} 1068 1069#endif 1070 1071static char * 1072pw_password(struct userconf * cnf, struct cargs * args, char const * user) 1073{ 1074 int i, l; 1075 char pwbuf[32]; 1076 u_char rndbuf[sizeof pwbuf]; 1077 1078 switch (cnf->default_password) { 1079 case -1: /* Random password */ 1080 l = (arc4random() % 8 + 8); /* 8 - 16 chars */ 1081 pw_getrand(rndbuf, l); 1082 for (i = 0; i < l; i++) 1083 pwbuf[i] = chars[rndbuf[i] % (sizeof(chars)-1)]; 1084 pwbuf[i] = '\0'; 1085 1086 /* 1087 * We give this information back to the user 1088 */ 1089 if (getarg(args, 'h') == NULL && getarg(args, 'N') == NULL) { 1090 if (isatty(STDOUT_FILENO)) 1091 printf("Password for '%s' is: ", user); 1092 printf("%s\n", pwbuf); 1093 fflush(stdout); 1094 } 1095 break; 1096 1097 case -2: /* No password at all! */ 1098 return ""; 1099 1100 case 0: /* No login - default */ 1101 default: 1102 return "*"; 1103 1104 case 1: /* user's name */ 1105 strncpy(pwbuf, user, sizeof pwbuf); 1106 pwbuf[sizeof pwbuf - 1] = '\0'; 1107 break; 1108 } 1109 return pw_pwcrypt(pwbuf); 1110} 1111 1112 1113static int 1114print_user(struct passwd * pwd, int pretty, int v7) 1115{ 1116 if (!pretty) { 1117 char buf[_UC_MAXLINE]; 1118 1119 fmtpwentry(buf, pwd, v7 ? PWF_PASSWD : PWF_STANDARD); 1120 fputs(buf, stdout); 1121 } else { 1122 int j; 1123 char *p; 1124 struct group *grp = GETGRGID(pwd->pw_gid); 1125 char uname[60] = "User &", office[60] = "[None]", 1126 wphone[60] = "[None]", hphone[60] = "[None]"; 1127 char acexpire[32] = "[None]", pwexpire[32] = "[None]"; 1128 struct tm * tptr; 1129 1130 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) { 1131 strncpy(uname, p, sizeof uname); 1132 uname[sizeof uname - 1] = '\0'; 1133 if ((p = strtok(NULL, ",")) != NULL) { 1134 strncpy(office, p, sizeof office); 1135 office[sizeof office - 1] = '\0'; 1136 if ((p = strtok(NULL, ",")) != NULL) { 1137 strncpy(wphone, p, sizeof wphone); 1138 wphone[sizeof wphone - 1] = '\0'; 1139 if ((p = strtok(NULL, "")) != NULL) { 1140 strncpy(hphone, p, sizeof hphone); 1141 hphone[sizeof hphone - 1] = '\0'; 1142 } 1143 } 1144 } 1145 } 1146 /* 1147 * Handle '&' in gecos field 1148 */ 1149 if ((p = strchr(uname, '&')) != NULL) { 1150 int l = strlen(pwd->pw_name); 1151 int m = strlen(p); 1152 1153 memmove(p + l, p + 1, m); 1154 memmove(p, pwd->pw_name, l); 1155 *p = (char) toupper((unsigned char)*p); 1156 } 1157 if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL) 1158 strftime(acexpire, sizeof acexpire, "%a %Ef %Y %X", tptr); 1159 if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL) 1160 strftime(pwexpire, sizeof pwexpire, "%a %Ef %Y %X", tptr); 1161 printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n" 1162 " Full Name: %s\n" 1163 " Home: %-26.26s Class: %s\n" 1164 " Shell: %-26.26s Office: %s\n" 1165 "Work Phone: %-26.26s Home Phone: %s\n" 1166 "Acc Expire: %-26.26s Pwd Expire: %s\n", 1167 pwd->pw_name, (long) pwd->pw_uid, 1168 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid, 1169 uname, pwd->pw_dir, pwd->pw_class, 1170 pwd->pw_shell, office, wphone, hphone, 1171 acexpire, pwexpire); 1172 SETGRENT(); 1173 j = 0; 1174 while ((grp=GETGRENT()) != NULL) 1175 { 1176 int i = 0; 1177 while (grp->gr_mem[i] != NULL) 1178 { 1179 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0) 1180 { 1181 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name); 1182 break; 1183 } 1184 ++i; 1185 } 1186 } 1187 ENDGRENT(); 1188 printf("%s", j ? "\n" : ""); 1189 } 1190 return EXIT_SUCCESS; 1191} 1192 1193char * 1194pw_checkname(u_char *name, int gecos) 1195{ 1196 int l = 0; 1197 char const *notch = gecos ? ":!@" : " ,\t:+&#%$^()!@~*?<>=|\\/\""; 1198 1199 while (name[l]) { 1200 if (strchr(notch, name[l]) != NULL || name[l] < ' ' || name[l] == 127 || 1201 (!gecos && l==0 && name[l] == '-') || /* leading '-' */ 1202 (!gecos && name[l] & 0x80)) /* 8-bit */ 1203 errx(EX_DATAERR, (name[l] >= ' ' && name[l] < 127) 1204 ? "invalid character `%c' in field" 1205 : "invalid character 0x%02x in field", 1206 name[l]); 1207 ++l; 1208 } 1209 if (!gecos && l > LOGNAMESIZE) 1210 errx(EX_DATAERR, "name too long `%s'", name); 1211 return (char *)name; 1212} 1213 1214 1215static void 1216rmat(uid_t uid) 1217{ 1218 DIR *d = opendir("/var/at/jobs"); 1219 1220 if (d != NULL) { 1221 struct dirent *e; 1222 1223 while ((e = readdir(d)) != NULL) { 1224 struct stat st; 1225 1226 if (strncmp(e->d_name, ".lock", 5) != 0 && 1227 stat(e->d_name, &st) == 0 && 1228 !S_ISDIR(st.st_mode) && 1229 st.st_uid == uid) { 1230 char tmp[MAXPATHLEN]; 1231 1232 sprintf(tmp, "/usr/bin/atrm %s", e->d_name); 1233 system(tmp); 1234 } 1235 } 1236 closedir(d); 1237 } 1238} 1239 1240static void 1241rmskey(char const * name) 1242{ 1243 static const char etcskey[] = "/etc/skeykeys"; 1244 FILE *fp = fopen(etcskey, "r+"); 1245 1246 if (fp != NULL) { 1247 char tmp[1024]; 1248 off_t atofs = 0; 1249 int length = strlen(name); 1250 1251 while (fgets(tmp, sizeof tmp, fp) != NULL) { 1252 if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') { 1253 if (fseek(fp, atofs, SEEK_SET) == 0) { 1254 fwrite("#", 1, 1, fp); /* Comment username out */ 1255 } 1256 break; 1257 } 1258 atofs = ftell(fp); 1259 } 1260 /* 1261 * If we got an error of any sort, don't update! 1262 */ 1263 fclose(fp); 1264 } 1265} 1266 1267