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
51914Scasper * Common Development and Distribution License (the "License").
61914Scasper * 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
20132Srobinson */
21132Srobinson
22132Srobinson /*
2311134SCasper.Dik@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 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
300Sstevel@tonic-gate * 4.3 BSD under license from the Regents of the University of
310Sstevel@tonic-gate * California.
320Sstevel@tonic-gate */
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * ==== hack-attack: possibly MT-safe but definitely not MT-hot.
350Sstevel@tonic-gate * ==== turn this into a real switch frontend and backends
360Sstevel@tonic-gate *
370Sstevel@tonic-gate * Well, at least the API doesn't involve pointers-to-static.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate * netname utility routines convert from netnames to unix names (uid, gid)
420Sstevel@tonic-gate *
430Sstevel@tonic-gate * This module is operating system dependent!
440Sstevel@tonic-gate * What we define here will work with any unix system that has adopted
450Sstevel@tonic-gate * the Sun NIS domain architecture.
460Sstevel@tonic-gate */
470Sstevel@tonic-gate
480Sstevel@tonic-gate #undef NIS
490Sstevel@tonic-gate #include "mt.h"
500Sstevel@tonic-gate #include "rpc_mt.h"
510Sstevel@tonic-gate #include <stdio.h>
520Sstevel@tonic-gate #include <stdlib.h>
5311134SCasper.Dik@Sun.COM #include <unistd.h>
5411134SCasper.Dik@Sun.COM #include <alloca.h>
550Sstevel@tonic-gate #include <sys/types.h>
560Sstevel@tonic-gate #include <ctype.h>
570Sstevel@tonic-gate #include <grp.h>
580Sstevel@tonic-gate #include <pwd.h>
590Sstevel@tonic-gate #include <string.h>
600Sstevel@tonic-gate #include <syslog.h>
610Sstevel@tonic-gate #include <sys/param.h>
620Sstevel@tonic-gate #include <nsswitch.h>
630Sstevel@tonic-gate #include <rpc/rpc.h>
640Sstevel@tonic-gate #include <rpcsvc/nis.h>
650Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
660Sstevel@tonic-gate #include <nss_dbdefs.h>
670Sstevel@tonic-gate
680Sstevel@tonic-gate static const char OPSYS[] = "unix";
690Sstevel@tonic-gate static const char NETIDFILE[] = "/etc/netid";
700Sstevel@tonic-gate static const char NETID[] = "netid.byname";
710Sstevel@tonic-gate #define OPSYS_LEN 4
720Sstevel@tonic-gate
730Sstevel@tonic-gate extern int _getgroupsbymember(const char *, gid_t[], int, int);
740Sstevel@tonic-gate
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * the value for NOBODY_UID is set by the SVID. The following define also
770Sstevel@tonic-gate * appears in netname.c
780Sstevel@tonic-gate */
790Sstevel@tonic-gate
800Sstevel@tonic-gate #define NOBODY_UID 60001
810Sstevel@tonic-gate
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate * default publickey policy:
840Sstevel@tonic-gate * publickey: nis [NOTFOUND = return] files
850Sstevel@tonic-gate */
860Sstevel@tonic-gate
870Sstevel@tonic-gate
880Sstevel@tonic-gate /* NSW_NOTSUCCESS NSW_NOTFOUND NSW_UNAVAIL NSW_TRYAGAIN */
890Sstevel@tonic-gate #define DEF_ACTION {__NSW_RETURN, __NSW_RETURN, __NSW_CONTINUE, __NSW_CONTINUE}
900Sstevel@tonic-gate
910Sstevel@tonic-gate static struct __nsw_lookup lookup_files = {"files", DEF_ACTION, NULL, NULL},
920Sstevel@tonic-gate lookup_nis = {"nis", DEF_ACTION, NULL, &lookup_files};
930Sstevel@tonic-gate static struct __nsw_switchconfig publickey_default =
940Sstevel@tonic-gate {0, "publickey", 2, &lookup_nis};
950Sstevel@tonic-gate
960Sstevel@tonic-gate static mutex_t serialize_netname_r = DEFAULTMUTEX;
970Sstevel@tonic-gate
980Sstevel@tonic-gate struct netid_userdata {
990Sstevel@tonic-gate uid_t *uidp;
1000Sstevel@tonic-gate gid_t *gidp;
1010Sstevel@tonic-gate int *gidlenp;
1020Sstevel@tonic-gate gid_t *gidlist;
1030Sstevel@tonic-gate };
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate static int
parse_uid(char * s,struct netid_userdata * argp)106132Srobinson parse_uid(char *s, struct netid_userdata *argp)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate uid_t u;
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate if (!s || !isdigit(*s)) {
1110Sstevel@tonic-gate syslog(LOG_ERR,
11211262SRajagopal.Andra@Sun.COM "netname2user: expecting uid '%s'", s);
1130Sstevel@tonic-gate return (__NSW_NOTFOUND); /* xxx need a better error */
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate /* Fetch the uid */
1170Sstevel@tonic-gate u = (uid_t)(atoi(s));
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate if (u == 0) {
1200Sstevel@tonic-gate syslog(LOG_ERR, "netname2user: should not have uid 0");
1210Sstevel@tonic-gate return (__NSW_NOTFOUND);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate *(argp->uidp) = u;
1240Sstevel@tonic-gate return (__NSW_SUCCESS);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate /* parse a comma separated gid list */
1290Sstevel@tonic-gate static int
parse_gidlist(char * p,struct netid_userdata * argp)130132Srobinson parse_gidlist(char *p, struct netid_userdata *argp)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate int len;
1330Sstevel@tonic-gate gid_t g;
1340Sstevel@tonic-gate
135132Srobinson if (!p || (!isdigit(*p))) {
1360Sstevel@tonic-gate syslog(LOG_ERR,
13711262SRajagopal.Andra@Sun.COM "netname2user: missing group id list in '%s'.",
13811262SRajagopal.Andra@Sun.COM p);
1390Sstevel@tonic-gate return (__NSW_NOTFOUND);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate g = (gid_t)(atoi(p));
1430Sstevel@tonic-gate *(argp->gidp) = g;
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate len = 0;
1460Sstevel@tonic-gate while (p = strchr(p, ','))
1470Sstevel@tonic-gate argp->gidlist[len++] = (gid_t)atoi(++p);
1480Sstevel@tonic-gate *(argp->gidlenp) = len;
1490Sstevel@tonic-gate return (__NSW_SUCCESS);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * parse_netid_str()
1550Sstevel@tonic-gate *
1560Sstevel@tonic-gate * Parse uid and group information from the passed string.
1570Sstevel@tonic-gate *
1580Sstevel@tonic-gate * The format of the string passed is
1590Sstevel@tonic-gate * uid:gid,grp,grp, ...
1600Sstevel@tonic-gate *
1610Sstevel@tonic-gate */
1620Sstevel@tonic-gate static int
parse_netid_str(char * s,struct netid_userdata * argp)163132Srobinson parse_netid_str(char *s, struct netid_userdata *argp)
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate char *p;
1660Sstevel@tonic-gate int err;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate /* get uid */
1690Sstevel@tonic-gate err = parse_uid(s, argp);
170132Srobinson if (err != __NSW_SUCCESS)
1710Sstevel@tonic-gate return (err);
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /* Now get the group list */
1740Sstevel@tonic-gate p = strchr(s, ':');
1750Sstevel@tonic-gate if (!p) {
1760Sstevel@tonic-gate syslog(LOG_ERR,
17711262SRajagopal.Andra@Sun.COM "netname2user: missing group id list in '%s'", s);
1780Sstevel@tonic-gate return (__NSW_NOTFOUND);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate ++p; /* skip ':' */
1810Sstevel@tonic-gate err = parse_gidlist(p, argp);
1820Sstevel@tonic-gate return (err);
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate * netname2user_files()
1870Sstevel@tonic-gate *
1880Sstevel@tonic-gate * This routine fetches the netid information from the "files" nameservice.
1890Sstevel@tonic-gate * ie /etc/netid.
1900Sstevel@tonic-gate */
1910Sstevel@tonic-gate static int
netname2user_files(int * err,char * netname,struct netid_userdata * argp)192132Srobinson netname2user_files(int *err, char *netname, struct netid_userdata *argp)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate char buf[512]; /* one line from the file */
1950Sstevel@tonic-gate char *name;
1960Sstevel@tonic-gate char *value;
1970Sstevel@tonic-gate char *res;
1981914Scasper FILE *fd;
1990Sstevel@tonic-gate
2001914Scasper fd = fopen(NETIDFILE, "rF");
2011914Scasper if (fd == NULL) {
2020Sstevel@tonic-gate *err = __NSW_UNAVAIL;
2030Sstevel@tonic-gate return (0);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate * for each line in the file parse it appropriately
2070Sstevel@tonic-gate * file format is :
2080Sstevel@tonic-gate * netid uid:grp,grp,grp # for users
2090Sstevel@tonic-gate * netid 0:hostname # for hosts
2100Sstevel@tonic-gate */
2111914Scasper while (!feof(fd)) {
2121914Scasper res = fgets(buf, 512, fd);
2130Sstevel@tonic-gate if (res == NULL)
2140Sstevel@tonic-gate break;
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate /* Skip comments and blank lines */
2170Sstevel@tonic-gate if ((*res == '#') || (*res == '\n'))
2180Sstevel@tonic-gate continue;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate name = &(buf[0]);
2210Sstevel@tonic-gate while (isspace(*name))
2220Sstevel@tonic-gate name++;
2230Sstevel@tonic-gate if (*name == '\0') /* blank line continue */
2240Sstevel@tonic-gate continue;
2250Sstevel@tonic-gate value = name; /* will contain the value eventually */
226132Srobinson while (!isspace(*value))
2270Sstevel@tonic-gate value++;
2280Sstevel@tonic-gate if (*value == '\0') {
2290Sstevel@tonic-gate syslog(LOG_WARNING,
23011262SRajagopal.Andra@Sun.COM "netname2user: badly formatted line in %s.",
23111262SRajagopal.Andra@Sun.COM NETIDFILE);
2320Sstevel@tonic-gate continue;
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate *value++ = '\0'; /* nul terminate the name */
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate if (strcasecmp(name, netname) == 0) {
2371914Scasper (void) fclose(fd);
2380Sstevel@tonic-gate while (isspace(*value))
2390Sstevel@tonic-gate value++;
2400Sstevel@tonic-gate *err = parse_netid_str(value, argp);
2410Sstevel@tonic-gate return (*err == __NSW_SUCCESS);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate }
2441914Scasper (void) fclose(fd);
2450Sstevel@tonic-gate *err = __NSW_NOTFOUND;
2460Sstevel@tonic-gate return (0);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * netname2user_nis()
2510Sstevel@tonic-gate *
2520Sstevel@tonic-gate * This function reads the netid from the NIS (YP) nameservice.
2530Sstevel@tonic-gate */
2540Sstevel@tonic-gate static int
netname2user_nis(int * err,char * netname,struct netid_userdata * argp)255132Srobinson netname2user_nis(int *err, char *netname, struct netid_userdata *argp)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate char *domain;
2580Sstevel@tonic-gate int yperr;
2590Sstevel@tonic-gate char *lookup;
2600Sstevel@tonic-gate int len;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate domain = strchr(netname, '@');
263132Srobinson if (!domain) {
2640Sstevel@tonic-gate *err = __NSW_UNAVAIL;
2650Sstevel@tonic-gate return (0);
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate /* Point past the '@' character */
2690Sstevel@tonic-gate domain++;
2700Sstevel@tonic-gate lookup = NULL;
2710Sstevel@tonic-gate yperr = yp_match(domain, (char *)NETID, netname, strlen(netname),
27211262SRajagopal.Andra@Sun.COM &lookup, &len);
2730Sstevel@tonic-gate switch (yperr) {
2740Sstevel@tonic-gate case 0:
2750Sstevel@tonic-gate break; /* the successful case */
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate default :
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate * XXX not sure about yp_match semantics.
2800Sstevel@tonic-gate * should err be set to NOTFOUND here?
2810Sstevel@tonic-gate */
2820Sstevel@tonic-gate *err = __NSW_UNAVAIL;
2830Sstevel@tonic-gate return (0);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate if (lookup) {
2860Sstevel@tonic-gate lookup[len] = '\0';
2870Sstevel@tonic-gate *err = parse_netid_str(lookup, argp);
2880Sstevel@tonic-gate free(lookup);
2890Sstevel@tonic-gate return (*err == __NSW_SUCCESS);
2900Sstevel@tonic-gate }
291132Srobinson *err = __NSW_NOTFOUND;
292132Srobinson return (0);
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate * Build the uid and gid from the netname for users in LDAP.
2970Sstevel@tonic-gate * There is no netid container in LDAP. For this we build
2980Sstevel@tonic-gate * the netname to user data dynamically from the passwd and
2990Sstevel@tonic-gate * group data. This works only for users in a single domain.
3000Sstevel@tonic-gate * This function is an interim solution until we support a
3010Sstevel@tonic-gate * netid container in LDAP which enables us to do netname2user
3020Sstevel@tonic-gate * resolution for multiple domains.
3030Sstevel@tonic-gate */
3040Sstevel@tonic-gate static int
netname2user_ldap(int * err,char * netname,struct netid_userdata * argp)305132Srobinson netname2user_ldap(int *err, char *netname, struct netid_userdata *argp)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate char buf[NSS_LINELEN_PASSWD];
308132Srobinson char *p2, *lasts;
3090Sstevel@tonic-gate struct passwd pw;
3100Sstevel@tonic-gate uid_t uidnu;
3110Sstevel@tonic-gate int ngroups = 0;
3120Sstevel@tonic-gate int count;
3130Sstevel@tonic-gate char pwbuf[NSS_LINELEN_PASSWD];
31411134SCasper.Dik@Sun.COM int maxgrp = sysconf(_SC_NGROUPS_MAX);
31511134SCasper.Dik@Sun.COM gid_t *groups = alloca(maxgrp * sizeof (gid_t));
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate if (strlcpy(buf, netname, NSS_LINELEN_PASSWD) >= NSS_LINELEN_PASSWD) {
3180Sstevel@tonic-gate *err = __NSW_UNAVAIL;
3190Sstevel@tonic-gate return (0);
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate
3220Sstevel@tonic-gate /* get the uid from the netname */
323132Srobinson if (strtok_r(buf, ".", &lasts) == NULL) {
3240Sstevel@tonic-gate *err = __NSW_UNAVAIL;
3250Sstevel@tonic-gate return (0);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate if ((p2 = strtok_r(NULL, "@", &lasts)) == NULL) {
3280Sstevel@tonic-gate *err = __NSW_UNAVAIL;
3290Sstevel@tonic-gate return (0);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate uidnu = atoi(p2);
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate * check out the primary group and crosscheck the uid
3350Sstevel@tonic-gate * with the passwd data
3360Sstevel@tonic-gate */
3370Sstevel@tonic-gate if ((getpwuid_r(uidnu, &pw, pwbuf, sizeof (pwbuf))) == NULL) {
3380Sstevel@tonic-gate *err = __NSW_UNAVAIL;
3390Sstevel@tonic-gate return (0);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate *(argp->uidp) = pw.pw_uid;
3430Sstevel@tonic-gate *(argp->gidp) = pw.pw_gid;
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate /* search through all groups for membership */
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate groups[0] = pw.pw_gid;
3480Sstevel@tonic-gate
34911134SCasper.Dik@Sun.COM ngroups = _getgroupsbymember(pw.pw_name, groups, maxgrp,
35011262SRajagopal.Andra@Sun.COM (pw.pw_gid <= MAXUID) ? 1 : 0);
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate if (ngroups < 0) {
3530Sstevel@tonic-gate *err = __NSW_UNAVAIL;
3540Sstevel@tonic-gate return (0);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate *(argp->gidlenp) = ngroups;
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate for (count = 0; count < ngroups; count++) {
3600Sstevel@tonic-gate (argp->gidlist[count]) = groups[count];
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate *err = __NSW_SUCCESS;
3640Sstevel@tonic-gate return (1);
3650Sstevel@tonic-gate
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate * Convert network-name into unix credential
3700Sstevel@tonic-gate */
3710Sstevel@tonic-gate int
netname2user(const char netname[MAXNETNAMELEN+1],uid_t * uidp,gid_t * gidp,int * gidlenp,gid_t * gidlist)372132Srobinson netname2user(const char netname[MAXNETNAMELEN + 1], uid_t *uidp, gid_t *gidp,
373132Srobinson int *gidlenp, gid_t *gidlist)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate struct __nsw_switchconfig *conf;
3760Sstevel@tonic-gate struct __nsw_lookup *look;
3770Sstevel@tonic-gate enum __nsw_parse_err perr;
3780Sstevel@tonic-gate int needfree = 1, res;
3790Sstevel@tonic-gate struct netid_userdata argp;
3800Sstevel@tonic-gate int err;
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate * Take care of the special case of nobody. Compare the netname
3840Sstevel@tonic-gate * to the string "nobody". If they are equal, return the SVID
3850Sstevel@tonic-gate * standard value for nobody.
3860Sstevel@tonic-gate */
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate if (strcmp(netname, "nobody") == 0) {
3890Sstevel@tonic-gate *uidp = NOBODY_UID;
3900Sstevel@tonic-gate *gidp = NOBODY_UID;
3910Sstevel@tonic-gate *gidlenp = 0;
3920Sstevel@tonic-gate return (1);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate /*
3960Sstevel@tonic-gate * First we do some generic sanity checks on the name we were
3970Sstevel@tonic-gate * passed. This lets us assume they are correct in the backends.
3980Sstevel@tonic-gate *
3990Sstevel@tonic-gate * NOTE: this code only recognizes names of the form :
4000Sstevel@tonic-gate * unix.UID@domainname
4010Sstevel@tonic-gate */
402132Srobinson if (strncmp(netname, OPSYS, OPSYS_LEN) != 0)
4030Sstevel@tonic-gate return (0);
404132Srobinson if (!isdigit(netname[OPSYS_LEN+1])) /* check for uid string */
4050Sstevel@tonic-gate return (0);
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate argp.uidp = uidp;
4080Sstevel@tonic-gate argp.gidp = gidp;
4090Sstevel@tonic-gate argp.gidlenp = gidlenp;
4100Sstevel@tonic-gate argp.gidlist = gidlist;
411132Srobinson (void) mutex_lock(&serialize_netname_r);
4120Sstevel@tonic-gate
4130Sstevel@tonic-gate conf = __nsw_getconfig("publickey", &perr);
414132Srobinson if (!conf) {
4150Sstevel@tonic-gate conf = &publickey_default;
4160Sstevel@tonic-gate needfree = 0;
4170Sstevel@tonic-gate } else
4180Sstevel@tonic-gate needfree = 1; /* free the config structure */
4190Sstevel@tonic-gate
4200Sstevel@tonic-gate for (look = conf->lookups; look; look = look->next) {
42111262SRajagopal.Andra@Sun.COM if (strcmp(look->service_name, "nis") == 0)
4220Sstevel@tonic-gate res = netname2user_nis(&err, (char *)netname, &argp);
4230Sstevel@tonic-gate else if (strcmp(look->service_name, "files") == 0)
4240Sstevel@tonic-gate res = netname2user_files(&err, (char *)netname, &argp);
4250Sstevel@tonic-gate else if (strcmp(look->service_name, "ldap") == 0)
4260Sstevel@tonic-gate res = netname2user_ldap(&err, (char *)netname, &argp);
4270Sstevel@tonic-gate else {
4280Sstevel@tonic-gate syslog(LOG_INFO,
42911262SRajagopal.Andra@Sun.COM "netname2user: unknown nameservice for publickey"
43011262SRajagopal.Andra@Sun.COM "info '%s'\n", look->service_name);
4310Sstevel@tonic-gate err = __NSW_UNAVAIL;
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate switch (look->actions[err]) {
4340Sstevel@tonic-gate case __NSW_CONTINUE :
4350Sstevel@tonic-gate break;
4360Sstevel@tonic-gate case __NSW_RETURN :
4370Sstevel@tonic-gate if (needfree)
438*11411SSurya.Prakki@Sun.COM (void) __nsw_freeconfig(conf);
439132Srobinson (void) mutex_unlock(&serialize_netname_r);
4400Sstevel@tonic-gate return (res);
4410Sstevel@tonic-gate default :
4420Sstevel@tonic-gate syslog(LOG_ERR,
44311262SRajagopal.Andra@Sun.COM "netname2user: Unknown action for "
44411262SRajagopal.Andra@Sun.COM "nameservice '%s'", look->service_name);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate if (needfree)
448*11411SSurya.Prakki@Sun.COM (void) __nsw_freeconfig(conf);
449132Srobinson (void) mutex_unlock(&serialize_netname_r);
4500Sstevel@tonic-gate return (0);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate * Convert network-name to hostname (fully qualified)
4550Sstevel@tonic-gate * NOTE: this code only recognizes names of the form :
4560Sstevel@tonic-gate * unix.HOST@domainname
4570Sstevel@tonic-gate *
4580Sstevel@tonic-gate * This is very simple. Since the netname is of the form:
4590Sstevel@tonic-gate * unix.host@domainname
4600Sstevel@tonic-gate * We just construct the hostname using information from the domainname.
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate int
netname2host(const char netname[MAXNETNAMELEN+1],char * hostname,const int hostlen)463132Srobinson netname2host(const char netname[MAXNETNAMELEN + 1], char *hostname,
464132Srobinson const int hostlen)
4650Sstevel@tonic-gate {
4660Sstevel@tonic-gate char *p, *domainname;
4670Sstevel@tonic-gate int len, dlen;
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate if (!netname) {
4700Sstevel@tonic-gate syslog(LOG_ERR, "netname2host: null netname");
4710Sstevel@tonic-gate goto bad_exit;
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate if (strncmp(netname, OPSYS, OPSYS_LEN) != 0)
4750Sstevel@tonic-gate goto bad_netname;
4760Sstevel@tonic-gate p = (char *)netname + OPSYS_LEN; /* skip OPSYS part */
4770Sstevel@tonic-gate if (*p != '.')
4780Sstevel@tonic-gate goto bad_netname;
4790Sstevel@tonic-gate ++p; /* skip '.' */
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate domainname = strchr(p, '@'); /* get domain name */
4820Sstevel@tonic-gate if (domainname == 0)
4830Sstevel@tonic-gate goto bad_netname;
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate len = domainname - p; /* host sits between '.' and '@' */
4860Sstevel@tonic-gate domainname++; /* skip '@' sign */
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate if (len <= 0)
4890Sstevel@tonic-gate goto bad_netname;
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate if (hostlen < len) {
4920Sstevel@tonic-gate syslog(LOG_ERR,
49311262SRajagopal.Andra@Sun.COM "netname2host: insufficient space for hostname");
4940Sstevel@tonic-gate goto bad_exit;
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate
4970Sstevel@tonic-gate if (isdigit(*p)) /* don't want uid here */
4980Sstevel@tonic-gate goto bad_netname;
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate if (*p == '\0') /* check for null hostname */
5010Sstevel@tonic-gate goto bad_netname;
5020Sstevel@tonic-gate
503132Srobinson (void) strncpy(hostname, p, len);
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate /* make into fully qualified hostname by concatenating domain part */
5060Sstevel@tonic-gate dlen = strlen(domainname);
5070Sstevel@tonic-gate if (hostlen < (len + dlen + 2)) {
5080Sstevel@tonic-gate syslog(LOG_ERR,
50911262SRajagopal.Andra@Sun.COM "netname2host: insufficient space for hostname");
5100Sstevel@tonic-gate goto bad_exit;
5110Sstevel@tonic-gate }
5120Sstevel@tonic-gate
5130Sstevel@tonic-gate hostname[len] = '.';
514132Srobinson (void) strncpy(hostname+len+1, domainname, dlen);
5150Sstevel@tonic-gate hostname[len+dlen+1] = '\0';
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate return (1);
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate bad_netname:
5200Sstevel@tonic-gate syslog(LOG_ERR, "netname2host: invalid host netname %s", netname);
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate bad_exit:
5230Sstevel@tonic-gate hostname[0] = '\0';
5240Sstevel@tonic-gate return (0);
5250Sstevel@tonic-gate }
526