xref: /freebsd/lib/libc/gen/getgrent.3 (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
1.\" Copyright (c) 1989, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     From: @(#)getgrent.3	8.2 (Berkeley) 4/19/94
33.\" $FreeBSD$
34.\"
35.Dd April 16, 2003
36.Dt GETGRENT 3
37.Os
38.Sh NAME
39.Nm getgrent ,
40.Nm getgrent_r ,
41.Nm getgrnam ,
42.Nm getgrnam_r ,
43.Nm getgrgid ,
44.Nm getgrgid_r ,
45.Nm setgroupent ,
46.Nm setgrent ,
47.Nm endgrent
48.Nd group database operations
49.Sh LIBRARY
50.Lb libc
51.Sh SYNOPSIS
52.In grp.h
53.Ft struct group *
54.Fn getgrent void
55.Ft int
56.Fn getgrent_r "struct group *grp" "char *buffer" "size_t bufsize" "struct group **result"
57.Ft struct group *
58.Fn getgrnam "const char *name"
59.Ft int
60.Fn getgrnam_r "const char *name" "struct group *grp" "char *buffer" "size_t bufsize" "struct group **result"
61.Ft struct group *
62.Fn getgrgid "gid_t gid"
63.Ft int
64.Fn getgrgid_r "gid_t gid" "struct group *grp" "char *buffer" "size_t bufsize" "struct group **result"
65.Ft int
66.Fn setgroupent "int stayopen"
67.Ft int
68.Fn setgrent void
69.Ft void
70.Fn endgrent void
71.Sh DESCRIPTION
72These functions operate on the group database file
73.Pa /etc/group
74which is described
75in
76.Xr group 5 .
77Each line of the database is defined by the structure
78.Vt group
79found in the include
80file
81.In grp.h :
82.Bd -literal -offset indent
83struct group {
84	char	*gr_name;	/* group name */
85	char	*gr_passwd;	/* group password */
86	gid_t	gr_gid;		/* group id */
87	char	**gr_mem;	/* group members */
88};
89.Ed
90.Pp
91The functions
92.Fn getgrnam
93and
94.Fn getgrgid
95search the group database for the given group name pointed to by
96.Fa name
97or the group id pointed to by
98.Fa gid ,
99respectively, returning the first one encountered.  Identical group
100names or group gids may result in undefined behavior.
101.Pp
102The
103.Fn getgrent
104function
105sequentially reads the group database and is intended for programs
106that wish to step through the complete list of groups.
107.Pp
108The functions
109.Fn getgrent_r ,
110.Fn getgrnam_r ,
111and
112.Fn getgrgid_r
113are thread-safe versions of
114.Fn getgrent ,
115.Fn getgrnam ,
116and
117.Fn getgrgid ,
118respectively.
119The caller must provide storage for the results of the search in
120the
121.Fa grp ,
122.Fa buffer ,
123.Fa bufsize ,
124and
125.Fa result
126arguments.
127When these functions are successful, the
128.Fa grp
129argument will be filled-in, and a pointer to that argument will be
130stored in
131.Fa result .
132If an entry is not found or an error occurs,
133.Fa result
134will be set to
135.Dv NULL .
136.Pp
137These functions will open the group file for reading, if necessary.
138.Pp
139The
140.Fn setgroupent
141function
142opens the file, or rewinds it if it is already open.  If
143.Fa stayopen
144is non-zero, file descriptors are left open, significantly speeding
145functions subsequent calls.  This functionality is unnecessary for
146.Fn getgrent
147as it doesn't close its file descriptors by default.  It should also
148be noted that it is dangerous for long-running programs to use this
149functionality as the group file may be updated.
150.Pp
151The
152.Fn setgrent
153function
154is identical to
155.Fn setgroupent
156with an argument of zero.
157.Pp
158The
159.Fn endgrent
160function
161closes any open files.
162.Sh RETURN VALUES
163The functions
164.Fn getgrent ,
165.Fn getgrnam ,
166and
167.Fn getgrgid ,
168return a pointer to a group structure on success or
169.Dv NULL
170if the entry is not found or if an error occurs.
171If an error does occur,
172.Va errno
173will be set.
174Note that programs must explicitly set
175.Va errno
176to zero before calling any of these functions if they need to
177distinguish between a non-existent entry and an error.
178The functions
179.Fn getgrent_r ,
180.Fn getgrnam_r ,
181and
182.Fn getgrgid_r
183return 0 if no error occurred, or an error number to indicate failure.
184It is not an error if a matching entry is not found.
185(Thus, if
186.Fa result
187is set to
188.Dv NULL
189and the return value is 0, no matching entry exists.)
190.Pp
191The functions
192.Fn setgroupent
193and
194.Fn setgrent
195return the value 1 if successful, otherwise the value
1960 is returned.
197The functions
198.Fn endgrent
199and
200.Fn setgrfile
201have no return value.
202.Sh FILES
203.Bl -tag -width /etc/group -compact
204.It Pa /etc/group
205group database file
206.El
207.Sh SEE ALSO
208.Xr getpwent 3 ,
209.Xr group 5 ,
210.Xr nsswitch.conf 5 ,
211.Xr yp 8
212.Sh HISTORY
213The functions
214.Fn endgrent ,
215.Fn getgrent ,
216.Fn getgrnam ,
217.Fn getgrgid ,
218and
219.Fn setgrent
220appeared in
221.At v7 .
222The functions
223.Fn setgrfile
224and
225.Fn setgroupent
226appeared in
227.Bx 4.3 Reno .
228The functions
229.Fn getgrent_r ,
230.Fn getgrnam_r ,
231and
232.Fn getgrgid_r
233appeared in
234.Fx 5.1 .
235.Sh STANDARDS
236The
237.Fn getgrent ,
238.Fn getgrnam ,
239.Fn getgrnam_r ,
240.Fn getgrgid ,
241.Fn getgrgid_r
242and
243.Fn endgrent
244functions conform to
245.St -p1003.1-96 .
246The
247.Fn setgrent
248function differs from that standard in that its return type is
249.Vt int
250rather than
251.Vt void .
252.Sh COMPATIBILITY
253The historic function
254.Fn setgrfile ,
255which allowed the specification of alternate password databases, has
256been deprecated and is no longer available.
257.Sh BUGS
258The functions
259.Fn getgrent ,
260.Fn getgrnam ,
261.Fn getgrgid ,
262.Fn setgroupent
263and
264.Fn setgrent
265leave their results in an internal static object and return
266a pointer to that object.
267Subsequent calls to
268the same function
269will modify the same object.
270.Pp
271The functions
272.Fn getgrent ,
273.Fn getgrent_r ,
274.Fn endgrent ,
275.Fn setgroupent ,
276and
277.Fn setgrent
278are fairly useless in a networked environment and should be
279avoided, if possible.
280The
281.Fn getgrent
282and
283.Fn getgrent_r
284functions
285make no attempt to suppress duplicate information if multiple
286sources are specified in
287.Xr nsswitch.conf 5 .
288