Upgrade less(1). 1/2
[dragonfly.git] / lib / libc / rpc / netname.c
1 /*-
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without 
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice, 
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice, 
10  *   this list of conditions and the following disclaimer in the documentation 
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its 
13  *   contributors may be used to endorse or promote products derived 
14  *   from this software without specific prior written permission.
15  * 
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/lib/libc/rpc/netname.c,v 1.8 2004/10/16 06:11:35 obrien Exp $
29  *
30  * @(#)netname.c 1.8 91/03/11 Copyr 1986 Sun Micro
31  */
32
33 /*
34  * netname utility routines
35  * convert from unix names to network names and vice-versa
36  * This module is operating system dependent!
37  * What we define here will work with any unix system that has adopted
38  * the sun NIS domain architecture.
39  */
40
41 #include "namespace.h"
42 #include <sys/param.h>
43 #include <rpc/rpc.h>
44 #include <rpc/rpc_com.h>
45 #ifdef YP
46 #include <rpcsvc/yp_prot.h>
47 #include <rpcsvc/ypclnt.h>
48 #endif
49 #include <ctype.h>
50 #include <limits.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include "un-namespace.h"
56
57 #ifndef MAXHOSTNAMELEN
58 #define MAXHOSTNAMELEN 256
59 #endif
60 #ifndef NGROUPS
61 #define NGROUPS 16
62 #endif
63
64 #define TYPE_BIT(type)  (sizeof (type) * CHAR_BIT)
65
66 /*
67 ** 302 / 1000 is log10(2.0) rounded up.
68 ** Subtract one for the sign bit if the type is signed;
69 ** add one for integer division truncation;
70 ** add one more for a minus sign if the type is signed.
71 */
72 #define U_INT_STRLEN_MAXIMUM(type) \
73     (TYPE_BIT(type) * 302 / 1000 + 1)
74 #define S_INT_STRLEN_MAXIMUM(type) \
75     ((TYPE_BIT(type) - 1) * 302 / 1000 + 1 + 1)
76
77 static char *OPSYS = "unix";
78
79 /*
80  * Figure out my fully qualified network name
81  */
82 int
83 getnetname(char *name)
84 {
85         uid_t uid;
86
87         uid = geteuid();
88         if (uid == 0) {
89                 return (host2netname(name, NULL, NULL));
90         } else {
91                 return (user2netname(name, uid, NULL));
92         }
93 }
94
95
96 /*
97  * Convert unix cred to network-name
98  */
99 int
100 user2netname(char *netname, const uid_t uid, const char *domain)
101 {
102         char *dfltdom;
103
104         if (domain == NULL) {
105                 if (__rpc_get_default_domain(&dfltdom) != 0) {
106                         return (0);
107                 }
108                 domain = dfltdom;
109         }
110         if (strlen(domain) + 1 + U_INT_STRLEN_MAXIMUM(u_long) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
111                 return (0);
112         }
113         sprintf(netname, "%s.%ld@%s", OPSYS, (u_long)uid, domain);      
114         return (1);
115 }
116
117
118 /*
119  * Convert host to network-name
120  */
121 int
122 host2netname(char *netname, const char *host, const char *domain)
123 {
124         char *dfltdom;
125         char hostname[MAXHOSTNAMELEN+1];
126
127         if (domain == NULL) {
128                 if (__rpc_get_default_domain(&dfltdom) != 0) {
129                         return (0);
130                 }
131                 domain = dfltdom;
132         }
133         if (host == NULL) {
134                 gethostname(hostname, sizeof(hostname));
135                 host = hostname;
136         }
137         if (strlen(domain) + 1 + strlen(host) + 1 + strlen(OPSYS) > MAXNETNAMELEN) {
138                 return (0);
139         } 
140         sprintf(netname, "%s.%s@%s", OPSYS, host, domain);
141         return (1);
142 }