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 size_t ulen; 89 90 if ((me = (off64_t)ttyslot()) < 0) 91 return (NULL); 92 if ((uf = open64(UTMPX_FILE, 0)) < 0) 93 return (NULL); 94 (void) lseek64(uf, me * sizeof (ubuf), SEEK_SET); 95 if (read(uf, &ubuf, sizeof (ubuf)) != sizeof (ubuf)) { 96 (void) close(uf); 97 return (NULL); 98 } 99 (void) close(uf); 100 if (ubuf.ut_user[0] == '\0') 101 return (NULL); 102 103 /* Insufficient buffer size */ 104 ulen = strnlen(ubuf.ut_user, maxlen); 105 if (namelen <= ulen) { 106 errno = ERANGE; 107 return (NULL); 108 } 109 110 /* 111 * While the interface to getlogin_r says that a user should supply a 112 * buffer with at least LOGIN_NAME_MAX bytes, we shouldn't assume they 113 * have, especially since we've been supplied with its actual size. 114 * Doing otherwise is just asking us to corrupt memory (and has in the 115 * past). 116 */ 117 (void) strncpy(answer, ubuf.ut_user, ulen); 118 answer[ulen] = '\0'; 119 return (answer); 120 } 121 122 /* 123 * POSIX.1c Draft-6 version of the function getlogin_r. 124 * It was implemented by Solaris 2.3. 125 */ 126 char * 127 getlogint_r(char *answer, int namelen) 128 { 129 return (getl_r_common(answer, (size_t)namelen, LOGNAME_MAX_TRAD)); 130 } 131 132 /* 133 * POSIX.1c standard version of the function getlogin_r. 134 * User gets it via static getlogin_r from the header file. 135 */ 136 int 137 __posix_getlogint_r(char *name, int namelen) 138 { 139 int nerrno = 0; 140 int oerrno = errno; 141 142 errno = 0; 143 if (getl_r_common(name, (size_t)namelen, LOGNAME_MAX_TRAD) == NULL) { 144 if (errno == 0) 145 nerrno = EINVAL; 146 else 147 nerrno = errno; 148 } 149 errno = oerrno; 150 return (nerrno); 151 } 152 153 char * 154 getlogint(void) 155 { 156 char *answer = tsdalloc(_T_LOGIN, LOGIN_NAME_MAX_TRAD, NULL); 157 158 if (answer == NULL) 159 return (NULL); 160 return (getl_r_common(answer, LOGIN_NAME_MAX_TRAD, LOGNAME_MAX_TRAD)); 161 } 162 163 /* 164 * POSIX.1c Draft-6 version of the function getlogin_r. 165 * It was implemented by Solaris 2.3. 166 * For extended login names, selected by redefine_extname in unistd.h. 167 */ 168 char * 169 getloginx_r(char *answer, int namelen) 170 { 171 return (getl_r_common(answer, (size_t)namelen, NMAX)); 172 } 173 174 /* 175 * POSIX.1c standard version of the function getlogin_r. 176 * User gets it via static getlogin_r from the header file. 177 * For extended login names, selected by redefine_extname in unistd.h. 178 */ 179 int 180 __posix_getloginx_r(char *name, int namelen) 181 { 182 int nerrno = 0; 183 int oerrno = errno; 184 185 errno = 0; 186 if (getl_r_common(name, (size_t)namelen, NMAX) == NULL) { 187 if (errno == 0) 188 nerrno = EINVAL; 189 else 190 nerrno = errno; 191 } 192 errno = oerrno; 193 return (nerrno); 194 } 195 196 /* 197 * For extended login names, selected by redefine_extname in unistd.h. 198 */ 199 char * 200 getloginx(void) 201 { 202 char *answer = tsdalloc(_T_LOGIN, LOGIN_NAME_MAX, NULL); 203 204 if (answer == NULL) 205 return (NULL); 206 return (getl_r_common(answer, LOGIN_NAME_MAX, NMAX)); 207 } 208