10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 54321Scasper * Common Development and Distribution License (the "License"). 64321Scasper * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 227422SJohn.Sonnenschein@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <sys/types.h> 270Sstevel@tonic-gate #include <nsswitch.h> 280Sstevel@tonic-gate #include <stdlib.h> 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <string.h> 310Sstevel@tonic-gate #include <syslog.h> 320Sstevel@tonic-gate #include <stdlib.h> 330Sstevel@tonic-gate #include <unistd.h> 340Sstevel@tonic-gate 350Sstevel@tonic-gate #include "ns_sldap.h" 360Sstevel@tonic-gate #include <nss_dbdefs.h> 370Sstevel@tonic-gate #include <nsswitch.h> 380Sstevel@tonic-gate #include <pwd.h> 390Sstevel@tonic-gate #include <shadow.h> 400Sstevel@tonic-gate #include <rpcsvc/nis.h> 410Sstevel@tonic-gate 420Sstevel@tonic-gate #include "passwdutil.h" 430Sstevel@tonic-gate 440Sstevel@tonic-gate static struct passwd *nisplus_getpw_from_master(const char *, char *); 450Sstevel@tonic-gate static struct spwd *nisplus_getsp_from_master(const char *, char *); 460Sstevel@tonic-gate 470Sstevel@tonic-gate /* 480Sstevel@tonic-gate * name_to_int(rep) 490Sstevel@tonic-gate * 500Sstevel@tonic-gate * Translate the repository to a bitmask. 510Sstevel@tonic-gate * if we don't recognise the repository name, we return REP_ERANGE 520Sstevel@tonic-gate */ 530Sstevel@tonic-gate int 540Sstevel@tonic-gate name_to_int(char *rep_name) 550Sstevel@tonic-gate { 560Sstevel@tonic-gate int result = REP_ERANGE; 570Sstevel@tonic-gate 580Sstevel@tonic-gate if (strcmp(rep_name, "files") == 0) 590Sstevel@tonic-gate result = REP_FILES; 600Sstevel@tonic-gate else if (strcmp(rep_name, "nis") == 0) 610Sstevel@tonic-gate result = REP_NIS; 620Sstevel@tonic-gate else if (strcmp(rep_name, "nisplus") == 0) 630Sstevel@tonic-gate result = REP_NISPLUS; 640Sstevel@tonic-gate else if (strcmp(rep_name, "ldap") == 0) 650Sstevel@tonic-gate result = REP_LDAP; 660Sstevel@tonic-gate else if (strcmp(rep_name, "compat") == 0) { 670Sstevel@tonic-gate struct __nsw_switchconfig *cfg; 680Sstevel@tonic-gate enum __nsw_parse_err pserr; 690Sstevel@tonic-gate 700Sstevel@tonic-gate cfg = __nsw_getconfig("passwd_compat", &pserr); 710Sstevel@tonic-gate if (cfg == NULL) { 720Sstevel@tonic-gate result = REP_FILES | REP_NIS; 730Sstevel@tonic-gate } else { 740Sstevel@tonic-gate if (strcmp(cfg->lookups->service_name, "nisplus") == 0) 750Sstevel@tonic-gate result = REP_FILES | REP_NISPLUS; 760Sstevel@tonic-gate else if (strcmp(cfg->lookups->service_name, "ldap") == 770Sstevel@tonic-gate 0) 780Sstevel@tonic-gate result = REP_FILES | REP_LDAP; 790Sstevel@tonic-gate else 800Sstevel@tonic-gate result = REP_ERANGE; 810Sstevel@tonic-gate __nsw_freeconfig(cfg); 820Sstevel@tonic-gate } 830Sstevel@tonic-gate } 840Sstevel@tonic-gate 850Sstevel@tonic-gate return (result); 860Sstevel@tonic-gate } 870Sstevel@tonic-gate 880Sstevel@tonic-gate /* 890Sstevel@tonic-gate * Figure out which repository we use in compat mode. 900Sstevel@tonic-gate */ 910Sstevel@tonic-gate int 920Sstevel@tonic-gate get_compat_mode(void) 930Sstevel@tonic-gate { 940Sstevel@tonic-gate struct __nsw_switchconfig *cfg; 950Sstevel@tonic-gate enum __nsw_parse_err pserr; 960Sstevel@tonic-gate int result = REP_COMPAT_NIS; 970Sstevel@tonic-gate 980Sstevel@tonic-gate if ((cfg = __nsw_getconfig("passwd_compat", &pserr)) != NULL) { 990Sstevel@tonic-gate if (strcmp(cfg->lookups->service_name, "nisplus") == 0) 1000Sstevel@tonic-gate result = REP_COMPAT_NISPLUS; 1010Sstevel@tonic-gate else if (strcmp(cfg->lookups->service_name, "ldap") == 0) 1020Sstevel@tonic-gate result = REP_COMPAT_LDAP; 1030Sstevel@tonic-gate } 1040Sstevel@tonic-gate __nsw_freeconfig(cfg); 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate return (result); 1070Sstevel@tonic-gate } 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate /* 1100Sstevel@tonic-gate * get_ns(rep, accesstype) 1110Sstevel@tonic-gate * 1120Sstevel@tonic-gate * returns a bitmask of repositories to use based on either 1130Sstevel@tonic-gate * 1. the repository that is given as argument 1140Sstevel@tonic-gate * 2. the nsswitch.conf file 1150Sstevel@tonic-gate * 3. the type of access requested 1160Sstevel@tonic-gate * 1170Sstevel@tonic-gate * "accesstype" indicates whether we are reading from or writing to the 1180Sstevel@tonic-gate * repository. We need to know this since "compat" will translate into 1190Sstevel@tonic-gate * REP_NSS (the nss-switch) for READ access (needed to decode 1200Sstevel@tonic-gate * the black-magic '+' entries) but it translates into a bitmask 1210Sstevel@tonic-gate * on WRITE access. 1220Sstevel@tonic-gate * 1230Sstevel@tonic-gate * If we detect read-access in compat mode, we augment the result 1240Sstevel@tonic-gate * with one of REP_COMPAT_{NIS,NISPLUS,LDAP}. We need this in order to 1250Sstevel@tonic-gate * implement ATTR_REP_NAME in nss_getpwnam. 1260Sstevel@tonic-gate * 1270Sstevel@tonic-gate * A return value of REP_NOREP indicates an error. 1280Sstevel@tonic-gate */ 1290Sstevel@tonic-gate int 1300Sstevel@tonic-gate get_ns(pwu_repository_t *rep, int accesstype) 1310Sstevel@tonic-gate { 1320Sstevel@tonic-gate struct __nsw_switchconfig *conf = NULL; 1330Sstevel@tonic-gate enum __nsw_parse_err pserr; 1340Sstevel@tonic-gate struct __nsw_lookup *lkp; 1350Sstevel@tonic-gate struct __nsw_lookup *lkp2; 136*8040SBaban.Kenkre@Sun.COM struct __nsw_lookup *lkp3; 137*8040SBaban.Kenkre@Sun.COM struct __nsw_lookup *lkpn; 1380Sstevel@tonic-gate int result = REP_NOREP; 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate if (rep != PWU_DEFAULT_REP) { 1410Sstevel@tonic-gate result = name_to_int(rep->type); 1420Sstevel@tonic-gate return (result); 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate conf = __nsw_getconfig("passwd", &pserr); 1460Sstevel@tonic-gate if (conf == NULL) { 1470Sstevel@tonic-gate /* 1480Sstevel@tonic-gate * No config found. The user didn't supply a repository, 1490Sstevel@tonic-gate * so we try to change the password in the default 1500Sstevel@tonic-gate * repositories (files and nis) even though we cannot 1510Sstevel@tonic-gate * find the name service switch entry. (Backward compat) 1520Sstevel@tonic-gate */ 1530Sstevel@tonic-gate syslog(LOG_ERR, "passwdutil.so: nameservice switch entry for " 154*8040SBaban.Kenkre@Sun.COM "passwd not found."); 1550Sstevel@tonic-gate result = REP_FILES | REP_NIS; 1560Sstevel@tonic-gate return (result); 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate lkp = conf->lookups; 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate /* 162*8040SBaban.Kenkre@Sun.COM * Supported nsswitch.conf can have a maximum of 3 repositories. 1630Sstevel@tonic-gate * If we encounter an unsupported nsswitch.conf, we return REP_NSS 1640Sstevel@tonic-gate * to fall back to the nsswitch backend. 165*8040SBaban.Kenkre@Sun.COM * 166*8040SBaban.Kenkre@Sun.COM * Note that specifying 'ad' in the configuration is acceptable 167*8040SBaban.Kenkre@Sun.COM * though changing AD users' passwords through passwd(1) is not. 168*8040SBaban.Kenkre@Sun.COM * Therefore "ad" will be silently ignored. 1690Sstevel@tonic-gate */ 1700Sstevel@tonic-gate if (conf->num_lookups == 1) { 1710Sstevel@tonic-gate /* files or compat */ 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate if (strcmp(lkp->service_name, "files") == 0) { 1740Sstevel@tonic-gate result = name_to_int(lkp->service_name); 1750Sstevel@tonic-gate } else if (strcmp(lkp->service_name, "compat") == 0) { 1760Sstevel@tonic-gate if (accesstype == PWU_READ) 1770Sstevel@tonic-gate result = REP_NSS | get_compat_mode(); 1780Sstevel@tonic-gate else 1790Sstevel@tonic-gate result = name_to_int(lkp->service_name); 1800Sstevel@tonic-gate } else 1810Sstevel@tonic-gate result = REP_NSS; 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate } else if (conf->num_lookups == 2) { 1840Sstevel@tonic-gate lkp2 = lkp->next; 1850Sstevel@tonic-gate if (strcmp(lkp->service_name, "files") == 0) { 1860Sstevel@tonic-gate result = REP_FILES; 1870Sstevel@tonic-gate if (strcmp(lkp2->service_name, "ldap") == 0) 1880Sstevel@tonic-gate result |= REP_LDAP; 1890Sstevel@tonic-gate else if (strcmp(lkp2->service_name, "nis") == 0) 1900Sstevel@tonic-gate result |= REP_NIS; 1910Sstevel@tonic-gate else if (strcmp(lkp2->service_name, "nisplus") == 0) 1920Sstevel@tonic-gate result |= REP_NISPLUS; 193*8040SBaban.Kenkre@Sun.COM else if (strcmp(lkp2->service_name, "ad") != 0) 194*8040SBaban.Kenkre@Sun.COM result = REP_NSS; 195*8040SBaban.Kenkre@Sun.COM /* AD is ignored */ 196*8040SBaban.Kenkre@Sun.COM } else { 197*8040SBaban.Kenkre@Sun.COM result = REP_NSS; 198*8040SBaban.Kenkre@Sun.COM } 199*8040SBaban.Kenkre@Sun.COM } else if (conf->num_lookups == 3) { 200*8040SBaban.Kenkre@Sun.COM /* 201*8040SBaban.Kenkre@Sun.COM * Valid configurations with 3 repositories are: 202*8040SBaban.Kenkre@Sun.COM * files ad [nis | ldap | nisplus] OR 203*8040SBaban.Kenkre@Sun.COM * files [nis | ldap | nisplus] ad 204*8040SBaban.Kenkre@Sun.COM */ 205*8040SBaban.Kenkre@Sun.COM lkp2 = lkp->next; 206*8040SBaban.Kenkre@Sun.COM lkp3 = lkp2->next; 207*8040SBaban.Kenkre@Sun.COM if (strcmp(lkp2->service_name, "ad") == 0) 208*8040SBaban.Kenkre@Sun.COM lkpn = lkp3; 209*8040SBaban.Kenkre@Sun.COM else if (strcmp(lkp3->service_name, "ad") == 0) 210*8040SBaban.Kenkre@Sun.COM lkpn = lkp2; 211*8040SBaban.Kenkre@Sun.COM else 212*8040SBaban.Kenkre@Sun.COM lkpn = NULL; 213*8040SBaban.Kenkre@Sun.COM if (strcmp(lkp->service_name, "files") == 0 && 214*8040SBaban.Kenkre@Sun.COM lkpn != NULL) { 215*8040SBaban.Kenkre@Sun.COM result = REP_FILES; 216*8040SBaban.Kenkre@Sun.COM if (strcmp(lkpn->service_name, "ldap") == 0) 217*8040SBaban.Kenkre@Sun.COM result |= REP_LDAP; 218*8040SBaban.Kenkre@Sun.COM else if (strcmp(lkpn->service_name, "nis") == 0) 219*8040SBaban.Kenkre@Sun.COM result |= REP_NIS; 220*8040SBaban.Kenkre@Sun.COM else if (strcmp(lkpn->service_name, "nisplus") == 0) 221*8040SBaban.Kenkre@Sun.COM result |= REP_NISPLUS; 2220Sstevel@tonic-gate else 2230Sstevel@tonic-gate result = REP_NSS; 2240Sstevel@tonic-gate } else { 2250Sstevel@tonic-gate result = REP_NSS; 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate } else { 2280Sstevel@tonic-gate result = REP_NSS; 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate __nsw_freeconfig(conf); 2320Sstevel@tonic-gate return (result); 2330Sstevel@tonic-gate } 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate static void 2360Sstevel@tonic-gate nss_ldap_passwd(p) 2370Sstevel@tonic-gate nss_db_params_t *p; 2380Sstevel@tonic-gate { 2390Sstevel@tonic-gate p->name = NSS_DBNAM_PASSWD; 2400Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2410Sstevel@tonic-gate p->default_config = "ldap"; 2420Sstevel@tonic-gate } 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate static void 2450Sstevel@tonic-gate nss_ldap_shadow(p) 2460Sstevel@tonic-gate nss_db_params_t *p; 2470Sstevel@tonic-gate { 2480Sstevel@tonic-gate p->name = NSS_DBNAM_SHADOW; 2490Sstevel@tonic-gate p->config_name = NSS_DBNAM_PASSWD; /* Use config for "passwd" */ 2500Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2510Sstevel@tonic-gate p->default_config = "ldap"; 2520Sstevel@tonic-gate } 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate #ifdef PAM_NIS 2560Sstevel@tonic-gate static void 2570Sstevel@tonic-gate nss_nis_passwd(p) 2580Sstevel@tonic-gate nss_db_params_t *p; 2590Sstevel@tonic-gate { 2600Sstevel@tonic-gate p->name = NSS_DBNAM_PASSWD; 2610Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2620Sstevel@tonic-gate p->default_config = "nis"; 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate static void 2660Sstevel@tonic-gate nss_nis_shadow(p) 2670Sstevel@tonic-gate nss_db_params_t *p; 2680Sstevel@tonic-gate { 2690Sstevel@tonic-gate p->name = NSS_DBNAM_SHADOW; 2700Sstevel@tonic-gate p->config_name = NSS_DBNAM_PASSWD; /* Use config for "passwd" */ 2710Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2720Sstevel@tonic-gate p->default_config = "nis"; 2730Sstevel@tonic-gate } 2740Sstevel@tonic-gate #endif /* PAM_NIS */ 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate 2770Sstevel@tonic-gate static void 2780Sstevel@tonic-gate nss_nisplus_passwd(p) 2790Sstevel@tonic-gate nss_db_params_t *p; 2800Sstevel@tonic-gate { 2810Sstevel@tonic-gate p->name = NSS_DBNAM_PASSWD; 2820Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2830Sstevel@tonic-gate p->default_config = "nisplus"; 2840Sstevel@tonic-gate } 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate static void 2870Sstevel@tonic-gate nss_nisplus_shadow(p) 2880Sstevel@tonic-gate nss_db_params_t *p; 2890Sstevel@tonic-gate { 2900Sstevel@tonic-gate p->name = NSS_DBNAM_SHADOW; 2910Sstevel@tonic-gate p->config_name = NSS_DBNAM_PASSWD; /* Use config for "passwd" */ 2920Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2930Sstevel@tonic-gate p->default_config = "nisplus"; 2940Sstevel@tonic-gate } 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate static char * 2980Sstevel@tonic-gate gettok(nextpp) 2990Sstevel@tonic-gate char **nextpp; 3000Sstevel@tonic-gate { 3010Sstevel@tonic-gate char *p = *nextpp; 3020Sstevel@tonic-gate char *q = p; 3030Sstevel@tonic-gate char c; 3040Sstevel@tonic-gate 3050Sstevel@tonic-gate if (p == 0) { 3060Sstevel@tonic-gate return (0); 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate while ((c = *q) != '\0' && c != ':') { 3090Sstevel@tonic-gate q++; 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate if (c == '\0') { 3120Sstevel@tonic-gate *nextpp = 0; 3130Sstevel@tonic-gate } else { 3140Sstevel@tonic-gate *q++ = '\0'; 3150Sstevel@tonic-gate *nextpp = q; 3160Sstevel@tonic-gate } 3170Sstevel@tonic-gate return (p); 3180Sstevel@tonic-gate } 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate /* 3210Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 3220Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 3230Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 3240Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 3250Sstevel@tonic-gate */ 3260Sstevel@tonic-gate static int 3270Sstevel@tonic-gate str2passwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen) 3280Sstevel@tonic-gate { 3290Sstevel@tonic-gate struct passwd *passwd = (struct passwd *)ent; 3300Sstevel@tonic-gate char *p, *next; 3310Sstevel@tonic-gate int black_magic; /* "+" or "-" entry */ 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate if (lenstr + 1 > buflen) { 3340Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate /* 3370Sstevel@tonic-gate * We copy the input string into the output buffer and 3380Sstevel@tonic-gate * operate on it in place. 3390Sstevel@tonic-gate */ 3400Sstevel@tonic-gate (void) memcpy(buffer, instr, lenstr); 3410Sstevel@tonic-gate buffer[lenstr] = '\0'; 3420Sstevel@tonic-gate 3430Sstevel@tonic-gate next = buffer; 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate passwd->pw_name = p = gettok(&next); /* username */ 3460Sstevel@tonic-gate if (*p == '\0') { 3470Sstevel@tonic-gate /* Empty username; not allowed */ 3480Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate black_magic = (*p == '+' || *p == '-'); 3510Sstevel@tonic-gate if (black_magic) { 3520Sstevel@tonic-gate passwd->pw_uid = UID_NOBODY; 3530Sstevel@tonic-gate passwd->pw_gid = GID_NOBODY; 3540Sstevel@tonic-gate /* 3550Sstevel@tonic-gate * pwconv tests pw_passwd and pw_age == NULL 3560Sstevel@tonic-gate */ 3570Sstevel@tonic-gate passwd->pw_passwd = ""; 3580Sstevel@tonic-gate passwd->pw_age = ""; 3590Sstevel@tonic-gate /* 3600Sstevel@tonic-gate * the rest of the passwd entry is "optional" 3610Sstevel@tonic-gate */ 3620Sstevel@tonic-gate passwd->pw_comment = ""; 3630Sstevel@tonic-gate passwd->pw_gecos = ""; 3640Sstevel@tonic-gate passwd->pw_dir = ""; 3650Sstevel@tonic-gate passwd->pw_shell = ""; 3660Sstevel@tonic-gate } 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate passwd->pw_passwd = p = gettok(&next); /* password */ 3690Sstevel@tonic-gate if (p == 0) { 3700Sstevel@tonic-gate if (black_magic) 3710Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 3720Sstevel@tonic-gate else 3730Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate for (; *p != '\0'; p++) { /* age */ 3760Sstevel@tonic-gate if (*p == ',') { 3770Sstevel@tonic-gate *p++ = '\0'; 3780Sstevel@tonic-gate break; 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate passwd->pw_age = p; 3820Sstevel@tonic-gate 3830Sstevel@tonic-gate p = next; /* uid */ 3840Sstevel@tonic-gate if (p == 0 || *p == '\0') { 3850Sstevel@tonic-gate if (black_magic) 3860Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 3870Sstevel@tonic-gate else 3880Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate if (!black_magic) { 3910Sstevel@tonic-gate passwd->pw_uid = strtol(p, &next, 10); 3920Sstevel@tonic-gate if (next == p) { 3930Sstevel@tonic-gate /* uid field should be nonempty */ 3940Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3950Sstevel@tonic-gate } 3960Sstevel@tonic-gate /* 3970Sstevel@tonic-gate * The old code (in 2.0 thru 2.5) would check 3980Sstevel@tonic-gate * for the uid being negative, or being greater 3990Sstevel@tonic-gate * than 60001 (the rfs limit). If it met either of 4000Sstevel@tonic-gate * these conditions, the uid was translated to 60001. 4010Sstevel@tonic-gate * 4024321Scasper * Now we just check for ephemeral uids; anything else 4030Sstevel@tonic-gate * is administrative policy 4040Sstevel@tonic-gate */ 4054321Scasper if (passwd->pw_uid > MAXUID) 4060Sstevel@tonic-gate passwd->pw_uid = UID_NOBODY; 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate if (*next++ != ':') { 4090Sstevel@tonic-gate if (black_magic) 4100Sstevel@tonic-gate p = gettok(&next); 4110Sstevel@tonic-gate else 4120Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate p = next; /* gid */ 4150Sstevel@tonic-gate if (p == 0 || *p == '\0') { 4160Sstevel@tonic-gate if (black_magic) 4170Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4180Sstevel@tonic-gate else 4190Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate if (!black_magic) { 4220Sstevel@tonic-gate passwd->pw_gid = strtol(p, &next, 10); 4230Sstevel@tonic-gate if (next == p) { 4240Sstevel@tonic-gate /* gid field should be nonempty */ 4250Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4260Sstevel@tonic-gate } 4270Sstevel@tonic-gate /* 4280Sstevel@tonic-gate * gid should be non-negative; anything else 4290Sstevel@tonic-gate * is administrative policy. 4300Sstevel@tonic-gate */ 4314321Scasper if (passwd->pw_gid > MAXUID) 4320Sstevel@tonic-gate passwd->pw_gid = GID_NOBODY; 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate if (*next++ != ':') { 4350Sstevel@tonic-gate if (black_magic) 4360Sstevel@tonic-gate p = gettok(&next); 4370Sstevel@tonic-gate else 4380Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4390Sstevel@tonic-gate } 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate passwd->pw_gecos = passwd->pw_comment = p = gettok(&next); 4420Sstevel@tonic-gate if (p == 0) { 4430Sstevel@tonic-gate if (black_magic) 4440Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4450Sstevel@tonic-gate else 4460Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4470Sstevel@tonic-gate } 4480Sstevel@tonic-gate 4490Sstevel@tonic-gate passwd->pw_dir = p = gettok(&next); 4500Sstevel@tonic-gate if (p == 0) { 4510Sstevel@tonic-gate if (black_magic) 4520Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4530Sstevel@tonic-gate else 4540Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4550Sstevel@tonic-gate } 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate passwd->pw_shell = p = gettok(&next); 4580Sstevel@tonic-gate if (p == 0) { 4590Sstevel@tonic-gate if (black_magic) 4600Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4610Sstevel@tonic-gate else 4620Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4630Sstevel@tonic-gate } 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate /* Better not be any more fields... */ 4660Sstevel@tonic-gate if (next == 0) { 4670Sstevel@tonic-gate /* Successfully parsed and stored */ 4680Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4690Sstevel@tonic-gate } 4700Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4710Sstevel@tonic-gate } 4720Sstevel@tonic-gate 4730Sstevel@tonic-gate typedef const char *constp; 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate /* 4760Sstevel@tonic-gate * Return value 1 means success and more input, 0 means error or no more 4770Sstevel@tonic-gate */ 4780Sstevel@tonic-gate static int 4790Sstevel@tonic-gate getfield(nextp, limit, uns, valp) 4800Sstevel@tonic-gate constp *nextp; 4810Sstevel@tonic-gate constp limit; 4820Sstevel@tonic-gate int uns; 4830Sstevel@tonic-gate void *valp; 4840Sstevel@tonic-gate { 4850Sstevel@tonic-gate constp p = *nextp; 4860Sstevel@tonic-gate char *endfield; 4870Sstevel@tonic-gate char numbuf[12]; /* Holds -2^31 and trailing ':' */ 4880Sstevel@tonic-gate int len; 4890Sstevel@tonic-gate long x; 4900Sstevel@tonic-gate unsigned long ux; 4910Sstevel@tonic-gate 4920Sstevel@tonic-gate if (p == 0 || p >= limit) { 4930Sstevel@tonic-gate return (0); 4940Sstevel@tonic-gate } 4950Sstevel@tonic-gate if (*p == ':') { 4960Sstevel@tonic-gate p++; 4970Sstevel@tonic-gate *nextp = p; 4980Sstevel@tonic-gate return (p < limit); 4990Sstevel@tonic-gate } 5000Sstevel@tonic-gate if ((len = limit - p) > sizeof (numbuf) - 1) { 5010Sstevel@tonic-gate len = sizeof (numbuf) - 1; 5020Sstevel@tonic-gate } 5030Sstevel@tonic-gate /* 5040Sstevel@tonic-gate * We want to use strtol() and we have a readonly non-zero-terminated 5050Sstevel@tonic-gate * string, so first we copy and terminate the interesting bit. 5060Sstevel@tonic-gate * Ugh. (It's convenient to terminate with a colon rather than \0). 5070Sstevel@tonic-gate */ 5080Sstevel@tonic-gate if ((endfield = memccpy(numbuf, p, ':', len)) == 0) { 5090Sstevel@tonic-gate if (len != limit - p) { 5100Sstevel@tonic-gate /* Error -- field is too big to be a legit number */ 5110Sstevel@tonic-gate return (0); 5120Sstevel@tonic-gate } 5130Sstevel@tonic-gate numbuf[len] = ':'; 5140Sstevel@tonic-gate p = limit; 5150Sstevel@tonic-gate } else { 5160Sstevel@tonic-gate p += (endfield - numbuf); 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate if (uns) { 5190Sstevel@tonic-gate ux = strtoul(numbuf, &endfield, 10); 5200Sstevel@tonic-gate if (*endfield != ':') { 5210Sstevel@tonic-gate /* Error -- expected <integer><colon> */ 5220Sstevel@tonic-gate return (0); 5230Sstevel@tonic-gate } 5240Sstevel@tonic-gate *((unsigned int *)valp) = (unsigned int)ux; 5250Sstevel@tonic-gate } else { 5260Sstevel@tonic-gate x = strtol(numbuf, &endfield, 10); 5270Sstevel@tonic-gate if (*endfield != ':') { 5280Sstevel@tonic-gate /* Error -- expected <integer><colon> */ 5290Sstevel@tonic-gate return (0); 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate *((int *)valp) = (int)x; 5320Sstevel@tonic-gate } 5330Sstevel@tonic-gate *nextp = p; 5340Sstevel@tonic-gate return (p < limit); 5350Sstevel@tonic-gate } 5360Sstevel@tonic-gate 5370Sstevel@tonic-gate /* 5380Sstevel@tonic-gate * str2spwd() -- convert a string to a shadow passwd entry. The parser is 5390Sstevel@tonic-gate * more liberal than the passwd or group parsers; since it's legitimate 5400Sstevel@tonic-gate * for almost all the fields here to be blank, the parser lets one omit 5410Sstevel@tonic-gate * any number of blank fields at the end of the entry. The acceptable 5420Sstevel@tonic-gate * forms for '+' and '-' entries are the same as those for normal entries. 5430Sstevel@tonic-gate * === Is this likely to do more harm than good? 5440Sstevel@tonic-gate * 5450Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 5460Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 5470Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 5480Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 5490Sstevel@tonic-gate */ 5500Sstevel@tonic-gate int 5510Sstevel@tonic-gate str2spwd(instr, lenstr, ent, buffer, buflen) 5520Sstevel@tonic-gate const char *instr; 5530Sstevel@tonic-gate int lenstr; 5540Sstevel@tonic-gate void *ent; /* really (struct spwd *) */ 5550Sstevel@tonic-gate char *buffer; 5560Sstevel@tonic-gate int buflen; 5570Sstevel@tonic-gate { 5580Sstevel@tonic-gate struct spwd *shadow = (struct spwd *)ent; 5590Sstevel@tonic-gate const char *p = instr, *limit; 5600Sstevel@tonic-gate char *bufp; 5610Sstevel@tonic-gate int lencopy, black_magic; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate limit = p + lenstr; 5640Sstevel@tonic-gate if ((p = memchr(instr, ':', lenstr)) == 0 || 5650Sstevel@tonic-gate ++p >= limit || 5660Sstevel@tonic-gate (p = memchr(p, ':', limit - p)) == 0) { 5670Sstevel@tonic-gate lencopy = lenstr; 5680Sstevel@tonic-gate p = 0; 5690Sstevel@tonic-gate } else { 5700Sstevel@tonic-gate lencopy = p - instr; 5710Sstevel@tonic-gate p++; 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate if (lencopy + 1 > buflen) { 5740Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 5750Sstevel@tonic-gate } 5760Sstevel@tonic-gate (void) memcpy(buffer, instr, lencopy); 5770Sstevel@tonic-gate buffer[lencopy] = 0; 5780Sstevel@tonic-gate 5790Sstevel@tonic-gate black_magic = (*instr == '+' || *instr == '-'); 5800Sstevel@tonic-gate shadow->sp_namp = bufp = buffer; 5810Sstevel@tonic-gate shadow->sp_pwdp = 0; 5820Sstevel@tonic-gate shadow->sp_lstchg = -1; 5830Sstevel@tonic-gate shadow->sp_min = -1; 5840Sstevel@tonic-gate shadow->sp_max = -1; 5850Sstevel@tonic-gate shadow->sp_warn = -1; 5860Sstevel@tonic-gate shadow->sp_inact = -1; 5870Sstevel@tonic-gate shadow->sp_expire = -1; 5880Sstevel@tonic-gate shadow->sp_flag = 0; 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate if ((bufp = strchr(bufp, ':')) == 0) { 5910Sstevel@tonic-gate if (black_magic) 5920Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5930Sstevel@tonic-gate else 5940Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 5950Sstevel@tonic-gate } 5960Sstevel@tonic-gate *bufp++ = '\0'; 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate shadow->sp_pwdp = bufp; 5990Sstevel@tonic-gate if (instr == 0) { 6000Sstevel@tonic-gate if ((bufp = strchr(bufp, ':')) == 0) { 6010Sstevel@tonic-gate if (black_magic) 6020Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 6030Sstevel@tonic-gate else 6040Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 6050Sstevel@tonic-gate } 6060Sstevel@tonic-gate *bufp++ = '\0'; 6070Sstevel@tonic-gate p = bufp; 6080Sstevel@tonic-gate } /* else p was set when we copied name and passwd into the buffer */ 6090Sstevel@tonic-gate 6100Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_lstchg)) 6110Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 6120Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_min)) 6130Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 6140Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_max)) 6150Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 6160Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_warn)) 6170Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 6180Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_inact)) 6190Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 6200Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_expire)) 6210Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 6220Sstevel@tonic-gate if (!getfield(&p, limit, 1, &shadow->sp_flag)) 6230Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 6240Sstevel@tonic-gate if (p != limit) { 6250Sstevel@tonic-gate /* Syntax error -- garbage at end of line */ 6260Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 6290Sstevel@tonic-gate } 6300Sstevel@tonic-gate 6310Sstevel@tonic-gate static nss_XbyY_buf_t *buffer; 6320Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root); 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate #define GETBUF() \ 6350Sstevel@tonic-gate NSS_XbyY_ALLOC(&buffer, sizeof (struct passwd), NSS_BUFLEN_PASSWD) 6360Sstevel@tonic-gate 6370Sstevel@tonic-gate #pragma fini(endutilpwent) 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate static void 6400Sstevel@tonic-gate endutilpwent(void) 6410Sstevel@tonic-gate { 6420Sstevel@tonic-gate NSS_XbyY_FREE(&buffer); 6430Sstevel@tonic-gate nss_delete(&db_root); 6440Sstevel@tonic-gate } 6450Sstevel@tonic-gate 6460Sstevel@tonic-gate struct passwd * 6470Sstevel@tonic-gate getpwnam_from(const char *name, pwu_repository_t *rep, int reptype) 6480Sstevel@tonic-gate { 6490Sstevel@tonic-gate nss_XbyY_buf_t *b = GETBUF(); 6500Sstevel@tonic-gate nss_XbyY_args_t arg; 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate if (b == 0) 6530Sstevel@tonic-gate return (0); 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd); 6560Sstevel@tonic-gate arg.key.name = name; 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate switch (reptype) { 6590Sstevel@tonic-gate case REP_LDAP: 6600Sstevel@tonic-gate (void) nss_search(&db_root, nss_ldap_passwd, 6610Sstevel@tonic-gate NSS_DBOP_PASSWD_BYNAME, &arg); 6620Sstevel@tonic-gate break; 6630Sstevel@tonic-gate case REP_NISPLUS: 6640Sstevel@tonic-gate if (rep && rep->scope) 6650Sstevel@tonic-gate return (nisplus_getpw_from_master(name, rep->scope)); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate (void) nss_search(&db_root, nss_nisplus_passwd, 6680Sstevel@tonic-gate NSS_DBOP_PASSWD_BYNAME, &arg); 6690Sstevel@tonic-gate break; 6700Sstevel@tonic-gate #ifdef PAM_NIS 6710Sstevel@tonic-gate case REP_NIS: 6720Sstevel@tonic-gate (void) nss_search(&db_root, nss_nis_passwd, 6730Sstevel@tonic-gate NSS_DBOP_PASSWD_BYNAME, &arg); 6740Sstevel@tonic-gate break; 6750Sstevel@tonic-gate #endif 6760Sstevel@tonic-gate default: 6770Sstevel@tonic-gate return (NULL); 6780Sstevel@tonic-gate } 6790Sstevel@tonic-gate 6800Sstevel@tonic-gate return (struct passwd *)NSS_XbyY_FINI(&arg); 6810Sstevel@tonic-gate } 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate /*ARGSUSED*/ 6840Sstevel@tonic-gate struct passwd * 6850Sstevel@tonic-gate getpwuid_from(uid_t uid, pwu_repository_t *rep, int reptype) 6860Sstevel@tonic-gate { 6870Sstevel@tonic-gate nss_XbyY_buf_t *b = GETBUF(); 6880Sstevel@tonic-gate nss_XbyY_args_t arg; 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate if (b == 0) 6910Sstevel@tonic-gate return (0); 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd); 6940Sstevel@tonic-gate arg.key.uid = uid; 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate switch (reptype) { 6970Sstevel@tonic-gate case REP_LDAP: 6980Sstevel@tonic-gate (void) nss_search(&db_root, nss_ldap_passwd, 6990Sstevel@tonic-gate NSS_DBOP_PASSWD_BYUID, &arg); 7000Sstevel@tonic-gate break; 7010Sstevel@tonic-gate case REP_NISPLUS: 7020Sstevel@tonic-gate (void) nss_search(&db_root, nss_nisplus_passwd, 7030Sstevel@tonic-gate NSS_DBOP_PASSWD_BYUID, &arg); 7040Sstevel@tonic-gate break; 7050Sstevel@tonic-gate #ifdef PAM_NIS 7060Sstevel@tonic-gate case REP_NIS: 7070Sstevel@tonic-gate (void) nss_search(&db_root, nss_nis_passwd, 7080Sstevel@tonic-gate NSS_DBOP_PASSWD_BYUID, &arg); 7090Sstevel@tonic-gate break; 7100Sstevel@tonic-gate #endif 7110Sstevel@tonic-gate default: 7120Sstevel@tonic-gate return (NULL); 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate return (struct passwd *)NSS_XbyY_FINI(&arg); 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate static nss_XbyY_buf_t *spbuf; 7190Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(spdb_root); 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate #define GETSPBUF() \ 7220Sstevel@tonic-gate NSS_XbyY_ALLOC(&spbuf, sizeof (struct spwd), NSS_BUFLEN_SHADOW) 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate #pragma fini(endutilspent) 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate static void 7270Sstevel@tonic-gate endutilspent(void) 7280Sstevel@tonic-gate { 7290Sstevel@tonic-gate NSS_XbyY_FREE(&spbuf); 7300Sstevel@tonic-gate nss_delete(&spdb_root); 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate struct spwd * 7340Sstevel@tonic-gate getspnam_from(const char *name, pwu_repository_t *rep, int reptype) 7350Sstevel@tonic-gate { 7360Sstevel@tonic-gate nss_XbyY_buf_t *b = GETSPBUF(); 7370Sstevel@tonic-gate nss_XbyY_args_t arg; 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate if (b == 0) 7400Sstevel@tonic-gate return (0); 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2spwd); 7430Sstevel@tonic-gate arg.key.name = name; 7440Sstevel@tonic-gate switch (reptype) { 7450Sstevel@tonic-gate case REP_LDAP: 7460Sstevel@tonic-gate (void) nss_search(&spdb_root, nss_ldap_shadow, 7470Sstevel@tonic-gate NSS_DBOP_SHADOW_BYNAME, &arg); 7480Sstevel@tonic-gate break; 7490Sstevel@tonic-gate case REP_NISPLUS: 7500Sstevel@tonic-gate if (rep && rep->scope) 7510Sstevel@tonic-gate return (nisplus_getsp_from_master(name, rep->scope)); 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate (void) nss_search(&spdb_root, nss_nisplus_shadow, 7540Sstevel@tonic-gate NSS_DBOP_SHADOW_BYNAME, &arg); 7550Sstevel@tonic-gate break; 7560Sstevel@tonic-gate #ifdef PAM_NIS 7570Sstevel@tonic-gate case REP_NIS: 7580Sstevel@tonic-gate (void) nss_search(&spdb_root, nss_nis_shadow, 7590Sstevel@tonic-gate NSS_DBOP_SHADOW_BYNAME, &arg); 7600Sstevel@tonic-gate break; 7610Sstevel@tonic-gate #endif 7620Sstevel@tonic-gate default: 7630Sstevel@tonic-gate return (NULL); 7640Sstevel@tonic-gate } 7650Sstevel@tonic-gate return (struct spwd *)NSS_XbyY_FINI(&arg); 7660Sstevel@tonic-gate } 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate 7690Sstevel@tonic-gate static nis_result * 7700Sstevel@tonic-gate nisplus_match(const char *name, char *domain, char *buf, int len) 7710Sstevel@tonic-gate { 7720Sstevel@tonic-gate int n; 7730Sstevel@tonic-gate int flags; 7740Sstevel@tonic-gate nis_result *res; 7750Sstevel@tonic-gate nis_object *object; 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate n = snprintf(buf, len, "[name=%s],passwd.org_dir.%s", name, domain); 7780Sstevel@tonic-gate if (n >= len) { 7790Sstevel@tonic-gate syslog(LOG_ERR, "nisplus_match: name too long"); 7800Sstevel@tonic-gate return (NULL); 7810Sstevel@tonic-gate } 7820Sstevel@tonic-gate if (buf[n-1] != '.') { 7830Sstevel@tonic-gate if (n == len-1) { 7840Sstevel@tonic-gate syslog(LOG_ERR, "nisplus_match: name too long"); 7850Sstevel@tonic-gate return (NULL); 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate buf[n++] = '.'; 7880Sstevel@tonic-gate buf[n] = '\0'; 7890Sstevel@tonic-gate } 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate flags = USE_DGRAM | FOLLOW_LINKS | FOLLOW_PATH | MASTER_ONLY; 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate res = nis_list(buf, flags, NULL, NULL); 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate if (res == NULL) { 7960Sstevel@tonic-gate syslog(LOG_ERR, "nisplus_match: nis_list returned NULL"); 7970Sstevel@tonic-gate return (NULL); 7980Sstevel@tonic-gate } 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate if (NIS_RES_STATUS(res) != NIS_SUCCESS || NIS_RES_NUMOBJ(res) != 1) { 8010Sstevel@tonic-gate syslog(LOG_ERR, "nisplus_match: match failed: %s", 8020Sstevel@tonic-gate nis_sperrno(NIS_RES_STATUS(res))); 8030Sstevel@tonic-gate nis_freeresult(res); 8040Sstevel@tonic-gate return (NULL); 8050Sstevel@tonic-gate } 8060Sstevel@tonic-gate 8070Sstevel@tonic-gate object = NIS_RES_OBJECT(res); 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate if (object->EN_data.en_cols.en_cols_len < 8) { 8100Sstevel@tonic-gate syslog(LOG_ERR, "nisplus_match: " 8110Sstevel@tonic-gate "not a valid passwd table entry for user %s", name); 8120Sstevel@tonic-gate nis_freeresult(res); 8130Sstevel@tonic-gate return (NULL); 8140Sstevel@tonic-gate } 8150Sstevel@tonic-gate 8160Sstevel@tonic-gate return (res); 8170Sstevel@tonic-gate } 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate #define SAFE_STRDUP(dst, idx) \ 8200Sstevel@tonic-gate if ((idx) <= 3 && ENTRY_VAL(nret, (idx)) == NULL) { \ 8210Sstevel@tonic-gate syslog(LOG_ERR, \ 8220Sstevel@tonic-gate "passwdutil: missing field from password entry"); \ 8230Sstevel@tonic-gate goto error; \ 8240Sstevel@tonic-gate } \ 8250Sstevel@tonic-gate len = ENTRY_LEN(nret, (idx)); \ 8260Sstevel@tonic-gate (dst) = malloc(len+1); \ 8270Sstevel@tonic-gate if ((dst) == NULL) { \ 8280Sstevel@tonic-gate syslog(LOG_ERR, "passwdutil: out of memory"); \ 8290Sstevel@tonic-gate goto error; \ 8300Sstevel@tonic-gate } \ 8310Sstevel@tonic-gate (dst)[len] = '\0'; \ 8320Sstevel@tonic-gate (void) strncpy((dst), \ 8330Sstevel@tonic-gate ENTRY_VAL(nret, (idx)) ? ENTRY_VAL(nret, (idx)) : "", \ 8340Sstevel@tonic-gate len); 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate 8380Sstevel@tonic-gate static struct passwd * 8390Sstevel@tonic-gate nisplus_getpw_from_master(const char *name, char *domain) 8400Sstevel@tonic-gate { 8410Sstevel@tonic-gate char lookup[NIS_MAXNAMELEN+1]; 8420Sstevel@tonic-gate nis_result *res; 8430Sstevel@tonic-gate nis_object *nret; 8440Sstevel@tonic-gate int len; 8450Sstevel@tonic-gate char *p; 8460Sstevel@tonic-gate struct passwd *pw; 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate if ((pw = calloc(1, sizeof (*pw))) == NULL) 8490Sstevel@tonic-gate return (NULL); 8500Sstevel@tonic-gate 8510Sstevel@tonic-gate res = nisplus_match(name, domain, lookup, sizeof (lookup)); 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate if (res == NULL) 8540Sstevel@tonic-gate return (NULL); 8550Sstevel@tonic-gate 8560Sstevel@tonic-gate nret = NIS_RES_OBJECT(res); 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate SAFE_STRDUP(pw->pw_name, 0); 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate if ((pw->pw_passwd = strdup("x")) == NULL) { 8610Sstevel@tonic-gate syslog(LOG_ERR, "passwdutil: out of memory"); 8620Sstevel@tonic-gate goto error; 8630Sstevel@tonic-gate } 8640Sstevel@tonic-gate 8650Sstevel@tonic-gate SAFE_STRDUP(p, 2); 8660Sstevel@tonic-gate pw->pw_uid = atoi(p); 8670Sstevel@tonic-gate free(p); 8680Sstevel@tonic-gate 8690Sstevel@tonic-gate SAFE_STRDUP(p, 3); 8700Sstevel@tonic-gate pw->pw_gid = atoi(p); 8710Sstevel@tonic-gate free(p); 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate pw->pw_age = NULL; 8740Sstevel@tonic-gate pw->pw_comment = NULL; 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate /*CONSTANTCONDITION*/ 8770Sstevel@tonic-gate SAFE_STRDUP(pw->pw_gecos, 4); 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate /*CONSTANTCONDITION*/ 8800Sstevel@tonic-gate SAFE_STRDUP(pw->pw_dir, 5); 8810Sstevel@tonic-gate 8820Sstevel@tonic-gate /*CONSTANTCONDITION*/ 8830Sstevel@tonic-gate SAFE_STRDUP(pw->pw_shell, 6); 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate nis_freeresult(res); 8860Sstevel@tonic-gate 8870Sstevel@tonic-gate return (pw); 8880Sstevel@tonic-gate 8890Sstevel@tonic-gate error: 8900Sstevel@tonic-gate nis_freeresult(res); 8910Sstevel@tonic-gate if (pw->pw_name) 8920Sstevel@tonic-gate free(pw->pw_name); 8930Sstevel@tonic-gate if (pw->pw_passwd) 8940Sstevel@tonic-gate free(pw->pw_passwd); 8950Sstevel@tonic-gate if (pw->pw_gecos) 8960Sstevel@tonic-gate free(pw->pw_gecos); 8970Sstevel@tonic-gate if (pw->pw_dir) 8980Sstevel@tonic-gate free(pw->pw_dir); 8990Sstevel@tonic-gate if (pw->pw_shell) 9000Sstevel@tonic-gate free(pw->pw_shell); 9010Sstevel@tonic-gate free(pw); 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate return (NULL); 9040Sstevel@tonic-gate } 9050Sstevel@tonic-gate 9060Sstevel@tonic-gate /* 9070Sstevel@tonic-gate * struct spwd * nisplus_getsp_from_master() 9080Sstevel@tonic-gate * 9090Sstevel@tonic-gate * Get the shadow structure from a NIS+ master. 9100Sstevel@tonic-gate * This routine normally runs with EUID==0. This can cause trouble 9110Sstevel@tonic-gate * if the NIS+ tables are locked down so that only the owner can 9120Sstevel@tonic-gate * access the encrypted password. If we detect that scenario, we switch 9130Sstevel@tonic-gate * EUID to the owner of the record and refetch it. 9140Sstevel@tonic-gate */ 9150Sstevel@tonic-gate static struct spwd * 9160Sstevel@tonic-gate nisplus_getsp_from_master(const char *name, char *domain) 9170Sstevel@tonic-gate { 9180Sstevel@tonic-gate char lookup[NIS_MAXNAMELEN+1]; 9190Sstevel@tonic-gate nis_result *res = NULL; 9200Sstevel@tonic-gate nis_object *nret = NULL; 9210Sstevel@tonic-gate int len; 9220Sstevel@tonic-gate struct spwd *spw; 9230Sstevel@tonic-gate char *shadow = NULL; 9240Sstevel@tonic-gate const char *p = NULL; 9250Sstevel@tonic-gate const char *limit; 9260Sstevel@tonic-gate 9270Sstevel@tonic-gate res = nisplus_match(name, domain, lookup, sizeof (lookup)); 9280Sstevel@tonic-gate if (res == NULL) 9290Sstevel@tonic-gate return (NULL); 9300Sstevel@tonic-gate 9310Sstevel@tonic-gate nret = NIS_RES_OBJECT(res); 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate /*CONSTANTCONDITION*/ 9340Sstevel@tonic-gate SAFE_STRDUP(shadow, 7); 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate /* 9377422SJohn.Sonnenschein@Sun.COM * If we got NOPWDRTR as password, try again with EUID set 9387422SJohn.Sonnenschein@Sun.COM * to the UID of the record-owner. 9390Sstevel@tonic-gate */ 9407422SJohn.Sonnenschein@Sun.COM if (strncmp(shadow, NOPWDRTR, 4) == 0) { 9410Sstevel@tonic-gate char *p; 9420Sstevel@tonic-gate uid_t owner_uid; 9430Sstevel@tonic-gate uid_t euid = geteuid(); 9440Sstevel@tonic-gate 9450Sstevel@tonic-gate SAFE_STRDUP(p, 2); /* record-owner field */ 9460Sstevel@tonic-gate owner_uid = atoi(p); 9470Sstevel@tonic-gate free(p); 9480Sstevel@tonic-gate 9490Sstevel@tonic-gate if (owner_uid != euid) { 9500Sstevel@tonic-gate /* re-obtain entry using owners EUID */ 9510Sstevel@tonic-gate free(shadow); 9520Sstevel@tonic-gate nis_freeresult(res); 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate (void) seteuid(owner_uid); 9550Sstevel@tonic-gate res = nisplus_match(name, domain, lookup, 9560Sstevel@tonic-gate sizeof (lookup)); 9570Sstevel@tonic-gate (void) seteuid(euid); 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate if (res == NULL) 9600Sstevel@tonic-gate return (NULL); 9610Sstevel@tonic-gate nret = NIS_RES_OBJECT(res); 9620Sstevel@tonic-gate 9630Sstevel@tonic-gate /*CONSTANTCONDITION*/ 9640Sstevel@tonic-gate SAFE_STRDUP(shadow, 7); 9650Sstevel@tonic-gate } 9660Sstevel@tonic-gate } 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate if ((spw = calloc(1, sizeof (*spw))) == NULL) { 9690Sstevel@tonic-gate nis_freeresult(res); 9700Sstevel@tonic-gate return (NULL); 9710Sstevel@tonic-gate } 9720Sstevel@tonic-gate 9730Sstevel@tonic-gate SAFE_STRDUP(spw->sp_namp, 0); 9740Sstevel@tonic-gate SAFE_STRDUP(spw->sp_pwdp, 1); 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate nis_freeresult(res); 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate limit = shadow + strlen(shadow) + 1; 9790Sstevel@tonic-gate p = shadow; 9800Sstevel@tonic-gate 9810Sstevel@tonic-gate spw->sp_lstchg = -1; 9820Sstevel@tonic-gate spw->sp_min = -1; 9830Sstevel@tonic-gate spw->sp_max = -1; 9840Sstevel@tonic-gate spw->sp_warn = -1; 9850Sstevel@tonic-gate spw->sp_inact = -1; 9860Sstevel@tonic-gate spw->sp_expire = -1; 9870Sstevel@tonic-gate spw->sp_flag = 0; 9880Sstevel@tonic-gate 9890Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_lstchg)) 9900Sstevel@tonic-gate goto out; 9910Sstevel@tonic-gate 9920Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_min)) 9930Sstevel@tonic-gate goto out; 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_max)) 9960Sstevel@tonic-gate goto out; 9970Sstevel@tonic-gate 9980Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_warn)) 9990Sstevel@tonic-gate goto out; 10000Sstevel@tonic-gate 10010Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_inact)) 10020Sstevel@tonic-gate goto out; 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate if (!getfield(&p, limit, 0, &spw->sp_expire)) 10050Sstevel@tonic-gate goto out; 10060Sstevel@tonic-gate 10070Sstevel@tonic-gate if (!getfield(&p, limit, 1, &spw->sp_flag)) 10080Sstevel@tonic-gate goto out; 10090Sstevel@tonic-gate 10100Sstevel@tonic-gate if (p != limit) { 10110Sstevel@tonic-gate syslog(LOG_ERR, "passwdutil: garbage at end of record"); 10120Sstevel@tonic-gate goto error; 10130Sstevel@tonic-gate } 10140Sstevel@tonic-gate 10150Sstevel@tonic-gate out: 10160Sstevel@tonic-gate free(shadow); 10170Sstevel@tonic-gate return (spw); 10180Sstevel@tonic-gate 10190Sstevel@tonic-gate error: 10200Sstevel@tonic-gate if (spw->sp_namp) 10210Sstevel@tonic-gate free(spw->sp_namp); 10220Sstevel@tonic-gate if (spw->sp_pwdp) 10230Sstevel@tonic-gate free(spw->sp_pwdp); 10240Sstevel@tonic-gate free(spw); 10250Sstevel@tonic-gate if (shadow) 10260Sstevel@tonic-gate free(shadow); 10270Sstevel@tonic-gate return (NULL); 10280Sstevel@tonic-gate } 1029