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