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