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