2013-11-25 19:04:36 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009, Sun Microsystems, Inc.
|
|
|
|
* All rights reserved.
|
1997-05-28 05:00:11 +00:00
|
|
|
*
|
2013-11-25 19:04:36 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* - Neither the name of Sun Microsystems, Inc. nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
1997-05-28 05:00:11 +00:00
|
|
|
*/
|
2004-10-16 06:11:35 +00:00
|
|
|
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
1997-05-28 05:00:11 +00:00
|
|
|
static char sccsid[] = "@(#)netnamer.c 1.13 91/03/11 Copyr 1986 Sun Micro";
|
|
|
|
#endif
|
2002-03-22 23:18:37 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
2004-10-16 06:11:35 +00:00
|
|
|
|
1997-05-28 05:00:11 +00:00
|
|
|
/*
|
|
|
|
* netname utility routines convert from unix names to network names and
|
|
|
|
* vice-versa This module is operating system dependent! What we define here
|
|
|
|
* will work with any unix system that has adopted the sun NIS domain
|
|
|
|
* architecture.
|
|
|
|
*/
|
Bring in a hybrid of SunSoft's transport-independent RPC (TI-RPC) and
associated changes that had to happen to make this possible as well as
bugs fixed along the way.
Bring in required TLI library routines to support this.
Since we don't support TLI we've essentially copied what NetBSD
has done, adding a thin layer to emulate direct the TLI calls
into BSD socket calls.
This is mostly from Sun's tirpc release that was made in 1994,
however some fixes were backported from the 1999 release (supposedly
only made available after this porting effort was underway).
The submitter has agreed to continue on and bring us up to the
1999 release.
Several key features are introduced with this update:
Client calls are thread safe. (1999 code has server side thread
safe)
Updated, a more modern interface.
Many userland updates were done to bring the code up to par with
the recent RPC API.
There is an update to the pthreads library, a function
pthread_main_np() was added to emulate a function of Sun's threads
library.
While we're at it, bring in NetBSD's lockd, it's been far too
long of a wait.
New rpcbind(8) replaces portmap(8) (supporting communication over
an authenticated Unix-domain socket, and by default only allowing
set and unset requests over that channel). It's much more secure
than the old portmapper.
Umount(8), mountd(8), mount_nfs(8), nfsd(8) have also been upgraded
to support TI-RPC and to support IPV6.
Umount(8) is also fixed to unmount pathnames longer than 80 chars,
which are currently truncated by the Kernel statfs structure.
Submitted by: Martin Blapp <mb@imp.ch>
Manpage review: ru
Secure RPC implemented by: wpaul
2001-03-19 12:50:13 +00:00
|
|
|
#include "namespace.h"
|
1997-05-28 05:00:11 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <rpc/rpc.h>
|
|
|
|
#include <rpc/rpc_com.h>
|
|
|
|
#ifdef YP
|
|
|
|
#include <rpcsvc/yp_prot.h>
|
|
|
|
#include <rpcsvc/ypclnt.h>
|
|
|
|
#endif
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
Bring in a hybrid of SunSoft's transport-independent RPC (TI-RPC) and
associated changes that had to happen to make this possible as well as
bugs fixed along the way.
Bring in required TLI library routines to support this.
Since we don't support TLI we've essentially copied what NetBSD
has done, adding a thin layer to emulate direct the TLI calls
into BSD socket calls.
This is mostly from Sun's tirpc release that was made in 1994,
however some fixes were backported from the 1999 release (supposedly
only made available after this porting effort was underway).
The submitter has agreed to continue on and bring us up to the
1999 release.
Several key features are introduced with this update:
Client calls are thread safe. (1999 code has server side thread
safe)
Updated, a more modern interface.
Many userland updates were done to bring the code up to par with
the recent RPC API.
There is an update to the pthreads library, a function
pthread_main_np() was added to emulate a function of Sun's threads
library.
While we're at it, bring in NetBSD's lockd, it's been far too
long of a wait.
New rpcbind(8) replaces portmap(8) (supporting communication over
an authenticated Unix-domain socket, and by default only allowing
set and unset requests over that channel). It's much more secure
than the old portmapper.
Umount(8), mountd(8), mount_nfs(8), nfsd(8) have also been upgraded
to support TI-RPC and to support IPV6.
Umount(8) is also fixed to unmount pathnames longer than 80 chars,
which are currently truncated by the Kernel statfs structure.
Submitted by: Martin Blapp <mb@imp.ch>
Manpage review: ru
Secure RPC implemented by: wpaul
2001-03-19 12:50:13 +00:00
|
|
|
#include "un-namespace.h"
|
1997-05-28 05:00:11 +00:00
|
|
|
|
|
|
|
static char *OPSYS = "unix";
|
2004-11-13 20:40:32 +00:00
|
|
|
#ifdef YP
|
1997-05-28 05:00:11 +00:00
|
|
|
static char *NETID = "netid.byname";
|
2004-11-13 20:40:32 +00:00
|
|
|
#endif
|
1997-05-28 05:00:11 +00:00
|
|
|
static char *NETIDFILE = "/etc/netid";
|
|
|
|
|
2002-03-21 22:49:10 +00:00
|
|
|
static int getnetid( char *, char * );
|
|
|
|
static int _getgroups( char *, gid_t * );
|
1997-05-28 05:00:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert network-name into unix credential
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
netname2user(netname, uidp, gidp, gidlenp, gidlist)
|
|
|
|
char netname[MAXNETNAMELEN + 1];
|
|
|
|
uid_t *uidp;
|
|
|
|
gid_t *gidp;
|
|
|
|
int *gidlenp;
|
|
|
|
gid_t *gidlist;
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int gidlen;
|
|
|
|
uid_t uid;
|
1998-06-30 17:21:48 +00:00
|
|
|
long luid;
|
1997-05-28 05:00:11 +00:00
|
|
|
struct passwd *pwd;
|
|
|
|
char val[1024];
|
|
|
|
char *val1, *val2;
|
|
|
|
char *domain;
|
|
|
|
int vallen;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (getnetid(netname, val)) {
|
2000-08-29 21:04:07 +00:00
|
|
|
char *res = val;
|
|
|
|
|
|
|
|
p = strsep(&res, ":");
|
1997-05-28 05:00:11 +00:00
|
|
|
if (p == NULL)
|
|
|
|
return (0);
|
2000-08-29 21:04:07 +00:00
|
|
|
*uidp = (uid_t) atol(p);
|
|
|
|
p = strsep(&res, "\n,");
|
1997-05-28 05:00:11 +00:00
|
|
|
if (p == NULL) {
|
|
|
|
return (0);
|
|
|
|
}
|
2000-08-29 21:04:07 +00:00
|
|
|
*gidp = (gid_t) atol(p);
|
2009-06-19 17:10:35 +00:00
|
|
|
for (gidlen = 0; gidlen < NGRPS; gidlen++) {
|
2000-08-29 21:04:07 +00:00
|
|
|
p = strsep(&res, "\n,");
|
1997-05-28 05:00:11 +00:00
|
|
|
if (p == NULL)
|
|
|
|
break;
|
|
|
|
gidlist[gidlen] = (gid_t) atol(p);
|
|
|
|
}
|
|
|
|
*gidlenp = gidlen;
|
|
|
|
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
val1 = strchr(netname, '.');
|
|
|
|
if (val1 == NULL)
|
|
|
|
return (0);
|
|
|
|
if (strncmp(netname, OPSYS, (val1-netname)))
|
|
|
|
return (0);
|
|
|
|
val1++;
|
|
|
|
val2 = strchr(val1, '@');
|
|
|
|
if (val2 == NULL)
|
|
|
|
return (0);
|
|
|
|
vallen = val2 - val1;
|
|
|
|
if (vallen > (1024 - 1))
|
|
|
|
vallen = 1024 - 1;
|
|
|
|
(void) strncpy(val, val1, 1024);
|
|
|
|
val[vallen] = 0;
|
|
|
|
|
2002-02-05 23:43:43 +00:00
|
|
|
err = __rpc_get_default_domain(&domain); /* change to rpc */
|
1997-05-28 05:00:11 +00:00
|
|
|
if (err)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (strcmp(val2 + 1, domain))
|
|
|
|
return (0); /* wrong domain */
|
|
|
|
|
1998-06-30 17:21:48 +00:00
|
|
|
if (sscanf(val, "%ld", &luid) != 1)
|
1997-05-28 05:00:11 +00:00
|
|
|
return (0);
|
1998-06-30 17:21:48 +00:00
|
|
|
uid = luid;
|
|
|
|
|
1997-05-28 05:00:11 +00:00
|
|
|
/* use initgroups method */
|
|
|
|
pwd = getpwuid(uid);
|
|
|
|
if (pwd == NULL)
|
|
|
|
return (0);
|
|
|
|
*uidp = pwd->pw_uid;
|
|
|
|
*gidp = pwd->pw_gid;
|
|
|
|
*gidlenp = _getgroups(pwd->pw_name, gidlist);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* initgroups
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
_getgroups(uname, groups)
|
|
|
|
char *uname;
|
2009-06-19 17:10:35 +00:00
|
|
|
gid_t groups[NGRPS];
|
1997-05-28 05:00:11 +00:00
|
|
|
{
|
|
|
|
gid_t ngroups = 0;
|
2002-03-21 18:49:23 +00:00
|
|
|
struct group *grp;
|
|
|
|
int i;
|
|
|
|
int j;
|
1997-05-28 05:00:11 +00:00
|
|
|
int filter;
|
|
|
|
|
|
|
|
setgrent();
|
|
|
|
while ((grp = getgrent())) {
|
|
|
|
for (i = 0; grp->gr_mem[i]; i++)
|
|
|
|
if (!strcmp(grp->gr_mem[i], uname)) {
|
2009-06-19 17:10:35 +00:00
|
|
|
if (ngroups == NGRPS) {
|
1997-05-28 05:00:11 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,
|
|
|
|
"initgroups: %s is in too many groups\n", uname);
|
|
|
|
#endif
|
|
|
|
goto toomany;
|
|
|
|
}
|
|
|
|
/* filter out duplicate group entries */
|
|
|
|
filter = 0;
|
|
|
|
for (j = 0; j < ngroups; j++)
|
|
|
|
if (groups[j] == grp->gr_gid) {
|
|
|
|
filter++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!filter)
|
|
|
|
groups[ngroups++] = grp->gr_gid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
toomany:
|
|
|
|
endgrent();
|
|
|
|
return (ngroups);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert network-name to hostname
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
netname2host(netname, hostname, hostlen)
|
|
|
|
char netname[MAXNETNAMELEN + 1];
|
|
|
|
char *hostname;
|
|
|
|
int hostlen;
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
char valbuf[1024];
|
|
|
|
char *val;
|
|
|
|
char *val2;
|
|
|
|
int vallen;
|
|
|
|
char *domain;
|
|
|
|
|
|
|
|
if (getnetid(netname, valbuf)) {
|
|
|
|
val = valbuf;
|
|
|
|
if ((*val == '0') && (val[1] == ':')) {
|
|
|
|
(void) strncpy(hostname, val + 2, hostlen);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
val = strchr(netname, '.');
|
|
|
|
if (val == NULL)
|
|
|
|
return (0);
|
|
|
|
if (strncmp(netname, OPSYS, (val - netname)))
|
|
|
|
return (0);
|
|
|
|
val++;
|
|
|
|
val2 = strchr(val, '@');
|
|
|
|
if (val2 == NULL)
|
|
|
|
return (0);
|
|
|
|
vallen = val2 - val;
|
|
|
|
if (vallen > (hostlen - 1))
|
|
|
|
vallen = hostlen - 1;
|
|
|
|
(void) strncpy(hostname, val, vallen);
|
|
|
|
hostname[vallen] = 0;
|
|
|
|
|
2002-02-05 23:43:43 +00:00
|
|
|
err = __rpc_get_default_domain(&domain); /* change to rpc */
|
1997-05-28 05:00:11 +00:00
|
|
|
if (err)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (strcmp(val2 + 1, domain))
|
|
|
|
return (0); /* wrong domain */
|
|
|
|
else
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* reads the file /etc/netid looking for a + to optionally go to the
|
|
|
|
* network information service.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
getnetid(key, ret)
|
|
|
|
char *key, *ret;
|
|
|
|
{
|
|
|
|
char buf[1024]; /* big enough */
|
|
|
|
char *res;
|
|
|
|
char *mkey;
|
|
|
|
char *mval;
|
|
|
|
FILE *fd;
|
|
|
|
#ifdef YP
|
|
|
|
char *domain;
|
|
|
|
int err;
|
|
|
|
char *lookup;
|
|
|
|
int len;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fd = fopen(NETIDFILE, "r");
|
2000-08-29 21:04:07 +00:00
|
|
|
if (fd == NULL) {
|
1997-05-28 05:00:11 +00:00
|
|
|
#ifdef YP
|
|
|
|
res = "+";
|
|
|
|
goto getnetidyp;
|
|
|
|
#else
|
|
|
|
return (0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
for (;;) {
|
2000-08-29 21:04:07 +00:00
|
|
|
if (fd == NULL)
|
1997-05-28 05:00:11 +00:00
|
|
|
return (0); /* getnetidyp brings us here */
|
2000-08-29 21:04:07 +00:00
|
|
|
res = fgets(buf, sizeof(buf), fd);
|
|
|
|
if (res == NULL) {
|
1997-05-28 05:00:11 +00:00
|
|
|
fclose(fd);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (res[0] == '#')
|
|
|
|
continue;
|
|
|
|
else if (res[0] == '+') {
|
|
|
|
#ifdef YP
|
|
|
|
getnetidyp:
|
|
|
|
err = yp_get_default_domain(&domain);
|
|
|
|
if (err) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lookup = NULL;
|
|
|
|
err = yp_match(domain, NETID, key,
|
|
|
|
strlen(key), &lookup, &len);
|
|
|
|
if (err) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "match failed error %d\n", err);
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
lookup[len] = 0;
|
|
|
|
strcpy(ret, lookup);
|
|
|
|
free(lookup);
|
1997-06-12 18:42:43 +00:00
|
|
|
if (fd != NULL)
|
|
|
|
fclose(fd);
|
1997-05-28 05:00:11 +00:00
|
|
|
return (2);
|
|
|
|
#else /* YP */
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,
|
|
|
|
"Bad record in %s '+' -- NIS not supported in this library copy\n",
|
|
|
|
NETIDFILE);
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
#endif /* YP */
|
|
|
|
} else {
|
2000-08-29 21:04:07 +00:00
|
|
|
mkey = strsep(&res, "\t ");
|
1997-05-28 05:00:11 +00:00
|
|
|
if (mkey == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Bad record in %s -- %s", NETIDFILE, buf);
|
|
|
|
continue;
|
|
|
|
}
|
2000-08-29 21:04:07 +00:00
|
|
|
do {
|
|
|
|
mval = strsep(&res, " \t#\n");
|
|
|
|
} while (mval != NULL && !*mval);
|
1997-05-28 05:00:11 +00:00
|
|
|
if (mval == NULL) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Bad record in %s val problem - %s", NETIDFILE, buf);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcmp(mkey, key) == 0) {
|
|
|
|
strcpy(ret, mval);
|
|
|
|
fclose(fd);
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|