475 lines
13 KiB
HTML
475 lines
13 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of GETPWNAM</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>GETPWNAM</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/pwd.h">pwd.h</A>></B>
|
|
|
|
<B>struct passwd *getpwnam(const char *</B><I>name</I><B>);</B>
|
|
|
|
<B>struct passwd *getpwuid(uid_t </B><I>uid</I><B>);</B>
|
|
|
|
<B>int getpwnam_r(const char *</B><I>name</I><B>, struct passwd *</B><I>pwd</I><B>,</B>
|
|
<B> char *</B><I>buf</I><B>, size_t </B><I>buflen</I><B>, struct passwd **</B><I>result</I><B>);</B>
|
|
|
|
<B>int getpwuid_r(uid_t </B><I>uid</I><B>, struct passwd *</B><I>pwd</I><B>,</B>
|
|
<B> char *</B><I>buf</I><B>, size_t </B><I>buflen</I><B>, struct passwd **</B><I>result</I><B>);</B>
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
|
|
<B>getpwnam_r</B>(),
|
|
|
|
<B>getpwuid_r</B>():
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
_POSIX_C_SOURCE
|
|
<BR> || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
|
|
</DL>
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>getpwnam</B>()
|
|
|
|
function returns a pointer to a structure containing
|
|
the broken-out fields of the record in the password database
|
|
(e.g., the local password file
|
|
<I>/etc/passwd</I>,
|
|
|
|
NIS, and LDAP)
|
|
that matches the username
|
|
<I>name</I>.
|
|
|
|
<P>
|
|
|
|
The
|
|
<B>getpwuid</B>()
|
|
|
|
function returns a pointer to a structure containing
|
|
the broken-out fields of the record in the password database
|
|
that matches the user ID
|
|
<I>uid</I>.
|
|
|
|
<P>
|
|
|
|
The <I>passwd</I> structure is defined in <I><<A HREF="file:///usr/include/pwd.h">pwd.h</A>></I> as follows:
|
|
<P>
|
|
|
|
|
|
|
|
struct passwd {
|
|
<BR> char *pw_name; /* username */
|
|
<BR> char *pw_passwd; /* user password */
|
|
<BR> uid_t pw_uid; /* user ID */
|
|
<BR> gid_t pw_gid; /* group ID */
|
|
<BR> char *pw_gecos; /* user information */
|
|
<BR> char *pw_dir; /* home directory */
|
|
<BR> char *pw_shell; /* shell program */
|
|
};
|
|
|
|
|
|
<P>
|
|
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?5+passwd">passwd</A></B>(5)
|
|
|
|
for more information about these fields.
|
|
<P>
|
|
|
|
The
|
|
<B>getpwnam_r</B>()
|
|
|
|
and
|
|
<B>getpwuid_r</B>()
|
|
|
|
functions obtain the same information as
|
|
<B>getpwnam</B>()
|
|
|
|
and
|
|
<B>getpwuid</B>(),
|
|
|
|
but store the retrieved
|
|
<I>passwd</I>
|
|
|
|
structure in the space pointed to by
|
|
<I>pwd</I>.
|
|
|
|
The string fields pointed to by the members of the
|
|
<I>passwd</I>
|
|
|
|
structure are stored in the buffer
|
|
<I>buf</I>
|
|
|
|
of size
|
|
<I>buflen</I>.
|
|
|
|
A pointer to the result (in case of success) or NULL (in case no entry
|
|
was found or an error occurred) is stored in
|
|
<I>*result</I>.
|
|
|
|
<P>
|
|
|
|
The call
|
|
<P>
|
|
|
|
<BR> sysconf(_SC_GETPW_R_SIZE_MAX)
|
|
<P>
|
|
|
|
returns either -1, without changing
|
|
<I>errno</I>,
|
|
|
|
or an initial suggested size for
|
|
<I>buf</I>.
|
|
|
|
(If this size is too small,
|
|
the call fails with
|
|
<B>ERANGE</B>,
|
|
|
|
in which case the caller can retry with a larger buffer.)
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
The
|
|
<B>getpwnam</B>()
|
|
|
|
and
|
|
<B>getpwuid</B>()
|
|
|
|
functions return a pointer to a
|
|
<I>passwd</I>
|
|
|
|
structure, or NULL if the matching entry is not found or
|
|
an error occurs.
|
|
If an error occurs,
|
|
<I>errno</I>
|
|
|
|
is set appropriately.
|
|
If one wants to check
|
|
<I>errno</I>
|
|
|
|
after the call, it should be set to zero before the call.
|
|
<P>
|
|
|
|
The return value may point to a static area, and may be overwritten
|
|
by subsequent calls to
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getpwent">getpwent</A></B>(3),
|
|
|
|
<B>getpwnam</B>(),
|
|
|
|
or
|
|
<B>getpwuid</B>().
|
|
|
|
(Do not pass the returned pointer to
|
|
<B><A HREF="/cgi-bin/man/man2html?3+free">free</A></B>(3).)
|
|
|
|
<P>
|
|
|
|
On success,
|
|
<B>getpwnam_r</B>()
|
|
|
|
and
|
|
<B>getpwuid_r</B>()
|
|
|
|
return zero, and set
|
|
<I>*result</I>
|
|
|
|
to
|
|
<I>pwd</I>.
|
|
|
|
If no matching password record was found,
|
|
these functions return 0 and store NULL in
|
|
<I>*result</I>.
|
|
|
|
In case of error, an error number is returned, and NULL is stored in
|
|
<I>*result</I>.
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="2"><B>0</B> or <B>ENOENT</B> or <B>ESRCH</B> or <B>EBADF</B> or <B>EPERM</B> or ...
|
|
|
|
<DD>
|
|
The given
|
|
<I>name</I>
|
|
|
|
or
|
|
<I>uid</I>
|
|
|
|
was not found.
|
|
<DT id="3"><B>EINTR</B>
|
|
|
|
<DD>
|
|
A signal was caught; see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+signal">signal</A></B>(7).
|
|
|
|
<DT id="4"><B>EIO</B>
|
|
|
|
<DD>
|
|
I/O error.
|
|
<DT id="5"><B>EMFILE</B>
|
|
|
|
<DD>
|
|
The per-process limit on the number of open file descriptors has been reached.
|
|
<DT id="6"><B>ENFILE</B>
|
|
|
|
<DD>
|
|
The system-wide limit on the total number of open files has been reached.
|
|
<DT id="7"><B>ENOMEM</B>
|
|
|
|
<DD>
|
|
|
|
Insufficient memory to allocate
|
|
<I>passwd</I>
|
|
|
|
structure.
|
|
|
|
<DT id="8"><B>ERANGE</B>
|
|
|
|
<DD>
|
|
Insufficient buffer space supplied.
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>NOTE</H2>
|
|
|
|
The user password database mostly refers to <I>/etc/passwd</I>.
|
|
However, with recent systems it also refers to network wide databases
|
|
using NIS, LDAP and other local files as configured in
|
|
<I>/etc/nsswitch.conf</I>.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>FILES</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="9"><I>/etc/passwd</I>
|
|
|
|
<DD>
|
|
local password database file
|
|
<DT id="10"><I>/etc/nsswitch.conf</I>
|
|
|
|
<DD>
|
|
System Databases and Name Service Switch configuration file
|
|
</DL>
|
|
<A NAME="lbAI"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>getpwnam</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Unsafe race:pwnam locale<BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>getpwuid</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Unsafe race:pwuid locale<BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>getpwnam_r</B>(),
|
|
|
|
<BR>
|
|
|
|
<B>getpwuid_r</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe locale<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
|
|
The
|
|
<I>pw_gecos</I>
|
|
|
|
field is not specified in POSIX, but is present on most implementations.
|
|
<A NAME="lbAK"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
The formulation given above under "RETURN VALUE" is from POSIX.1-2001.
|
|
It does not call "not found" an error, and hence does not specify what value
|
|
<I>errno</I>
|
|
|
|
might have in this situation.
|
|
But that makes it impossible to recognize
|
|
errors.
|
|
One might argue that according to POSIX
|
|
<I>errno</I>
|
|
|
|
should be left unchanged if an entry is not found.
|
|
Experiments on various
|
|
UNIX-like systems show that lots of different values occur in this
|
|
situation: 0, ENOENT, EBADF, ESRCH, EWOULDBLOCK, EPERM, and probably others.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<P>
|
|
|
|
The
|
|
<I>pw_dir</I>
|
|
|
|
field contains the name of the initial working directory of the user.
|
|
Login programs use the value of this field to initialize the
|
|
<B>HOME</B>
|
|
|
|
environment variable for the login shell.
|
|
An application that wants to determine its user's home directory
|
|
should inspect the value of
|
|
<B>HOME</B>
|
|
|
|
(rather than the value
|
|
<I>getpwuid(getuid())->pw_dir</I>)
|
|
|
|
since this allows the user to modify their notion of
|
|
"the home directory" during a login session.
|
|
To determine the (initial) home directory of another user,
|
|
it is necessary to use
|
|
<I>getpwnam(username)->pw_dir</I>
|
|
|
|
or similar.
|
|
<A NAME="lbAL"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below demonstrates the use of
|
|
<B>getpwnam_r</B>()
|
|
|
|
to find the full username and user ID for the username
|
|
supplied as a command-line argument.
|
|
<P>
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/pwd.h">pwd.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> struct passwd pwd;
|
|
<BR> struct passwd *result;
|
|
<BR> char *buf;
|
|
<BR> size_t bufsize;
|
|
<BR> int s;
|
|
<P>
|
|
<BR> if (argc != 2) {
|
|
<BR> fprintf(stderr, "Usage: %s username\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
|
|
<BR> if (bufsize == -1) /* Value was indeterminate */
|
|
<BR> bufsize = 16384; /* Should be more than enough */
|
|
<P>
|
|
<BR> buf = malloc(bufsize);
|
|
<BR> if (buf == NULL) {
|
|
<BR> perror("malloc");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
|
|
<BR> if (result == NULL) {
|
|
<BR> if (s == 0)
|
|
<BR> printf("Not found\n");
|
|
<BR> else {
|
|
<BR> errno = s;
|
|
<BR> perror("getpwnam_r");
|
|
<BR> }
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("Name: %s; UID: %ld\n", pwd.pw_gecos, (long) pwd.pw_uid);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+endpwent">endpwent</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fgetpwent">fgetpwent</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getgrnam">getgrnam</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getpw">getpw</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getpwent">getpwent</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+getspnam">getspnam</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+putpwent">putpwent</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+setpwent">setpwent</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?5+nsswitch.conf">nsswitch.conf</A></B>(5),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?5+passwd">passwd</A></B>(5)
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="11"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="12"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="13"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="14"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="15"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="16"><A HREF="#lbAG">NOTE</A><DD>
|
|
<DT id="17"><A HREF="#lbAH">FILES</A><DD>
|
|
<DT id="18"><A HREF="#lbAI">ATTRIBUTES</A><DD>
|
|
<DT id="19"><A HREF="#lbAJ">CONFORMING TO</A><DD>
|
|
<DT id="20"><A HREF="#lbAK">NOTES</A><DD>
|
|
<DT id="21"><A HREF="#lbAL">EXAMPLE</A><DD>
|
|
<DT id="22"><A HREF="#lbAM">SEE ALSO</A><DD>
|
|
<DT id="23"><A HREF="#lbAN">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:44 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|