17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5004388ebScasper * Common Development and Distribution License (the "License"). 6004388ebScasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*5477a4d9Sgww * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <sys/types.h> 277c478bd9Sstevel@tonic-gate #include <fcntl.h> 287c478bd9Sstevel@tonic-gate #include <errno.h> 297c478bd9Sstevel@tonic-gate #include <stdlib.h> 307c478bd9Sstevel@tonic-gate #include <sys/stat.h> 317c478bd9Sstevel@tonic-gate #include <pwd.h> 327c478bd9Sstevel@tonic-gate #include <shadow.h> 337c478bd9Sstevel@tonic-gate #include <string.h> 347c478bd9Sstevel@tonic-gate #include <strings.h> 357c478bd9Sstevel@tonic-gate #include <stdlib.h> 367c478bd9Sstevel@tonic-gate #include <unistd.h> 377c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h> 387c478bd9Sstevel@tonic-gate #include <macros.h> 397c478bd9Sstevel@tonic-gate #include <syslog.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include <limits.h> /* LOGNAME_MAX -- max Solaris user name */ 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include "passwdutil.h" 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate int files_lock(void); 467c478bd9Sstevel@tonic-gate int files_unlock(void); 477c478bd9Sstevel@tonic-gate int files_checkhistory(char *user, char *passwd, pwu_repository_t *rep); 487c478bd9Sstevel@tonic-gate int files_getattr(char *name, attrlist *item, pwu_repository_t *rep); 497c478bd9Sstevel@tonic-gate int files_getpwnam(char *name, attrlist *items, pwu_repository_t *rep, 507c478bd9Sstevel@tonic-gate void **buf); 517c478bd9Sstevel@tonic-gate int files_update(attrlist *items, pwu_repository_t *rep, void *buf); 527c478bd9Sstevel@tonic-gate int files_putpwnam(char *name, char *oldpw, char *dummy, 537c478bd9Sstevel@tonic-gate pwu_repository_t *rep, void *buf); 547c478bd9Sstevel@tonic-gate int files_user_to_authenticate(char *name, pwu_repository_t *rep, 557c478bd9Sstevel@tonic-gate char **auth_user, int *privileged); 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate static int files_update_history(char *name, struct spwd *spwd); 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate /* 607c478bd9Sstevel@tonic-gate * files function pointer table, used by passwdutil_init to initialize 617c478bd9Sstevel@tonic-gate * the global Repository-OPerations table "rops" 627c478bd9Sstevel@tonic-gate */ 637c478bd9Sstevel@tonic-gate struct repops files_repops = { 647c478bd9Sstevel@tonic-gate files_checkhistory, 657c478bd9Sstevel@tonic-gate files_getattr, 667c478bd9Sstevel@tonic-gate files_getpwnam, 677c478bd9Sstevel@tonic-gate files_update, 687c478bd9Sstevel@tonic-gate files_putpwnam, 697c478bd9Sstevel@tonic-gate files_user_to_authenticate, 707c478bd9Sstevel@tonic-gate files_lock, 717c478bd9Sstevel@tonic-gate files_unlock 727c478bd9Sstevel@tonic-gate }; 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate /* 757c478bd9Sstevel@tonic-gate * this structure defines the buffer used to keep state between 767c478bd9Sstevel@tonic-gate * get/update/put calls 777c478bd9Sstevel@tonic-gate */ 787c478bd9Sstevel@tonic-gate struct pwbuf { 797c478bd9Sstevel@tonic-gate int update_history; 807c478bd9Sstevel@tonic-gate struct passwd *pwd; 817c478bd9Sstevel@tonic-gate char *pwd_scratch; 827c478bd9Sstevel@tonic-gate struct spwd *spwd; 837c478bd9Sstevel@tonic-gate char *spwd_scratch; 847c478bd9Sstevel@tonic-gate char *new_sp_pwdp; 857c478bd9Sstevel@tonic-gate }; 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate /* 887c478bd9Sstevel@tonic-gate * We should use sysconf, but there is no sysconf name for SHADOW 897c478bd9Sstevel@tonic-gate * so we use these from nss_dbdefs 907c478bd9Sstevel@tonic-gate */ 917c478bd9Sstevel@tonic-gate #define PWD_SCRATCH_SIZE NSS_LINELEN_PASSWD 927c478bd9Sstevel@tonic-gate #define SPW_SCRATCH_SIZE NSS_LINELEN_SHADOW 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate /* 957c478bd9Sstevel@tonic-gate * lock functions for files repository 967c478bd9Sstevel@tonic-gate */ 977c478bd9Sstevel@tonic-gate int 987c478bd9Sstevel@tonic-gate files_lock(void) 997c478bd9Sstevel@tonic-gate { 1007c478bd9Sstevel@tonic-gate int res; 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate if (lckpwdf()) { 1037c478bd9Sstevel@tonic-gate switch (errno) { 1047c478bd9Sstevel@tonic-gate case EINTR: 1057c478bd9Sstevel@tonic-gate res = PWU_BUSY; 1067c478bd9Sstevel@tonic-gate break; 1077c478bd9Sstevel@tonic-gate case EACCES: 1087c478bd9Sstevel@tonic-gate res = PWU_DENIED; 1097c478bd9Sstevel@tonic-gate break; 1107c478bd9Sstevel@tonic-gate case 0: 1117c478bd9Sstevel@tonic-gate res = PWU_SUCCESS; 1127c478bd9Sstevel@tonic-gate break; 1137c478bd9Sstevel@tonic-gate } 1147c478bd9Sstevel@tonic-gate } else 1157c478bd9Sstevel@tonic-gate res = PWU_SUCCESS; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate return (res); 1187c478bd9Sstevel@tonic-gate } 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate int 1217c478bd9Sstevel@tonic-gate files_unlock(void) 1227c478bd9Sstevel@tonic-gate { 1237c478bd9Sstevel@tonic-gate if (ulckpwdf()) 1247c478bd9Sstevel@tonic-gate return (PWU_SYSTEM_ERROR); 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate return (PWU_SUCCESS); 1277c478bd9Sstevel@tonic-gate } 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate /* 1307c478bd9Sstevel@tonic-gate * files_privileged 1317c478bd9Sstevel@tonic-gate * 1327c478bd9Sstevel@tonic-gate * Are we a privileged user with regard to the files repository? 1337c478bd9Sstevel@tonic-gate */ 1347c478bd9Sstevel@tonic-gate int 1357c478bd9Sstevel@tonic-gate files_privileged(void) 1367c478bd9Sstevel@tonic-gate { 1377c478bd9Sstevel@tonic-gate return (getuid() == 0); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* 1417c478bd9Sstevel@tonic-gate * 1427c478bd9Sstevel@tonic-gate * private_getpwnam_r() 1437c478bd9Sstevel@tonic-gate * 1447c478bd9Sstevel@tonic-gate * A private implementation of getpwnam_r which does *not* fall back to 1457c478bd9Sstevel@tonic-gate * other services possibly defined in nsswitch.conf 1467c478bd9Sstevel@tonic-gate * 1477c478bd9Sstevel@tonic-gate * behaves like getpwnam_r(). 1487c478bd9Sstevel@tonic-gate */ 1497c478bd9Sstevel@tonic-gate struct passwd * 1507c478bd9Sstevel@tonic-gate private_getpwnam_r(const char *name, struct passwd *result, char *buffer, 1517c478bd9Sstevel@tonic-gate int buflen) 1527c478bd9Sstevel@tonic-gate { 1537c478bd9Sstevel@tonic-gate FILE *fp; 1547c478bd9Sstevel@tonic-gate int found; 1557c478bd9Sstevel@tonic-gate 156004388ebScasper if ((fp = fopen(PASSWD, "rF")) == NULL) 1577c478bd9Sstevel@tonic-gate return (NULL); 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate found = 0; 1607c478bd9Sstevel@tonic-gate while (!found && fgetpwent_r(fp, result, buffer, buflen) != NULL) { 1617c478bd9Sstevel@tonic-gate if (strcmp(name, result->pw_name) == 0) 1627c478bd9Sstevel@tonic-gate found = 1; 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate (void) fclose(fp); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate if (!found) { 1687c478bd9Sstevel@tonic-gate (void) memset(buffer, 0, buflen); 1697c478bd9Sstevel@tonic-gate (void) memset(result, 0, sizeof (*result)); 1707c478bd9Sstevel@tonic-gate return (NULL); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate return (result); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate /* 1777c478bd9Sstevel@tonic-gate * private_getspnam_r() 1787c478bd9Sstevel@tonic-gate * 1797c478bd9Sstevel@tonic-gate * A private implementation of getspnam_r which does *not* fall back to 1807c478bd9Sstevel@tonic-gate * other services possibly defined in nsswitch.conf. 1817c478bd9Sstevel@tonic-gate * 1827c478bd9Sstevel@tonic-gate * Behaves like getspnam_r(). Since we use fgetspent_t(), all numeric 1837c478bd9Sstevel@tonic-gate * fields that are undefined in /etc/shadow will be set to -1. 1847c478bd9Sstevel@tonic-gate * 1857c478bd9Sstevel@tonic-gate */ 1867c478bd9Sstevel@tonic-gate struct spwd * 1877c478bd9Sstevel@tonic-gate private_getspnam_r(const char *name, struct spwd *result, char *buffer, 1887c478bd9Sstevel@tonic-gate int buflen) 1897c478bd9Sstevel@tonic-gate { 1907c478bd9Sstevel@tonic-gate FILE *fp; 1917c478bd9Sstevel@tonic-gate int found; 1927c478bd9Sstevel@tonic-gate 193004388ebScasper fp = fopen(SHADOW, "rF"); 1947c478bd9Sstevel@tonic-gate if (fp == NULL) 1957c478bd9Sstevel@tonic-gate return (NULL); 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate found = 0; 1987c478bd9Sstevel@tonic-gate while (!found && fgetspent_r(fp, result, buffer, buflen) != NULL) { 1997c478bd9Sstevel@tonic-gate if (strcmp(name, result->sp_namp) == 0) 2007c478bd9Sstevel@tonic-gate found = 1; 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate (void) fclose(fp); 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate if (!found) { 2067c478bd9Sstevel@tonic-gate (void) memset(buffer, 0, buflen); 2077c478bd9Sstevel@tonic-gate (void) memset(result, 0, sizeof (*result)); 2087c478bd9Sstevel@tonic-gate return (NULL); 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate return (result); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * files_getpwnam(name, items, rep, buf) 2157c478bd9Sstevel@tonic-gate * 2167c478bd9Sstevel@tonic-gate */ 2177c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2187c478bd9Sstevel@tonic-gate int 2197c478bd9Sstevel@tonic-gate files_getpwnam(char *name, attrlist *items, pwu_repository_t *rep, void **buf) 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate attrlist *p; 2227c478bd9Sstevel@tonic-gate struct pwbuf *pwbuf; 2237c478bd9Sstevel@tonic-gate int err = PWU_SUCCESS; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate *buf = calloc(1, sizeof (struct pwbuf)); 2267c478bd9Sstevel@tonic-gate pwbuf = (struct pwbuf *)*buf; 227997ec710Sgww if (pwbuf == NULL) 228997ec710Sgww return (PWU_NOMEM); 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate /* 2317c478bd9Sstevel@tonic-gate * determine which password structure (/etc/passwd or /etc/shadow) 2327c478bd9Sstevel@tonic-gate * we need for the items we need to update 2337c478bd9Sstevel@tonic-gate */ 2347c478bd9Sstevel@tonic-gate for (p = items; p != NULL; p = p->next) { 2357c478bd9Sstevel@tonic-gate switch (p->type) { 2367c478bd9Sstevel@tonic-gate case ATTR_NAME: 2377c478bd9Sstevel@tonic-gate case ATTR_UID: 2387c478bd9Sstevel@tonic-gate case ATTR_GID: 2397c478bd9Sstevel@tonic-gate case ATTR_AGE: 2407c478bd9Sstevel@tonic-gate case ATTR_COMMENT: 2417c478bd9Sstevel@tonic-gate case ATTR_GECOS: 2427c478bd9Sstevel@tonic-gate case ATTR_HOMEDIR: 2437c478bd9Sstevel@tonic-gate case ATTR_SHELL: 2447c478bd9Sstevel@tonic-gate if (pwbuf->pwd == NULL) { 2457c478bd9Sstevel@tonic-gate pwbuf->pwd = malloc(sizeof (struct passwd)); 2467c478bd9Sstevel@tonic-gate if (pwbuf->pwd == NULL) { 2477c478bd9Sstevel@tonic-gate err = PWU_NOMEM; 2487c478bd9Sstevel@tonic-gate goto error; 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate break; 2527c478bd9Sstevel@tonic-gate case ATTR_PASSWD: 2537c478bd9Sstevel@tonic-gate case ATTR_PASSWD_SERVER_POLICY: 2547c478bd9Sstevel@tonic-gate case ATTR_LSTCHG: 2557c478bd9Sstevel@tonic-gate case ATTR_MIN: 2567c478bd9Sstevel@tonic-gate case ATTR_MAX: 2577c478bd9Sstevel@tonic-gate case ATTR_WARN: 2587c478bd9Sstevel@tonic-gate case ATTR_INACT: 2597c478bd9Sstevel@tonic-gate case ATTR_EXPIRE: 2607c478bd9Sstevel@tonic-gate case ATTR_FLAG: 2617c478bd9Sstevel@tonic-gate case ATTR_LOCK_ACCOUNT: 2627c478bd9Sstevel@tonic-gate case ATTR_EXPIRE_PASSWORD: 2637c478bd9Sstevel@tonic-gate case ATTR_FAILED_LOGINS: 2647c478bd9Sstevel@tonic-gate case ATTR_INCR_FAILED_LOGINS: 2657c478bd9Sstevel@tonic-gate case ATTR_RST_FAILED_LOGINS: 2667c478bd9Sstevel@tonic-gate case ATTR_NOLOGIN_ACCOUNT: 2677c478bd9Sstevel@tonic-gate case ATTR_UNLOCK_ACCOUNT: 2687c478bd9Sstevel@tonic-gate if (pwbuf->spwd == NULL) { 2697c478bd9Sstevel@tonic-gate pwbuf->spwd = malloc(sizeof (struct spwd)); 2707c478bd9Sstevel@tonic-gate if (pwbuf->spwd == NULL) { 2717c478bd9Sstevel@tonic-gate err = PWU_NOMEM; 2727c478bd9Sstevel@tonic-gate goto error; 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate break; 2767c478bd9Sstevel@tonic-gate default: 2777c478bd9Sstevel@tonic-gate /* 2787c478bd9Sstevel@tonic-gate * Some other repository might have different values 2797c478bd9Sstevel@tonic-gate * so we ignore those. 2807c478bd9Sstevel@tonic-gate */ 2817c478bd9Sstevel@tonic-gate break; 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate if (pwbuf->pwd) { 2867c478bd9Sstevel@tonic-gate if ((pwbuf->pwd_scratch = malloc(PWD_SCRATCH_SIZE)) == NULL) { 2877c478bd9Sstevel@tonic-gate err = PWU_NOMEM; 2887c478bd9Sstevel@tonic-gate goto error; 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate if (private_getpwnam_r(name, pwbuf->pwd, pwbuf->pwd_scratch, 2917c478bd9Sstevel@tonic-gate PWD_SCRATCH_SIZE) == NULL) { 2927c478bd9Sstevel@tonic-gate err = PWU_NOT_FOUND; 2937c478bd9Sstevel@tonic-gate goto error; 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate if (pwbuf->spwd) { 2987c478bd9Sstevel@tonic-gate if ((pwbuf->spwd_scratch = malloc(SPW_SCRATCH_SIZE)) == NULL) { 2997c478bd9Sstevel@tonic-gate err = PWU_NOMEM; 3007c478bd9Sstevel@tonic-gate goto error; 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate if (private_getspnam_r(name, pwbuf->spwd, pwbuf->spwd_scratch, 3037c478bd9Sstevel@tonic-gate SPW_SCRATCH_SIZE) == NULL) { 3047c478bd9Sstevel@tonic-gate err = PWU_NOT_FOUND; 3057c478bd9Sstevel@tonic-gate goto error; 3067c478bd9Sstevel@tonic-gate } 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate return (PWU_SUCCESS); 3107c478bd9Sstevel@tonic-gate error: 3117c478bd9Sstevel@tonic-gate if (pwbuf->pwd) free(pwbuf->pwd); 3127c478bd9Sstevel@tonic-gate if (pwbuf->pwd_scratch) free(pwbuf->pwd_scratch); 3137c478bd9Sstevel@tonic-gate if (pwbuf->spwd) free(pwbuf->spwd); 3147c478bd9Sstevel@tonic-gate if (pwbuf->spwd_scratch) free(pwbuf->spwd_scratch); 3157c478bd9Sstevel@tonic-gate free(pwbuf); 3167c478bd9Sstevel@tonic-gate *buf = NULL; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate return (err); 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate /* 3227c478bd9Sstevel@tonic-gate * int files_user_to_authenticate(name, rep, auth_user, privileged) 3237c478bd9Sstevel@tonic-gate * Determine which user needs to be authenticated. For files, the 3247c478bd9Sstevel@tonic-gate * possible return values are: 3257c478bd9Sstevel@tonic-gate * PWU_NOT_FOUND 3267c478bd9Sstevel@tonic-gate * PWU_SUCCESS and (auth_user == NULL || auth_user = user) 3277c478bd9Sstevel@tonic-gate * PWU_DENIED 3284a7ceb24Sjjj * PWU_NOMEM 3297c478bd9Sstevel@tonic-gate */ 3307c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3317c478bd9Sstevel@tonic-gate int 3327c478bd9Sstevel@tonic-gate files_user_to_authenticate(char *user, pwu_repository_t *rep, 3337c478bd9Sstevel@tonic-gate char **auth_user, int *privileged) 3347c478bd9Sstevel@tonic-gate { 3357c478bd9Sstevel@tonic-gate struct pwbuf *pwbuf; 3367c478bd9Sstevel@tonic-gate int res; 3377c478bd9Sstevel@tonic-gate attrlist attr_tmp[1] = { { ATTR_UID, NULL, NULL } }; 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate /* check to see if target user is present in files */ 3407c478bd9Sstevel@tonic-gate res = files_getpwnam(user, &attr_tmp[0], rep, (void **)&pwbuf); 3417c478bd9Sstevel@tonic-gate if (res != PWU_SUCCESS) 3427c478bd9Sstevel@tonic-gate return (res); 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate if (files_privileged()) { 3457c478bd9Sstevel@tonic-gate *auth_user = NULL; 3467c478bd9Sstevel@tonic-gate *privileged = 1; 3477c478bd9Sstevel@tonic-gate res = PWU_SUCCESS; 3487c478bd9Sstevel@tonic-gate } else { 3497c478bd9Sstevel@tonic-gate *privileged = 0; 3507c478bd9Sstevel@tonic-gate if (getuid() == pwbuf->pwd->pw_uid) { 3514a7ceb24Sjjj if ((*auth_user = strdup(user)) == NULL) { 3524a7ceb24Sjjj res = PWU_NOMEM; 3534a7ceb24Sjjj } else { 3547c478bd9Sstevel@tonic-gate res = PWU_SUCCESS; 3554a7ceb24Sjjj } 3567c478bd9Sstevel@tonic-gate } else { 3577c478bd9Sstevel@tonic-gate res = PWU_DENIED; 3587c478bd9Sstevel@tonic-gate } 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate if (pwbuf->pwd) free(pwbuf->pwd); 3627c478bd9Sstevel@tonic-gate if (pwbuf->pwd_scratch) free(pwbuf->pwd_scratch); 3637c478bd9Sstevel@tonic-gate if (pwbuf->spwd) free(pwbuf->spwd); 3647c478bd9Sstevel@tonic-gate if (pwbuf->spwd_scratch) free(pwbuf->spwd_scratch); 3657c478bd9Sstevel@tonic-gate free(pwbuf); 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate return (res); 3687c478bd9Sstevel@tonic-gate } 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate /* 3717c478bd9Sstevel@tonic-gate * Password history file format: 3727c478bd9Sstevel@tonic-gate * user:crypw1: ... crypwn: such that n <= MAXHISTORY 3737c478bd9Sstevel@tonic-gate */ 3747c478bd9Sstevel@tonic-gate #define HISTORY "/etc/security/passhistory" 3757c478bd9Sstevel@tonic-gate #define HISTEMP "/etc/security/pwhistemp" 3767c478bd9Sstevel@tonic-gate #define OHISTORY "/etc/security/opwhistory" 3777c478bd9Sstevel@tonic-gate #define HISTMODE S_IRUSR /* mode to create history file */ 3787c478bd9Sstevel@tonic-gate /* 3797c478bd9Sstevel@tonic-gate * XXX 3807c478bd9Sstevel@tonic-gate * 3*LOGNAME_MAX just in case there are long user names. 3817c478bd9Sstevel@tonic-gate * Traditionally Solaris LOGNAME_MAX (_POSIX_LOGIN_NAME_MAX) is 13, 3827c478bd9Sstevel@tonic-gate * but some sites often user more. 3837c478bd9Sstevel@tonic-gate * If LOGNAME_MAX ever becomes reasonable (128) and actually enforced, 3847c478bd9Sstevel@tonic-gate * fix up here. 3857c478bd9Sstevel@tonic-gate * XXX 3867c478bd9Sstevel@tonic-gate */ 3877c478bd9Sstevel@tonic-gate #define MAX_LOGNAME (3 * LOGNAME_MAX) 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate /* 3907c478bd9Sstevel@tonic-gate * files_checkhistory - check if a user's new password is in the user's 3917c478bd9Sstevel@tonic-gate * old password history. 3927c478bd9Sstevel@tonic-gate * 3937c478bd9Sstevel@tonic-gate * Entry 3947c478bd9Sstevel@tonic-gate * user = username. 3957c478bd9Sstevel@tonic-gate * passwd = new clear text password. 3967c478bd9Sstevel@tonic-gate * 3977c478bd9Sstevel@tonic-gate * Exit 3987c478bd9Sstevel@tonic-gate * PWU_SUCCESS, passwd found in user's old password history. 3997c478bd9Sstevel@tonic-gate * The caller should only be interested and fail if 4007c478bd9Sstevel@tonic-gate * PWU_SUCCESS is returned. 4017c478bd9Sstevel@tonic-gate * PWU_NOT_FOUND, passwd not in user's old password history. 4027c478bd9Sstevel@tonic-gate * PWU_errors, PWU_ errors from other routines. 4037c478bd9Sstevel@tonic-gate * 4047c478bd9Sstevel@tonic-gate */ 4057c478bd9Sstevel@tonic-gate int 4067c478bd9Sstevel@tonic-gate files_checkhistory(char *user, char *passwd, pwu_repository_t *rep) 4077c478bd9Sstevel@tonic-gate { 4087c478bd9Sstevel@tonic-gate attrlist attr; 4097c478bd9Sstevel@tonic-gate int res; 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate attr.type = ATTR_HISTORY; 4127c478bd9Sstevel@tonic-gate attr.data.val_s = NULL; 4137c478bd9Sstevel@tonic-gate attr.next = NULL; 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate debug("files_checkhistory(user=%s)", user); 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate /* 4187c478bd9Sstevel@tonic-gate * XXX 4197c478bd9Sstevel@tonic-gate * This depends on the underlying files_getattr implementation 4207c478bd9Sstevel@tonic-gate * treating user not found in backing store or no history as 4217c478bd9Sstevel@tonic-gate * an error. 4227c478bd9Sstevel@tonic-gate * XXX 4237c478bd9Sstevel@tonic-gate */ 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate if ((res = files_getattr(user, &attr, rep)) == PWU_SUCCESS) { 4267c478bd9Sstevel@tonic-gate char *s; 4277c478bd9Sstevel@tonic-gate char *crypt_passwd; 4287c478bd9Sstevel@tonic-gate int histsize; 4297c478bd9Sstevel@tonic-gate char *last = attr.data.val_s; 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate if ((histsize = def_getint("HISTORY=", DEFHISTORY)) == 0) { 4327c478bd9Sstevel@tonic-gate debug("files_checkhistory: no history requested"); 4337c478bd9Sstevel@tonic-gate res = PWU_NOT_FOUND; 4347c478bd9Sstevel@tonic-gate goto out; 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate debug("files_checkhistory: histsize = %d", histsize); 4387c478bd9Sstevel@tonic-gate if (histsize > MAXHISTORY) 4397c478bd9Sstevel@tonic-gate histsize = MAXHISTORY; 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate debug("line to test\n\t%s", last); 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate /* compare crypt_passwd to attr.data.val_s strings. */ 4447c478bd9Sstevel@tonic-gate res = PWU_NOT_FOUND; 4457c478bd9Sstevel@tonic-gate while ((histsize-- > 0) && 4467c478bd9Sstevel@tonic-gate (((s = strtok_r(NULL, ":", &last)) != NULL) && 4477c478bd9Sstevel@tonic-gate (*s != '\n'))) { 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate crypt_passwd = crypt(passwd, s); 4507c478bd9Sstevel@tonic-gate debug("files_checkhistory: user_pw=%s, history_pw=%s", 4517c478bd9Sstevel@tonic-gate crypt_passwd, s); 4527c478bd9Sstevel@tonic-gate if (strcmp(crypt_passwd, s) == 0) { 4537c478bd9Sstevel@tonic-gate res = PWU_SUCCESS; 4547c478bd9Sstevel@tonic-gate break; 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate } 4577c478bd9Sstevel@tonic-gate debug("files_checkhistory(%s, %s) = %d", user, crypt_passwd, 4587c478bd9Sstevel@tonic-gate res); 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate out: 4617c478bd9Sstevel@tonic-gate if (attr.data.val_s != NULL) 4627c478bd9Sstevel@tonic-gate free(attr.data.val_s); 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate return (res); 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate /* 4687c478bd9Sstevel@tonic-gate * files_getattr(name, items, rep) 4697c478bd9Sstevel@tonic-gate * 4707c478bd9Sstevel@tonic-gate * Get attributes specified in list 'items' 4717c478bd9Sstevel@tonic-gate */ 4727c478bd9Sstevel@tonic-gate int 4737c478bd9Sstevel@tonic-gate files_getattr(char *name, attrlist *items, pwu_repository_t *rep) 4747c478bd9Sstevel@tonic-gate { 4757c478bd9Sstevel@tonic-gate struct pwbuf *pwbuf; 4767c478bd9Sstevel@tonic-gate struct passwd *pw; 4777c478bd9Sstevel@tonic-gate struct spwd *spw; 4787c478bd9Sstevel@tonic-gate attrlist *w; 4797c478bd9Sstevel@tonic-gate int res; 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate res = files_getpwnam(name, items, rep, (void **)&pwbuf); 4827c478bd9Sstevel@tonic-gate if (res != PWU_SUCCESS) 4837c478bd9Sstevel@tonic-gate return (res); 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate pw = pwbuf->pwd; 4867c478bd9Sstevel@tonic-gate spw = pwbuf->spwd; 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate for (w = items; res == PWU_SUCCESS && w != NULL; w = w->next) { 4897c478bd9Sstevel@tonic-gate switch (w->type) { 4907c478bd9Sstevel@tonic-gate case ATTR_NAME: 4917c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_name)) == NULL) 4927c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 4937c478bd9Sstevel@tonic-gate break; 4947c478bd9Sstevel@tonic-gate case ATTR_COMMENT: 4957c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_comment)) == NULL) 4967c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 4977c478bd9Sstevel@tonic-gate break; 4987c478bd9Sstevel@tonic-gate case ATTR_GECOS: 4997c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_gecos)) == NULL) 5007c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 5017c478bd9Sstevel@tonic-gate break; 5027c478bd9Sstevel@tonic-gate case ATTR_HOMEDIR: 5037c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_dir)) == NULL) 5047c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 5057c478bd9Sstevel@tonic-gate break; 5067c478bd9Sstevel@tonic-gate case ATTR_SHELL: 5077c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_shell)) == NULL) 5087c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 5097c478bd9Sstevel@tonic-gate break; 5107c478bd9Sstevel@tonic-gate /* 5117c478bd9Sstevel@tonic-gate * Nothing special needs to be done for 5127c478bd9Sstevel@tonic-gate * server policy 5137c478bd9Sstevel@tonic-gate */ 5147c478bd9Sstevel@tonic-gate case ATTR_PASSWD: 5157c478bd9Sstevel@tonic-gate case ATTR_PASSWD_SERVER_POLICY: 5167c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(spw->sp_pwdp)) == NULL) 5177c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 5187c478bd9Sstevel@tonic-gate break; 5197c478bd9Sstevel@tonic-gate case ATTR_AGE: 5207c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_age)) == NULL) 5217c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 5227c478bd9Sstevel@tonic-gate break; 5237c478bd9Sstevel@tonic-gate case ATTR_REP_NAME: 5247c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup("files")) == NULL) 5257c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 5267c478bd9Sstevel@tonic-gate break; 5277c478bd9Sstevel@tonic-gate case ATTR_HISTORY: { 5287c478bd9Sstevel@tonic-gate FILE *history; 5297c478bd9Sstevel@tonic-gate char buf[MAX_LOGNAME + MAXHISTORY + 5307c478bd9Sstevel@tonic-gate (MAXHISTORY * CRYPT_MAXCIPHERTEXTLEN)+1]; 5317c478bd9Sstevel@tonic-gate char *s, *s1; 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate debug("files_getattr: Get password history for %s ", 5347c478bd9Sstevel@tonic-gate name); 5357c478bd9Sstevel@tonic-gate 536004388ebScasper if ((history = fopen(HISTORY, "rF")) == NULL) { 5377c478bd9Sstevel@tonic-gate debug("files_getattr: %s not found", HISTORY); 5387c478bd9Sstevel@tonic-gate res = PWU_OPEN_FAILED; 5397c478bd9Sstevel@tonic-gate goto getattr_exit; 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate res = PWU_NOT_FOUND; 5427c478bd9Sstevel@tonic-gate while ((s = fgets(buf, sizeof (buf), history)) != 5437c478bd9Sstevel@tonic-gate NULL) { 5447c478bd9Sstevel@tonic-gate s1 = strchr(s, ':'); 5457c478bd9Sstevel@tonic-gate if (s1 != NULL) { 5467c478bd9Sstevel@tonic-gate *s1 = '\0'; 5477c478bd9Sstevel@tonic-gate } else { 5487c478bd9Sstevel@tonic-gate res = PWU_NOT_FOUND; 5497c478bd9Sstevel@tonic-gate break; 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate #ifdef DEBUG 5527c478bd9Sstevel@tonic-gate debug("got history line for %s", s); 5537c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 5547c478bd9Sstevel@tonic-gate if (strcmp(s, name) == 0) { 5557c478bd9Sstevel@tonic-gate /* found user */ 5567c478bd9Sstevel@tonic-gate if ((items->data.val_s = 5577c478bd9Sstevel@tonic-gate strdup(s1+1)) == NULL) 5587c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 5597c478bd9Sstevel@tonic-gate else 5607c478bd9Sstevel@tonic-gate res = PWU_SUCCESS; 5617c478bd9Sstevel@tonic-gate break; 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate (void) fclose(history); 5657c478bd9Sstevel@tonic-gate break; 5667c478bd9Sstevel@tonic-gate } 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate /* integer values */ 5697c478bd9Sstevel@tonic-gate case ATTR_UID: 5707c478bd9Sstevel@tonic-gate w->data.val_i = pw->pw_uid; 5717c478bd9Sstevel@tonic-gate break; 5727c478bd9Sstevel@tonic-gate case ATTR_GID: 5737c478bd9Sstevel@tonic-gate w->data.val_i = pw->pw_gid; 5747c478bd9Sstevel@tonic-gate break; 5757c478bd9Sstevel@tonic-gate case ATTR_LSTCHG: 5767c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_lstchg; 5777c478bd9Sstevel@tonic-gate break; 5787c478bd9Sstevel@tonic-gate case ATTR_MIN: 5797c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_min; 5807c478bd9Sstevel@tonic-gate break; 5817c478bd9Sstevel@tonic-gate case ATTR_MAX: 5827c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_max; 5837c478bd9Sstevel@tonic-gate break; 5847c478bd9Sstevel@tonic-gate case ATTR_WARN: 5857c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_warn; 5867c478bd9Sstevel@tonic-gate break; 5877c478bd9Sstevel@tonic-gate case ATTR_INACT: 5887c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_inact; 5897c478bd9Sstevel@tonic-gate break; 5907c478bd9Sstevel@tonic-gate case ATTR_EXPIRE: 5917c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_expire; 5927c478bd9Sstevel@tonic-gate break; 5937c478bd9Sstevel@tonic-gate case ATTR_FLAG: 5947c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_flag; 5957c478bd9Sstevel@tonic-gate break; 5967c478bd9Sstevel@tonic-gate case ATTR_FAILED_LOGINS: 5977c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_flag & FAILCOUNT_MASK; 5987c478bd9Sstevel@tonic-gate break; 5997c478bd9Sstevel@tonic-gate default: 6007c478bd9Sstevel@tonic-gate break; 6017c478bd9Sstevel@tonic-gate } 6027c478bd9Sstevel@tonic-gate } 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate getattr_exit: 6057c478bd9Sstevel@tonic-gate if (pwbuf->pwd) free(pwbuf->pwd); 6067c478bd9Sstevel@tonic-gate if (pwbuf->pwd_scratch) free(pwbuf->pwd_scratch); 6077c478bd9Sstevel@tonic-gate if (pwbuf->spwd) free(pwbuf->spwd); 608a009b9b5Sps57422 if (pwbuf->spwd_scratch) free(pwbuf->spwd_scratch); 6097c478bd9Sstevel@tonic-gate free(pwbuf); 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate return (res); 6127c478bd9Sstevel@tonic-gate } 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate /* 6157c478bd9Sstevel@tonic-gate * max_present(list) 6167c478bd9Sstevel@tonic-gate * 6177c478bd9Sstevel@tonic-gate * see if attribute ATTR_MAX, with value != -1, is present in 6187c478bd9Sstevel@tonic-gate * attribute-list "list". 6197c478bd9Sstevel@tonic-gate * 6207c478bd9Sstevel@tonic-gate * returns 1 if present, 0 otherwise. 6217c478bd9Sstevel@tonic-gate */ 6227c478bd9Sstevel@tonic-gate static int 6237c478bd9Sstevel@tonic-gate max_present(attrlist *list) 6247c478bd9Sstevel@tonic-gate { 6257c478bd9Sstevel@tonic-gate while (list != NULL) 6267c478bd9Sstevel@tonic-gate if (list->type == ATTR_MAX && list->data.val_i != -1) 6277c478bd9Sstevel@tonic-gate return (1); 6287c478bd9Sstevel@tonic-gate else 6297c478bd9Sstevel@tonic-gate list = list->next; 6307c478bd9Sstevel@tonic-gate 6317c478bd9Sstevel@tonic-gate return (0); 6327c478bd9Sstevel@tonic-gate } 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate /* 6357c478bd9Sstevel@tonic-gate * files_update(items, rep, buf) 6367c478bd9Sstevel@tonic-gate * 6377c478bd9Sstevel@tonic-gate * update the information in buf with the attributes specified in 6387c478bd9Sstevel@tonic-gate * items. 6397c478bd9Sstevel@tonic-gate */ 6407c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 6417c478bd9Sstevel@tonic-gate int 6427c478bd9Sstevel@tonic-gate files_update(attrlist *items, pwu_repository_t *rep, void *buf) 6437c478bd9Sstevel@tonic-gate { 6447c478bd9Sstevel@tonic-gate struct pwbuf *pwbuf = (struct pwbuf *)buf; 6457c478bd9Sstevel@tonic-gate struct passwd *pw; 6467c478bd9Sstevel@tonic-gate struct spwd *spw; 6477c478bd9Sstevel@tonic-gate attrlist *p; 6487c478bd9Sstevel@tonic-gate int aging_needed = 0; 6497c478bd9Sstevel@tonic-gate int aging_set = 0; 6507c478bd9Sstevel@tonic-gate int disable_aging; 6517c478bd9Sstevel@tonic-gate char *pword; 6527c478bd9Sstevel@tonic-gate int len; 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate pw = pwbuf->pwd; 6557c478bd9Sstevel@tonic-gate spw = pwbuf->spwd; 6567c478bd9Sstevel@tonic-gate pwbuf->update_history = 0; 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate /* 6597c478bd9Sstevel@tonic-gate * if sp_max==0 : disable passwd aging after updating the password 6607c478bd9Sstevel@tonic-gate */ 6617c478bd9Sstevel@tonic-gate disable_aging = (spw != NULL && spw->sp_max == 0); 6627c478bd9Sstevel@tonic-gate 6637c478bd9Sstevel@tonic-gate for (p = items; p != NULL; p = p->next) { 6647c478bd9Sstevel@tonic-gate switch (p->type) { 6657c478bd9Sstevel@tonic-gate case ATTR_NAME: 6667c478bd9Sstevel@tonic-gate break; /* We are able to handle this, but... */ 6677c478bd9Sstevel@tonic-gate case ATTR_UID: 6687c478bd9Sstevel@tonic-gate pw->pw_uid = (uid_t)p->data.val_i; 6697c478bd9Sstevel@tonic-gate break; 6707c478bd9Sstevel@tonic-gate case ATTR_GID: 6717c478bd9Sstevel@tonic-gate pw->pw_gid = (gid_t)p->data.val_i; 6727c478bd9Sstevel@tonic-gate break; 6737c478bd9Sstevel@tonic-gate case ATTR_AGE: 6747c478bd9Sstevel@tonic-gate pw->pw_age = p->data.val_s; 6757c478bd9Sstevel@tonic-gate break; 6767c478bd9Sstevel@tonic-gate case ATTR_COMMENT: 6777c478bd9Sstevel@tonic-gate pw->pw_comment = p->data.val_s; 6787c478bd9Sstevel@tonic-gate break; 6797c478bd9Sstevel@tonic-gate case ATTR_GECOS: 6807c478bd9Sstevel@tonic-gate pw->pw_gecos = p->data.val_s; 6817c478bd9Sstevel@tonic-gate break; 6827c478bd9Sstevel@tonic-gate case ATTR_HOMEDIR: 6837c478bd9Sstevel@tonic-gate pw->pw_dir = p->data.val_s; 6847c478bd9Sstevel@tonic-gate break; 6857c478bd9Sstevel@tonic-gate case ATTR_SHELL: 6867c478bd9Sstevel@tonic-gate pw->pw_shell = p->data.val_s; 6877c478bd9Sstevel@tonic-gate break; 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate /* 6907c478bd9Sstevel@tonic-gate * Nothing special needs to be done for 6917c478bd9Sstevel@tonic-gate * server policy 6927c478bd9Sstevel@tonic-gate */ 6937c478bd9Sstevel@tonic-gate case ATTR_PASSWD: 6947c478bd9Sstevel@tonic-gate case ATTR_PASSWD_SERVER_POLICY: 6957c478bd9Sstevel@tonic-gate /* 6967c478bd9Sstevel@tonic-gate * There is a special case only for files: if the 6977c478bd9Sstevel@tonic-gate * password is to be deleted (-d to passwd), 6987c478bd9Sstevel@tonic-gate * p->data.val_s will be NULL. 6997c478bd9Sstevel@tonic-gate */ 7007c478bd9Sstevel@tonic-gate if (p->data.val_s == NULL) { 7017c478bd9Sstevel@tonic-gate spw->sp_pwdp = ""; 7027c478bd9Sstevel@tonic-gate } else { 7037c478bd9Sstevel@tonic-gate char *salt = NULL; 7047c478bd9Sstevel@tonic-gate char *hash = NULL; 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate salt = crypt_gensalt(spw->sp_pwdp, pw); 7077c478bd9Sstevel@tonic-gate 7087c478bd9Sstevel@tonic-gate if (salt == NULL) { 7097c478bd9Sstevel@tonic-gate if (errno == ENOMEM) 7107c478bd9Sstevel@tonic-gate return (PWU_NOMEM); 7117c478bd9Sstevel@tonic-gate /* algorithm problem? */ 7127c478bd9Sstevel@tonic-gate syslog(LOG_AUTH | LOG_ALERT, 7137c478bd9Sstevel@tonic-gate "passwdutil: crypt_gensalt %m"); 7147c478bd9Sstevel@tonic-gate return (PWU_UPDATE_FAILED); 7157c478bd9Sstevel@tonic-gate } 7167c478bd9Sstevel@tonic-gate hash = crypt(p->data.val_s, salt); 7177c478bd9Sstevel@tonic-gate free(salt); 7187c478bd9Sstevel@tonic-gate if (hash == NULL) { 7197c478bd9Sstevel@tonic-gate errno = ENOMEM; 7207c478bd9Sstevel@tonic-gate return (PWU_NOMEM); 7217c478bd9Sstevel@tonic-gate } 7227c478bd9Sstevel@tonic-gate pword = strdup(hash); 7237c478bd9Sstevel@tonic-gate if (pword == NULL) { 7247c478bd9Sstevel@tonic-gate errno = ENOMEM; 7257c478bd9Sstevel@tonic-gate return (PWU_NOMEM); 7267c478bd9Sstevel@tonic-gate } 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate if (pwbuf->new_sp_pwdp) 7297c478bd9Sstevel@tonic-gate free(pwbuf->new_sp_pwdp); 7307c478bd9Sstevel@tonic-gate pwbuf->new_sp_pwdp = pword; 7317c478bd9Sstevel@tonic-gate spw->sp_pwdp = pword; 7327c478bd9Sstevel@tonic-gate aging_needed = 1; 7337c478bd9Sstevel@tonic-gate pwbuf->update_history = 1; 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate spw->sp_flag &= ~FAILCOUNT_MASK; /* reset count */ 7367c478bd9Sstevel@tonic-gate spw->sp_lstchg = DAY_NOW_32; 7377c478bd9Sstevel@tonic-gate break; 7387c478bd9Sstevel@tonic-gate case ATTR_LOCK_ACCOUNT: 7397c478bd9Sstevel@tonic-gate if (spw->sp_pwdp == NULL) { 7407c478bd9Sstevel@tonic-gate spw->sp_pwdp = LOCKSTRING; 741*5477a4d9Sgww } else if ((strncmp(spw->sp_pwdp, LOCKSTRING, 742*5477a4d9Sgww sizeof (LOCKSTRING)-1) != 0) && 743*5477a4d9Sgww (strcmp(spw->sp_pwdp, NOLOGINSTRING) != 0)) { 7447c478bd9Sstevel@tonic-gate len = sizeof (LOCKSTRING)-1 + 7457c478bd9Sstevel@tonic-gate strlen(spw->sp_pwdp) + 1; 7467c478bd9Sstevel@tonic-gate pword = malloc(len); 7477c478bd9Sstevel@tonic-gate if (pword == NULL) { 7487c478bd9Sstevel@tonic-gate errno = ENOMEM; 7497c478bd9Sstevel@tonic-gate return (PWU_NOMEM); 7507c478bd9Sstevel@tonic-gate } 7517c478bd9Sstevel@tonic-gate (void) strlcpy(pword, LOCKSTRING, len); 7527c478bd9Sstevel@tonic-gate (void) strlcat(pword, spw->sp_pwdp, len); 7537c478bd9Sstevel@tonic-gate if (pwbuf->new_sp_pwdp) 7547c478bd9Sstevel@tonic-gate free(pwbuf->new_sp_pwdp); 7557c478bd9Sstevel@tonic-gate pwbuf->new_sp_pwdp = pword; 7567c478bd9Sstevel@tonic-gate spw->sp_pwdp = pword; 7577c478bd9Sstevel@tonic-gate } 7587c478bd9Sstevel@tonic-gate spw->sp_lstchg = DAY_NOW_32; 7597c478bd9Sstevel@tonic-gate break; 7607c478bd9Sstevel@tonic-gate case ATTR_UNLOCK_ACCOUNT: 7617c478bd9Sstevel@tonic-gate if (spw->sp_pwdp != NULL && 7627c478bd9Sstevel@tonic-gate strncmp(spw->sp_pwdp, LOCKSTRING, 7637c478bd9Sstevel@tonic-gate sizeof (LOCKSTRING)-1) == 0) { 7647c478bd9Sstevel@tonic-gate (void) strcpy(spw->sp_pwdp, spw->sp_pwdp + 7657c478bd9Sstevel@tonic-gate sizeof (LOCKSTRING)-1); 7667c478bd9Sstevel@tonic-gate } 7677c478bd9Sstevel@tonic-gate spw->sp_lstchg = DAY_NOW_32; 7687c478bd9Sstevel@tonic-gate break; 7697c478bd9Sstevel@tonic-gate case ATTR_NOLOGIN_ACCOUNT: 7707c478bd9Sstevel@tonic-gate spw->sp_pwdp = NOLOGINSTRING; 7717c478bd9Sstevel@tonic-gate if (pwbuf->new_sp_pwdp) { 7727c478bd9Sstevel@tonic-gate free(pwbuf->new_sp_pwdp); 7737c478bd9Sstevel@tonic-gate pwbuf->new_sp_pwdp = NULL; 7747c478bd9Sstevel@tonic-gate } 7757c478bd9Sstevel@tonic-gate spw->sp_lstchg = DAY_NOW_32; 7767c478bd9Sstevel@tonic-gate break; 7777c478bd9Sstevel@tonic-gate case ATTR_EXPIRE_PASSWORD: 7787c478bd9Sstevel@tonic-gate spw->sp_lstchg = 0; 7797c478bd9Sstevel@tonic-gate break; 7807c478bd9Sstevel@tonic-gate case ATTR_LSTCHG: 7817c478bd9Sstevel@tonic-gate spw->sp_lstchg = p->data.val_i; 7827c478bd9Sstevel@tonic-gate break; 7837c478bd9Sstevel@tonic-gate case ATTR_MIN: 7847c478bd9Sstevel@tonic-gate if (spw->sp_max == -1 && 7857c478bd9Sstevel@tonic-gate p->data.val_i != -1 && max_present(p->next) == 0) 7867c478bd9Sstevel@tonic-gate return (PWU_AGING_DISABLED); 7877c478bd9Sstevel@tonic-gate spw->sp_min = p->data.val_i; 7887c478bd9Sstevel@tonic-gate aging_set = 1; 7897c478bd9Sstevel@tonic-gate break; 7907c478bd9Sstevel@tonic-gate case ATTR_MAX: 7917c478bd9Sstevel@tonic-gate if (p->data.val_i == -1) { 7927c478bd9Sstevel@tonic-gate /* Turn aging off -> Reset min and warn too */ 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate spw->sp_min = -1; 7957c478bd9Sstevel@tonic-gate spw->sp_warn = -1; 7967c478bd9Sstevel@tonic-gate } else { 7977c478bd9Sstevel@tonic-gate /* Turn aging on */ 7987c478bd9Sstevel@tonic-gate 7997c478bd9Sstevel@tonic-gate if (spw->sp_min == -1) { 8007c478bd9Sstevel@tonic-gate /* 8017c478bd9Sstevel@tonic-gate * If minage has not been set with 8027c478bd9Sstevel@tonic-gate * a command-line option, we set it 8037c478bd9Sstevel@tonic-gate * to zero. 8047c478bd9Sstevel@tonic-gate */ 8057c478bd9Sstevel@tonic-gate spw->sp_min = 0; 8067c478bd9Sstevel@tonic-gate } 8077c478bd9Sstevel@tonic-gate 8087c478bd9Sstevel@tonic-gate /* 8097c478bd9Sstevel@tonic-gate * If aging was turned off, we update lstchg. 8107c478bd9Sstevel@tonic-gate * 8117c478bd9Sstevel@tonic-gate * We take care not to update lstchg if the 8127c478bd9Sstevel@tonic-gate * user has no password, otherwise the user 8137c478bd9Sstevel@tonic-gate * might not be required to provide a password 8147c478bd9Sstevel@tonic-gate * the next time [s]he logs-in. 8157c478bd9Sstevel@tonic-gate * 8167c478bd9Sstevel@tonic-gate * Also, if lstchg != -1 (i.e., not set in 8177c478bd9Sstevel@tonic-gate * /etc/shadow), we keep the old value. 8187c478bd9Sstevel@tonic-gate */ 8197c478bd9Sstevel@tonic-gate if (spw->sp_max == -1 && 8207c478bd9Sstevel@tonic-gate spw->sp_pwdp != NULL && *spw->sp_pwdp && 8217c478bd9Sstevel@tonic-gate spw->sp_lstchg == -1) { 8227c478bd9Sstevel@tonic-gate spw->sp_lstchg = DAY_NOW_32; 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate } 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate spw->sp_max = p->data.val_i; 8277c478bd9Sstevel@tonic-gate 8287c478bd9Sstevel@tonic-gate aging_set = 1; 8297c478bd9Sstevel@tonic-gate 8307c478bd9Sstevel@tonic-gate break; 8317c478bd9Sstevel@tonic-gate case ATTR_WARN: 8327c478bd9Sstevel@tonic-gate if (spw->sp_max == -1 && p->data.val_i != -1 && 8337c478bd9Sstevel@tonic-gate max_present(p->next) == 0) 8347c478bd9Sstevel@tonic-gate return (PWU_AGING_DISABLED); 8357c478bd9Sstevel@tonic-gate spw->sp_warn = p->data.val_i; 8367c478bd9Sstevel@tonic-gate break; 8377c478bd9Sstevel@tonic-gate case ATTR_INACT: 8387c478bd9Sstevel@tonic-gate spw->sp_inact = p->data.val_i; 8397c478bd9Sstevel@tonic-gate break; 8407c478bd9Sstevel@tonic-gate case ATTR_EXPIRE: 8417c478bd9Sstevel@tonic-gate spw->sp_expire = p->data.val_i; 8427c478bd9Sstevel@tonic-gate break; 8437c478bd9Sstevel@tonic-gate case ATTR_FLAG: 8447c478bd9Sstevel@tonic-gate spw->sp_flag = p->data.val_i; 8457c478bd9Sstevel@tonic-gate break; 8467c478bd9Sstevel@tonic-gate case ATTR_INCR_FAILED_LOGINS: 8477c478bd9Sstevel@tonic-gate { 8487c478bd9Sstevel@tonic-gate int count = (spw->sp_flag & FAILCOUNT_MASK) + 1; 8497c478bd9Sstevel@tonic-gate spw->sp_flag &= ~FAILCOUNT_MASK; 8507c478bd9Sstevel@tonic-gate spw->sp_flag |= min(FAILCOUNT_MASK, count); 8517c478bd9Sstevel@tonic-gate p->data.val_i = count; 8527c478bd9Sstevel@tonic-gate } 8537c478bd9Sstevel@tonic-gate break; 8547c478bd9Sstevel@tonic-gate case ATTR_RST_FAILED_LOGINS: 8557c478bd9Sstevel@tonic-gate p->data.val_i = spw->sp_flag & FAILCOUNT_MASK; 8567c478bd9Sstevel@tonic-gate spw->sp_flag &= ~FAILCOUNT_MASK; 8577c478bd9Sstevel@tonic-gate break; 8587c478bd9Sstevel@tonic-gate default: 8597c478bd9Sstevel@tonic-gate break; 8607c478bd9Sstevel@tonic-gate } 8617c478bd9Sstevel@tonic-gate } 8627c478bd9Sstevel@tonic-gate 8637c478bd9Sstevel@tonic-gate /* 8647c478bd9Sstevel@tonic-gate * What should the new aging values look like? 8657c478bd9Sstevel@tonic-gate * 8667c478bd9Sstevel@tonic-gate * There are a number of different conditions 8677c478bd9Sstevel@tonic-gate * 8687c478bd9Sstevel@tonic-gate * a) aging is already configured: don't touch it 8697c478bd9Sstevel@tonic-gate * 8707c478bd9Sstevel@tonic-gate * b) disable_aging is set: disable aging 8717c478bd9Sstevel@tonic-gate * 8727c478bd9Sstevel@tonic-gate * c) aging is not configured: turn on default aging; 8737c478bd9Sstevel@tonic-gate * 8747c478bd9Sstevel@tonic-gate * b) and c) of course only if aging_needed and !aging_set. 8757c478bd9Sstevel@tonic-gate * (i.e., password changed, and aging values not changed) 8767c478bd9Sstevel@tonic-gate */ 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate if (spw != NULL && spw->sp_max <= 0) { 8797c478bd9Sstevel@tonic-gate /* a) aging not yet configured */ 8807c478bd9Sstevel@tonic-gate if (aging_needed && !aging_set) { 8817c478bd9Sstevel@tonic-gate if (disable_aging) { 8827c478bd9Sstevel@tonic-gate /* b) turn off aging */ 8837c478bd9Sstevel@tonic-gate spw->sp_min = spw->sp_max = spw->sp_warn = -1; 8847c478bd9Sstevel@tonic-gate } else { 8857c478bd9Sstevel@tonic-gate /* c) */ 8867c478bd9Sstevel@tonic-gate turn_on_default_aging(spw); 8877c478bd9Sstevel@tonic-gate } 8887c478bd9Sstevel@tonic-gate } 8897c478bd9Sstevel@tonic-gate } 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate return (PWU_SUCCESS); 8927c478bd9Sstevel@tonic-gate } 8937c478bd9Sstevel@tonic-gate 8947c478bd9Sstevel@tonic-gate /* 8957c478bd9Sstevel@tonic-gate * files_update_shadow(char *name, struct spwd *spwd) 8967c478bd9Sstevel@tonic-gate * 8977c478bd9Sstevel@tonic-gate * update the shadow password file SHADOW to contain the spwd structure 8987c478bd9Sstevel@tonic-gate * "spwd" for user "name" 8997c478bd9Sstevel@tonic-gate */ 9007c478bd9Sstevel@tonic-gate int 9017c478bd9Sstevel@tonic-gate files_update_shadow(char *name, struct spwd *spwd) 9027c478bd9Sstevel@tonic-gate { 9037c478bd9Sstevel@tonic-gate struct stat64 stbuf; 9047c478bd9Sstevel@tonic-gate FILE *dst; 9057c478bd9Sstevel@tonic-gate FILE *src; 9067c478bd9Sstevel@tonic-gate struct spwd cur; 9077c478bd9Sstevel@tonic-gate char buf[SPW_SCRATCH_SIZE]; 9087c478bd9Sstevel@tonic-gate int tempfd; 9097c478bd9Sstevel@tonic-gate mode_t filemode; 9107c478bd9Sstevel@tonic-gate int result = -1; 9117c478bd9Sstevel@tonic-gate int err = PWU_SUCCESS; 9127c478bd9Sstevel@tonic-gate 9137c478bd9Sstevel@tonic-gate /* Mode of the shadow file should be 400 or 000 */ 9147c478bd9Sstevel@tonic-gate if (stat64(SHADOW, &stbuf) < 0) { 9157c478bd9Sstevel@tonic-gate err = PWU_STAT_FAILED; 9167c478bd9Sstevel@tonic-gate goto shadow_exit; 9177c478bd9Sstevel@tonic-gate } 9187c478bd9Sstevel@tonic-gate 9197c478bd9Sstevel@tonic-gate /* copy mode from current shadow file (0400 or 0000) */ 9207c478bd9Sstevel@tonic-gate filemode = stbuf.st_mode & S_IRUSR; 9217c478bd9Sstevel@tonic-gate 9227c478bd9Sstevel@tonic-gate /* 9237c478bd9Sstevel@tonic-gate * we can't specify filemodes to fopen(), and we SHOULD NOT 9247c478bd9Sstevel@tonic-gate * set umask in multi-thread safe libraries, so we use 9257c478bd9Sstevel@tonic-gate * a combination of open() and fdopen() 9267c478bd9Sstevel@tonic-gate */ 9277c478bd9Sstevel@tonic-gate tempfd = open(SHADTEMP, O_WRONLY|O_CREAT|O_TRUNC, filemode); 9287c478bd9Sstevel@tonic-gate if (tempfd < 0) { 9297c478bd9Sstevel@tonic-gate err = PWU_OPEN_FAILED; 9307c478bd9Sstevel@tonic-gate goto shadow_exit; 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate (void) fchown(tempfd, (uid_t)0, stbuf.st_gid); 9337c478bd9Sstevel@tonic-gate 934004388ebScasper if ((dst = fdopen(tempfd, "wF")) == NULL) { 9357c478bd9Sstevel@tonic-gate err = PWU_OPEN_FAILED; 9367c478bd9Sstevel@tonic-gate goto shadow_exit; 9377c478bd9Sstevel@tonic-gate } 9387c478bd9Sstevel@tonic-gate 939004388ebScasper if ((src = fopen(SHADOW, "rF")) == NULL) { 9407c478bd9Sstevel@tonic-gate err = PWU_OPEN_FAILED; 9417c478bd9Sstevel@tonic-gate (void) fclose(dst); 9427c478bd9Sstevel@tonic-gate (void) unlink(SHADTEMP); 9437c478bd9Sstevel@tonic-gate goto shadow_exit; 9447c478bd9Sstevel@tonic-gate } 9457c478bd9Sstevel@tonic-gate 9467c478bd9Sstevel@tonic-gate /* 9477c478bd9Sstevel@tonic-gate * copy old shadow to temporary file while replacing the entry 9487c478bd9Sstevel@tonic-gate * that matches "name". 9497c478bd9Sstevel@tonic-gate */ 9507c478bd9Sstevel@tonic-gate while (fgetspent_r(src, &cur, buf, sizeof (buf)) != NULL) { 9517c478bd9Sstevel@tonic-gate 9527c478bd9Sstevel@tonic-gate if (strcmp(cur.sp_namp, name) == 0) 9537c478bd9Sstevel@tonic-gate result = putspent(spwd, dst); 9547c478bd9Sstevel@tonic-gate else 9557c478bd9Sstevel@tonic-gate result = putspent(&cur, dst); 9567c478bd9Sstevel@tonic-gate 9577c478bd9Sstevel@tonic-gate if (result != 0) { 9587c478bd9Sstevel@tonic-gate err = PWU_WRITE_FAILED; 9597c478bd9Sstevel@tonic-gate (void) fclose(src); 9607c478bd9Sstevel@tonic-gate (void) fclose(dst); 9617c478bd9Sstevel@tonic-gate goto shadow_exit; 9627c478bd9Sstevel@tonic-gate } 9637c478bd9Sstevel@tonic-gate } 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate (void) fclose(src); 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate if (fclose(dst) != 0) { 9687c478bd9Sstevel@tonic-gate /* 9697c478bd9Sstevel@tonic-gate * Something went wrong (ENOSPC for example). Don't 9707c478bd9Sstevel@tonic-gate * use the resulting temporary file! 9717c478bd9Sstevel@tonic-gate */ 9727c478bd9Sstevel@tonic-gate err = PWU_CLOSE_FAILED; 9737c478bd9Sstevel@tonic-gate (void) unlink(SHADTEMP); 9747c478bd9Sstevel@tonic-gate goto shadow_exit; 9757c478bd9Sstevel@tonic-gate } 9767c478bd9Sstevel@tonic-gate 9777c478bd9Sstevel@tonic-gate /* 9787c478bd9Sstevel@tonic-gate * Rename stmp to shadow: 9797c478bd9Sstevel@tonic-gate * 1. make sure /etc/oshadow is gone 9807c478bd9Sstevel@tonic-gate * 2. ln /etc/shadow /etc/oshadow 9817c478bd9Sstevel@tonic-gate * 3. mv /etc/stmp /etc/shadow 9827c478bd9Sstevel@tonic-gate */ 9837c478bd9Sstevel@tonic-gate if (unlink(OSHADOW) && access(OSHADOW, 0) == 0) { 9847c478bd9Sstevel@tonic-gate err = PWU_UPDATE_FAILED; 9857c478bd9Sstevel@tonic-gate (void) unlink(SHADTEMP); 9867c478bd9Sstevel@tonic-gate goto shadow_exit; 9877c478bd9Sstevel@tonic-gate } 9887c478bd9Sstevel@tonic-gate 9897c478bd9Sstevel@tonic-gate if (link(SHADOW, OSHADOW) == -1) { 9907c478bd9Sstevel@tonic-gate err = PWU_UPDATE_FAILED; 9917c478bd9Sstevel@tonic-gate (void) unlink(SHADTEMP); 9927c478bd9Sstevel@tonic-gate goto shadow_exit; 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate if (rename(SHADTEMP, SHADOW) == -1) { 9967c478bd9Sstevel@tonic-gate err = PWU_UPDATE_FAILED; 9977c478bd9Sstevel@tonic-gate (void) unlink(SHADTEMP); 9987c478bd9Sstevel@tonic-gate goto shadow_exit; 9997c478bd9Sstevel@tonic-gate } 10007c478bd9Sstevel@tonic-gate (void) unlink(OSHADOW); 10017c478bd9Sstevel@tonic-gate 10027c478bd9Sstevel@tonic-gate shadow_exit: 10037c478bd9Sstevel@tonic-gate return (err); 10047c478bd9Sstevel@tonic-gate } 10057c478bd9Sstevel@tonic-gate 10067c478bd9Sstevel@tonic-gate int 10077c478bd9Sstevel@tonic-gate files_update_passwd(char *name, struct passwd *pwd) 10087c478bd9Sstevel@tonic-gate { 10097c478bd9Sstevel@tonic-gate struct stat64 stbuf; 10107c478bd9Sstevel@tonic-gate FILE *src, *dst; 10117c478bd9Sstevel@tonic-gate int tempfd; 10127c478bd9Sstevel@tonic-gate struct passwd cur; 10137c478bd9Sstevel@tonic-gate char buf[PWD_SCRATCH_SIZE]; 10147c478bd9Sstevel@tonic-gate int result; 10157c478bd9Sstevel@tonic-gate int err = PWU_SUCCESS; 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate if (stat64(PASSWD, &stbuf) < 0) { 10187c478bd9Sstevel@tonic-gate err = PWU_STAT_FAILED; 10197c478bd9Sstevel@tonic-gate goto passwd_exit; 10207c478bd9Sstevel@tonic-gate } 10217c478bd9Sstevel@tonic-gate 10227c478bd9Sstevel@tonic-gate /* see files_update_shadow() for open()+fdopen() rationale */ 10237c478bd9Sstevel@tonic-gate 10247c478bd9Sstevel@tonic-gate if ((tempfd = open(PASSTEMP, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) { 10257c478bd9Sstevel@tonic-gate err = PWU_OPEN_FAILED; 10267c478bd9Sstevel@tonic-gate goto passwd_exit; 10277c478bd9Sstevel@tonic-gate } 1028004388ebScasper if ((dst = fdopen(tempfd, "wF")) == NULL) { 10297c478bd9Sstevel@tonic-gate err = PWU_OPEN_FAILED; 10307c478bd9Sstevel@tonic-gate goto passwd_exit; 10317c478bd9Sstevel@tonic-gate } 1032004388ebScasper if ((src = fopen(PASSWD, "rF")) == NULL) { 10337c478bd9Sstevel@tonic-gate err = PWU_OPEN_FAILED; 10347c478bd9Sstevel@tonic-gate (void) fclose(dst); 10357c478bd9Sstevel@tonic-gate (void) unlink(PASSTEMP); 10367c478bd9Sstevel@tonic-gate goto passwd_exit; 10377c478bd9Sstevel@tonic-gate } 10387c478bd9Sstevel@tonic-gate 10397c478bd9Sstevel@tonic-gate /* 10407c478bd9Sstevel@tonic-gate * copy old password entries to temporary file while replacing 10417c478bd9Sstevel@tonic-gate * the entry that matches "name" 10427c478bd9Sstevel@tonic-gate */ 10437c478bd9Sstevel@tonic-gate while (fgetpwent_r(src, &cur, buf, sizeof (buf)) != NULL) { 10447c478bd9Sstevel@tonic-gate if (strcmp(cur.pw_name, name) == 0) 10457c478bd9Sstevel@tonic-gate result = putpwent(pwd, dst); 10467c478bd9Sstevel@tonic-gate else 10477c478bd9Sstevel@tonic-gate result = putpwent(&cur, dst); 10487c478bd9Sstevel@tonic-gate if (result != 0) { 10497c478bd9Sstevel@tonic-gate err = PWU_WRITE_FAILED; 10507c478bd9Sstevel@tonic-gate (void) fclose(src); 10517c478bd9Sstevel@tonic-gate (void) fclose(dst); 10527c478bd9Sstevel@tonic-gate goto passwd_exit; 10537c478bd9Sstevel@tonic-gate } 10547c478bd9Sstevel@tonic-gate } 10557c478bd9Sstevel@tonic-gate 10567c478bd9Sstevel@tonic-gate (void) fclose(src); 10577c478bd9Sstevel@tonic-gate if (fclose(dst) != 0) { 10587c478bd9Sstevel@tonic-gate err = PWU_CLOSE_FAILED; 10597c478bd9Sstevel@tonic-gate goto passwd_exit; /* Don't trust the temporary file */ 10607c478bd9Sstevel@tonic-gate } 10617c478bd9Sstevel@tonic-gate 10627c478bd9Sstevel@tonic-gate /* Rename temp to passwd */ 10637c478bd9Sstevel@tonic-gate if (unlink(OPASSWD) && access(OPASSWD, 0) == 0) { 10647c478bd9Sstevel@tonic-gate err = PWU_UPDATE_FAILED; 10657c478bd9Sstevel@tonic-gate (void) unlink(PASSTEMP); 10667c478bd9Sstevel@tonic-gate goto passwd_exit; 10677c478bd9Sstevel@tonic-gate } 10687c478bd9Sstevel@tonic-gate 10697c478bd9Sstevel@tonic-gate if (link(PASSWD, OPASSWD) == -1) { 10707c478bd9Sstevel@tonic-gate err = PWU_UPDATE_FAILED; 10717c478bd9Sstevel@tonic-gate (void) unlink(PASSTEMP); 10727c478bd9Sstevel@tonic-gate goto passwd_exit; 10737c478bd9Sstevel@tonic-gate } 10747c478bd9Sstevel@tonic-gate 10757c478bd9Sstevel@tonic-gate if (rename(PASSTEMP, PASSWD) == -1) { 10767c478bd9Sstevel@tonic-gate err = PWU_UPDATE_FAILED; 10777c478bd9Sstevel@tonic-gate (void) unlink(PASSTEMP); 10787c478bd9Sstevel@tonic-gate goto passwd_exit; 10797c478bd9Sstevel@tonic-gate } 10807c478bd9Sstevel@tonic-gate 10817c478bd9Sstevel@tonic-gate (void) chmod(PASSWD, 0644); 10827c478bd9Sstevel@tonic-gate 10837c478bd9Sstevel@tonic-gate passwd_exit: 10847c478bd9Sstevel@tonic-gate return (err); 10857c478bd9Sstevel@tonic-gate 10867c478bd9Sstevel@tonic-gate } 10877c478bd9Sstevel@tonic-gate 10887c478bd9Sstevel@tonic-gate /* 10897c478bd9Sstevel@tonic-gate * files_putpwnam(name, oldpw, dummy, rep, buf) 10907c478bd9Sstevel@tonic-gate * 10917c478bd9Sstevel@tonic-gate * store the password attributes contained in "buf" in /etc/passwd and 10927c478bd9Sstevel@tonic-gate * /etc/shadow. The dummy parameter is a placeholder for NIS+ 10937c478bd9Sstevel@tonic-gate * updates where the "oldrpc" password is passed. 10947c478bd9Sstevel@tonic-gate */ 10957c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 10967c478bd9Sstevel@tonic-gate int 10977c478bd9Sstevel@tonic-gate files_putpwnam(char *name, char *oldpw, char *dummy, 10987c478bd9Sstevel@tonic-gate pwu_repository_t *rep, void *buf) 10997c478bd9Sstevel@tonic-gate { 11007c478bd9Sstevel@tonic-gate struct pwbuf *pwbuf = (struct pwbuf *)buf; 11017c478bd9Sstevel@tonic-gate int result = PWU_SUCCESS; 11027c478bd9Sstevel@tonic-gate 11037c478bd9Sstevel@tonic-gate if (pwbuf->pwd) { 11047c478bd9Sstevel@tonic-gate result = files_update_passwd(name, pwbuf->pwd); 11057c478bd9Sstevel@tonic-gate } 11067c478bd9Sstevel@tonic-gate 11077c478bd9Sstevel@tonic-gate if (result == PWU_SUCCESS && pwbuf->spwd) { 11087c478bd9Sstevel@tonic-gate if (pwbuf->update_history != 0) { 11097c478bd9Sstevel@tonic-gate debug("update_history = %d", pwbuf->update_history); 11107c478bd9Sstevel@tonic-gate result = files_update_history(name, pwbuf->spwd); 11117c478bd9Sstevel@tonic-gate } else { 11127c478bd9Sstevel@tonic-gate debug("no password change"); 11137c478bd9Sstevel@tonic-gate } 11147c478bd9Sstevel@tonic-gate if (result == PWU_SUCCESS) { 11157c478bd9Sstevel@tonic-gate result = files_update_shadow(name, pwbuf->spwd); 11167c478bd9Sstevel@tonic-gate } 11177c478bd9Sstevel@tonic-gate } 11187c478bd9Sstevel@tonic-gate 11197c478bd9Sstevel@tonic-gate if (pwbuf->pwd) { 11207c478bd9Sstevel@tonic-gate (void) memset(pwbuf->pwd, 0, sizeof (struct passwd)); 11217c478bd9Sstevel@tonic-gate (void) memset(pwbuf->pwd_scratch, 0, PWD_SCRATCH_SIZE); 11227c478bd9Sstevel@tonic-gate free(pwbuf->pwd); 11237c478bd9Sstevel@tonic-gate free(pwbuf->pwd_scratch); 11247c478bd9Sstevel@tonic-gate } 11257c478bd9Sstevel@tonic-gate if (pwbuf->spwd) { 11267c478bd9Sstevel@tonic-gate (void) memset(pwbuf->spwd, 0, sizeof (struct spwd)); 11277c478bd9Sstevel@tonic-gate (void) memset(pwbuf->spwd_scratch, 0, SPW_SCRATCH_SIZE); 11287c478bd9Sstevel@tonic-gate free(pwbuf->spwd); 11297c478bd9Sstevel@tonic-gate free(pwbuf->spwd_scratch); 11307c478bd9Sstevel@tonic-gate } 11317c478bd9Sstevel@tonic-gate if (pwbuf->new_sp_pwdp) { 11327c478bd9Sstevel@tonic-gate free(pwbuf->new_sp_pwdp); 11337c478bd9Sstevel@tonic-gate } 11347c478bd9Sstevel@tonic-gate 11357c478bd9Sstevel@tonic-gate return (result); 11367c478bd9Sstevel@tonic-gate } 11377c478bd9Sstevel@tonic-gate 11387c478bd9Sstevel@tonic-gate /* 11397c478bd9Sstevel@tonic-gate * NOTE: This is all covered under the repository lock held for updating 11407c478bd9Sstevel@tonic-gate * passwd(4) and shadow(4). 11417c478bd9Sstevel@tonic-gate */ 11427c478bd9Sstevel@tonic-gate int 11437c478bd9Sstevel@tonic-gate files_update_history(char *name, struct spwd *spwd) 11447c478bd9Sstevel@tonic-gate { 11457c478bd9Sstevel@tonic-gate int histsize; 11467c478bd9Sstevel@tonic-gate int tmpfd; 11477c478bd9Sstevel@tonic-gate FILE *src; /* history database file */ 11487c478bd9Sstevel@tonic-gate FILE *dst; /* temp history database being updated */ 11497c478bd9Sstevel@tonic-gate struct stat64 statbuf; 11507c478bd9Sstevel@tonic-gate char buf[MAX_LOGNAME + MAXHISTORY + 11517c478bd9Sstevel@tonic-gate (MAXHISTORY * CRYPT_MAXCIPHERTEXTLEN)+1]; 11527c478bd9Sstevel@tonic-gate int found; 11537c478bd9Sstevel@tonic-gate 11547c478bd9Sstevel@tonic-gate if ((histsize = def_getint("HISTORY=", DEFHISTORY)) == 0) { 11557c478bd9Sstevel@tonic-gate debug("files_update_history(%s) no history, unlinking", name); 11567c478bd9Sstevel@tonic-gate (void) unlink(HISTORY); 11577c478bd9Sstevel@tonic-gate return (PWU_SUCCESS); /* no history update defined */ 11587c478bd9Sstevel@tonic-gate } 11597c478bd9Sstevel@tonic-gate debug("files_update_history(%s, %s) histsize = %d", name, spwd->sp_pwdp, 11607c478bd9Sstevel@tonic-gate histsize); 11617c478bd9Sstevel@tonic-gate 11627c478bd9Sstevel@tonic-gate if (histsize > MAXHISTORY) 11637c478bd9Sstevel@tonic-gate histsize = MAXHISTORY; 11647c478bd9Sstevel@tonic-gate if ((tmpfd = open(HISTEMP, O_WRONLY|O_CREAT|O_TRUNC, HISTMODE)) < 0) { 11657c478bd9Sstevel@tonic-gate return (PWU_OPEN_FAILED); 11667c478bd9Sstevel@tonic-gate } 11677c478bd9Sstevel@tonic-gate (void) fchown(tmpfd, (uid_t)0, (gid_t)0); 11687c478bd9Sstevel@tonic-gate 11697c478bd9Sstevel@tonic-gate /* get ready to copy */ 1170004388ebScasper if (((src = fopen(HISTORY, "rF")) == NULL) && 11717c478bd9Sstevel@tonic-gate (errno != ENOENT)) { 11727c478bd9Sstevel@tonic-gate (void) unlink(HISTEMP); 11737c478bd9Sstevel@tonic-gate return (PWU_OPEN_FAILED); 11747c478bd9Sstevel@tonic-gate } 1175004388ebScasper if ((dst = fdopen(tmpfd, "wF")) == NULL) { 11767c478bd9Sstevel@tonic-gate (void) fclose(src); 11777c478bd9Sstevel@tonic-gate (void) unlink(HISTEMP); 11787c478bd9Sstevel@tonic-gate return (PWU_OPEN_FAILED); 11797c478bd9Sstevel@tonic-gate } 11807c478bd9Sstevel@tonic-gate 11817c478bd9Sstevel@tonic-gate /* Copy and update if found. Add if not found. */ 11827c478bd9Sstevel@tonic-gate 11837c478bd9Sstevel@tonic-gate found = 0; 11847c478bd9Sstevel@tonic-gate 11857c478bd9Sstevel@tonic-gate while ((src != NULL) && 11867c478bd9Sstevel@tonic-gate (fgets(buf, sizeof (buf), src) != NULL)) { 11877c478bd9Sstevel@tonic-gate char *user; 11887c478bd9Sstevel@tonic-gate char *last; 11897c478bd9Sstevel@tonic-gate 11907c478bd9Sstevel@tonic-gate /* get username field */ 11917c478bd9Sstevel@tonic-gate user = strtok_r(buf, ":", &last); 11927c478bd9Sstevel@tonic-gate 11937c478bd9Sstevel@tonic-gate #ifdef DEBUG 11947c478bd9Sstevel@tonic-gate debug("files_update_history: read=\"%s\"", user); 11957c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 11967c478bd9Sstevel@tonic-gate 11977c478bd9Sstevel@tonic-gate if (strcmp(user, name) == 0) { 11987c478bd9Sstevel@tonic-gate char *crypt; 11997c478bd9Sstevel@tonic-gate int i; 12007c478bd9Sstevel@tonic-gate 12017c478bd9Sstevel@tonic-gate /* found user, update */ 12027c478bd9Sstevel@tonic-gate found++; 12037c478bd9Sstevel@tonic-gate (void) fprintf(dst, "%s:%s:", name, spwd->sp_pwdp); 12047c478bd9Sstevel@tonic-gate debug("files_update_history: update user\n" 12057c478bd9Sstevel@tonic-gate "\t%s:%s:", name, spwd->sp_pwdp); 12067c478bd9Sstevel@tonic-gate 12077c478bd9Sstevel@tonic-gate /* get old crypted password history */ 12087c478bd9Sstevel@tonic-gate for (i = 0; i < MAXHISTORY-1; i++) { 12097c478bd9Sstevel@tonic-gate crypt = strtok_r(NULL, ":", &last); 12107c478bd9Sstevel@tonic-gate if (crypt == NULL || 12117c478bd9Sstevel@tonic-gate *crypt == '\n') { 12127c478bd9Sstevel@tonic-gate break; 12137c478bd9Sstevel@tonic-gate } 12147c478bd9Sstevel@tonic-gate (void) fprintf(dst, "%s:", crypt); 12157c478bd9Sstevel@tonic-gate debug("\t%d = %s:", i+1, crypt); 12167c478bd9Sstevel@tonic-gate } 12177c478bd9Sstevel@tonic-gate (void) fprintf(dst, "\n"); 12187c478bd9Sstevel@tonic-gate } else { 12197c478bd9Sstevel@tonic-gate 12207c478bd9Sstevel@tonic-gate /* copy other users to updated file */ 12217c478bd9Sstevel@tonic-gate (void) fprintf(dst, "%s:%s", user, last); 12227c478bd9Sstevel@tonic-gate #ifdef DEBUG 12237c478bd9Sstevel@tonic-gate debug("files_update_history: copy line %s", 12247c478bd9Sstevel@tonic-gate user); 12257c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 12267c478bd9Sstevel@tonic-gate } 12277c478bd9Sstevel@tonic-gate } 12287c478bd9Sstevel@tonic-gate 12297c478bd9Sstevel@tonic-gate if (found == 0) { 12307c478bd9Sstevel@tonic-gate 12317c478bd9Sstevel@tonic-gate /* user not found, add to history file */ 12327c478bd9Sstevel@tonic-gate (void) fprintf(dst, "%s:%s:\n", name, spwd->sp_pwdp); 12337c478bd9Sstevel@tonic-gate debug("files_update_history: add line\n" 12347c478bd9Sstevel@tonic-gate "\t%s:%s:", name, spwd->sp_pwdp); 12357c478bd9Sstevel@tonic-gate } 12367c478bd9Sstevel@tonic-gate 12377c478bd9Sstevel@tonic-gate (void) fclose(src); 12387c478bd9Sstevel@tonic-gate 12397c478bd9Sstevel@tonic-gate /* If something messed up in file system, loose the update */ 12407c478bd9Sstevel@tonic-gate if (fclose(dst) != 0) { 12417c478bd9Sstevel@tonic-gate 12427c478bd9Sstevel@tonic-gate debug("files_update_history: update file close failed %d", 12437c478bd9Sstevel@tonic-gate errno); 12447c478bd9Sstevel@tonic-gate (void) unlink(HISTEMP); 12457c478bd9Sstevel@tonic-gate return (PWU_CLOSE_FAILED); 12467c478bd9Sstevel@tonic-gate } 12477c478bd9Sstevel@tonic-gate 12487c478bd9Sstevel@tonic-gate /* 12497c478bd9Sstevel@tonic-gate * rename history to ohistory, 12507c478bd9Sstevel@tonic-gate * rename tmp to history, 12517c478bd9Sstevel@tonic-gate * unlink ohistory. 12527c478bd9Sstevel@tonic-gate */ 12537c478bd9Sstevel@tonic-gate 12547c478bd9Sstevel@tonic-gate (void) unlink(OHISTORY); 12557c478bd9Sstevel@tonic-gate 12567c478bd9Sstevel@tonic-gate if (stat64(OHISTORY, &statbuf) == 0 || 12577c478bd9Sstevel@tonic-gate ((src != NULL) && (link(HISTORY, OHISTORY) != 0)) || 12587c478bd9Sstevel@tonic-gate rename(HISTEMP, HISTORY) != 0) { 12597c478bd9Sstevel@tonic-gate 12607c478bd9Sstevel@tonic-gate /* old history won't go away, loose the update */ 12617c478bd9Sstevel@tonic-gate debug("files_update_history: update file rename failed %d", 12627c478bd9Sstevel@tonic-gate errno); 12637c478bd9Sstevel@tonic-gate (void) unlink(HISTEMP); 12647c478bd9Sstevel@tonic-gate return (PWU_UPDATE_FAILED); 12657c478bd9Sstevel@tonic-gate } 12667c478bd9Sstevel@tonic-gate 12677c478bd9Sstevel@tonic-gate (void) unlink(OHISTORY); 12687c478bd9Sstevel@tonic-gate return (PWU_SUCCESS); 12697c478bd9Sstevel@tonic-gate } 1270