]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - lib/libc/rpc/netname.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / lib / libc / rpc / netname.c
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.
9  *
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.
13  *
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.
17  *
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.
21  *
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.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30
31 #if defined(LIBC_SCCS) && !defined(lint)
32 static char sccsid[] = "@(#)netname.c 1.8 91/03/11 Copyr 1986 Sun Micro";
33 #endif
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 /*
38  * netname utility routines
39  * convert from unix names to network names and vice-versa
40  * This module is operating system dependent!
41  * What we define here will work with any unix system that has adopted
42  * the sun NIS domain architecture.
43  */
44
45 #include "namespace.h"
46 #include <sys/param.h>
47 #include <rpc/rpc.h>
48 #include <rpc/rpc_com.h>
49 #ifdef YP
50 #include <rpcsvc/yp_prot.h>
51 #include <rpcsvc/ypclnt.h>
52 #endif
53 #include <ctype.h>
54 #include <limits.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include "un-namespace.h"
60
61 #ifndef MAXHOSTNAMELEN
62 #define MAXHOSTNAMELEN 256
63 #endif
64 #ifndef NGROUPS
65 #define NGROUPS 16
66 #endif
67
68 #define TYPE_BIT(type)  (sizeof (type) * CHAR_BIT)
69
70 #define TYPE_SIGNED(type) (((type) -1) < 0)
71
72 /*
73 ** 302 / 1000 is log10(2.0) rounded up.
74 ** Subtract one for the sign bit if the type is signed;
75 ** add one for integer division truncation;
76 ** add one more for a minus sign if the type is signed.
77 */
78 #define INT_STRLEN_MAXIMUM(type) \
79     ((TYPE_BIT(type) - TYPE_SIGNED(type)) * 302 / 1000 + 1 + TYPE_SIGNED(type))
80
81 static char *OPSYS = "unix";
82
83 /*
84  * Figure out my fully qualified network name
85  */
86 int
87 getnetname(name)
88         char name[MAXNETNAMELEN+1];
89 {
90         uid_t uid;
91
92         uid = geteuid();
93         if (uid == 0) {
94                 return (host2netname(name, (char *) NULL, (char *) NULL));
95         } else {
96                 return (user2netname(name, uid, (char *) NULL));
97         }
98 }
99
100
101 /*
102  * Convert unix cred to network-name
103  */
104 int
105 user2netname(netname, uid, domain)
106         char netname[MAXNETNAMELEN + 1];
107         const uid_t uid;
108         const char *domain;
109 {
110         char *dfltdom;
111
112         if (domain == NULL) {
113                 if (__rpc_get_default_domain(&dfltdom) != 0) {
114                         return (0);
115                 }
116                 domain = dfltdom;
117         }
118         if (strlen(domain) + 1 + INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
119                 return (0);
120         }
121         (void) sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);       
122         return (1);
123 }
124
125
126 /*
127  * Convert host to network-name
128  */
129 int
130 host2netname(netname, host, domain)
131         char netname[MAXNETNAMELEN + 1];
132         const char *host;
133         const char *domain;
134 {
135         char *dfltdom;
136         char hostname[MAXHOSTNAMELEN+1];
137
138         if (domain == NULL) {
139                 if (__rpc_get_default_domain(&dfltdom) != 0) {
140                         return (0);
141                 }
142                 domain = dfltdom;
143         }
144         if (host == NULL) {
145                 (void) gethostname(hostname, sizeof(hostname));
146                 host = hostname;
147         }
148         if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
149                 return (0);
150         } 
151         (void) sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
152         return (1);
153 }