xref: /freebsd/lib/libc/gen/getgrent.3 (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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.
171In the latter case,
172.Va errno
173will be set.
174The functions
175.Fn getgrent_r ,
176.Fn getgrnam_r ,
177and
178.Fn getgrgid_r
179return 0 if no error occurred, or an error number to indicate failure.
180It is not an error if a matching entry is not found.
181(Thus, if
182.Fa result
183is set to
184.Dv NULL
185and the return value is 0, no matching entry exists.)
186.Pp
187The functions
188.Fn setgroupent
189and
190.Fn setgrent
191return the value 1 if successful, otherwise the value
1920 is returned.
193The functions
194.Fn endgrent
195and
196.Fn setgrfile
197have no return value.
198.Sh FILES
199.Bl -tag -width /etc/group -compact
200.It Pa /etc/group
201group database file
202.El
203.Sh SEE ALSO
204.Xr getpwent 3 ,
205.Xr group 5 ,
206.Xr nsswitch.conf 5 ,
207.Xr yp 8
208.Sh HISTORY
209The functions
210.Fn endgrent ,
211.Fn getgrent ,
212.Fn getgrnam ,
213.Fn getgrgid ,
214and
215.Fn setgrent
216appeared in
217.At v7 .
218The functions
219.Fn setgrfile
220and
221.Fn setgroupent
222appeared in
223.Bx 4.3 Reno .
224The functions
225.Fn getgrent_r ,
226.Fn getgrnam_r ,
227and
228.Fn getgrgid_r
229appeared in
230.Fx 5.1 .
231.Sh STANDARDS
232The
233.Fn getgrent ,
234.Fn getgrnam ,
235.Fn getgrnam_r ,
236.Fn getgrgid ,
237.Fn getgrgid_r
238and
239.Fn endgrent
240functions conform to
241.St -p1003.1-96 .
242The
243.Fn setgrent
244function differs from that standard in that its return type is
245.Vt int
246rather than
247.Vt void .
248.Sh COMPATIBILITY
249The historic function
250.Fn setgrfile ,
251which allowed the specification of alternate password databases, has
252been deprecated and is no longer available.
253.Sh BUGS
254The functions
255.Fn getgrent ,
256.Fn getgrnam ,
257.Fn getgrgid ,
258.Fn setgroupent
259and
260.Fn setgrent
261leave their results in an internal static object and return
262a pointer to that object.
263Subsequent calls to
264the same function
265will modify the same object.
266.Pp
267The functions
268.Fn getgrent ,
269.Fn getgrent_r ,
270.Fn endgrent ,
271.Fn setgroupent ,
272and
273.Fn setgrent
274are fairly useless in a networked environment and should be
275avoided, if possible.
276The
277.Fn getgrent
278and
279.Fn getgrent_r
280functions
281make no attempt to suppress duplicate information if multiple
282sources are specified in
283.Xr nsswitch.conf 5 .
284