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