getpwent.c revision 1.1.1.2 1 1.1 christos /* $NetBSD: getpwent.c,v 1.1.1.2 2012/09/09 16:07:55 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos * Copyright (c) 1996,1999 by Internet Software Consortium.
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 1.1 christos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos #if !defined(LINT) && !defined(CODECENTER)
21 1.1.1.2 christos static const char rcsid[] = "Id: getpwent.c,v 1.3 2005/04/27 04:56:26 sra Exp ";
22 1.1 christos #endif
23 1.1 christos
24 1.1 christos /* Imports */
25 1.1 christos
26 1.1 christos #include "port_before.h"
27 1.1 christos
28 1.1 christos #if !defined(WANT_IRS_PW) || defined(__BIND_NOSTATIC)
29 1.1 christos static int __bind_irs_pw_unneeded;
30 1.1 christos #else
31 1.1 christos
32 1.1 christos #include <sys/types.h>
33 1.1 christos
34 1.1 christos #include <netinet/in.h>
35 1.1 christos #include <arpa/nameser.h>
36 1.1 christos
37 1.1 christos #include <errno.h>
38 1.1 christos #include <pwd.h>
39 1.1 christos #include <resolv.h>
40 1.1 christos #include <stdio.h>
41 1.1 christos #include <string.h>
42 1.1 christos
43 1.1 christos #include <irs.h>
44 1.1 christos
45 1.1 christos #include "port_after.h"
46 1.1 christos
47 1.1 christos #include "irs_data.h"
48 1.1 christos
49 1.1 christos /* Forward */
50 1.1 christos
51 1.1 christos static struct net_data * init(void);
52 1.1 christos
53 1.1 christos /* Public */
54 1.1 christos
55 1.1 christos struct passwd *
56 1.1 christos getpwent(void) {
57 1.1 christos struct net_data *net_data = init();
58 1.1 christos
59 1.1 christos return (getpwent_p(net_data));
60 1.1 christos }
61 1.1 christos
62 1.1 christos struct passwd *
63 1.1 christos getpwnam(const char *name) {
64 1.1 christos struct net_data *net_data = init();
65 1.1 christos
66 1.1 christos return (getpwnam_p(name, net_data));
67 1.1 christos }
68 1.1 christos
69 1.1 christos struct passwd *
70 1.1 christos getpwuid(uid_t uid) {
71 1.1 christos struct net_data *net_data = init();
72 1.1 christos
73 1.1 christos return (getpwuid_p(uid, net_data));
74 1.1 christos }
75 1.1 christos
76 1.1 christos int
77 1.1 christos setpassent(int stayopen) {
78 1.1 christos struct net_data *net_data = init();
79 1.1 christos
80 1.1 christos return (setpassent_p(stayopen, net_data));
81 1.1 christos }
82 1.1 christos
83 1.1 christos #ifdef SETPWENT_VOID
84 1.1 christos void
85 1.1 christos setpwent() {
86 1.1 christos struct net_data *net_data = init();
87 1.1 christos
88 1.1 christos setpwent_p(net_data);
89 1.1 christos }
90 1.1 christos #else
91 1.1 christos int
92 1.1 christos setpwent() {
93 1.1 christos struct net_data *net_data = init();
94 1.1 christos
95 1.1 christos return (setpwent_p(net_data));
96 1.1 christos }
97 1.1 christos #endif
98 1.1 christos
99 1.1 christos void
100 1.1 christos endpwent() {
101 1.1 christos struct net_data *net_data = init();
102 1.1 christos
103 1.1 christos endpwent_p(net_data);
104 1.1 christos }
105 1.1 christos
106 1.1 christos /* Shared private. */
107 1.1 christos
108 1.1 christos struct passwd *
109 1.1 christos getpwent_p(struct net_data *net_data) {
110 1.1 christos struct irs_pw *pw;
111 1.1 christos
112 1.1 christos if (!net_data || !(pw = net_data->pw))
113 1.1 christos return (NULL);
114 1.1 christos net_data->pw_last = (*pw->next)(pw);
115 1.1 christos return (net_data->pw_last);
116 1.1 christos }
117 1.1 christos
118 1.1 christos struct passwd *
119 1.1 christos getpwnam_p(const char *name, struct net_data *net_data) {
120 1.1 christos struct irs_pw *pw;
121 1.1 christos
122 1.1 christos if (!net_data || !(pw = net_data->pw))
123 1.1 christos return (NULL);
124 1.1 christos if (net_data->pw_stayopen && net_data->pw_last &&
125 1.1 christos !strcmp(net_data->pw_last->pw_name, name))
126 1.1 christos return (net_data->pw_last);
127 1.1 christos net_data->pw_last = (*pw->byname)(pw, name);
128 1.1 christos if (!net_data->pw_stayopen)
129 1.1 christos endpwent();
130 1.1 christos return (net_data->pw_last);
131 1.1 christos }
132 1.1 christos
133 1.1 christos struct passwd *
134 1.1 christos getpwuid_p(uid_t uid, struct net_data *net_data) {
135 1.1 christos struct irs_pw *pw;
136 1.1 christos
137 1.1 christos if (!net_data || !(pw = net_data->pw))
138 1.1 christos return (NULL);
139 1.1 christos if (net_data->pw_stayopen && net_data->pw_last &&
140 1.1 christos net_data->pw_last->pw_uid == uid)
141 1.1 christos return (net_data->pw_last);
142 1.1 christos net_data->pw_last = (*pw->byuid)(pw, uid);
143 1.1 christos if (!net_data->pw_stayopen)
144 1.1 christos endpwent();
145 1.1 christos return (net_data->pw_last);
146 1.1 christos }
147 1.1 christos
148 1.1 christos int
149 1.1 christos setpassent_p(int stayopen, struct net_data *net_data) {
150 1.1 christos struct irs_pw *pw;
151 1.1 christos
152 1.1 christos if (!net_data || !(pw = net_data->pw))
153 1.1 christos return (0);
154 1.1 christos (*pw->rewind)(pw);
155 1.1 christos net_data->pw_stayopen = (stayopen != 0);
156 1.1 christos if (stayopen == 0)
157 1.1 christos net_data_minimize(net_data);
158 1.1 christos return (1);
159 1.1 christos }
160 1.1 christos
161 1.1 christos #ifdef SETPWENT_VOID
162 1.1 christos void
163 1.1 christos setpwent_p(struct net_data *net_data) {
164 1.1 christos (void) setpassent_p(0, net_data);
165 1.1 christos }
166 1.1 christos #else
167 1.1 christos int
168 1.1 christos setpwent_p(struct net_data *net_data) {
169 1.1 christos return (setpassent_p(0, net_data));
170 1.1 christos }
171 1.1 christos #endif
172 1.1 christos
173 1.1 christos void
174 1.1 christos endpwent_p(struct net_data *net_data) {
175 1.1 christos struct irs_pw *pw;
176 1.1 christos
177 1.1 christos if ((net_data != NULL) && ((pw = net_data->pw) != NULL))
178 1.1 christos (*pw->minimize)(pw);
179 1.1 christos }
180 1.1 christos
181 1.1 christos /* Private */
182 1.1 christos
183 1.1 christos static struct net_data *
184 1.1 christos init() {
185 1.1 christos struct net_data *net_data;
186 1.1 christos if (!(net_data = net_data_init(NULL)))
187 1.1 christos goto error;
188 1.1 christos if (!net_data->pw) {
189 1.1 christos net_data->pw = (*net_data->irs->pw_map)(net_data->irs);
190 1.1 christos
191 1.1 christos if (!net_data->pw || !net_data->res) {
192 1.1 christos error:
193 1.1 christos errno = EIO;
194 1.1 christos return (NULL);
195 1.1 christos }
196 1.1 christos (*net_data->pw->res_set)(net_data->pw, net_data->res, NULL);
197 1.1 christos }
198 1.1 christos
199 1.1 christos return (net_data);
200 1.1 christos }
201 1.1 christos
202 1.1 christos #endif /* WANT_IRS_PW */
203 1.1 christos /*! \file */
204