Debian Codegraph @

lib/getlogin_r.c

raw
1 /* Provide a working getlogin_r for systems which lack it.
2
3 Copyright (C) 2005-2007, 2010-2026 Free Software Foundation, Inc.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 /* Written by Paul Eggert, Derek Price, and Bruno Haible. */
19
20 #include <config.h>
21
22 /* Specification. */
23 #include <unistd.h>
24
25 #include <errno.h>
26 #include <string.h>
27
28 #include "malloca.h"
29
30 #if defined _WIN32 && ! defined __CYGWIN__
31 # define WIN32_LEAN_AND_MEAN
32 # include <windows.h>
33 /* Don't assume that UNICODE is not defined. */
34 # undef GetUserName
35 # define GetUserName GetUserNameA
36 #else
37 # if !HAVE_DECL_GETLOGIN
38 extern char *getlogin (void);
39 # endif
40 #endif
41
42 #if defined __linux__ || defined __ANDROID__
43 # include <fcntl.h>
44 # include <pwd.h>
45 # include <stdlib.h>
46 # include <string.h>
47 # include <sys/stat.h>
48 #endif
49
50 /* See unistd.in.h for documentation. */
51 int
52 getlogin_r (char *name, size_t size)
53 {
54 #undef getlogin_r
55 #if defined _WIN32 && ! defined __CYGWIN__
56 /* Native Windows platform. */
57
58 /* When size > 0x7fff, the doc says that GetUserName will fail.
59 Actually, on Windows XP SP3, it succeeds. But let's be safe,
60 for the sake of older Windows versions. */
61 if (size > 0x7fff)
62 size = 0x7fff;
63 DWORD sz = size;
64 if (!GetUserName (name, &sz))
65 {
66 if (GetLastError () == ERROR_INSUFFICIENT_BUFFER)
67 /* In this case, the doc says that sz contains the required size, but
68 actually, on Windows XP SP3, it contains 2 * the required size. */
69 return ERANGE;
70 else
71 return ENOENT;
72 }
73 return 0;
74 #elif defined __linux__ || defined __ANDROID__
75 /* Linux. */
76 {
77 /* Read the login uid from the /proc file system. */
78 int fd = open ("/proc/self/loginuid", O_RDONLY);
79 if (fd >= 0)
80 {
81 char buf[20 + 1];
82 int n = read (fd, buf, sizeof (buf) - 1);
83 if (n > 0)
84 {
85 buf[n] = '\0';
86 char *endptr;
87 unsigned long uid = strtoul (buf, &endptr, 10);
88 if (endptr == buf + n && uid != (uid_t) -1)
89 {
90 /* Convert the uid to a user name. */
91 struct passwd pbuf1;
92 char pbuf2[1024];
93 struct passwd *p;
94 if (getpwuid_r (uid, &pbuf1, pbuf2, sizeof (pbuf2), &p) == 0)
95 {
96 if (strlen (p->pw_name) < size)
97 {
98 strcpy (name, p->pw_name);
99 close (fd);
100 return 0;
101 }
102 return ERANGE;
103 }
104 }
105 }
106 close (fd);
107 }
108 }
109 {
110 /* Find the tty connected to the current process. */
111 char tty[1024];
112 if (ttyname_r (STDIN_FILENO, tty, sizeof (tty)) == 0)
113 {
114 /* We cannot use read_utmp here, since it is not multithread-safe. */
115 /* Fallback for systems which don't maintain an utmp database
116 or for ttys that are not recorded in that the utmp database:
117 Look at the owner of that tty. */
118 struct stat statbuf;
119 if (stat (tty, &statbuf) >= 0)
120 {
121 uid_t uid = statbuf.st_uid;
122 /* Convert the uid to a user name. */
123 struct passwd pbuf1;
124 char pbuf2[1024];
125 struct passwd *p;
126 if (getpwuid_r (uid, &pbuf1, pbuf2, sizeof (pbuf2), &p) == 0)
127 {
128 if (strlen (p->pw_name) < size)
129 {
130 strcpy (name, p->pw_name);
131 return 0;
132 }
133 return ERANGE;
134 }
135 }
136 }
137 }
138 /* ENOENT is a reasonable errno value if getlogin returns NULL. */
139 return ENOENT;
140 #elif HAVE_GETLOGIN_R
141 /* Platform with a getlogin_r() function. */
142 int ret = getlogin_r (name, size);
143
144 if (ret == 0)
145 {
146 const char *nul = memchr (name, '\0', size);
147 if (nul == NULL)
148 /* name contains a truncated result. */
149 return ERANGE;
150 if (size > 0 && nul == name + size - 1)
151 {
152 /* strlen(name) == size-1. Determine whether the untruncated result
153 would have had length size-1 or size. */
154 char *room = (char *) malloca (size + 1);
155 if (room == NULL)
156 return ENOMEM;
157 ret = getlogin_r (room, size + 1);
158 /* The untruncated result should be the same as in the first call. */
159 if (ret == 0 && !memeq (name, room, size))
160 /* The untruncated result would have been different. */
161 ret = ERANGE;
162 freea (room);
163 }
164 }
165 return ret;
166 #else
167 /* Platform with a getlogin() function. */
168 errno = 0;
169 char *n = getlogin ();
170 if (!n)
171 /* ENOENT is a reasonable errno value if getlogin returns NULL. */
172 return (errno != 0 ? errno : ENOENT);
173
174 size_t nlen = strlen (n);
175 if (size <= nlen)
176 return ERANGE;
177 memcpy (name, n, nlen + 1);
178 return 0;
179 #endif
180 }