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 5c6dc4be3Sgww * Common Development and Distribution License (the "License"). 6c6dc4be3Sgww * 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*36e852a1SRaja Andra * 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 <errno.h> 287c478bd9Sstevel@tonic-gate #include <stdlib.h> 297c478bd9Sstevel@tonic-gate #include <pwd.h> 307c478bd9Sstevel@tonic-gate #include <shadow.h> 317c478bd9Sstevel@tonic-gate #include <string.h> 327c478bd9Sstevel@tonic-gate #include <stdlib.h> 337c478bd9Sstevel@tonic-gate #include <unistd.h> 347c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include "passwdutil.h" 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate /* from files_attr.c */ 397c478bd9Sstevel@tonic-gate struct passwd *private_getpwnam_r(const char *name, struct passwd *result, 407c478bd9Sstevel@tonic-gate char *buffer, int buflen); 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate int nss_getattr(char *name, attrlist *item, pwu_repository_t *rep); 437c478bd9Sstevel@tonic-gate int nss_getpwnam(char *name, attrlist *items, pwu_repository_t *rep, 447c478bd9Sstevel@tonic-gate void **buf); 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* 477c478bd9Sstevel@tonic-gate * nss function pointer table, used by passwdutil_init to initialize 487c478bd9Sstevel@tonic-gate * the global Repository-OPerations table "rops" 497c478bd9Sstevel@tonic-gate */ 507c478bd9Sstevel@tonic-gate struct repops nss_repops = { 517c478bd9Sstevel@tonic-gate NULL, /* checkhistory */ 527c478bd9Sstevel@tonic-gate nss_getattr, 537c478bd9Sstevel@tonic-gate nss_getpwnam, 547c478bd9Sstevel@tonic-gate NULL, /* update */ 557c478bd9Sstevel@tonic-gate NULL, /* putpwnam */ 567c478bd9Sstevel@tonic-gate NULL, /* user_to_authenticate */ 577c478bd9Sstevel@tonic-gate NULL, /* lock */ 587c478bd9Sstevel@tonic-gate NULL /* unlock */ 597c478bd9Sstevel@tonic-gate }; 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate /* 627c478bd9Sstevel@tonic-gate * this structure defines the buffer used to keep state between 637c478bd9Sstevel@tonic-gate * get/update/put calls 647c478bd9Sstevel@tonic-gate */ 657c478bd9Sstevel@tonic-gate struct pwbuf { 667c478bd9Sstevel@tonic-gate struct passwd *pwd; 677c478bd9Sstevel@tonic-gate char *pwd_scratch; 687c478bd9Sstevel@tonic-gate struct spwd *spwd; 697c478bd9Sstevel@tonic-gate char *spwd_scratch; 707c478bd9Sstevel@tonic-gate char *rep_name; 717c478bd9Sstevel@tonic-gate }; 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* 747c478bd9Sstevel@tonic-gate * We should use sysconf, but there is no sysconf name for SHADOW 757c478bd9Sstevel@tonic-gate * so we use these from nss_dbdefs 767c478bd9Sstevel@tonic-gate */ 777c478bd9Sstevel@tonic-gate #define PWD_SCRATCH_SIZE NSS_LINELEN_PASSWD 787c478bd9Sstevel@tonic-gate #define SPW_SCRATCH_SIZE NSS_LINELEN_SHADOW 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* 827c478bd9Sstevel@tonic-gate * nss_getpwnam(name, items, rep, buf) 837c478bd9Sstevel@tonic-gate * 847c478bd9Sstevel@tonic-gate */ 857c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 867c478bd9Sstevel@tonic-gate int 877c478bd9Sstevel@tonic-gate nss_getpwnam(char *name, attrlist *items, pwu_repository_t *rep, void **buf) 887c478bd9Sstevel@tonic-gate { 897c478bd9Sstevel@tonic-gate attrlist *p; 907c478bd9Sstevel@tonic-gate struct pwbuf *pwbuf; 917c478bd9Sstevel@tonic-gate int repositories = REP_ERANGE; /* changed if ATTR_REP_NAME is set */ 927c478bd9Sstevel@tonic-gate int err = PWU_SUCCESS; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate *buf = calloc(1, sizeof (struct pwbuf)); 957c478bd9Sstevel@tonic-gate pwbuf = (struct pwbuf *)*buf; 96c6dc4be3Sgww if (pwbuf == NULL) 97c6dc4be3Sgww return (PWU_NOMEM); 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * determine which password structure (/etc/passwd or /etc/shadow) 1017c478bd9Sstevel@tonic-gate * we need for the items we need to update 1027c478bd9Sstevel@tonic-gate */ 1037c478bd9Sstevel@tonic-gate for (p = items; p != NULL; p = p->next) { 1047c478bd9Sstevel@tonic-gate switch (p->type) { 1057c478bd9Sstevel@tonic-gate case ATTR_NAME: 1067c478bd9Sstevel@tonic-gate case ATTR_UID: 1077c478bd9Sstevel@tonic-gate case ATTR_GID: 1087c478bd9Sstevel@tonic-gate case ATTR_AGE: 1097c478bd9Sstevel@tonic-gate case ATTR_COMMENT: 1107c478bd9Sstevel@tonic-gate case ATTR_GECOS: 1117c478bd9Sstevel@tonic-gate case ATTR_HOMEDIR: 1127c478bd9Sstevel@tonic-gate case ATTR_SHELL: 1137c478bd9Sstevel@tonic-gate if (pwbuf->pwd == NULL) 1147c478bd9Sstevel@tonic-gate pwbuf->pwd = (struct passwd *) 1157c478bd9Sstevel@tonic-gate malloc(sizeof (struct passwd)); 1167c478bd9Sstevel@tonic-gate if (pwbuf->pwd == NULL) { 1177c478bd9Sstevel@tonic-gate errno = ENOMEM; 1187c478bd9Sstevel@tonic-gate if (pwbuf->spwd) 1197c478bd9Sstevel@tonic-gate free(pwbuf->spwd); 1207c478bd9Sstevel@tonic-gate return (PWU_NOMEM); 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate break; 1237c478bd9Sstevel@tonic-gate case ATTR_PASSWD: 1247c478bd9Sstevel@tonic-gate case ATTR_PASSWD_SERVER_POLICY: 1257c478bd9Sstevel@tonic-gate case ATTR_LSTCHG: 1267c478bd9Sstevel@tonic-gate case ATTR_MIN: 1277c478bd9Sstevel@tonic-gate case ATTR_MAX: 1287c478bd9Sstevel@tonic-gate case ATTR_WARN: 1297c478bd9Sstevel@tonic-gate case ATTR_INACT: 1307c478bd9Sstevel@tonic-gate case ATTR_EXPIRE: 1317c478bd9Sstevel@tonic-gate case ATTR_FLAG: 1327c478bd9Sstevel@tonic-gate case ATTR_LOCK_ACCOUNT: 1337c478bd9Sstevel@tonic-gate case ATTR_EXPIRE_PASSWORD: 1347c478bd9Sstevel@tonic-gate case ATTR_FAILED_LOGINS: 1357c478bd9Sstevel@tonic-gate if (pwbuf->spwd == NULL) 1367c478bd9Sstevel@tonic-gate pwbuf->spwd = (struct spwd *) 1377c478bd9Sstevel@tonic-gate malloc(sizeof (struct spwd)); 1387c478bd9Sstevel@tonic-gate if (pwbuf->spwd == NULL) { 1397c478bd9Sstevel@tonic-gate errno = ENOMEM; 1407c478bd9Sstevel@tonic-gate if (pwbuf->pwd) 1417c478bd9Sstevel@tonic-gate free(pwbuf->pwd); 1427c478bd9Sstevel@tonic-gate return (PWU_NOMEM); 1437c478bd9Sstevel@tonic-gate } 1447c478bd9Sstevel@tonic-gate break; 1457c478bd9Sstevel@tonic-gate case ATTR_REP_NAME: 1467c478bd9Sstevel@tonic-gate /* get the compat names (REP_COMPAT_*) */ 1477c478bd9Sstevel@tonic-gate repositories = get_ns(rep, PWU_READ); 1487c478bd9Sstevel@tonic-gate break; 1497c478bd9Sstevel@tonic-gate default: 1507c478bd9Sstevel@tonic-gate /* 1517c478bd9Sstevel@tonic-gate * Some other repository might have different values 1527c478bd9Sstevel@tonic-gate * so we ignore those. 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate break; 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate if (pwbuf->pwd) { 1597c478bd9Sstevel@tonic-gate if ((pwbuf->pwd_scratch = malloc(PWD_SCRATCH_SIZE)) == NULL) { 1607c478bd9Sstevel@tonic-gate err = PWU_NOMEM; 1617c478bd9Sstevel@tonic-gate goto error; 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate if (getpwnam_r(name, pwbuf->pwd, pwbuf->pwd_scratch, 1647c478bd9Sstevel@tonic-gate PWD_SCRATCH_SIZE) == NULL) { 1657c478bd9Sstevel@tonic-gate err = PWU_NOT_FOUND; 1667c478bd9Sstevel@tonic-gate goto error; 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate if (pwbuf->spwd) { 1717c478bd9Sstevel@tonic-gate if ((pwbuf->spwd_scratch = malloc(SPW_SCRATCH_SIZE)) == NULL) { 1727c478bd9Sstevel@tonic-gate err = PWU_NOMEM; 1737c478bd9Sstevel@tonic-gate goto error; 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate if (getspnam_r(name, pwbuf->spwd, pwbuf->spwd_scratch, 1767c478bd9Sstevel@tonic-gate SPW_SCRATCH_SIZE) == NULL) { 1777c478bd9Sstevel@tonic-gate err = PWU_NOT_FOUND; 1787c478bd9Sstevel@tonic-gate goto error; 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate /* pwbuf->rep_name tells us where the user in fact comes from */ 1837c478bd9Sstevel@tonic-gate if (repositories != REP_ERANGE) { 1847c478bd9Sstevel@tonic-gate struct passwd pwd; 1857c478bd9Sstevel@tonic-gate char pwd_scratch[PWD_SCRATCH_SIZE]; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate /* can we find the user locally? */ 1887c478bd9Sstevel@tonic-gate if (private_getpwnam_r(name, &pwd, pwd_scratch, 1897c478bd9Sstevel@tonic-gate PWD_SCRATCH_SIZE) != NULL) 1907c478bd9Sstevel@tonic-gate pwbuf->rep_name = "files"; 1917c478bd9Sstevel@tonic-gate else if (repositories & REP_COMPAT_LDAP) 1927c478bd9Sstevel@tonic-gate pwbuf->rep_name = "ldap"; 1937c478bd9Sstevel@tonic-gate else if (repositories & REP_COMPAT_NIS) 1947c478bd9Sstevel@tonic-gate pwbuf->rep_name = "nis"; 1957c478bd9Sstevel@tonic-gate else 1967c478bd9Sstevel@tonic-gate pwbuf->rep_name = "nss"; 1977c478bd9Sstevel@tonic-gate } else 1987c478bd9Sstevel@tonic-gate pwbuf->rep_name = "nss"; 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate return (PWU_SUCCESS); 2017c478bd9Sstevel@tonic-gate error: 2027c478bd9Sstevel@tonic-gate if (pwbuf->pwd) free(pwbuf->pwd); 2037c478bd9Sstevel@tonic-gate if (pwbuf->pwd_scratch) free(pwbuf->pwd_scratch); 2047c478bd9Sstevel@tonic-gate if (pwbuf->spwd) free(pwbuf->spwd); 2057c478bd9Sstevel@tonic-gate if (pwbuf->spwd_scratch) free(pwbuf->spwd_scratch); 2067c478bd9Sstevel@tonic-gate free(pwbuf); 2077c478bd9Sstevel@tonic-gate *buf = NULL; 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate return (err); 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* 2147c478bd9Sstevel@tonic-gate * nss_getattr(name, items, rep) 2157c478bd9Sstevel@tonic-gate * 2167c478bd9Sstevel@tonic-gate * Get attributes specified in list 'items' 2177c478bd9Sstevel@tonic-gate */ 2187c478bd9Sstevel@tonic-gate int 2197c478bd9Sstevel@tonic-gate nss_getattr(char *name, attrlist *items, pwu_repository_t *rep) 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate struct pwbuf *pwbuf; 2227c478bd9Sstevel@tonic-gate struct passwd *pw; 2237c478bd9Sstevel@tonic-gate struct spwd *spw; 2247c478bd9Sstevel@tonic-gate attrlist *w; 2257c478bd9Sstevel@tonic-gate int res = 0; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate res = nss_getpwnam(name, items, rep, (void **)&pwbuf); 2287c478bd9Sstevel@tonic-gate if (res != PWU_SUCCESS) 2297c478bd9Sstevel@tonic-gate return (res); 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate pw = pwbuf->pwd; 2327c478bd9Sstevel@tonic-gate spw = pwbuf->spwd; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate for (w = items; res == PWU_SUCCESS && w != NULL; w = w->next) { 2357c478bd9Sstevel@tonic-gate switch (w->type) { 2367c478bd9Sstevel@tonic-gate case ATTR_NAME: 2377c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_name)) == NULL) 2387c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 2397c478bd9Sstevel@tonic-gate break; 2407c478bd9Sstevel@tonic-gate case ATTR_COMMENT: 2417c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_comment)) == NULL) 2427c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 2437c478bd9Sstevel@tonic-gate break; 2447c478bd9Sstevel@tonic-gate case ATTR_GECOS: 2457c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_gecos)) == NULL) 2467c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 2477c478bd9Sstevel@tonic-gate break; 2487c478bd9Sstevel@tonic-gate case ATTR_HOMEDIR: 2497c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_dir)) == NULL) 2507c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 2517c478bd9Sstevel@tonic-gate break; 2527c478bd9Sstevel@tonic-gate case ATTR_SHELL: 2537c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_shell)) == NULL) 2547c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 2557c478bd9Sstevel@tonic-gate break; 2567c478bd9Sstevel@tonic-gate /* 2577c478bd9Sstevel@tonic-gate * Nothing special needs to be done for 2587c478bd9Sstevel@tonic-gate * server policy 2597c478bd9Sstevel@tonic-gate */ 2607c478bd9Sstevel@tonic-gate case ATTR_PASSWD: 2617c478bd9Sstevel@tonic-gate case ATTR_PASSWD_SERVER_POLICY: 2627c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(spw->sp_pwdp)) == NULL) 2637c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 2647c478bd9Sstevel@tonic-gate break; 2657c478bd9Sstevel@tonic-gate case ATTR_AGE: 2667c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pw->pw_age)) == NULL) 2677c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 2687c478bd9Sstevel@tonic-gate break; 2697c478bd9Sstevel@tonic-gate case ATTR_REP_NAME: 2707c478bd9Sstevel@tonic-gate if ((w->data.val_s = strdup(pwbuf->rep_name)) == NULL) 2717c478bd9Sstevel@tonic-gate res = PWU_NOMEM; 2727c478bd9Sstevel@tonic-gate break; 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate /* integer values */ 2757c478bd9Sstevel@tonic-gate case ATTR_UID: 2767c478bd9Sstevel@tonic-gate w->data.val_i = pw->pw_uid; 2777c478bd9Sstevel@tonic-gate break; 2787c478bd9Sstevel@tonic-gate case ATTR_GID: 2797c478bd9Sstevel@tonic-gate w->data.val_i = pw->pw_gid; 2807c478bd9Sstevel@tonic-gate break; 2817c478bd9Sstevel@tonic-gate case ATTR_LSTCHG: 2827c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_lstchg; 2837c478bd9Sstevel@tonic-gate break; 2847c478bd9Sstevel@tonic-gate case ATTR_MIN: 2857c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_min; 2867c478bd9Sstevel@tonic-gate break; 2877c478bd9Sstevel@tonic-gate case ATTR_MAX: 2887c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_max; 2897c478bd9Sstevel@tonic-gate break; 2907c478bd9Sstevel@tonic-gate case ATTR_WARN: 2917c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_warn; 2927c478bd9Sstevel@tonic-gate break; 2937c478bd9Sstevel@tonic-gate case ATTR_INACT: 2947c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_inact; 2957c478bd9Sstevel@tonic-gate break; 2967c478bd9Sstevel@tonic-gate case ATTR_EXPIRE: 2977c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_expire; 2987c478bd9Sstevel@tonic-gate break; 2997c478bd9Sstevel@tonic-gate case ATTR_FLAG: 3007c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_flag; 3017c478bd9Sstevel@tonic-gate break; 3027c478bd9Sstevel@tonic-gate case ATTR_FAILED_LOGINS: 3037c478bd9Sstevel@tonic-gate w->data.val_i = spw->sp_flag & FAILCOUNT_MASK; 3047c478bd9Sstevel@tonic-gate break; 3057c478bd9Sstevel@tonic-gate default: 3067c478bd9Sstevel@tonic-gate break; 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate 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 3167c478bd9Sstevel@tonic-gate return (res); 3177c478bd9Sstevel@tonic-gate } 318