MFC r1.28:
[dragonfly.git] / lib / libc / rpc / netname.c
blob456465288e27153b3bec9d424e0451289533085e
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
30 * $FreeBSD: src/lib/libc/rpc/netname.c,v 1.2.6.1 2000/08/23 00:05:29 jhb Exp $
31 * $DragonFly: src/lib/libc/rpc/netname.c,v 1.3 2005/11/13 12:27:04 swildner Exp $
33 * @(#)netname.c 1.8 91/03/11 Copyr 1986 Sun Micro
37 * netname utility routines
38 * convert from unix names to network names and vice-versa
39 * This module is operating system dependent!
40 * What we define here will work with any unix system that has adopted
41 * the sun NIS domain architecture.
44 #include <sys/param.h>
45 #include <rpc/rpc.h>
46 #include <rpc/rpc_com.h>
47 #ifdef YP
48 #include <rpcsvc/yp_prot.h>
49 #include <rpcsvc/ypclnt.h>
50 #endif
51 #include <ctype.h>
52 #include <limits.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
58 #ifndef MAXHOSTNAMELEN
59 #define MAXHOSTNAMELEN 256
60 #endif
61 #ifndef NGROUPS
62 #define NGROUPS 16
63 #endif
65 #define TYPE_BIT(type) (sizeof (type) * CHAR_BIT)
67 #define TYPE_SIGNED(type) (((type) -1) < 0)
70 ** 302 / 1000 is log10(2.0) rounded up.
71 ** Subtract one for the sign bit if the type is signed;
72 ** add one for integer division truncation;
73 ** add one more for a minus sign if the type is signed.
75 #define INT_STRLEN_MAXIMUM(type) \
76 ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type))
78 static char *OPSYS = "unix";
81 * Figure out my fully qualified network name
83 int
84 getnetname(char *name)
86 uid_t uid;
88 uid = geteuid();
89 if (uid == 0) {
90 return (host2netname(name, (char *) NULL, (char *) NULL));
91 } else {
92 return (user2netname(name, uid, (char *) NULL));
98 * Convert unix cred to network-name
101 user2netname(char *netname, uid_t uid, char *domain)
103 char *dfltdom;
105 if (domain == NULL) {
106 if (_rpc_get_default_domain(&dfltdom) != 0) {
107 return (0);
109 domain = dfltdom;
111 if (strlen(domain) + 1 + INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
112 return (0);
114 sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);
115 return (1);
120 * Convert host to network-name
123 host2netname(char *netname, char *host, char *domain)
125 char *dfltdom;
126 char hostname[MAXHOSTNAMELEN+1];
128 if (domain == NULL) {
129 if (_rpc_get_default_domain(&dfltdom) != 0) {
130 return (0);
132 domain = dfltdom;
134 if (host == NULL) {
135 gethostname(hostname, sizeof(hostname));
136 host = hostname;
138 if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
139 return (0);
141 sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
142 return (1);