17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1b4Sraf * Common Development and Distribution License (the "License").
67257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
230a1278f2SGary Mills * Copyright (c) 2013 Gary Mills
240a1278f2SGary Mills *
257257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
267c478bd9Sstevel@tonic-gate * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
307c478bd9Sstevel@tonic-gate /* All Rights Reserved */
317c478bd9Sstevel@tonic-gate
320a1278f2SGary Mills #pragma weak _getlogin = getloginx
330a1278f2SGary Mills #pragma weak _getlogin_r = getloginx_r
347c478bd9Sstevel@tonic-gate
357257d1b4Sraf #include "lint.h"
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <limits.h>
427c478bd9Sstevel@tonic-gate #include "utmpx.h"
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate #include <thread.h>
467c478bd9Sstevel@tonic-gate #include <synch.h>
477c478bd9Sstevel@tonic-gate #include <mtlib.h>
487c478bd9Sstevel@tonic-gate #include "tsd.h"
497c478bd9Sstevel@tonic-gate
500a1278f2SGary Mills /* Revert the renames done in unistd.h */
510a1278f2SGary Mills #ifdef __PRAGMA_REDEFINE_EXTNAME
520a1278f2SGary Mills #pragma redefine_extname getlogint getlogin
530a1278f2SGary Mills #pragma redefine_extname getlogint_r getlogin_r
540a1278f2SGary Mills #pragma redefine_extname __posix_getlogint_r __posix_getlogin_r
550a1278f2SGary Mills #else /* __PRAGMA_REDEFINE_EXTNAME */
560a1278f2SGary Mills #ifdef getlogin
570a1278f2SGary Mills #undef getlogin
580a1278f2SGary Mills #endif /* getlogin */
590a1278f2SGary Mills #ifdef getlogin_r
600a1278f2SGary Mills #undef getlogin_r
610a1278f2SGary Mills #endif /* getlogin_r */
620a1278f2SGary Mills #ifdef __posix_getlogin_r
630a1278f2SGary Mills #undef __posix_getlogin_r
640a1278f2SGary Mills #endif /* __posix_getlogin_r */
650a1278f2SGary Mills #define getlogint getlogin
660a1278f2SGary Mills #define getlogint_r getlogin_r
670a1278f2SGary Mills #define __posix_getlogint_r __posix_getlogin_r
680a1278f2SGary Mills #endif /* __PRAGMA_REDEFINE_EXTNAME */
69746f551dSGary Mills extern char *getlogint(void);
70746f551dSGary Mills extern char *getlogint_r(char *, int);
71746f551dSGary Mills extern int __posix_getlogint_r(char *, int);
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate /*
740a1278f2SGary Mills * Use the full length of a login name.
750a1278f2SGary Mills * The utmpx interface provides for a 32 character login name.
767c478bd9Sstevel@tonic-gate */
770a1278f2SGary Mills #define NMAX (sizeof (((struct utmpx *)0)->ut_user))
780a1278f2SGary Mills
790a1278f2SGary Mills /*
800a1278f2SGary Mills * Common function
810a1278f2SGary Mills */
820a1278f2SGary Mills static char *
getl_r_common(char * answer,size_t namelen,size_t maxlen)830a1278f2SGary Mills getl_r_common(char *answer, size_t namelen, size_t maxlen)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate int uf;
867c478bd9Sstevel@tonic-gate off64_t me;
877c478bd9Sstevel@tonic-gate struct futmpx ubuf;
88*61f9f3e6SRobert Mustacchi size_t ulen;
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if ((me = (off64_t)ttyslot()) < 0)
917c478bd9Sstevel@tonic-gate return (NULL);
927c478bd9Sstevel@tonic-gate if ((uf = open64(UTMPX_FILE, 0)) < 0)
937c478bd9Sstevel@tonic-gate return (NULL);
947c478bd9Sstevel@tonic-gate (void) lseek64(uf, me * sizeof (ubuf), SEEK_SET);
957c478bd9Sstevel@tonic-gate if (read(uf, &ubuf, sizeof (ubuf)) != sizeof (ubuf)) {
967c478bd9Sstevel@tonic-gate (void) close(uf);
977c478bd9Sstevel@tonic-gate return (NULL);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate (void) close(uf);
1007c478bd9Sstevel@tonic-gate if (ubuf.ut_user[0] == '\0')
1017c478bd9Sstevel@tonic-gate return (NULL);
1020a1278f2SGary Mills
1030a1278f2SGary Mills /* Insufficient buffer size */
104*61f9f3e6SRobert Mustacchi ulen = strnlen(ubuf.ut_user, maxlen);
105*61f9f3e6SRobert Mustacchi if (namelen <= ulen) {
1060a1278f2SGary Mills errno = ERANGE;
1070a1278f2SGary Mills return (NULL);
1080a1278f2SGary Mills }
109*61f9f3e6SRobert Mustacchi
110*61f9f3e6SRobert Mustacchi /*
111*61f9f3e6SRobert Mustacchi * While the interface to getlogin_r says that a user should supply a
112*61f9f3e6SRobert Mustacchi * buffer with at least LOGIN_NAME_MAX bytes, we shouldn't assume they
113*61f9f3e6SRobert Mustacchi * have, especially since we've been supplied with its actual size.
114*61f9f3e6SRobert Mustacchi * Doing otherwise is just asking us to corrupt memory (and has in the
115*61f9f3e6SRobert Mustacchi * past).
116*61f9f3e6SRobert Mustacchi */
117*61f9f3e6SRobert Mustacchi (void) strncpy(answer, ubuf.ut_user, ulen);
118*61f9f3e6SRobert Mustacchi answer[ulen] = '\0';
119*61f9f3e6SRobert Mustacchi return (answer);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate /*
1230a1278f2SGary Mills * POSIX.1c Draft-6 version of the function getlogin_r.
1240a1278f2SGary Mills * It was implemented by Solaris 2.3.
1250a1278f2SGary Mills */
1260a1278f2SGary Mills char *
getlogint_r(char * answer,int namelen)1270a1278f2SGary Mills getlogint_r(char *answer, int namelen)
1280a1278f2SGary Mills {
1290a1278f2SGary Mills return (getl_r_common(answer, (size_t)namelen, LOGNAME_MAX_TRAD));
1300a1278f2SGary Mills }
1310a1278f2SGary Mills
1320a1278f2SGary Mills /*
1337c478bd9Sstevel@tonic-gate * POSIX.1c standard version of the function getlogin_r.
1347c478bd9Sstevel@tonic-gate * User gets it via static getlogin_r from the header file.
1357c478bd9Sstevel@tonic-gate */
1367c478bd9Sstevel@tonic-gate int
__posix_getlogint_r(char * name,int namelen)1370a1278f2SGary Mills __posix_getlogint_r(char *name, int namelen)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate int nerrno = 0;
1407c478bd9Sstevel@tonic-gate int oerrno = errno;
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate errno = 0;
1430a1278f2SGary Mills if (getl_r_common(name, (size_t)namelen, LOGNAME_MAX_TRAD) == NULL) {
1447c478bd9Sstevel@tonic-gate if (errno == 0)
1457c478bd9Sstevel@tonic-gate nerrno = EINVAL;
1467c478bd9Sstevel@tonic-gate else
1477c478bd9Sstevel@tonic-gate nerrno = errno;
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate errno = oerrno;
1507c478bd9Sstevel@tonic-gate return (nerrno);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate char *
getlogint(void)1540a1278f2SGary Mills getlogint(void)
1557c478bd9Sstevel@tonic-gate {
1560a1278f2SGary Mills char *answer = tsdalloc(_T_LOGIN, LOGIN_NAME_MAX_TRAD, NULL);
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate if (answer == NULL)
1597c478bd9Sstevel@tonic-gate return (NULL);
1600a1278f2SGary Mills return (getl_r_common(answer, LOGIN_NAME_MAX_TRAD, LOGNAME_MAX_TRAD));
1610a1278f2SGary Mills }
1620a1278f2SGary Mills
1630a1278f2SGary Mills /*
1640a1278f2SGary Mills * POSIX.1c Draft-6 version of the function getlogin_r.
1650a1278f2SGary Mills * It was implemented by Solaris 2.3.
1660a1278f2SGary Mills * For extended login names, selected by redefine_extname in unistd.h.
1670a1278f2SGary Mills */
1680a1278f2SGary Mills char *
getloginx_r(char * answer,int namelen)1690a1278f2SGary Mills getloginx_r(char *answer, int namelen)
1700a1278f2SGary Mills {
1710a1278f2SGary Mills return (getl_r_common(answer, (size_t)namelen, NMAX));
1720a1278f2SGary Mills }
1730a1278f2SGary Mills
1740a1278f2SGary Mills /*
1750a1278f2SGary Mills * POSIX.1c standard version of the function getlogin_r.
1760a1278f2SGary Mills * User gets it via static getlogin_r from the header file.
1770a1278f2SGary Mills * For extended login names, selected by redefine_extname in unistd.h.
1780a1278f2SGary Mills */
1790a1278f2SGary Mills int
__posix_getloginx_r(char * name,int namelen)1800a1278f2SGary Mills __posix_getloginx_r(char *name, int namelen)
1810a1278f2SGary Mills {
1820a1278f2SGary Mills int nerrno = 0;
1830a1278f2SGary Mills int oerrno = errno;
1840a1278f2SGary Mills
1850a1278f2SGary Mills errno = 0;
1860a1278f2SGary Mills if (getl_r_common(name, (size_t)namelen, NMAX) == NULL) {
1870a1278f2SGary Mills if (errno == 0)
1880a1278f2SGary Mills nerrno = EINVAL;
1890a1278f2SGary Mills else
1900a1278f2SGary Mills nerrno = errno;
1910a1278f2SGary Mills }
1920a1278f2SGary Mills errno = oerrno;
1930a1278f2SGary Mills return (nerrno);
1940a1278f2SGary Mills }
1950a1278f2SGary Mills
1960a1278f2SGary Mills /*
1970a1278f2SGary Mills * For extended login names, selected by redefine_extname in unistd.h.
1980a1278f2SGary Mills */
1990a1278f2SGary Mills char *
getloginx(void)2000a1278f2SGary Mills getloginx(void)
2010a1278f2SGary Mills {
2020a1278f2SGary Mills char *answer = tsdalloc(_T_LOGIN, LOGIN_NAME_MAX, NULL);
2030a1278f2SGary Mills
2040a1278f2SGary Mills if (answer == NULL)
2050a1278f2SGary Mills return (NULL);
2060a1278f2SGary Mills return (getl_r_common(answer, LOGIN_NAME_MAX, NMAX));
2077c478bd9Sstevel@tonic-gate }
208