]>
Commit | Line | Data |
---|---|---|
fea681da | 1 | .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) |
c6ec3685 MK |
2 | .\" and Copyright 2008, Linux Foundation, written by Michael Kerrisk |
3 | .\" <mtk.manpages@gmail.com> | |
fea681da | 4 | .\" |
5fbde956 | 5 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft |
fea681da MK |
6 | .\" |
7 | .\" References consulted: | |
8 | .\" Linux libc source code | |
6b2fc294 | 9 | .\" Lewine's "POSIX Programmer's Guide" (O'Reilly & Associates, 1991) |
fea681da MK |
10 | .\" 386BSD man pages |
11 | .\" | |
12 | .\" Modified 1993-07-24 by Rik Faith (faith@cs.unc.edu) | |
13 | .\" Modified 1996-05-27 by Martin Schulze (joey@linux.de) | |
14 | .\" Modified 2003-11-15 by aeb | |
c6ec3685 | 15 | .\" 2008-11-07, mtk, Added an example program for getpwnam_r(). |
fea681da | 16 | .\" |
7bd6328f | 17 | .TH GETPWNAM 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual" |
fea681da MK |
18 | .SH NAME |
19 | getpwnam, getpwnam_r, getpwuid, getpwuid_r \- get password file entry | |
42009080 AC |
20 | .SH LIBRARY |
21 | Standard C library | |
22 | .RI ( libc ", " \-lc ) | |
fea681da MK |
23 | .SH SYNOPSIS |
24 | .nf | |
25 | .B #include <sys/types.h> | |
26 | .B #include <pwd.h> | |
68e4db0a | 27 | .PP |
fea681da | 28 | .BI "struct passwd *getpwnam(const char *" name ); |
fea681da | 29 | .BI "struct passwd *getpwuid(uid_t " uid ); |
68e4db0a | 30 | .PP |
4dfd2b4f AC |
31 | .BI "int getpwnam_r(const char *restrict " name \ |
32 | ", struct passwd *restrict " pwd , | |
33 | .BI " char *restrict " buf ", size_t " buflen , | |
34 | .BI " struct passwd **restrict " result ); | |
35 | .BI "int getpwuid_r(uid_t " uid ", struct passwd *restrict " pwd , | |
36 | .BI " char *restrict " buf ", size_t " buflen , | |
37 | .BI " struct passwd **restrict " result ); | |
fea681da | 38 | .fi |
68e4db0a | 39 | .PP |
d39ad78f | 40 | .RS -4 |
cc4615cc MK |
41 | Feature Test Macro Requirements for glibc (see |
42 | .BR feature_test_macros (7)): | |
d39ad78f | 43 | .RE |
68e4db0a | 44 | .PP |
cc4615cc MK |
45 | .BR getpwnam_r (), |
46 | .BR getpwuid_r (): | |
9d2adbae MK |
47 | .nf |
48 | _POSIX_C_SOURCE | |
49 | || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE | |
50 | .fi | |
fea681da MK |
51 | .SH DESCRIPTION |
52 | The | |
63aa9df0 | 53 | .BR getpwnam () |
fea681da | 54 | function returns a pointer to a structure containing |
f936cf26 | 55 | the broken-out fields of the record in the password database |
c13182ef | 56 | (e.g., the local password file |
f2738b39 MK |
57 | .IR /etc/passwd , |
58 | NIS, and LDAP) | |
18701562 | 59 | that matches the username |
fea681da MK |
60 | .IR name . |
61 | .PP | |
62 | The | |
63aa9df0 | 63 | .BR getpwuid () |
fea681da | 64 | function returns a pointer to a structure containing |
f2738b39 | 65 | the broken-out fields of the record in the password database |
f936cf26 | 66 | that matches the user ID |
fea681da MK |
67 | .IR uid . |
68 | .PP | |
fea681da | 69 | The \fIpasswd\fP structure is defined in \fI<pwd.h>\fP as follows: |
51f5698d | 70 | .PP |
bd191423 | 71 | .in +4n |
b8302363 | 72 | .EX |
fea681da | 73 | struct passwd { |
18701562 | 74 | char *pw_name; /* username */ |
f2738b39 | 75 | char *pw_passwd; /* user password */ |
34c97781 MK |
76 | uid_t pw_uid; /* user ID */ |
77 | gid_t pw_gid; /* group ID */ | |
958680cd | 78 | char *pw_gecos; /* user information */ |
f2738b39 MK |
79 | char *pw_dir; /* home directory */ |
80 | char *pw_shell; /* shell program */ | |
fea681da | 81 | }; |
b8302363 | 82 | .EE |
bd191423 | 83 | .in |
fea681da | 84 | .PP |
011520b8 MK |
85 | See |
86 | .BR passwd (5) | |
87 | for more information about these fields. | |
88 | .PP | |
89 | The | |
90 | .BR getpwnam_r () | |
91 | and | |
92 | .BR getpwuid_r () | |
93 | functions obtain the same information as | |
94 | .BR getpwnam () | |
95 | and | |
96 | .BR getpwuid (), | |
97 | but store the retrieved | |
98 | .I passwd | |
99 | structure in the space pointed to by | |
100 | .IR pwd . | |
101 | The string fields pointed to by the members of the | |
102 | .I passwd | |
103 | structure are stored in the buffer | |
104 | .I buf | |
105 | of size | |
106 | .IR buflen . | |
107 | A pointer to the result (in case of success) or NULL (in case no entry | |
108 | was found or an error occurred) is stored in | |
109 | .IR *result . | |
110 | .PP | |
f6dc3611 | 111 | The call |
847e0d88 | 112 | .PP |
1ae6b2c7 AC |
113 | .in +4n |
114 | .EX | |
115 | sysconf(_SC_GETPW_R_SIZE_MAX) | |
116 | .EE | |
117 | .in | |
847e0d88 | 118 | .PP |
f6dc3611 MK |
119 | returns either \-1, without changing |
120 | .IR errno , | |
121 | or an initial suggested size for | |
122 | .IR buf . | |
123 | (If this size is too small, | |
124 | the call fails with | |
125 | .BR ERANGE , | |
126 | in which case the caller can retry with a larger buffer.) | |
47297adb | 127 | .SH RETURN VALUE |
60a90ecd MK |
128 | The |
129 | .BR getpwnam () | |
130 | and | |
131 | .BR getpwuid () | |
132 | functions return a pointer to a | |
f2738b39 MK |
133 | .I passwd |
134 | structure, or NULL if the matching entry is not found or | |
135 | an error occurs. | |
136 | If an error occurs, | |
fea681da | 137 | .I errno |
f6a4078b | 138 | is set to indicate the error. |
f2738b39 | 139 | If one wants to check |
fea681da MK |
140 | .I errno |
141 | after the call, it should be set to zero before the call. | |
dd3568a1 | 142 | .PP |
57c1b002 | 143 | The return value may point to a static area, and may be overwritten |
fea681da | 144 | by subsequent calls to |
3a72373c | 145 | .BR getpwent (3), |
63aa9df0 | 146 | .BR getpwnam (), |
fea681da | 147 | or |
63aa9df0 | 148 | .BR getpwuid (). |
48e4cfa0 MK |
149 | (Do not pass the returned pointer to |
150 | .BR free (3).) | |
dd3568a1 | 151 | .PP |
bbab485d | 152 | On success, |
60a90ecd MK |
153 | .BR getpwnam_r () |
154 | and | |
155 | .BR getpwuid_r () | |
bbab485d | 156 | return zero, and set |
1ae6b2c7 | 157 | .I *result |
bbab485d | 158 | to |
3adc12e9 | 159 | .IR pwd . |
bbab485d MK |
160 | If no matching password record was found, |
161 | these functions return 0 and store NULL in | |
3adc12e9 | 162 | .IR *result . |
bbab485d | 163 | In case of error, an error number is returned, and NULL is stored in |
3adc12e9 | 164 | .IR *result . |
fea681da MK |
165 | .SH ERRORS |
166 | .TP | |
be1db87e | 167 | .BR 0 " or " ENOENT " or " ESRCH " or " EBADF " or " EPERM " or ..." |
fea681da MK |
168 | The given |
169 | .I name | |
170 | or | |
171 | .I uid | |
172 | was not found. | |
173 | .TP | |
174 | .B EINTR | |
bb14af39 MK |
175 | A signal was caught; see |
176 | .BR signal (7). | |
fea681da MK |
177 | .TP |
178 | .B EIO | |
179 | I/O error. | |
180 | .TP | |
181 | .B EMFILE | |
26c32fab | 182 | The per-process limit on the number of open file descriptors has been reached. |
fea681da MK |
183 | .TP |
184 | .B ENFILE | |
e258766b | 185 | The system-wide limit on the total number of open files has been reached. |
fea681da MK |
186 | .TP |
187 | .B ENOMEM | |
f2738b39 | 188 | .\" not in POSIX |
b2133414 MK |
189 | Insufficient memory to allocate |
190 | .I passwd | |
191 | structure. | |
fea681da MK |
192 | .\" This structure is static, allocated 0 or 1 times. No memory leak. (libc45) |
193 | .TP | |
194 | .B ERANGE | |
195 | Insufficient buffer space supplied. | |
196 | .SH FILES | |
197 | .TP | |
198 | .I /etc/passwd | |
f2738b39 | 199 | local password database file |
404197c7 | 200 | .SH ATTRIBUTES |
2973863a MK |
201 | For an explanation of the terms used in this section, see |
202 | .BR attributes (7). | |
c466875e MK |
203 | .ad l |
204 | .nh | |
2973863a MK |
205 | .TS |
206 | allbox; | |
b32feea5 | 207 | lb lb lbx |
2973863a MK |
208 | l l l. |
209 | Interface Attribute Value | |
210 | T{ | |
693e6cf5 | 211 | .BR getpwnam () |
b32feea5 MK |
212 | T} Thread safety T{ |
213 | MT-Unsafe race:pwnam locale | |
214 | T} | |
693e6cf5 | 215 | T{ |
404197c7 | 216 | .BR getpwuid () |
b32feea5 MK |
217 | T} Thread safety T{ |
218 | MT-Unsafe race:pwuid locale | |
219 | T} | |
2973863a MK |
220 | T{ |
221 | .BR getpwnam_r (), | |
404197c7 | 222 | .BR getpwuid_r () |
b32feea5 MK |
223 | T} Thread safety T{ |
224 | MT-Safe locale | |
225 | T} | |
2973863a | 226 | .TE |
c466875e MK |
227 | .hy |
228 | .ad | |
229 | .sp 1 | |
3113c7f3 | 230 | .SH STANDARDS |
9720f05e | 231 | POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD. |
958680cd MK |
232 | The |
233 | .I pw_gecos | |
234 | field is not specified in POSIX, but is present on most implementations. | |
fea681da | 235 | .SH NOTES |
68e1685c | 236 | The formulation given above under "RETURN VALUE" is from POSIX.1-2001. |
6b2fc294 | 237 | It does not call "not found" an error, and hence does not specify what value |
fea681da | 238 | .I errno |
c13182ef MK |
239 | might have in this situation. |
240 | But that makes it impossible to recognize | |
241 | errors. | |
242 | One might argue that according to POSIX | |
fea681da | 243 | .I errno |
c13182ef MK |
244 | should be left unchanged if an entry is not found. |
245 | Experiments on various | |
008f1ecc | 246 | UNIX-like systems show that lots of different values occur in this |
a797afac | 247 | situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM, and probably others. |
fea681da MK |
248 | .\" more precisely: |
249 | .\" AIX 5.1 - gives ESRCH | |
250 | .\" OSF1 4.0g - gives EWOULDBLOCK | |
db85058b MK |
251 | .\" libc, glibc up to version 2.6, Irix 6.5 - give ENOENT |
252 | .\" glibc since version 2.7 - give 0 | |
fea681da MK |
253 | .\" FreeBSD 4.8, OpenBSD 3.2, NetBSD 1.6 - give EPERM |
254 | .\" SunOS 5.8 - gives EBADF | |
255 | .\" Tru64 5.1b, HP-UX-11i, SunOS 5.7 - give 0 | |
847e0d88 | 256 | .PP |
c13182ef MK |
257 | The |
258 | .I pw_dir | |
259 | field contains the name of the initial working directory of the user. | |
2f0af33b MK |
260 | Login programs use the value of this field to initialize the |
261 | .B HOME | |
262 | environment variable for the login shell. | |
6b2fc294 | 263 | An application that wants to determine its user's home directory |
2f0af33b MK |
264 | should inspect the value of |
265 | .B HOME | |
266 | (rather than the value | |
94e9d9fe | 267 | .IR getpwuid(getuid())\->pw_dir ) |
6b2fc294 MK |
268 | since this allows the user to modify their notion of |
269 | "the home directory" during a login session. | |
270 | To determine the (initial) home directory of another user, | |
c13182ef | 271 | it is necessary to use |
94e9d9fe | 272 | .I getpwnam("username")\->pw_dir |
6b2fc294 | 273 | or similar. |
a14af333 | 274 | .SH EXAMPLES |
c6ec3685 MK |
275 | The program below demonstrates the use of |
276 | .BR getpwnam_r () | |
277 | to find the full username and user ID for the username | |
278 | supplied as a command-line argument. | |
847e0d88 | 279 | .PP |
207050fa | 280 | .EX |
c6ec3685 | 281 | #include <pwd.h> |
8eb90116 | 282 | #include <stdint.h> |
c6ec3685 MK |
283 | #include <stdio.h> |
284 | #include <stdlib.h> | |
285 | #include <unistd.h> | |
286 | #include <errno.h> | |
287 | ||
288 | int | |
289 | main(int argc, char *argv[]) | |
290 | { | |
291 | struct passwd pwd; | |
292 | struct passwd *result; | |
293 | char *buf; | |
e5d76e6b | 294 | long bufsize; |
c6ec3685 MK |
295 | int s; |
296 | ||
297 | if (argc != 2) { | |
d1a71985 | 298 | fprintf(stderr, "Usage: %s username\en", argv[0]); |
c6ec3685 MK |
299 | exit(EXIT_FAILURE); |
300 | } | |
301 | ||
302 | bufsize = sysconf(_SC_GETPW_R_SIZE_MAX); | |
303 | if (bufsize == \-1) /* Value was indeterminate */ | |
304 | bufsize = 16384; /* Should be more than enough */ | |
305 | ||
306 | buf = malloc(bufsize); | |
307 | if (buf == NULL) { | |
308 | perror("malloc"); | |
309 | exit(EXIT_FAILURE); | |
310 | } | |
311 | ||
312 | s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result); | |
313 | if (result == NULL) { | |
314 | if (s == 0) | |
d1a71985 | 315 | printf("Not found\en"); |
c6ec3685 MK |
316 | else { |
317 | errno = s; | |
318 | perror("getpwnam_r"); | |
319 | } | |
320 | exit(EXIT_FAILURE); | |
321 | } | |
322 | ||
8eb90116 AC |
323 | printf("Name: %s; UID: %jd\en", pwd.pw_gecos, |
324 | (intmax_t) pwd.pw_uid); | |
c6ec3685 MK |
325 | exit(EXIT_SUCCESS); |
326 | } | |
207050fa | 327 | .EE |
47297adb | 328 | .SH SEE ALSO |
fea681da MK |
329 | .BR endpwent (3), |
330 | .BR fgetpwent (3), | |
331 | .BR getgrnam (3), | |
332 | .BR getpw (3), | |
333 | .BR getpwent (3), | |
1ed50462 | 334 | .BR getspnam (3), |
fea681da MK |
335 | .BR putpwent (3), |
336 | .BR setpwent (3), | |
337 | .BR passwd (5) |