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 /* 23 * Copyright (c) 2013 Gary Mills 24 * 25 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 29 /* Copyright (c) 1988 AT&T */ 30 /* All Rights Reserved */ 31 32 #pragma weak _getlogin = getloginx 33 #pragma weak _getlogin_r = getloginx_r 34 35 #include "lint.h" 36 #include <sys/types.h> 37 #include <sys/stat.h> 38 #include <fcntl.h> 39 #include <string.h> 40 #include <stdlib.h> 41 #include <limits.h> 42 #include "utmpx.h" 43 #include <unistd.h> 44 #include <errno.h> 45 #include <thread.h> 46 #include <synch.h> 47 #include <mtlib.h> 48 #include "tsd.h" 49 50 /* Revert the renames done in unistd.h */ 51 #ifdef __PRAGMA_REDEFINE_EXTNAME 52 #pragma redefine_extname getlogint getlogin 53 #pragma redefine_extname getlogint_r getlogin_r 54 #pragma redefine_extname __posix_getlogint_r __posix_getlogin_r 55 #else /* __PRAGMA_REDEFINE_EXTNAME */ 56 #ifdef getlogin 57 #undef getlogin 58 #endif /* getlogin */ 59 #ifdef getlogin_r 60 #undef getlogin_r 61 #endif /* getlogin_r */ 62 #ifdef __posix_getlogin_r 63 #undef __posix_getlogin_r 64 #endif /* __posix_getlogin_r */ 65 #define getlogint getlogin 66 #define getlogint_r getlogin_r 67 #define __posix_getlogint_r __posix_getlogin_r 68 #endif /* __PRAGMA_REDEFINE_EXTNAME */ 69 extern char *getlogint(void); 70 extern char *getlogint_r(char *, int); 71 extern int __posix_getlogint_r(char *, int); 72 73 /* 74 * Use the full length of a login name. 75 * The utmpx interface provides for a 32 character login name. 76 */ 77 #define NMAX (sizeof (((struct utmpx *)0)->ut_user)) 78 79 /* 80 * Common function 81 */ 82 static char * 83 getl_r_common(char *answer, size_t namelen, size_t maxlen) 84 { 85 int uf; 86 off64_t me; 87 struct futmpx ubuf; 88 89 if ((me = (off64_t)ttyslot()) < 0) 90 return (NULL); 91 if ((uf = open64(UTMPX_FILE, 0)) < 0) 92 return (NULL); 93 (void) lseek64(uf, me * sizeof (ubuf), SEEK_SET); 94 if (read(uf, &ubuf, sizeof (ubuf)) != sizeof (ubuf)) { 95 (void) close(uf); 96 return (NULL); 97 } 98 (void) close(uf); 99 if (ubuf.ut_user[0] == '\0') 100 return (NULL); 101 102 /* Insufficient buffer size */ 103 if (namelen < strnlen(&ubuf.ut_user[0], maxlen)) { 104 errno = ERANGE; 105 return (NULL); 106 } 107 (void) strncpy(&answer[0], &ubuf.ut_user[0], maxlen); 108 answer[maxlen] = '\0'; 109 return (&answer[0]); 110 } 111 112 /* 113 * POSIX.1c Draft-6 version of the function getlogin_r. 114 * It was implemented by Solaris 2.3. 115 */ 116 char * 117 getlogint_r(char *answer, int namelen) 118 { 119 return (getl_r_common(answer, (size_t)namelen, LOGNAME_MAX_TRAD)); 120 } 121 122 /* 123 * POSIX.1c standard version of the function getlogin_r. 124 * User gets it via static getlogin_r from the header file. 125 */ 126 int 127 __posix_getlogint_r(char *name, int namelen) 128 { 129 int nerrno = 0; 130 int oerrno = errno; 131 132 errno = 0; 133 if (getl_r_common(name, (size_t)namelen, LOGNAME_MAX_TRAD) == NULL) { 134 if (errno == 0) 135 nerrno = EINVAL; 136 else 137 nerrno = errno; 138 } 139 errno = oerrno; 140 return (nerrno); 141 } 142 143 char * 144 getlogint(void) 145 { 146 char *answer = tsdalloc(_T_LOGIN, LOGIN_NAME_MAX_TRAD, NULL); 147 148 if (answer == NULL) 149 return (NULL); 150 return (getl_r_common(answer, LOGIN_NAME_MAX_TRAD, LOGNAME_MAX_TRAD)); 151 } 152 153 /* 154 * POSIX.1c Draft-6 version of the function getlogin_r. 155 * It was implemented by Solaris 2.3. 156 * For extended login names, selected by redefine_extname in unistd.h. 157 */ 158 char * 159 getloginx_r(char *answer, int namelen) 160 { 161 return (getl_r_common(answer, (size_t)namelen, NMAX)); 162 } 163 164 /* 165 * POSIX.1c standard version of the function getlogin_r. 166 * User gets it via static getlogin_r from the header file. 167 * For extended login names, selected by redefine_extname in unistd.h. 168 */ 169 int 170 __posix_getloginx_r(char *name, int namelen) 171 { 172 int nerrno = 0; 173 int oerrno = errno; 174 175 errno = 0; 176 if (getl_r_common(name, (size_t)namelen, NMAX) == NULL) { 177 if (errno == 0) 178 nerrno = EINVAL; 179 else 180 nerrno = errno; 181 } 182 errno = oerrno; 183 return (nerrno); 184 } 185 186 /* 187 * For extended login names, selected by redefine_extname in unistd.h. 188 */ 189 char * 190 getloginx(void) 191 { 192 char *answer = tsdalloc(_T_LOGIN, LOGIN_NAME_MAX, NULL); 193 194 if (answer == NULL) 195 return (NULL); 196 return (getl_r_common(answer, LOGIN_NAME_MAX, NMAX)); 197 } 198