man-pages/man3/getpwnam.3.html
2021-03-31 01:06:50 +01:00

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">&nbsp;</A>
<H2>NAME</H2>
getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;</B>
<B>#include &lt;<A HREF="file:///usr/include/pwd.h">pwd.h</A>&gt;</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>&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;/*&nbsp;Glibc&nbsp;versions&nbsp;&lt;=&nbsp;2.19:&nbsp;*/&nbsp;_BSD_SOURCE&nbsp;||&nbsp;_SVID_SOURCE
</DL>
<A NAME="lbAD">&nbsp;</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>&lt;<A HREF="file:///usr/include/pwd.h">pwd.h</A>&gt;</I> as follows:
<P>
struct passwd {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;*pw_name;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;username&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;*pw_passwd;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;user&nbsp;password&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;uid_t&nbsp;&nbsp;&nbsp;pw_uid;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;user&nbsp;ID&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;gid_t&nbsp;&nbsp;&nbsp;pw_gid;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;group&nbsp;ID&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;*pw_gecos;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;user&nbsp;information&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;*pw_dir;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;home&nbsp;directory&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;&nbsp;*pw_shell;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;shell&nbsp;program&nbsp;*/
};
<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>&nbsp;&nbsp;&nbsp;&nbsp;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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</A>
<H2>NOTES</H2>
The formulation given above under &quot;RETURN VALUE&quot; is from POSIX.1-2001.
It does not call &quot;not found&quot; 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())-&gt;pw_dir</I>)
since this allows the user to modify their notion of
&quot;the home directory&quot; during a login session.
To determine the (initial) home directory of another user,
it is necessary to use
<I>getpwnam(username)-&gt;pw_dir</I>
or similar.
<A NAME="lbAL">&nbsp;</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 &lt;<A HREF="file:///usr/include/pwd.h">pwd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;passwd&nbsp;pwd;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;passwd&nbsp;*result;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*buf;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;bufsize;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;s;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;username\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;bufsize&nbsp;=&nbsp;sysconf(_SC_GETPW_R_SIZE_MAX);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(bufsize&nbsp;==&nbsp;-1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Value&nbsp;was&nbsp;indeterminate&nbsp;*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bufsize&nbsp;=&nbsp;16384;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Should&nbsp;be&nbsp;more&nbsp;than&nbsp;enough&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;buf&nbsp;=&nbsp;malloc(bufsize);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(buf&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;malloc&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;getpwnam_r(argv[1],&nbsp;&amp;pwd,&nbsp;buf,&nbsp;bufsize,&nbsp;&amp;result);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(result&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;==&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Not&nbsp;found\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;errno&nbsp;=&nbsp;s;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;getpwnam_r&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Name:&nbsp;%s;&nbsp;UID:&nbsp;%ld\n&quot;,&nbsp;pwd.pw_gecos,&nbsp;(long)&nbsp;pwd.pw_uid);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAM">&nbsp;</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">&nbsp;</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">&nbsp;</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>