1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/mman.h> 28 #include <stdio.h> 29 #include <string.h> 30 #include <stdlib.h> 31 #include <nss_dbdefs.h> 32 #include <user_attr.h> 33 #include <getxby_door.h> 34 #include <pwd.h> 35 36 37 /* Externs from libnsl */ 38 extern userstr_t *_getusernam(const char *, userstr_t *, char *, int, int *); 39 extern userstr_t *_getuserattr(userstr_t *, char *, int, int *); 40 extern userstr_t *_fgetuserattr(FILE *, userstr_t *, char *, int); 41 extern void _setuserattr(void); 42 extern void _enduserattr(void); 43 44 45 static userattr_t *userstr2attr(userstr_t *); 46 47 48 userattr_t * 49 getuserattr() 50 { 51 int err = 0; 52 char buf[NSS_BUFLEN_USERATTR]; 53 userstr_t user; 54 userstr_t *tmp; 55 56 (void) memset(&user, 0, sizeof (userattr_t)); 57 tmp = _getuserattr(&user, buf, NSS_BUFLEN_USERATTR, &err); 58 return (userstr2attr(tmp)); 59 } 60 61 62 userattr_t * 63 fgetuserattr(FILE *f) 64 { 65 char buf[NSS_BUFLEN_USERATTR]; 66 userstr_t user; 67 userstr_t *tmp; 68 69 (void) memset(&user, 0, sizeof (userattr_t)); 70 tmp = _fgetuserattr(f, &user, buf, NSS_BUFLEN_USERATTR); 71 return (userstr2attr(tmp)); 72 } 73 74 75 userattr_t * 76 getusernam(const char *name) 77 { 78 int err = 0; 79 char buf[NSS_BUFLEN_USERATTR]; 80 userstr_t user; 81 userstr_t *resptr = (userstr_t *)NULL; 82 83 resptr = _getusernam(name, &user, buf, NSS_BUFLEN_USERATTR, &err); 84 85 return (userstr2attr(resptr)); 86 87 } 88 89 90 userattr_t * 91 getuseruid(uid_t u) 92 { 93 struct passwd pwd; 94 char buf[NSS_BUFLEN_PASSWD]; 95 96 if (getpwuid_r(u, &pwd, buf, NSS_BUFLEN_PASSWD) == NULL) 97 return ((userattr_t *)NULL); 98 return (getusernam(pwd.pw_name)); 99 } 100 101 102 void 103 setuserattr() 104 { 105 _setuserattr(); 106 } 107 108 109 void 110 enduserattr() 111 { 112 _enduserattr(); 113 } 114 115 116 void 117 free_userattr(userattr_t *user) 118 { 119 if (user) { 120 free(user->name); 121 free(user->qualifier); 122 free(user->res1); 123 free(user->res2); 124 _kva_free(user->attr); 125 free(user); 126 } 127 } 128 129 130 static userattr_t * 131 userstr2attr(userstr_t *user) 132 { 133 userattr_t *newuser; 134 135 if (user == NULL) 136 return ((userattr_t *)NULL); 137 138 if ((newuser = (userattr_t *)malloc(sizeof (userattr_t))) == NULL) 139 return ((userattr_t *)NULL); 140 141 newuser->name = _do_unescape(user->name); 142 newuser->qualifier = _do_unescape(user->qualifier); 143 newuser->res1 = _do_unescape(user->res1); 144 newuser->res2 = _do_unescape(user->res2); 145 newuser->attr = _str2kva(user->attr, KV_ASSIGN, KV_DELIMITER); 146 return (newuser); 147 } 148 149 150 #ifdef DEBUG 151 void 152 print_userattr(userattr_t *user) 153 { 154 extern void print_kva(kva_t *); 155 char *empty = "empty"; 156 157 if (user == NULL) { 158 printf("NULL\n"); 159 return; 160 } 161 162 printf("name=%s\n", user->name ? user->name : empty); 163 printf("qualifier=%s\n", user->qualifier ? user->qualifier : empty); 164 printf("res1=%s\n", user->res1 ? user->res1 : empty); 165 printf("res2=%s\n", user->res2 ? user->res2 : empty); 166 printf("attr=\n"); 167 print_kva(user->attr); 168 fflush(stdout); 169 } 170 #endif /* DEBUG */ 171