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 5f48205beScasper * Common Development and Distribution License (the "License"). 6f48205beScasper * 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*66e150d7SJohn Sonnenschein * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <sys/types.h> 287c478bd9Sstevel@tonic-gate #include <nsswitch.h> 297c478bd9Sstevel@tonic-gate #include <stdlib.h> 307c478bd9Sstevel@tonic-gate #include <stdio.h> 317c478bd9Sstevel@tonic-gate #include <string.h> 327c478bd9Sstevel@tonic-gate #include <syslog.h> 337c478bd9Sstevel@tonic-gate #include <stdlib.h> 347c478bd9Sstevel@tonic-gate #include <unistd.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include "ns_sldap.h" 377c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h> 387c478bd9Sstevel@tonic-gate #include <nsswitch.h> 397c478bd9Sstevel@tonic-gate #include <pwd.h> 407c478bd9Sstevel@tonic-gate #include <shadow.h> 417c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h> 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #include "passwdutil.h" 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate static struct passwd *nisplus_getpw_from_master(const char *, char *); 467c478bd9Sstevel@tonic-gate static struct spwd *nisplus_getsp_from_master(const char *, char *); 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* 497c478bd9Sstevel@tonic-gate * name_to_int(rep) 507c478bd9Sstevel@tonic-gate * 517c478bd9Sstevel@tonic-gate * Translate the repository to a bitmask. 527c478bd9Sstevel@tonic-gate * if we don't recognise the repository name, we return REP_ERANGE 537c478bd9Sstevel@tonic-gate */ 547c478bd9Sstevel@tonic-gate int 557c478bd9Sstevel@tonic-gate name_to_int(char *rep_name) 567c478bd9Sstevel@tonic-gate { 577c478bd9Sstevel@tonic-gate int result = REP_ERANGE; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate if (strcmp(rep_name, "files") == 0) 607c478bd9Sstevel@tonic-gate result = REP_FILES; 617c478bd9Sstevel@tonic-gate else if (strcmp(rep_name, "nis") == 0) 627c478bd9Sstevel@tonic-gate result = REP_NIS; 637c478bd9Sstevel@tonic-gate else if (strcmp(rep_name, "nisplus") == 0) 647c478bd9Sstevel@tonic-gate result = REP_NISPLUS; 657c478bd9Sstevel@tonic-gate else if (strcmp(rep_name, "ldap") == 0) 667c478bd9Sstevel@tonic-gate result = REP_LDAP; 677c478bd9Sstevel@tonic-gate else if (strcmp(rep_name, "compat") == 0) { 687c478bd9Sstevel@tonic-gate struct __nsw_switchconfig *cfg; 697c478bd9Sstevel@tonic-gate enum __nsw_parse_err pserr; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate cfg = __nsw_getconfig("passwd_compat", &pserr); 727c478bd9Sstevel@tonic-gate if (cfg == NULL) { 737c478bd9Sstevel@tonic-gate result = REP_FILES | REP_NIS; 747c478bd9Sstevel@tonic-gate } else { 757c478bd9Sstevel@tonic-gate if (strcmp(cfg->lookups->service_name, "nisplus") == 0) 767c478bd9Sstevel@tonic-gate result = REP_FILES | REP_NISPLUS; 777c478bd9Sstevel@tonic-gate else if (strcmp(cfg->lookups->service_name, "ldap") == 787c478bd9Sstevel@tonic-gate 0) 797c478bd9Sstevel@tonic-gate result = REP_FILES | REP_LDAP; 807c478bd9Sstevel@tonic-gate else 817c478bd9Sstevel@tonic-gate result = REP_ERANGE; 827c478bd9Sstevel@tonic-gate __nsw_freeconfig(cfg); 837c478bd9Sstevel@tonic-gate } 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate return (result); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate /* 907c478bd9Sstevel@tonic-gate * Figure out which repository we use in compat mode. 917c478bd9Sstevel@tonic-gate */ 927c478bd9Sstevel@tonic-gate int 937c478bd9Sstevel@tonic-gate get_compat_mode(void) 947c478bd9Sstevel@tonic-gate { 957c478bd9Sstevel@tonic-gate struct __nsw_switchconfig *cfg; 967c478bd9Sstevel@tonic-gate enum __nsw_parse_err pserr; 977c478bd9Sstevel@tonic-gate int result = REP_COMPAT_NIS; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate if ((cfg = __nsw_getconfig("passwd_compat", &pserr)) != NULL) { 1007c478bd9Sstevel@tonic-gate if (strcmp(cfg->lookups->service_name, "nisplus") == 0) 1017c478bd9Sstevel@tonic-gate result = REP_COMPAT_NISPLUS; 1027c478bd9Sstevel@tonic-gate else if (strcmp(cfg->lookups->service_name, "ldap") == 0) 1037c478bd9Sstevel@tonic-gate result = REP_COMPAT_LDAP; 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate __nsw_freeconfig(cfg); 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate return (result); 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate /* 1117c478bd9Sstevel@tonic-gate * get_ns(rep, accesstype) 1127c478bd9Sstevel@tonic-gate * 1137c478bd9Sstevel@tonic-gate * returns a bitmask of repositories to use based on either 1147c478bd9Sstevel@tonic-gate * 1. the repository that is given as argument 1157c478bd9Sstevel@tonic-gate * 2. the nsswitch.conf file 1167c478bd9Sstevel@tonic-gate * 3. the type of access requested 1177c478bd9Sstevel@tonic-gate * 1187c478bd9Sstevel@tonic-gate * "accesstype" indicates whether we are reading from or writing to the 1197c478bd9Sstevel@tonic-gate * repository. We need to know this since "compat" will translate into 1207c478bd9Sstevel@tonic-gate * REP_NSS (the nss-switch) for READ access (needed to decode 1217c478bd9Sstevel@tonic-gate * the black-magic '+' entries) but it translates into a bitmask 1227c478bd9Sstevel@tonic-gate * on WRITE access. 1237c478bd9Sstevel@tonic-gate * 1247c478bd9Sstevel@tonic-gate * If we detect read-access in compat mode, we augment the result 1257c478bd9Sstevel@tonic-gate * with one of REP_COMPAT_{NIS,NISPLUS,LDAP}. We need this in order to 1267c478bd9Sstevel@tonic-gate * implement ATTR_REP_NAME in nss_getpwnam. 1277c478bd9Sstevel@tonic-gate * 1287c478bd9Sstevel@tonic-gate * A return value of REP_NOREP indicates an error. 1297c478bd9Sstevel@tonic-gate */ 1307c478bd9Sstevel@tonic-gate int 1317c478bd9Sstevel@tonic-gate get_ns(pwu_repository_t *rep, int accesstype) 1327c478bd9Sstevel@tonic-gate { 1337c478bd9Sstevel@tonic-gate struct __nsw_switchconfig *conf = NULL; 1347c478bd9Sstevel@tonic-gate enum __nsw_parse_err pserr; 1357c478bd9Sstevel@tonic-gate struct __nsw_lookup *lkp; 1367c478bd9Sstevel@tonic-gate struct __nsw_lookup *lkp2; 1377c478bd9Sstevel@tonic-gate int result = REP_NOREP; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate if (rep != PWU_DEFAULT_REP) { 1407c478bd9Sstevel@tonic-gate result = name_to_int(rep->type); 1417c478bd9Sstevel@tonic-gate return (result); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate conf = __nsw_getconfig("passwd", &pserr); 1457c478bd9Sstevel@tonic-gate if (conf == NULL) { 1467c478bd9Sstevel@tonic-gate /* 1477c478bd9Sstevel@tonic-gate * No config found. The user didn't supply a repository, 1487c478bd9Sstevel@tonic-gate * so we try to change the password in the default 1497c478bd9Sstevel@tonic-gate * repositories (files and nis) even though we cannot 1507c478bd9Sstevel@tonic-gate * find the name service switch entry. (Backward compat) 1517c478bd9Sstevel@tonic-gate */ 1527c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "passwdutil.so: nameservice switch entry for " 1537c478bd9Sstevel@tonic-gate "passwd not found."); 1547c478bd9Sstevel@tonic-gate result = REP_FILES | REP_NIS; 1557c478bd9Sstevel@tonic-gate return (result); 1567c478bd9Sstevel@tonic-gate } 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate lkp = conf->lookups; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate /* 1617c478bd9Sstevel@tonic-gate * Supported nsswitch.conf can have a maximum of 2 repositories. 1627c478bd9Sstevel@tonic-gate * If we encounter an unsupported nsswitch.conf, we return REP_NSS 1637c478bd9Sstevel@tonic-gate * to fall back to the nsswitch backend. 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate if (conf->num_lookups == 1) { 1667c478bd9Sstevel@tonic-gate /* files or compat */ 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (strcmp(lkp->service_name, "files") == 0) { 1697c478bd9Sstevel@tonic-gate result = name_to_int(lkp->service_name); 1707c478bd9Sstevel@tonic-gate } else if (strcmp(lkp->service_name, "compat") == 0) { 1717c478bd9Sstevel@tonic-gate if (accesstype == PWU_READ) 1727c478bd9Sstevel@tonic-gate result = REP_NSS | get_compat_mode(); 1737c478bd9Sstevel@tonic-gate else 1747c478bd9Sstevel@tonic-gate result = name_to_int(lkp->service_name); 1757c478bd9Sstevel@tonic-gate } else 1767c478bd9Sstevel@tonic-gate result = REP_NSS; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate } else if (conf->num_lookups == 2) { 1797c478bd9Sstevel@tonic-gate lkp2 = lkp->next; 1807c478bd9Sstevel@tonic-gate if (strcmp(lkp->service_name, "files") == 0) { 1817c478bd9Sstevel@tonic-gate result = REP_FILES; 1827c478bd9Sstevel@tonic-gate if (strcmp(lkp2->service_name, "ldap") == 0) 1837c478bd9Sstevel@tonic-gate result |= REP_LDAP; 1847c478bd9Sstevel@tonic-gate else if (strcmp(lkp2->service_name, "nis") == 0) 1857c478bd9Sstevel@tonic-gate result |= REP_NIS; 1867c478bd9Sstevel@tonic-gate else if (strcmp(lkp2->service_name, "nisplus") == 0) 1877c478bd9Sstevel@tonic-gate result |= REP_NISPLUS; 1887c478bd9Sstevel@tonic-gate else 1897c478bd9Sstevel@tonic-gate result = REP_NSS; 1907c478bd9Sstevel@tonic-gate } else { 1917c478bd9Sstevel@tonic-gate result = REP_NSS; 1927c478bd9Sstevel@tonic-gate } 1937c478bd9Sstevel@tonic-gate } else { 1947c478bd9Sstevel@tonic-gate result = REP_NSS; 1957c478bd9Sstevel@tonic-gate } 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate __nsw_freeconfig(conf); 1987c478bd9Sstevel@tonic-gate return (result); 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate static void 2027c478bd9Sstevel@tonic-gate nss_ldap_passwd(p) 2037c478bd9Sstevel@tonic-gate nss_db_params_t *p; 2047c478bd9Sstevel@tonic-gate { 2057c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_PASSWD; 2067c478bd9Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2077c478bd9Sstevel@tonic-gate p->default_config = "ldap"; 2087c478bd9Sstevel@tonic-gate } 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate static void 2117c478bd9Sstevel@tonic-gate nss_ldap_shadow(p) 2127c478bd9Sstevel@tonic-gate nss_db_params_t *p; 2137c478bd9Sstevel@tonic-gate { 2147c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_SHADOW; 2157c478bd9Sstevel@tonic-gate p->config_name = NSS_DBNAM_PASSWD; /* Use config for "passwd" */ 2167c478bd9Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2177c478bd9Sstevel@tonic-gate p->default_config = "ldap"; 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate #ifdef PAM_NIS 2227c478bd9Sstevel@tonic-gate static void 2237c478bd9Sstevel@tonic-gate nss_nis_passwd(p) 2247c478bd9Sstevel@tonic-gate nss_db_params_t *p; 2257c478bd9Sstevel@tonic-gate { 2267c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_PASSWD; 2277c478bd9Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2287c478bd9Sstevel@tonic-gate p->default_config = "nis"; 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate static void 2327c478bd9Sstevel@tonic-gate nss_nis_shadow(p) 2337c478bd9Sstevel@tonic-gate nss_db_params_t *p; 2347c478bd9Sstevel@tonic-gate { 2357c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_SHADOW; 2367c478bd9Sstevel@tonic-gate p->config_name = NSS_DBNAM_PASSWD; /* Use config for "passwd" */ 2377c478bd9Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2387c478bd9Sstevel@tonic-gate p->default_config = "nis"; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate #endif /* PAM_NIS */ 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate static void 2447c478bd9Sstevel@tonic-gate nss_nisplus_passwd(p) 2457c478bd9Sstevel@tonic-gate nss_db_params_t *p; 2467c478bd9Sstevel@tonic-gate { 2477c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_PASSWD; 2487c478bd9Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2497c478bd9Sstevel@tonic-gate p->default_config = "nisplus"; 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate static void 2537c478bd9Sstevel@tonic-gate nss_nisplus_shadow(p) 2547c478bd9Sstevel@tonic-gate nss_db_params_t *p; 2557c478bd9Sstevel@tonic-gate { 2567c478bd9Sstevel@tonic-gate p->name = NSS_DBNAM_SHADOW; 2577c478bd9Sstevel@tonic-gate p->config_name = NSS_DBNAM_PASSWD; /* Use config for "passwd" */ 2587c478bd9Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2597c478bd9Sstevel@tonic-gate p->default_config = "nisplus"; 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate static char * 2647c478bd9Sstevel@tonic-gate gettok(nextpp) 2657c478bd9Sstevel@tonic-gate char **nextpp; 2667c478bd9Sstevel@tonic-gate { 2677c478bd9Sstevel@tonic-gate char *p = *nextpp; 2687c478bd9Sstevel@tonic-gate char *q = p; 2697c478bd9Sstevel@tonic-gate char c; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate if (p == 0) { 2727c478bd9Sstevel@tonic-gate return (0); 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate while ((c = *q) != '\0' && c != ':') { 2757c478bd9Sstevel@tonic-gate q++; 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate if (c == '\0') { 2787c478bd9Sstevel@tonic-gate *nextpp = 0; 2797c478bd9Sstevel@tonic-gate } else { 2807c478bd9Sstevel@tonic-gate *q++ = '\0'; 2817c478bd9Sstevel@tonic-gate *nextpp = q; 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate return (p); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 2887c478bd9Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 2897c478bd9Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 2907c478bd9Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 2917c478bd9Sstevel@tonic-gate */ 2927c478bd9Sstevel@tonic-gate static int 2937c478bd9Sstevel@tonic-gate str2passwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen) 2947c478bd9Sstevel@tonic-gate { 2957c478bd9Sstevel@tonic-gate struct passwd *passwd = (struct passwd *)ent; 2967c478bd9Sstevel@tonic-gate char *p, *next; 2977c478bd9Sstevel@tonic-gate int black_magic; /* "+" or "-" entry */ 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate if (lenstr + 1 > buflen) { 3007c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate /* 3037c478bd9Sstevel@tonic-gate * We copy the input string into the output buffer and 3047c478bd9Sstevel@tonic-gate * operate on it in place. 3057c478bd9Sstevel@tonic-gate */ 3067c478bd9Sstevel@tonic-gate (void) memcpy(buffer, instr, lenstr); 3077c478bd9Sstevel@tonic-gate buffer[lenstr] = '\0'; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate next = buffer; 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate passwd->pw_name = p = gettok(&next); /* username */ 3127c478bd9Sstevel@tonic-gate if (*p == '\0') { 3137c478bd9Sstevel@tonic-gate /* Empty username; not allowed */ 3147c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate black_magic = (*p == '+' || *p == '-'); 3177c478bd9Sstevel@tonic-gate if (black_magic) { 3187c478bd9Sstevel@tonic-gate passwd->pw_uid = UID_NOBODY; 3197c478bd9Sstevel@tonic-gate passwd->pw_gid = GID_NOBODY; 3207c478bd9Sstevel@tonic-gate /* 3217c478bd9Sstevel@tonic-gate * pwconv tests pw_passwd and pw_age == NULL 3227c478bd9Sstevel@tonic-gate */ 3237c478bd9Sstevel@tonic-gate passwd->pw_passwd = ""; 3247c478bd9Sstevel@tonic-gate passwd->pw_age = ""; 3257c478bd9Sstevel@tonic-gate /* 3267c478bd9Sstevel@tonic-gate * the rest of the passwd entry is "optional" 3277c478bd9Sstevel@tonic-gate */ 3287c478bd9Sstevel@tonic-gate passwd->pw_comment = ""; 3297c478bd9Sstevel@tonic-gate passwd->pw_gecos = ""; 3307c478bd9Sstevel@tonic-gate passwd->pw_dir = ""; 3317c478bd9Sstevel@tonic-gate passwd->pw_shell = ""; 3327c478bd9Sstevel@tonic-gate } 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate passwd->pw_passwd = p = gettok(&next); /* password */ 3357c478bd9Sstevel@tonic-gate if (p == 0) { 3367c478bd9Sstevel@tonic-gate if (black_magic) 3377c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 3387c478bd9Sstevel@tonic-gate else 3397c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3407c478bd9Sstevel@tonic-gate } 3417c478bd9Sstevel@tonic-gate for (; *p != '\0'; p++) { /* age */ 3427c478bd9Sstevel@tonic-gate if (*p == ',') { 3437c478bd9Sstevel@tonic-gate *p++ = '\0'; 3447c478bd9Sstevel@tonic-gate break; 3457c478bd9Sstevel@tonic-gate } 3467c478bd9Sstevel@tonic-gate } 3477c478bd9Sstevel@tonic-gate passwd->pw_age = p; 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate p = next; /* uid */ 3507c478bd9Sstevel@tonic-gate if (p == 0 || *p == '\0') { 3517c478bd9Sstevel@tonic-gate if (black_magic) 3527c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 3537c478bd9Sstevel@tonic-gate else 3547c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate if (!black_magic) { 3577c478bd9Sstevel@tonic-gate passwd->pw_uid = strtol(p, &next, 10); 3587c478bd9Sstevel@tonic-gate if (next == p) { 3597c478bd9Sstevel@tonic-gate /* uid field should be nonempty */ 3607c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate /* 3637c478bd9Sstevel@tonic-gate * The old code (in 2.0 thru 2.5) would check 3647c478bd9Sstevel@tonic-gate * for the uid being negative, or being greater 3657c478bd9Sstevel@tonic-gate * than 60001 (the rfs limit). If it met either of 3667c478bd9Sstevel@tonic-gate * these conditions, the uid was translated to 60001. 3677c478bd9Sstevel@tonic-gate * 368f48205beScasper * Now we just check for ephemeral uids; anything else 3697c478bd9Sstevel@tonic-gate * is administrative policy 3707c478bd9Sstevel@tonic-gate */ 371f48205beScasper if (passwd->pw_uid > MAXUID) 3727c478bd9Sstevel@tonic-gate passwd->pw_uid = UID_NOBODY; 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate if (*next++ != ':') { 3757c478bd9Sstevel@tonic-gate if (black_magic) 3767c478bd9Sstevel@tonic-gate p = gettok(&next); 3777c478bd9Sstevel@tonic-gate else 3787c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3797c478bd9Sstevel@tonic-gate } 3807c478bd9Sstevel@tonic-gate p = next; /* gid */ 3817c478bd9Sstevel@tonic-gate if (p == 0 || *p == '\0') { 3827c478bd9Sstevel@tonic-gate if (black_magic) 3837c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 3847c478bd9Sstevel@tonic-gate else 3857c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate if (!black_magic) { 3887c478bd9Sstevel@tonic-gate passwd->pw_gid = strtol(p, &next, 10); 3897c478bd9Sstevel@tonic-gate if (next == p) { 3907c478bd9Sstevel@tonic-gate /* gid field should be nonempty */ 3917c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate /* 3947c478bd9Sstevel@tonic-gate * gid should be non-negative; anything else 3957c478bd9Sstevel@tonic-gate * is administrative policy. 3967c478bd9Sstevel@tonic-gate */ 397f48205beScasper if (passwd->pw_gid > MAXUID) 3987c478bd9Sstevel@tonic-gate passwd->pw_gid = GID_NOBODY; 3997c478bd9Sstevel@tonic-gate } 4007c478bd9Sstevel@tonic-gate if (*next++ != ':') { 4017c478bd9Sstevel@tonic-gate if (black_magic) 4027c478bd9Sstevel@tonic-gate p = gettok(&next); 4037c478bd9Sstevel@tonic-gate else 4047c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate passwd->pw_gecos = passwd->pw_comment = p = gettok(&next); 4087c478bd9Sstevel@tonic-gate if (p == 0) { 4097c478bd9Sstevel@tonic-gate if (black_magic) 4107c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4117c478bd9Sstevel@tonic-gate else 4127c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate passwd->pw_dir = p = gettok(&next); 4167c478bd9Sstevel@tonic-gate if (p == 0) { 4177c478bd9Sstevel@tonic-gate if (black_magic) 4187c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4197c478bd9Sstevel@tonic-gate else 4207c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate passwd->pw_shell = p = gettok(&next); 4247c478bd9Sstevel@tonic-gate if (p == 0) { 4257c478bd9Sstevel@tonic-gate if (black_magic) 4267c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4277c478bd9Sstevel@tonic-gate else 4287c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate /* Better not be any more fields... */ 4327c478bd9Sstevel@tonic-gate if (next == 0) { 4337c478bd9Sstevel@tonic-gate /* Successfully parsed and stored */ 4347c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate typedef const char *constp; 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate /* 4427c478bd9Sstevel@tonic-gate * Return value 1 means success and more input, 0 means error or no more 4437c478bd9Sstevel@tonic-gate */ 4447c478bd9Sstevel@tonic-gate static int 4457c478bd9Sstevel@tonic-gate getfield(nextp, limit, uns, valp) 4467c478bd9Sstevel@tonic-gate constp *nextp; 4477c478bd9Sstevel@tonic-gate constp limit; 4487c478bd9Sstevel@tonic-gate int uns; 4497c478bd9Sstevel@tonic-gate void *valp; 4507c478bd9Sstevel@tonic-gate { 4517c478bd9Sstevel@tonic-gate constp p = *nextp; 4527c478bd9Sstevel@tonic-gate char *endfield; 4537c478bd9Sstevel@tonic-gate char numbuf[12]; /* Holds -2^31 and trailing ':' */ 4547c478bd9Sstevel@tonic-gate int len; 4557c478bd9Sstevel@tonic-gate long x; 4567c478bd9Sstevel@tonic-gate unsigned long ux; 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate if (p == 0 || p >= limit) { 4597c478bd9Sstevel@tonic-gate return (0); 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate if (*p == ':') { 4627c478bd9Sstevel@tonic-gate p++; 4637c478bd9Sstevel@tonic-gate *nextp = p; 4647c478bd9Sstevel@tonic-gate return (p < limit); 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate if ((len = limit - p) > sizeof (numbuf) - 1) { 4677c478bd9Sstevel@tonic-gate len = sizeof (numbuf) - 1; 4687c478bd9Sstevel@tonic-gate } 4697c478bd9Sstevel@tonic-gate /* 4707c478bd9Sstevel@tonic-gate * We want to use strtol() and we have a readonly non-zero-terminated 4717c478bd9Sstevel@tonic-gate * string, so first we copy and terminate the interesting bit. 4727c478bd9Sstevel@tonic-gate * Ugh. (It's convenient to terminate with a colon rather than \0). 4737c478bd9Sstevel@tonic-gate */ 4747c478bd9Sstevel@tonic-gate if ((endfield = memccpy(numbuf, p, ':', len)) == 0) { 4757c478bd9Sstevel@tonic-gate if (len != limit - p) { 4767c478bd9Sstevel@tonic-gate /* Error -- field is too big to be a legit number */ 4777c478bd9Sstevel@tonic-gate return (0); 4787c478bd9Sstevel@tonic-gate } 4797c478bd9Sstevel@tonic-gate numbuf[len] = ':'; 4807c478bd9Sstevel@tonic-gate p = limit; 4817c478bd9Sstevel@tonic-gate } else { 4827c478bd9Sstevel@tonic-gate p += (endfield - numbuf); 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate if (uns) { 4857c478bd9Sstevel@tonic-gate ux = strtoul(numbuf, &endfield, 10); 4867c478bd9Sstevel@tonic-gate if (*endfield != ':') { 4877c478bd9Sstevel@tonic-gate /* Error -- expected <integer><colon> */ 4887c478bd9Sstevel@tonic-gate return (0); 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate *((unsigned int *)valp) = (unsigned int)ux; 4917c478bd9Sstevel@tonic-gate } else { 4927c478bd9Sstevel@tonic-gate x = strtol(numbuf, &endfield, 10); 4937c478bd9Sstevel@tonic-gate if (*endfield != ':') { 4947c478bd9Sstevel@tonic-gate /* Error -- expected <integer><colon> */ 4957c478bd9Sstevel@tonic-gate return (0); 4967c478bd9Sstevel@tonic-gate } 4977c478bd9Sstevel@tonic-gate *((int *)valp) = (int)x; 4987c478bd9Sstevel@tonic-gate } 4997c478bd9Sstevel@tonic-gate *nextp = p; 5007c478bd9Sstevel@tonic-gate return (p < limit); 5017c478bd9Sstevel@tonic-gate } 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate /* 5047c478bd9Sstevel@tonic-gate * str2spwd() -- convert a string to a shadow passwd entry. The parser is 5057c478bd9Sstevel@tonic-gate * more liberal than the passwd or group parsers; since it's legitimate 5067c478bd9Sstevel@tonic-gate * for almost all the fields here to be blank, the parser lets one omit 5077c478bd9Sstevel@tonic-gate * any number of blank fields at the end of the entry. The acceptable 5087c478bd9Sstevel@tonic-gate * forms for '+' and '-' entries are the same as those for normal entries. 5097c478bd9Sstevel@tonic-gate * === Is this likely to do more harm than good? 5107c478bd9Sstevel@tonic-gate * 5117c478bd9Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 5127c478bd9Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 5137c478bd9Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 5147c478bd9Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 5157c478bd9Sstevel@tonic-gate */ 5167c478bd9Sstevel@tonic-gate int 5177c478bd9Sstevel@tonic-gate str2spwd(instr, lenstr, ent, buffer, buflen) 5187c478bd9Sstevel@tonic-gate const char *instr; 5197c478bd9Sstevel@tonic-gate int lenstr; 5207c478bd9Sstevel@tonic-gate void *ent; /* really (struct spwd *) */ 5217c478bd9Sstevel@tonic-gate char *buffer; 5227c478bd9Sstevel@tonic-gate int buflen; 5237c478bd9Sstevel@tonic-gate { 5247c478bd9Sstevel@tonic-gate struct spwd *shadow = (struct spwd *)ent; 5257c478bd9Sstevel@tonic-gate const char *p = instr, *limit; 5267c478bd9Sstevel@tonic-gate char *bufp; 5277c478bd9Sstevel@tonic-gate int lencopy, black_magic; 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate limit = p + lenstr; 5307c478bd9Sstevel@tonic-gate if ((p = memchr(instr, ':', lenstr)) == 0 || 5317c478bd9Sstevel@tonic-gate ++p >= limit || 5327c478bd9Sstevel@tonic-gate (p = memchr(p, ':', limit - p)) == 0) { 5337c478bd9Sstevel@tonic-gate lencopy = lenstr; 5347c478bd9Sstevel@tonic-gate p = 0; 5357c478bd9Sstevel@tonic-gate } else { 5367c478bd9Sstevel@tonic-gate lencopy = p - instr; 5377c478bd9Sstevel@tonic-gate p++; 5387c478bd9Sstevel@tonic-gate } 5397c478bd9Sstevel@tonic-gate if (lencopy + 1 > buflen) { 5407c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate (void) memcpy(buffer, instr, lencopy); 5437c478bd9Sstevel@tonic-gate buffer[lencopy] = 0; 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate black_magic = (*instr == '+' || *instr == '-'); 5467c478bd9Sstevel@tonic-gate shadow->sp_namp = bufp = buffer; 5477c478bd9Sstevel@tonic-gate shadow->sp_pwdp = 0; 5487c478bd9Sstevel@tonic-gate shadow->sp_lstchg = -1; 5497c478bd9Sstevel@tonic-gate shadow->sp_min = -1; 5507c478bd9Sstevel@tonic-gate shadow->sp_max = -1; 5517c478bd9Sstevel@tonic-gate shadow->sp_warn = -1; 5527c478bd9Sstevel@tonic-gate shadow->sp_inact = -1; 5537c478bd9Sstevel@tonic-gate shadow->sp_expire = -1; 5547c478bd9Sstevel@tonic-gate shadow->sp_flag = 0; 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate if ((bufp = strchr(bufp, ':')) == 0) { 5577c478bd9Sstevel@tonic-gate if (black_magic) 5587c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5597c478bd9Sstevel@tonic-gate else 5607c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 5617c478bd9Sstevel@tonic-gate } 5627c478bd9Sstevel@tonic-gate *bufp++ = '\0'; 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate shadow->sp_pwdp = bufp; 5657c478bd9Sstevel@tonic-gate if (instr == 0) { 5667c478bd9Sstevel@tonic-gate if ((bufp = strchr(bufp, ':')) == 0) { 5677c478bd9Sstevel@tonic-gate if (black_magic) 5687c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5697c478bd9Sstevel@tonic-gate else 5707c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 5717c478bd9Sstevel@tonic-gate } 5727c478bd9Sstevel@tonic-gate *bufp++ = '\0'; 5737c478bd9Sstevel@tonic-gate p = bufp; 5747c478bd9Sstevel@tonic-gate } /* else p was set when we copied name and passwd into the buffer */ 5757c478bd9Sstevel@tonic-gate 5767c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_lstchg)) 5777c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5787c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_min)) 5797c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5807c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_max)) 5817c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5827c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_warn)) 5837c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5847c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_inact)) 5857c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5867c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_expire)) 5877c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5887c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 1, &shadow->sp_flag)) 5897c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5907c478bd9Sstevel@tonic-gate if (p != limit) { 5917c478bd9Sstevel@tonic-gate /* Syntax error -- garbage at end of line */ 5927c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5957c478bd9Sstevel@tonic-gate } 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *buffer; 5987c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root); 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate #define GETBUF() \ 6017c478bd9Sstevel@tonic-gate NSS_XbyY_ALLOC(&buffer, sizeof (struct passwd), NSS_BUFLEN_PASSWD) 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate #pragma fini(endutilpwent) 6047c478bd9Sstevel@tonic-gate 6057c478bd9Sstevel@tonic-gate static void 6067c478bd9Sstevel@tonic-gate endutilpwent(void) 6077c478bd9Sstevel@tonic-gate { 6087c478bd9Sstevel@tonic-gate NSS_XbyY_FREE(&buffer); 6097c478bd9Sstevel@tonic-gate nss_delete(&db_root); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate struct passwd * 6137c478bd9Sstevel@tonic-gate getpwnam_from(const char *name, pwu_repository_t *rep, int reptype) 6147c478bd9Sstevel@tonic-gate { 6157c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *b = GETBUF(); 6167c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg; 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate if (b == 0) 6197c478bd9Sstevel@tonic-gate return (0); 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd); 6227c478bd9Sstevel@tonic-gate arg.key.name = name; 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate switch (reptype) { 6257c478bd9Sstevel@tonic-gate case REP_LDAP: 6267c478bd9Sstevel@tonic-gate (void) nss_search(&db_root, nss_ldap_passwd, 6277c478bd9Sstevel@tonic-gate NSS_DBOP_PASSWD_BYNAME, &arg); 6287c478bd9Sstevel@tonic-gate break; 6297c478bd9Sstevel@tonic-gate case REP_NISPLUS: 6307c478bd9Sstevel@tonic-gate if (rep && rep->scope) 6317c478bd9Sstevel@tonic-gate return (nisplus_getpw_from_master(name, rep->scope)); 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate (void) nss_search(&db_root, nss_nisplus_passwd, 6347c478bd9Sstevel@tonic-gate NSS_DBOP_PASSWD_BYNAME, &arg); 6357c478bd9Sstevel@tonic-gate break; 6367c478bd9Sstevel@tonic-gate #ifdef PAM_NIS 6377c478bd9Sstevel@tonic-gate case REP_NIS: 6387c478bd9Sstevel@tonic-gate (void) nss_search(&db_root, nss_nis_passwd, 6397c478bd9Sstevel@tonic-gate NSS_DBOP_PASSWD_BYNAME, &arg); 6407c478bd9Sstevel@tonic-gate break; 6417c478bd9Sstevel@tonic-gate #endif 6427c478bd9Sstevel@tonic-gate default: 6437c478bd9Sstevel@tonic-gate return (NULL); 6447c478bd9Sstevel@tonic-gate } 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate return (struct passwd *)NSS_XbyY_FINI(&arg); 6477c478bd9Sstevel@tonic-gate } 6487c478bd9Sstevel@tonic-gate 6497c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 6507c478bd9Sstevel@tonic-gate struct passwd * 6517c478bd9Sstevel@tonic-gate getpwuid_from(uid_t uid, pwu_repository_t *rep, int reptype) 6527c478bd9Sstevel@tonic-gate { 6537c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *b = GETBUF(); 6547c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg; 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate if (b == 0) 6577c478bd9Sstevel@tonic-gate return (0); 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd); 6607c478bd9Sstevel@tonic-gate arg.key.uid = uid; 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate switch (reptype) { 6637c478bd9Sstevel@tonic-gate case REP_LDAP: 6647c478bd9Sstevel@tonic-gate (void) nss_search(&db_root, nss_ldap_passwd, 6657c478bd9Sstevel@tonic-gate NSS_DBOP_PASSWD_BYUID, &arg); 6667c478bd9Sstevel@tonic-gate break; 6677c478bd9Sstevel@tonic-gate case REP_NISPLUS: 6687c478bd9Sstevel@tonic-gate (void) nss_search(&db_root, nss_nisplus_passwd, 6697c478bd9Sstevel@tonic-gate NSS_DBOP_PASSWD_BYUID, &arg); 6707c478bd9Sstevel@tonic-gate break; 6717c478bd9Sstevel@tonic-gate #ifdef PAM_NIS 6727c478bd9Sstevel@tonic-gate case REP_NIS: 6737c478bd9Sstevel@tonic-gate (void) nss_search(&db_root, nss_nis_passwd, 6747c478bd9Sstevel@tonic-gate NSS_DBOP_PASSWD_BYUID, &arg); 6757c478bd9Sstevel@tonic-gate break; 6767c478bd9Sstevel@tonic-gate #endif 6777c478bd9Sstevel@tonic-gate default: 6787c478bd9Sstevel@tonic-gate return (NULL); 6797c478bd9Sstevel@tonic-gate } 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate return (struct passwd *)NSS_XbyY_FINI(&arg); 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate static nss_XbyY_buf_t *spbuf; 6857c478bd9Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(spdb_root); 6867c478bd9Sstevel@tonic-gate 6877c478bd9Sstevel@tonic-gate #define GETSPBUF() \ 6887c478bd9Sstevel@tonic-gate NSS_XbyY_ALLOC(&spbuf, sizeof (struct spwd), NSS_BUFLEN_SHADOW) 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate #pragma fini(endutilspent) 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate static void 6937c478bd9Sstevel@tonic-gate endutilspent(void) 6947c478bd9Sstevel@tonic-gate { 6957c478bd9Sstevel@tonic-gate NSS_XbyY_FREE(&spbuf); 6967c478bd9Sstevel@tonic-gate nss_delete(&spdb_root); 6977c478bd9Sstevel@tonic-gate } 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate struct spwd * 7007c478bd9Sstevel@tonic-gate getspnam_from(const char *name, pwu_repository_t *rep, int reptype) 7017c478bd9Sstevel@tonic-gate { 7027c478bd9Sstevel@tonic-gate nss_XbyY_buf_t *b = GETSPBUF(); 7037c478bd9Sstevel@tonic-gate nss_XbyY_args_t arg; 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate if (b == 0) 7067c478bd9Sstevel@tonic-gate return (0); 7077c478bd9Sstevel@tonic-gate 7087c478bd9Sstevel@tonic-gate NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2spwd); 7097c478bd9Sstevel@tonic-gate arg.key.name = name; 7107c478bd9Sstevel@tonic-gate switch (reptype) { 7117c478bd9Sstevel@tonic-gate case REP_LDAP: 7127c478bd9Sstevel@tonic-gate (void) nss_search(&spdb_root, nss_ldap_shadow, 7137c478bd9Sstevel@tonic-gate NSS_DBOP_SHADOW_BYNAME, &arg); 7147c478bd9Sstevel@tonic-gate break; 7157c478bd9Sstevel@tonic-gate case REP_NISPLUS: 7167c478bd9Sstevel@tonic-gate if (rep && rep->scope) 7177c478bd9Sstevel@tonic-gate return (nisplus_getsp_from_master(name, rep->scope)); 7187c478bd9Sstevel@tonic-gate 7197c478bd9Sstevel@tonic-gate (void) nss_search(&spdb_root, nss_nisplus_shadow, 7207c478bd9Sstevel@tonic-gate NSS_DBOP_SHADOW_BYNAME, &arg); 7217c478bd9Sstevel@tonic-gate break; 7227c478bd9Sstevel@tonic-gate #ifdef PAM_NIS 7237c478bd9Sstevel@tonic-gate case REP_NIS: 7247c478bd9Sstevel@tonic-gate (void) nss_search(&spdb_root, nss_nis_shadow, 7257c478bd9Sstevel@tonic-gate NSS_DBOP_SHADOW_BYNAME, &arg); 7267c478bd9Sstevel@tonic-gate break; 7277c478bd9Sstevel@tonic-gate #endif 7287c478bd9Sstevel@tonic-gate default: 7297c478bd9Sstevel@tonic-gate return (NULL); 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate return (struct spwd *)NSS_XbyY_FINI(&arg); 7327c478bd9Sstevel@tonic-gate } 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate 7357c478bd9Sstevel@tonic-gate static nis_result * 7367c478bd9Sstevel@tonic-gate nisplus_match(const char *name, char *domain, char *buf, int len) 7377c478bd9Sstevel@tonic-gate { 7387c478bd9Sstevel@tonic-gate int n; 7397c478bd9Sstevel@tonic-gate int flags; 7407c478bd9Sstevel@tonic-gate nis_result *res; 7417c478bd9Sstevel@tonic-gate nis_object *object; 7427c478bd9Sstevel@tonic-gate 7437c478bd9Sstevel@tonic-gate n = snprintf(buf, len, "[name=%s],passwd.org_dir.%s", name, domain); 7447c478bd9Sstevel@tonic-gate if (n >= len) { 7457c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "nisplus_match: name too long"); 7467c478bd9Sstevel@tonic-gate return (NULL); 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate if (buf[n-1] != '.') { 7497c478bd9Sstevel@tonic-gate if (n == len-1) { 7507c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "nisplus_match: name too long"); 7517c478bd9Sstevel@tonic-gate return (NULL); 7527c478bd9Sstevel@tonic-gate } 7537c478bd9Sstevel@tonic-gate buf[n++] = '.'; 7547c478bd9Sstevel@tonic-gate buf[n] = '\0'; 7557c478bd9Sstevel@tonic-gate } 7567c478bd9Sstevel@tonic-gate 7577c478bd9Sstevel@tonic-gate flags = USE_DGRAM | FOLLOW_LINKS | FOLLOW_PATH | MASTER_ONLY; 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate res = nis_list(buf, flags, NULL, NULL); 7607c478bd9Sstevel@tonic-gate 7617c478bd9Sstevel@tonic-gate if (res == NULL) { 7627c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "nisplus_match: nis_list returned NULL"); 7637c478bd9Sstevel@tonic-gate return (NULL); 7647c478bd9Sstevel@tonic-gate } 7657c478bd9Sstevel@tonic-gate 7667c478bd9Sstevel@tonic-gate if (NIS_RES_STATUS(res) != NIS_SUCCESS || NIS_RES_NUMOBJ(res) != 1) { 7677c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "nisplus_match: match failed: %s", 7687c478bd9Sstevel@tonic-gate nis_sperrno(NIS_RES_STATUS(res))); 7697c478bd9Sstevel@tonic-gate nis_freeresult(res); 7707c478bd9Sstevel@tonic-gate return (NULL); 7717c478bd9Sstevel@tonic-gate } 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate object = NIS_RES_OBJECT(res); 7747c478bd9Sstevel@tonic-gate 7757c478bd9Sstevel@tonic-gate if (object->EN_data.en_cols.en_cols_len < 8) { 7767c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "nisplus_match: " 7777c478bd9Sstevel@tonic-gate "not a valid passwd table entry for user %s", name); 7787c478bd9Sstevel@tonic-gate nis_freeresult(res); 7797c478bd9Sstevel@tonic-gate return (NULL); 7807c478bd9Sstevel@tonic-gate } 7817c478bd9Sstevel@tonic-gate 7827c478bd9Sstevel@tonic-gate return (res); 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate 7857c478bd9Sstevel@tonic-gate #define SAFE_STRDUP(dst, idx) \ 7867c478bd9Sstevel@tonic-gate if ((idx) <= 3 && ENTRY_VAL(nret, (idx)) == NULL) { \ 7877c478bd9Sstevel@tonic-gate syslog(LOG_ERR, \ 7887c478bd9Sstevel@tonic-gate "passwdutil: missing field from password entry"); \ 7897c478bd9Sstevel@tonic-gate goto error; \ 7907c478bd9Sstevel@tonic-gate } \ 7917c478bd9Sstevel@tonic-gate len = ENTRY_LEN(nret, (idx)); \ 7927c478bd9Sstevel@tonic-gate (dst) = malloc(len+1); \ 7937c478bd9Sstevel@tonic-gate if ((dst) == NULL) { \ 7947c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "passwdutil: out of memory"); \ 7957c478bd9Sstevel@tonic-gate goto error; \ 7967c478bd9Sstevel@tonic-gate } \ 7977c478bd9Sstevel@tonic-gate (dst)[len] = '\0'; \ 7987c478bd9Sstevel@tonic-gate (void) strncpy((dst), \ 7997c478bd9Sstevel@tonic-gate ENTRY_VAL(nret, (idx)) ? ENTRY_VAL(nret, (idx)) : "", \ 8007c478bd9Sstevel@tonic-gate len); 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate 8047c478bd9Sstevel@tonic-gate static struct passwd * 8057c478bd9Sstevel@tonic-gate nisplus_getpw_from_master(const char *name, char *domain) 8067c478bd9Sstevel@tonic-gate { 8077c478bd9Sstevel@tonic-gate char lookup[NIS_MAXNAMELEN+1]; 8087c478bd9Sstevel@tonic-gate nis_result *res; 8097c478bd9Sstevel@tonic-gate nis_object *nret; 8107c478bd9Sstevel@tonic-gate int len; 8117c478bd9Sstevel@tonic-gate char *p; 8127c478bd9Sstevel@tonic-gate struct passwd *pw; 8137c478bd9Sstevel@tonic-gate 8147c478bd9Sstevel@tonic-gate if ((pw = calloc(1, sizeof (*pw))) == NULL) 8157c478bd9Sstevel@tonic-gate return (NULL); 8167c478bd9Sstevel@tonic-gate 8177c478bd9Sstevel@tonic-gate res = nisplus_match(name, domain, lookup, sizeof (lookup)); 8187c478bd9Sstevel@tonic-gate 8197c478bd9Sstevel@tonic-gate if (res == NULL) 8207c478bd9Sstevel@tonic-gate return (NULL); 8217c478bd9Sstevel@tonic-gate 8227c478bd9Sstevel@tonic-gate nret = NIS_RES_OBJECT(res); 8237c478bd9Sstevel@tonic-gate 8247c478bd9Sstevel@tonic-gate SAFE_STRDUP(pw->pw_name, 0); 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate if ((pw->pw_passwd = strdup("x")) == NULL) { 8277c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "passwdutil: out of memory"); 8287c478bd9Sstevel@tonic-gate goto error; 8297c478bd9Sstevel@tonic-gate } 8307c478bd9Sstevel@tonic-gate 8317c478bd9Sstevel@tonic-gate SAFE_STRDUP(p, 2); 8327c478bd9Sstevel@tonic-gate pw->pw_uid = atoi(p); 8337c478bd9Sstevel@tonic-gate free(p); 8347c478bd9Sstevel@tonic-gate 8357c478bd9Sstevel@tonic-gate SAFE_STRDUP(p, 3); 8367c478bd9Sstevel@tonic-gate pw->pw_gid = atoi(p); 8377c478bd9Sstevel@tonic-gate free(p); 8387c478bd9Sstevel@tonic-gate 8397c478bd9Sstevel@tonic-gate pw->pw_age = NULL; 8407c478bd9Sstevel@tonic-gate pw->pw_comment = NULL; 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ 8437c478bd9Sstevel@tonic-gate SAFE_STRDUP(pw->pw_gecos, 4); 8447c478bd9Sstevel@tonic-gate 8457c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ 8467c478bd9Sstevel@tonic-gate SAFE_STRDUP(pw->pw_dir, 5); 8477c478bd9Sstevel@tonic-gate 8487c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ 8497c478bd9Sstevel@tonic-gate SAFE_STRDUP(pw->pw_shell, 6); 8507c478bd9Sstevel@tonic-gate 8517c478bd9Sstevel@tonic-gate nis_freeresult(res); 8527c478bd9Sstevel@tonic-gate 8537c478bd9Sstevel@tonic-gate return (pw); 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate error: 8567c478bd9Sstevel@tonic-gate nis_freeresult(res); 8577c478bd9Sstevel@tonic-gate if (pw->pw_name) 8587c478bd9Sstevel@tonic-gate free(pw->pw_name); 8597c478bd9Sstevel@tonic-gate if (pw->pw_passwd) 8607c478bd9Sstevel@tonic-gate free(pw->pw_passwd); 8617c478bd9Sstevel@tonic-gate if (pw->pw_gecos) 8627c478bd9Sstevel@tonic-gate free(pw->pw_gecos); 8637c478bd9Sstevel@tonic-gate if (pw->pw_dir) 8647c478bd9Sstevel@tonic-gate free(pw->pw_dir); 8657c478bd9Sstevel@tonic-gate if (pw->pw_shell) 8667c478bd9Sstevel@tonic-gate free(pw->pw_shell); 8677c478bd9Sstevel@tonic-gate free(pw); 8687c478bd9Sstevel@tonic-gate 8697c478bd9Sstevel@tonic-gate return (NULL); 8707c478bd9Sstevel@tonic-gate } 8717c478bd9Sstevel@tonic-gate 8727c478bd9Sstevel@tonic-gate /* 8737c478bd9Sstevel@tonic-gate * struct spwd * nisplus_getsp_from_master() 8747c478bd9Sstevel@tonic-gate * 8757c478bd9Sstevel@tonic-gate * Get the shadow structure from a NIS+ master. 8767c478bd9Sstevel@tonic-gate * This routine normally runs with EUID==0. This can cause trouble 8777c478bd9Sstevel@tonic-gate * if the NIS+ tables are locked down so that only the owner can 8787c478bd9Sstevel@tonic-gate * access the encrypted password. If we detect that scenario, we switch 8797c478bd9Sstevel@tonic-gate * EUID to the owner of the record and refetch it. 8807c478bd9Sstevel@tonic-gate */ 8817c478bd9Sstevel@tonic-gate static struct spwd * 8827c478bd9Sstevel@tonic-gate nisplus_getsp_from_master(const char *name, char *domain) 8837c478bd9Sstevel@tonic-gate { 8847c478bd9Sstevel@tonic-gate char lookup[NIS_MAXNAMELEN+1]; 8857c478bd9Sstevel@tonic-gate nis_result *res = NULL; 8867c478bd9Sstevel@tonic-gate nis_object *nret = NULL; 8877c478bd9Sstevel@tonic-gate int len; 8887c478bd9Sstevel@tonic-gate struct spwd *spw; 8897c478bd9Sstevel@tonic-gate char *shadow = NULL; 8907c478bd9Sstevel@tonic-gate const char *p = NULL; 8917c478bd9Sstevel@tonic-gate const char *limit; 8927c478bd9Sstevel@tonic-gate 8937c478bd9Sstevel@tonic-gate res = nisplus_match(name, domain, lookup, sizeof (lookup)); 8947c478bd9Sstevel@tonic-gate if (res == NULL) 8957c478bd9Sstevel@tonic-gate return (NULL); 8967c478bd9Sstevel@tonic-gate 8977c478bd9Sstevel@tonic-gate nret = NIS_RES_OBJECT(res); 8987c478bd9Sstevel@tonic-gate 8997c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ 9007c478bd9Sstevel@tonic-gate SAFE_STRDUP(shadow, 7); 9017c478bd9Sstevel@tonic-gate 9027c478bd9Sstevel@tonic-gate /* 903*66e150d7SJohn Sonnenschein * If we got NOPWDRTR as password, try again with EUID set 904*66e150d7SJohn Sonnenschein * to the UID of the record-owner. 9057c478bd9Sstevel@tonic-gate */ 906*66e150d7SJohn Sonnenschein if (strncmp(shadow, NOPWDRTR, 4) == 0) { 9077c478bd9Sstevel@tonic-gate char *p; 9087c478bd9Sstevel@tonic-gate uid_t owner_uid; 9097c478bd9Sstevel@tonic-gate uid_t euid = geteuid(); 9107c478bd9Sstevel@tonic-gate 9117c478bd9Sstevel@tonic-gate SAFE_STRDUP(p, 2); /* record-owner field */ 9127c478bd9Sstevel@tonic-gate owner_uid = atoi(p); 9137c478bd9Sstevel@tonic-gate free(p); 9147c478bd9Sstevel@tonic-gate 9157c478bd9Sstevel@tonic-gate if (owner_uid != euid) { 9167c478bd9Sstevel@tonic-gate /* re-obtain entry using owners EUID */ 9177c478bd9Sstevel@tonic-gate free(shadow); 9187c478bd9Sstevel@tonic-gate nis_freeresult(res); 9197c478bd9Sstevel@tonic-gate 9207c478bd9Sstevel@tonic-gate (void) seteuid(owner_uid); 9217c478bd9Sstevel@tonic-gate res = nisplus_match(name, domain, lookup, 9227c478bd9Sstevel@tonic-gate sizeof (lookup)); 9237c478bd9Sstevel@tonic-gate (void) seteuid(euid); 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate if (res == NULL) 9267c478bd9Sstevel@tonic-gate return (NULL); 9277c478bd9Sstevel@tonic-gate nret = NIS_RES_OBJECT(res); 9287c478bd9Sstevel@tonic-gate 9297c478bd9Sstevel@tonic-gate /*CONSTANTCONDITION*/ 9307c478bd9Sstevel@tonic-gate SAFE_STRDUP(shadow, 7); 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate if ((spw = calloc(1, sizeof (*spw))) == NULL) { 9357c478bd9Sstevel@tonic-gate nis_freeresult(res); 9367c478bd9Sstevel@tonic-gate return (NULL); 9377c478bd9Sstevel@tonic-gate } 9387c478bd9Sstevel@tonic-gate 9397c478bd9Sstevel@tonic-gate SAFE_STRDUP(spw->sp_namp, 0); 9407c478bd9Sstevel@tonic-gate SAFE_STRDUP(spw->sp_pwdp, 1); 9417c478bd9Sstevel@tonic-gate 9427c478bd9Sstevel@tonic-gate nis_freeresult(res); 9437c478bd9Sstevel@tonic-gate 9447c478bd9Sstevel@tonic-gate limit = shadow + strlen(shadow) + 1; 9457c478bd9Sstevel@tonic-gate p = shadow; 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate spw->sp_lstchg = -1; 9487c478bd9Sstevel@tonic-gate spw->sp_min = -1; 9497c478bd9Sstevel@tonic-gate spw->sp_max = -1; 9507c478bd9Sstevel@tonic-gate spw->sp_warn = -1; 9517c478bd9Sstevel@tonic-gate spw->sp_inact = -1; 9527c478bd9Sstevel@tonic-gate spw->sp_expire = -1; 9537c478bd9Sstevel@tonic-gate spw->sp_flag = 0; 9547c478bd9Sstevel@tonic-gate 9557c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_lstchg)) 9567c478bd9Sstevel@tonic-gate goto out; 9577c478bd9Sstevel@tonic-gate 9587c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_min)) 9597c478bd9Sstevel@tonic-gate goto out; 9607c478bd9Sstevel@tonic-gate 9617c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_max)) 9627c478bd9Sstevel@tonic-gate goto out; 9637c478bd9Sstevel@tonic-gate 9647c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_warn)) 9657c478bd9Sstevel@tonic-gate goto out; 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_inact)) 9687c478bd9Sstevel@tonic-gate goto out; 9697c478bd9Sstevel@tonic-gate 9707c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_expire)) 9717c478bd9Sstevel@tonic-gate goto out; 9727c478bd9Sstevel@tonic-gate 9737c478bd9Sstevel@tonic-gate if (!getfield(&p, limit, 1, &spw->sp_flag)) 9747c478bd9Sstevel@tonic-gate goto out; 9757c478bd9Sstevel@tonic-gate 9767c478bd9Sstevel@tonic-gate if (p != limit) { 9777c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "passwdutil: garbage at end of record"); 9787c478bd9Sstevel@tonic-gate goto error; 9797c478bd9Sstevel@tonic-gate } 9807c478bd9Sstevel@tonic-gate 9817c478bd9Sstevel@tonic-gate out: 9827c478bd9Sstevel@tonic-gate free(shadow); 9837c478bd9Sstevel@tonic-gate return (spw); 9847c478bd9Sstevel@tonic-gate 9857c478bd9Sstevel@tonic-gate error: 9867c478bd9Sstevel@tonic-gate if (spw->sp_namp) 9877c478bd9Sstevel@tonic-gate free(spw->sp_namp); 9887c478bd9Sstevel@tonic-gate if (spw->sp_pwdp) 9897c478bd9Sstevel@tonic-gate free(spw->sp_pwdp); 9907c478bd9Sstevel@tonic-gate free(spw); 9917c478bd9Sstevel@tonic-gate if (shadow) 9927c478bd9Sstevel@tonic-gate free(shadow); 9937c478bd9Sstevel@tonic-gate return (NULL); 9947c478bd9Sstevel@tonic-gate } 995