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