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 /* 22*11262SRajagopal.Andra@Sun.COM * Copyright 2009 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 /* 450Sstevel@tonic-gate * name_to_int(rep) 460Sstevel@tonic-gate * 470Sstevel@tonic-gate * Translate the repository to a bitmask. 480Sstevel@tonic-gate * if we don't recognise the repository name, we return REP_ERANGE 490Sstevel@tonic-gate */ 500Sstevel@tonic-gate int 510Sstevel@tonic-gate name_to_int(char *rep_name) 520Sstevel@tonic-gate { 530Sstevel@tonic-gate int result = REP_ERANGE; 540Sstevel@tonic-gate 550Sstevel@tonic-gate if (strcmp(rep_name, "files") == 0) 560Sstevel@tonic-gate result = REP_FILES; 570Sstevel@tonic-gate else if (strcmp(rep_name, "nis") == 0) 580Sstevel@tonic-gate result = REP_NIS; 590Sstevel@tonic-gate else if (strcmp(rep_name, "ldap") == 0) 600Sstevel@tonic-gate result = REP_LDAP; 610Sstevel@tonic-gate else if (strcmp(rep_name, "compat") == 0) { 620Sstevel@tonic-gate struct __nsw_switchconfig *cfg; 630Sstevel@tonic-gate enum __nsw_parse_err pserr; 640Sstevel@tonic-gate 650Sstevel@tonic-gate cfg = __nsw_getconfig("passwd_compat", &pserr); 660Sstevel@tonic-gate if (cfg == NULL) { 670Sstevel@tonic-gate result = REP_FILES | REP_NIS; 680Sstevel@tonic-gate } else { 69*11262SRajagopal.Andra@Sun.COM if (strcmp(cfg->lookups->service_name, "ldap") == 0) 700Sstevel@tonic-gate result = REP_FILES | REP_LDAP; 710Sstevel@tonic-gate else 720Sstevel@tonic-gate result = REP_ERANGE; 730Sstevel@tonic-gate __nsw_freeconfig(cfg); 740Sstevel@tonic-gate } 750Sstevel@tonic-gate } 760Sstevel@tonic-gate 770Sstevel@tonic-gate return (result); 780Sstevel@tonic-gate } 790Sstevel@tonic-gate 800Sstevel@tonic-gate /* 810Sstevel@tonic-gate * Figure out which repository we use in compat mode. 820Sstevel@tonic-gate */ 830Sstevel@tonic-gate int 840Sstevel@tonic-gate get_compat_mode(void) 850Sstevel@tonic-gate { 860Sstevel@tonic-gate struct __nsw_switchconfig *cfg; 870Sstevel@tonic-gate enum __nsw_parse_err pserr; 880Sstevel@tonic-gate int result = REP_COMPAT_NIS; 890Sstevel@tonic-gate 900Sstevel@tonic-gate if ((cfg = __nsw_getconfig("passwd_compat", &pserr)) != NULL) { 91*11262SRajagopal.Andra@Sun.COM if (strcmp(cfg->lookups->service_name, "ldap") == 0) 920Sstevel@tonic-gate result = REP_COMPAT_LDAP; 930Sstevel@tonic-gate } 940Sstevel@tonic-gate __nsw_freeconfig(cfg); 950Sstevel@tonic-gate 960Sstevel@tonic-gate return (result); 970Sstevel@tonic-gate } 980Sstevel@tonic-gate 990Sstevel@tonic-gate /* 1000Sstevel@tonic-gate * get_ns(rep, accesstype) 1010Sstevel@tonic-gate * 1020Sstevel@tonic-gate * returns a bitmask of repositories to use based on either 1030Sstevel@tonic-gate * 1. the repository that is given as argument 1040Sstevel@tonic-gate * 2. the nsswitch.conf file 1050Sstevel@tonic-gate * 3. the type of access requested 1060Sstevel@tonic-gate * 1070Sstevel@tonic-gate * "accesstype" indicates whether we are reading from or writing to the 1080Sstevel@tonic-gate * repository. We need to know this since "compat" will translate into 1090Sstevel@tonic-gate * REP_NSS (the nss-switch) for READ access (needed to decode 1100Sstevel@tonic-gate * the black-magic '+' entries) but it translates into a bitmask 1110Sstevel@tonic-gate * on WRITE access. 1120Sstevel@tonic-gate * 1130Sstevel@tonic-gate * If we detect read-access in compat mode, we augment the result 114*11262SRajagopal.Andra@Sun.COM * with one of REP_COMPAT_{NIS,LDAP}. We need this in order to 1150Sstevel@tonic-gate * implement ATTR_REP_NAME in nss_getpwnam. 1160Sstevel@tonic-gate * 1170Sstevel@tonic-gate * A return value of REP_NOREP indicates an error. 1180Sstevel@tonic-gate */ 1190Sstevel@tonic-gate int 1200Sstevel@tonic-gate get_ns(pwu_repository_t *rep, int accesstype) 1210Sstevel@tonic-gate { 1220Sstevel@tonic-gate struct __nsw_switchconfig *conf = NULL; 1230Sstevel@tonic-gate enum __nsw_parse_err pserr; 1240Sstevel@tonic-gate struct __nsw_lookup *lkp; 1250Sstevel@tonic-gate struct __nsw_lookup *lkp2; 1268040SBaban.Kenkre@Sun.COM struct __nsw_lookup *lkp3; 1278040SBaban.Kenkre@Sun.COM struct __nsw_lookup *lkpn; 1280Sstevel@tonic-gate int result = REP_NOREP; 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate if (rep != PWU_DEFAULT_REP) { 1310Sstevel@tonic-gate result = name_to_int(rep->type); 1320Sstevel@tonic-gate return (result); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate conf = __nsw_getconfig("passwd", &pserr); 1360Sstevel@tonic-gate if (conf == NULL) { 1370Sstevel@tonic-gate /* 1380Sstevel@tonic-gate * No config found. The user didn't supply a repository, 1390Sstevel@tonic-gate * so we try to change the password in the default 1400Sstevel@tonic-gate * repositories (files and nis) even though we cannot 1410Sstevel@tonic-gate * find the name service switch entry. (Backward compat) 1420Sstevel@tonic-gate */ 1430Sstevel@tonic-gate syslog(LOG_ERR, "passwdutil.so: nameservice switch entry for " 1448040SBaban.Kenkre@Sun.COM "passwd not found."); 1450Sstevel@tonic-gate result = REP_FILES | REP_NIS; 1460Sstevel@tonic-gate return (result); 1470Sstevel@tonic-gate } 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate lkp = conf->lookups; 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate /* 1528040SBaban.Kenkre@Sun.COM * Supported nsswitch.conf can have a maximum of 3 repositories. 1530Sstevel@tonic-gate * If we encounter an unsupported nsswitch.conf, we return REP_NSS 1540Sstevel@tonic-gate * to fall back to the nsswitch backend. 1558040SBaban.Kenkre@Sun.COM * 1568040SBaban.Kenkre@Sun.COM * Note that specifying 'ad' in the configuration is acceptable 1578040SBaban.Kenkre@Sun.COM * though changing AD users' passwords through passwd(1) is not. 1588040SBaban.Kenkre@Sun.COM * Therefore "ad" will be silently ignored. 1590Sstevel@tonic-gate */ 1600Sstevel@tonic-gate if (conf->num_lookups == 1) { 1610Sstevel@tonic-gate /* files or compat */ 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate if (strcmp(lkp->service_name, "files") == 0) { 1640Sstevel@tonic-gate result = name_to_int(lkp->service_name); 1650Sstevel@tonic-gate } else if (strcmp(lkp->service_name, "compat") == 0) { 1660Sstevel@tonic-gate if (accesstype == PWU_READ) 1670Sstevel@tonic-gate result = REP_NSS | get_compat_mode(); 1680Sstevel@tonic-gate else 1690Sstevel@tonic-gate result = name_to_int(lkp->service_name); 1700Sstevel@tonic-gate } else 1710Sstevel@tonic-gate result = REP_NSS; 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate } else if (conf->num_lookups == 2) { 1740Sstevel@tonic-gate lkp2 = lkp->next; 1750Sstevel@tonic-gate if (strcmp(lkp->service_name, "files") == 0) { 1760Sstevel@tonic-gate result = REP_FILES; 1770Sstevel@tonic-gate if (strcmp(lkp2->service_name, "ldap") == 0) 1780Sstevel@tonic-gate result |= REP_LDAP; 1790Sstevel@tonic-gate else if (strcmp(lkp2->service_name, "nis") == 0) 1800Sstevel@tonic-gate result |= REP_NIS; 1818040SBaban.Kenkre@Sun.COM else if (strcmp(lkp2->service_name, "ad") != 0) 1828040SBaban.Kenkre@Sun.COM result = REP_NSS; 1838040SBaban.Kenkre@Sun.COM /* AD is ignored */ 1848040SBaban.Kenkre@Sun.COM } else { 1858040SBaban.Kenkre@Sun.COM result = REP_NSS; 1868040SBaban.Kenkre@Sun.COM } 1878040SBaban.Kenkre@Sun.COM } else if (conf->num_lookups == 3) { 1888040SBaban.Kenkre@Sun.COM /* 1898040SBaban.Kenkre@Sun.COM * Valid configurations with 3 repositories are: 190*11262SRajagopal.Andra@Sun.COM * files ad [nis | ldap ] OR 191*11262SRajagopal.Andra@Sun.COM * files [nis | ldap ] ad 1928040SBaban.Kenkre@Sun.COM */ 1938040SBaban.Kenkre@Sun.COM lkp2 = lkp->next; 1948040SBaban.Kenkre@Sun.COM lkp3 = lkp2->next; 1958040SBaban.Kenkre@Sun.COM if (strcmp(lkp2->service_name, "ad") == 0) 1968040SBaban.Kenkre@Sun.COM lkpn = lkp3; 1978040SBaban.Kenkre@Sun.COM else if (strcmp(lkp3->service_name, "ad") == 0) 1988040SBaban.Kenkre@Sun.COM lkpn = lkp2; 1998040SBaban.Kenkre@Sun.COM else 2008040SBaban.Kenkre@Sun.COM lkpn = NULL; 2018040SBaban.Kenkre@Sun.COM if (strcmp(lkp->service_name, "files") == 0 && 2028040SBaban.Kenkre@Sun.COM lkpn != NULL) { 2038040SBaban.Kenkre@Sun.COM result = REP_FILES; 2048040SBaban.Kenkre@Sun.COM if (strcmp(lkpn->service_name, "ldap") == 0) 2058040SBaban.Kenkre@Sun.COM result |= REP_LDAP; 2068040SBaban.Kenkre@Sun.COM else if (strcmp(lkpn->service_name, "nis") == 0) 2078040SBaban.Kenkre@Sun.COM result |= REP_NIS; 2080Sstevel@tonic-gate else 2090Sstevel@tonic-gate result = REP_NSS; 2100Sstevel@tonic-gate } else { 2110Sstevel@tonic-gate result = REP_NSS; 2120Sstevel@tonic-gate } 2130Sstevel@tonic-gate } else { 2140Sstevel@tonic-gate result = REP_NSS; 2150Sstevel@tonic-gate } 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate __nsw_freeconfig(conf); 2180Sstevel@tonic-gate return (result); 2190Sstevel@tonic-gate } 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate static void 2220Sstevel@tonic-gate nss_ldap_passwd(p) 2230Sstevel@tonic-gate nss_db_params_t *p; 2240Sstevel@tonic-gate { 2250Sstevel@tonic-gate p->name = NSS_DBNAM_PASSWD; 2260Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2270Sstevel@tonic-gate p->default_config = "ldap"; 2280Sstevel@tonic-gate } 2290Sstevel@tonic-gate 2300Sstevel@tonic-gate static void 2310Sstevel@tonic-gate nss_ldap_shadow(p) 2320Sstevel@tonic-gate nss_db_params_t *p; 2330Sstevel@tonic-gate { 2340Sstevel@tonic-gate p->name = NSS_DBNAM_SHADOW; 2350Sstevel@tonic-gate p->config_name = NSS_DBNAM_PASSWD; /* Use config for "passwd" */ 2360Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2370Sstevel@tonic-gate p->default_config = "ldap"; 2380Sstevel@tonic-gate } 2390Sstevel@tonic-gate 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate #ifdef PAM_NIS 2420Sstevel@tonic-gate static void 2430Sstevel@tonic-gate nss_nis_passwd(p) 2440Sstevel@tonic-gate nss_db_params_t *p; 2450Sstevel@tonic-gate { 2460Sstevel@tonic-gate p->name = NSS_DBNAM_PASSWD; 2470Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2480Sstevel@tonic-gate p->default_config = "nis"; 2490Sstevel@tonic-gate } 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate static void 2520Sstevel@tonic-gate nss_nis_shadow(p) 2530Sstevel@tonic-gate nss_db_params_t *p; 2540Sstevel@tonic-gate { 2550Sstevel@tonic-gate p->name = NSS_DBNAM_SHADOW; 2560Sstevel@tonic-gate p->config_name = NSS_DBNAM_PASSWD; /* Use config for "passwd" */ 2570Sstevel@tonic-gate p->flags |= NSS_USE_DEFAULT_CONFIG; 2580Sstevel@tonic-gate p->default_config = "nis"; 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate #endif /* PAM_NIS */ 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate static char * 2630Sstevel@tonic-gate gettok(nextpp) 2640Sstevel@tonic-gate char **nextpp; 2650Sstevel@tonic-gate { 2660Sstevel@tonic-gate char *p = *nextpp; 2670Sstevel@tonic-gate char *q = p; 2680Sstevel@tonic-gate char c; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if (p == 0) { 2710Sstevel@tonic-gate return (0); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate while ((c = *q) != '\0' && c != ':') { 2740Sstevel@tonic-gate q++; 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate if (c == '\0') { 2770Sstevel@tonic-gate *nextpp = 0; 2780Sstevel@tonic-gate } else { 2790Sstevel@tonic-gate *q++ = '\0'; 2800Sstevel@tonic-gate *nextpp = q; 2810Sstevel@tonic-gate } 2820Sstevel@tonic-gate return (p); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* 2860Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 2870Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 2880Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 2890Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 2900Sstevel@tonic-gate */ 2910Sstevel@tonic-gate static int 2920Sstevel@tonic-gate str2passwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen) 2930Sstevel@tonic-gate { 2940Sstevel@tonic-gate struct passwd *passwd = (struct passwd *)ent; 2950Sstevel@tonic-gate char *p, *next; 2960Sstevel@tonic-gate int black_magic; /* "+" or "-" entry */ 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate if (lenstr + 1 > buflen) { 2990Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate /* 3020Sstevel@tonic-gate * We copy the input string into the output buffer and 3030Sstevel@tonic-gate * operate on it in place. 3040Sstevel@tonic-gate */ 3050Sstevel@tonic-gate (void) memcpy(buffer, instr, lenstr); 3060Sstevel@tonic-gate buffer[lenstr] = '\0'; 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate next = buffer; 3090Sstevel@tonic-gate 3100Sstevel@tonic-gate passwd->pw_name = p = gettok(&next); /* username */ 3110Sstevel@tonic-gate if (*p == '\0') { 3120Sstevel@tonic-gate /* Empty username; not allowed */ 3130Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate black_magic = (*p == '+' || *p == '-'); 3160Sstevel@tonic-gate if (black_magic) { 3170Sstevel@tonic-gate passwd->pw_uid = UID_NOBODY; 3180Sstevel@tonic-gate passwd->pw_gid = GID_NOBODY; 3190Sstevel@tonic-gate /* 3200Sstevel@tonic-gate * pwconv tests pw_passwd and pw_age == NULL 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate passwd->pw_passwd = ""; 3230Sstevel@tonic-gate passwd->pw_age = ""; 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * the rest of the passwd entry is "optional" 3260Sstevel@tonic-gate */ 3270Sstevel@tonic-gate passwd->pw_comment = ""; 3280Sstevel@tonic-gate passwd->pw_gecos = ""; 3290Sstevel@tonic-gate passwd->pw_dir = ""; 3300Sstevel@tonic-gate passwd->pw_shell = ""; 3310Sstevel@tonic-gate } 3320Sstevel@tonic-gate 3330Sstevel@tonic-gate passwd->pw_passwd = p = gettok(&next); /* password */ 3340Sstevel@tonic-gate if (p == 0) { 3350Sstevel@tonic-gate if (black_magic) 3360Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 3370Sstevel@tonic-gate else 3380Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3390Sstevel@tonic-gate } 3400Sstevel@tonic-gate for (; *p != '\0'; p++) { /* age */ 3410Sstevel@tonic-gate if (*p == ',') { 3420Sstevel@tonic-gate *p++ = '\0'; 3430Sstevel@tonic-gate break; 3440Sstevel@tonic-gate } 3450Sstevel@tonic-gate } 3460Sstevel@tonic-gate passwd->pw_age = p; 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate p = next; /* uid */ 3490Sstevel@tonic-gate if (p == 0 || *p == '\0') { 3500Sstevel@tonic-gate if (black_magic) 3510Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 3520Sstevel@tonic-gate else 3530Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3540Sstevel@tonic-gate } 3550Sstevel@tonic-gate if (!black_magic) { 3560Sstevel@tonic-gate passwd->pw_uid = strtol(p, &next, 10); 3570Sstevel@tonic-gate if (next == p) { 3580Sstevel@tonic-gate /* uid field should be nonempty */ 3590Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3600Sstevel@tonic-gate } 3610Sstevel@tonic-gate /* 3620Sstevel@tonic-gate * The old code (in 2.0 thru 2.5) would check 3630Sstevel@tonic-gate * for the uid being negative, or being greater 3640Sstevel@tonic-gate * than 60001 (the rfs limit). If it met either of 3650Sstevel@tonic-gate * these conditions, the uid was translated to 60001. 3660Sstevel@tonic-gate * 3674321Scasper * Now we just check for ephemeral uids; anything else 3680Sstevel@tonic-gate * is administrative policy 3690Sstevel@tonic-gate */ 3704321Scasper if (passwd->pw_uid > MAXUID) 3710Sstevel@tonic-gate passwd->pw_uid = UID_NOBODY; 3720Sstevel@tonic-gate } 3730Sstevel@tonic-gate if (*next++ != ':') { 3740Sstevel@tonic-gate if (black_magic) 3750Sstevel@tonic-gate p = gettok(&next); 3760Sstevel@tonic-gate else 3770Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3780Sstevel@tonic-gate } 3790Sstevel@tonic-gate p = next; /* gid */ 3800Sstevel@tonic-gate if (p == 0 || *p == '\0') { 3810Sstevel@tonic-gate if (black_magic) 3820Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 3830Sstevel@tonic-gate else 3840Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3850Sstevel@tonic-gate } 3860Sstevel@tonic-gate if (!black_magic) { 3870Sstevel@tonic-gate passwd->pw_gid = strtol(p, &next, 10); 3880Sstevel@tonic-gate if (next == p) { 3890Sstevel@tonic-gate /* gid field should be nonempty */ 3900Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 3910Sstevel@tonic-gate } 3920Sstevel@tonic-gate /* 3930Sstevel@tonic-gate * gid should be non-negative; anything else 3940Sstevel@tonic-gate * is administrative policy. 3950Sstevel@tonic-gate */ 3964321Scasper if (passwd->pw_gid > MAXUID) 3970Sstevel@tonic-gate passwd->pw_gid = GID_NOBODY; 3980Sstevel@tonic-gate } 3990Sstevel@tonic-gate if (*next++ != ':') { 4000Sstevel@tonic-gate if (black_magic) 4010Sstevel@tonic-gate p = gettok(&next); 4020Sstevel@tonic-gate else 4030Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4040Sstevel@tonic-gate } 4050Sstevel@tonic-gate 4060Sstevel@tonic-gate passwd->pw_gecos = passwd->pw_comment = p = gettok(&next); 4070Sstevel@tonic-gate if (p == 0) { 4080Sstevel@tonic-gate if (black_magic) 4090Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4100Sstevel@tonic-gate else 4110Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4120Sstevel@tonic-gate } 4130Sstevel@tonic-gate 4140Sstevel@tonic-gate passwd->pw_dir = p = gettok(&next); 4150Sstevel@tonic-gate if (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 4220Sstevel@tonic-gate passwd->pw_shell = p = gettok(&next); 4230Sstevel@tonic-gate if (p == 0) { 4240Sstevel@tonic-gate if (black_magic) 4250Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4260Sstevel@tonic-gate else 4270Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4280Sstevel@tonic-gate } 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate /* Better not be any more fields... */ 4310Sstevel@tonic-gate if (next == 0) { 4320Sstevel@tonic-gate /* Successfully parsed and stored */ 4330Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 4340Sstevel@tonic-gate } 4350Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 4360Sstevel@tonic-gate } 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate typedef const char *constp; 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate /* 4410Sstevel@tonic-gate * Return value 1 means success and more input, 0 means error or no more 4420Sstevel@tonic-gate */ 4430Sstevel@tonic-gate static int 4440Sstevel@tonic-gate getfield(nextp, limit, uns, valp) 4450Sstevel@tonic-gate constp *nextp; 4460Sstevel@tonic-gate constp limit; 4470Sstevel@tonic-gate int uns; 4480Sstevel@tonic-gate void *valp; 4490Sstevel@tonic-gate { 4500Sstevel@tonic-gate constp p = *nextp; 4510Sstevel@tonic-gate char *endfield; 4520Sstevel@tonic-gate char numbuf[12]; /* Holds -2^31 and trailing ':' */ 4530Sstevel@tonic-gate int len; 4540Sstevel@tonic-gate long x; 4550Sstevel@tonic-gate unsigned long ux; 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate if (p == 0 || p >= limit) { 4580Sstevel@tonic-gate return (0); 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate if (*p == ':') { 4610Sstevel@tonic-gate p++; 4620Sstevel@tonic-gate *nextp = p; 4630Sstevel@tonic-gate return (p < limit); 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate if ((len = limit - p) > sizeof (numbuf) - 1) { 4660Sstevel@tonic-gate len = sizeof (numbuf) - 1; 4670Sstevel@tonic-gate } 4680Sstevel@tonic-gate /* 4690Sstevel@tonic-gate * We want to use strtol() and we have a readonly non-zero-terminated 4700Sstevel@tonic-gate * string, so first we copy and terminate the interesting bit. 4710Sstevel@tonic-gate * Ugh. (It's convenient to terminate with a colon rather than \0). 4720Sstevel@tonic-gate */ 4730Sstevel@tonic-gate if ((endfield = memccpy(numbuf, p, ':', len)) == 0) { 4740Sstevel@tonic-gate if (len != limit - p) { 4750Sstevel@tonic-gate /* Error -- field is too big to be a legit number */ 4760Sstevel@tonic-gate return (0); 4770Sstevel@tonic-gate } 4780Sstevel@tonic-gate numbuf[len] = ':'; 4790Sstevel@tonic-gate p = limit; 4800Sstevel@tonic-gate } else { 4810Sstevel@tonic-gate p += (endfield - numbuf); 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate if (uns) { 4840Sstevel@tonic-gate ux = strtoul(numbuf, &endfield, 10); 4850Sstevel@tonic-gate if (*endfield != ':') { 4860Sstevel@tonic-gate /* Error -- expected <integer><colon> */ 4870Sstevel@tonic-gate return (0); 4880Sstevel@tonic-gate } 4890Sstevel@tonic-gate *((unsigned int *)valp) = (unsigned int)ux; 4900Sstevel@tonic-gate } else { 4910Sstevel@tonic-gate x = strtol(numbuf, &endfield, 10); 4920Sstevel@tonic-gate if (*endfield != ':') { 4930Sstevel@tonic-gate /* Error -- expected <integer><colon> */ 4940Sstevel@tonic-gate return (0); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate *((int *)valp) = (int)x; 4970Sstevel@tonic-gate } 4980Sstevel@tonic-gate *nextp = p; 4990Sstevel@tonic-gate return (p < limit); 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate 5020Sstevel@tonic-gate /* 5030Sstevel@tonic-gate * str2spwd() -- convert a string to a shadow passwd entry. The parser is 5040Sstevel@tonic-gate * more liberal than the passwd or group parsers; since it's legitimate 5050Sstevel@tonic-gate * for almost all the fields here to be blank, the parser lets one omit 5060Sstevel@tonic-gate * any number of blank fields at the end of the entry. The acceptable 5070Sstevel@tonic-gate * forms for '+' and '-' entries are the same as those for normal entries. 5080Sstevel@tonic-gate * === Is this likely to do more harm than good? 5090Sstevel@tonic-gate * 5100Sstevel@tonic-gate * Return values: 0 = success, 1 = parse error, 2 = erange ... 5110Sstevel@tonic-gate * The structure pointer passed in is a structure in the caller's space 5120Sstevel@tonic-gate * wherein the field pointers would be set to areas in the buffer if 5130Sstevel@tonic-gate * need be. instring and buffer should be separate areas. 5140Sstevel@tonic-gate */ 5150Sstevel@tonic-gate int 5160Sstevel@tonic-gate str2spwd(instr, lenstr, ent, buffer, buflen) 5170Sstevel@tonic-gate const char *instr; 5180Sstevel@tonic-gate int lenstr; 5190Sstevel@tonic-gate void *ent; /* really (struct spwd *) */ 5200Sstevel@tonic-gate char *buffer; 5210Sstevel@tonic-gate int buflen; 5220Sstevel@tonic-gate { 5230Sstevel@tonic-gate struct spwd *shadow = (struct spwd *)ent; 5240Sstevel@tonic-gate const char *p = instr, *limit; 5250Sstevel@tonic-gate char *bufp; 5260Sstevel@tonic-gate int lencopy, black_magic; 5270Sstevel@tonic-gate 5280Sstevel@tonic-gate limit = p + lenstr; 5290Sstevel@tonic-gate if ((p = memchr(instr, ':', lenstr)) == 0 || 5300Sstevel@tonic-gate ++p >= limit || 5310Sstevel@tonic-gate (p = memchr(p, ':', limit - p)) == 0) { 5320Sstevel@tonic-gate lencopy = lenstr; 5330Sstevel@tonic-gate p = 0; 5340Sstevel@tonic-gate } else { 5350Sstevel@tonic-gate lencopy = p - instr; 5360Sstevel@tonic-gate p++; 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate if (lencopy + 1 > buflen) { 5390Sstevel@tonic-gate return (NSS_STR_PARSE_ERANGE); 5400Sstevel@tonic-gate } 5410Sstevel@tonic-gate (void) memcpy(buffer, instr, lencopy); 5420Sstevel@tonic-gate buffer[lencopy] = 0; 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate black_magic = (*instr == '+' || *instr == '-'); 5450Sstevel@tonic-gate shadow->sp_namp = bufp = buffer; 5460Sstevel@tonic-gate shadow->sp_pwdp = 0; 5470Sstevel@tonic-gate shadow->sp_lstchg = -1; 5480Sstevel@tonic-gate shadow->sp_min = -1; 5490Sstevel@tonic-gate shadow->sp_max = -1; 5500Sstevel@tonic-gate shadow->sp_warn = -1; 5510Sstevel@tonic-gate shadow->sp_inact = -1; 5520Sstevel@tonic-gate shadow->sp_expire = -1; 5530Sstevel@tonic-gate shadow->sp_flag = 0; 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate if ((bufp = strchr(bufp, ':')) == 0) { 5560Sstevel@tonic-gate if (black_magic) 5570Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5580Sstevel@tonic-gate else 5590Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate *bufp++ = '\0'; 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate shadow->sp_pwdp = bufp; 5640Sstevel@tonic-gate if (instr == 0) { 5650Sstevel@tonic-gate if ((bufp = strchr(bufp, ':')) == 0) { 5660Sstevel@tonic-gate if (black_magic) 5670Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5680Sstevel@tonic-gate else 5690Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate *bufp++ = '\0'; 5720Sstevel@tonic-gate p = bufp; 5730Sstevel@tonic-gate } /* else p was set when we copied name and passwd into the buffer */ 5740Sstevel@tonic-gate 5750Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_lstchg)) 5760Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5770Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_min)) 5780Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5790Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_max)) 5800Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5810Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_warn)) 5820Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5830Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_inact)) 5840Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5850Sstevel@tonic-gate if (!getfield(&p, limit, 0, &shadow->sp_expire)) 5860Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5870Sstevel@tonic-gate if (!getfield(&p, limit, 1, &shadow->sp_flag)) 5880Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5890Sstevel@tonic-gate if (p != limit) { 5900Sstevel@tonic-gate /* Syntax error -- garbage at end of line */ 5910Sstevel@tonic-gate return (NSS_STR_PARSE_PARSE); 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate return (NSS_STR_PARSE_SUCCESS); 5940Sstevel@tonic-gate } 5950Sstevel@tonic-gate 5960Sstevel@tonic-gate static nss_XbyY_buf_t *buffer; 5970Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(db_root); 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate #define GETBUF() \ 6000Sstevel@tonic-gate NSS_XbyY_ALLOC(&buffer, sizeof (struct passwd), NSS_BUFLEN_PASSWD) 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate #pragma fini(endutilpwent) 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate static void 6050Sstevel@tonic-gate endutilpwent(void) 6060Sstevel@tonic-gate { 6070Sstevel@tonic-gate NSS_XbyY_FREE(&buffer); 6080Sstevel@tonic-gate nss_delete(&db_root); 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate 611*11262SRajagopal.Andra@Sun.COM /*ARGSUSED*/ 6120Sstevel@tonic-gate struct passwd * 6130Sstevel@tonic-gate getpwnam_from(const char *name, pwu_repository_t *rep, int reptype) 6140Sstevel@tonic-gate { 6150Sstevel@tonic-gate nss_XbyY_buf_t *b = GETBUF(); 6160Sstevel@tonic-gate nss_XbyY_args_t arg; 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate if (b == 0) 6190Sstevel@tonic-gate return (0); 6200Sstevel@tonic-gate 6210Sstevel@tonic-gate NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd); 6220Sstevel@tonic-gate arg.key.name = name; 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate switch (reptype) { 6250Sstevel@tonic-gate case REP_LDAP: 6260Sstevel@tonic-gate (void) nss_search(&db_root, nss_ldap_passwd, 6270Sstevel@tonic-gate NSS_DBOP_PASSWD_BYNAME, &arg); 6280Sstevel@tonic-gate break; 6290Sstevel@tonic-gate #ifdef PAM_NIS 6300Sstevel@tonic-gate case REP_NIS: 6310Sstevel@tonic-gate (void) nss_search(&db_root, nss_nis_passwd, 6320Sstevel@tonic-gate NSS_DBOP_PASSWD_BYNAME, &arg); 6330Sstevel@tonic-gate break; 6340Sstevel@tonic-gate #endif 6350Sstevel@tonic-gate default: 6360Sstevel@tonic-gate return (NULL); 6370Sstevel@tonic-gate } 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate return (struct passwd *)NSS_XbyY_FINI(&arg); 6400Sstevel@tonic-gate } 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate /*ARGSUSED*/ 6430Sstevel@tonic-gate struct passwd * 6440Sstevel@tonic-gate getpwuid_from(uid_t uid, pwu_repository_t *rep, int reptype) 6450Sstevel@tonic-gate { 6460Sstevel@tonic-gate nss_XbyY_buf_t *b = GETBUF(); 6470Sstevel@tonic-gate nss_XbyY_args_t arg; 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate if (b == 0) 6500Sstevel@tonic-gate return (0); 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd); 6530Sstevel@tonic-gate arg.key.uid = uid; 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate switch (reptype) { 6560Sstevel@tonic-gate case REP_LDAP: 6570Sstevel@tonic-gate (void) nss_search(&db_root, nss_ldap_passwd, 6580Sstevel@tonic-gate NSS_DBOP_PASSWD_BYUID, &arg); 6590Sstevel@tonic-gate break; 6600Sstevel@tonic-gate #ifdef PAM_NIS 6610Sstevel@tonic-gate case REP_NIS: 6620Sstevel@tonic-gate (void) nss_search(&db_root, nss_nis_passwd, 6630Sstevel@tonic-gate NSS_DBOP_PASSWD_BYUID, &arg); 6640Sstevel@tonic-gate break; 6650Sstevel@tonic-gate #endif 6660Sstevel@tonic-gate default: 6670Sstevel@tonic-gate return (NULL); 6680Sstevel@tonic-gate } 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate return (struct passwd *)NSS_XbyY_FINI(&arg); 6710Sstevel@tonic-gate } 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate static nss_XbyY_buf_t *spbuf; 6740Sstevel@tonic-gate static DEFINE_NSS_DB_ROOT(spdb_root); 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate #define GETSPBUF() \ 6770Sstevel@tonic-gate NSS_XbyY_ALLOC(&spbuf, sizeof (struct spwd), NSS_BUFLEN_SHADOW) 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate #pragma fini(endutilspent) 6800Sstevel@tonic-gate 6810Sstevel@tonic-gate static void 6820Sstevel@tonic-gate endutilspent(void) 6830Sstevel@tonic-gate { 6840Sstevel@tonic-gate NSS_XbyY_FREE(&spbuf); 6850Sstevel@tonic-gate nss_delete(&spdb_root); 6860Sstevel@tonic-gate } 6870Sstevel@tonic-gate 688*11262SRajagopal.Andra@Sun.COM /*ARGSUSED*/ 6890Sstevel@tonic-gate struct spwd * 6900Sstevel@tonic-gate getspnam_from(const char *name, pwu_repository_t *rep, int reptype) 6910Sstevel@tonic-gate { 6920Sstevel@tonic-gate nss_XbyY_buf_t *b = GETSPBUF(); 6930Sstevel@tonic-gate nss_XbyY_args_t arg; 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate if (b == 0) 6960Sstevel@tonic-gate return (0); 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2spwd); 6990Sstevel@tonic-gate arg.key.name = name; 7000Sstevel@tonic-gate switch (reptype) { 7010Sstevel@tonic-gate case REP_LDAP: 7020Sstevel@tonic-gate (void) nss_search(&spdb_root, nss_ldap_shadow, 7030Sstevel@tonic-gate NSS_DBOP_SHADOW_BYNAME, &arg); 7040Sstevel@tonic-gate break; 7050Sstevel@tonic-gate #ifdef PAM_NIS 7060Sstevel@tonic-gate case REP_NIS: 7070Sstevel@tonic-gate (void) nss_search(&spdb_root, nss_nis_shadow, 7080Sstevel@tonic-gate NSS_DBOP_SHADOW_BYNAME, &arg); 7090Sstevel@tonic-gate break; 7100Sstevel@tonic-gate #endif 7110Sstevel@tonic-gate default: 7120Sstevel@tonic-gate return (NULL); 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate return (struct spwd *)NSS_XbyY_FINI(&arg); 7150Sstevel@tonic-gate } 716