xref: /openbsd-src/lib/libc/gen/getgrent.3 (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1.\"	$OpenBSD: getgrent.3,v 1.16 2008/07/28 20:19:32 jmc Exp $
2.\"
3.\" Copyright (c) 1989, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd $Mdocdate: July 28 2008 $
31.Dt GETGRENT 3
32.Os
33.Sh NAME
34.Nm getgrent ,
35.Nm getgrnam ,
36.Nm getgrnam_r ,
37.Nm getgrgid ,
38.Nm getgrgid_r ,
39.Nm setgroupent ,
40.\" .Nm setgrfile ,
41.Nm setgrent ,
42.Nm endgrent
43.Nd group database operations
44.Sh SYNOPSIS
45.Fd #include <sys/types.h>
46.Fd #include <grp.h>
47.Ft struct group *
48.Fn getgrent void
49.Ft struct group *
50.Fn getgrnam "const char *name"
51.Ft int
52.Fn getgrnam_r "const char *name" "struct group *grp" "char *buffer" "size_t bufsize" "struct group **result"
53.Ft struct group *
54.Fn getgrgid "gid_t gid"
55.Ft int
56.Fn getgrgid_r "gid_t gid" "struct group *grp" "char *buffer" "size_t bufsize" "struct group **result"
57.Ft int
58.Fn setgroupent "int stayopen"
59.\" .Ft void
60.\" .Fn setgrfile "const char *name"
61.Ft void
62.Fn setgrent void
63.Ft void
64.Fn endgrent void
65.Sh DESCRIPTION
66These functions operate on the group database file
67.Pa /etc/group
68which is described
69in
70.Xr group 5 .
71Each line of the database is defined by the structure
72.Li struct group
73found in the include
74file
75.Aq Pa grp.h :
76.Bd -literal -offset indent
77struct group {
78	char	*gr_name;	/* group name */
79	char	*gr_passwd;	/* group password */
80	gid_t	gr_gid;		/* group id */
81	char	**gr_mem;	/* group members */
82};
83.Ed
84.Pp
85The functions
86.Fn getgrnam
87and
88.Fn getgrgid
89search the group database for the given group name pointed to by
90.Fa name
91or the group ID pointed to by
92.Fa gid ,
93respectively, returning the first one encountered.
94Identical group names or group GIDs may result in undefined behavior.
95.Pp
96.Fn getgrent
97sequentially reads the group database and is intended for programs
98that wish to step through the complete list of groups.
99.Pp
100All three routines will open the group file for reading, if necessary.
101.Pp
102.Fn setgroupent
103opens the file, or rewinds it if it is already open.
104If
105.Fa stayopen
106is non-zero, file descriptors are left open, significantly speeding
107subsequent function calls.
108This functionality is unnecessary for
109.Fn getgrent
110as it doesn't close its file descriptors by default.
111It should also
112be noted that it is dangerous for long-running programs to use this
113functionality as the group file may be updated.
114.Pp
115.Fn setgrent
116is equivalent to
117.Fn setgroupent
118with an argument of zero.
119.Pp
120The
121.Fn endgrent
122function closes any open files.
123.Pp
124The
125.Fn getgrgid_r
126and
127.Fn getgrnam_r
128functions both update the group structure pointed to by
129.Fa grp
130and store a pointer to that structure at the location pointed to by
131.Fa result .
132The structure is filled with an entry from the group database with a
133matching
134.Fa gid
135or
136.Fa name .
137Storage referenced by the group structure will be allocated from the memory
138provided with the
139.Fa buffer
140parameter, which is
141.Fa bufsiz
142characters in size.
143.\" The maximum size needed for this buffer can be determined with the
144.\" .Dv _SC_GETGR_R_SIZE_MAX
145.\" .Xr sysconf 2
146.\" parameter.
147.Sh YP SUPPORT
148If YP is active, the functions
149.Fn getgrent
150and
151.Fn getgrnam
152also use the
153.Pa group.byname
154YP map and the function
155.Fn getgrgid
156also uses the
157.Pa group.bygid
158YP map in addition to the group file,
159respecting the order of normal and YP entries in the group file.
160.Sh RETURN VALUES
161The functions
162.Fn getgrent ,
163.Fn getgrnam ,
164and
165.Fn getgrgid
166return a pointer to the group entry if successful; if end-of-file
167is reached or an error occurs a null pointer is returned.
168The
169.Fn setgroupent
170function returns the value 1 if successful, otherwise 0.
171The
172.Fn endgrent
173and
174.Fn setgrent
175functions have no return value.
176The functions
177.Fn getgrgid_r
178and
179.Fn getgrnam_r
180store a null pointer at the location pointed to by
181.Fa result
182and return the error number
183if an error occurs, or the requested entry is not found.
184.Sh FILES
185.Bl -tag -width /etc/group -compact
186.It Pa /etc/group
187group database file
188.El
189.Sh SEE ALSO
190.Xr getpwent 3 ,
191.Xr ypclnt 3 ,
192.Xr group 5 ,
193.Xr yp 8
194.Sh HISTORY
195The functions
196.Fn endgrent ,
197.Fn getgrent ,
198.Fn getgrnam ,
199.Fn getgrgid ,
200and
201.Fn setgrent
202appeared in
203.At v7 .
204The functions
205.Fn setgrfile
206and
207.Fn setgroupent
208appeared in
209.Bx 4.3 Reno .
210.Pp
211The historic function
212.Xr setgrfile 3 ,
213which allowed the specification of alternate group databases, has
214been deprecated and is no longer available.
215.Sh BUGS
216The functions
217.Fn getgrent ,
218.Fn getgrnam ,
219.Fn getgrgid ,
220.Fn setgroupent ,
221and
222.Fn setgrent
223leave their results in an internal static object and return
224a pointer to that object.
225Subsequent calls to the same function will modify the same object.
226.Pp
227The functions
228.Fn getgrent ,
229.Fn endgrent ,
230.Fn setgroupent ,
231and
232.Fn setgrent
233are fairly useless in a networked environment and should be
234avoided, if possible.
235