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