File: ugidd.c

package info (click to toggle)
netstd 3.07-2hamm.5
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 6,384 kB
  • ctags: 9,087
  • sloc: ansic: 72,547; cpp: 6,141; makefile: 1,681; yacc: 1,615; sh: 1,220; perl: 303; awk: 46
file content (341 lines) | stat: -rw-r--r-- 6,905 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* UNFSD - copyright Mark A Shand, May 1988.
 * This software maybe be used for any purpose provided
 * the above copyright notice is retained.  It is supplied
 * as is, with no warranty expressed or implied.
 *
 * Authors:	Mark A. Shand
 *		Olaf Kirch <okir@monad.swb.de>
 */

#include "site.h"

/* Only compile ugidd if nfs server has support for it. */
#ifdef ENABLE_UGID_DAEMON

#include "system.h"
#include <rpc/rpc.h>
#include <rpc/pmap_clnt.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include "ugid.h"
#include "logging.h"
#include "haccess.h"
#ifdef HAVE_LIBWRAP_BUG
#include <syslog.h>
#endif


static void	ugidprog_1(struct svc_req *rqstp, SVCXPRT *transp);
static void	usage(void);

#ifndef HAVE_RPCGEN_C
#define authenticate_1_svc	authenticate_1
#define name_uid_1_svc		name_uid_1
#define group_gid_1_svc		group_gid_1
#define uid_name_1_svc		uid_name_1
#define gid_group_1_svc		gid_group_1
#endif


static struct option longopts[] = {
	{ "debug", 0, 0, 'd' },
	{ NULL, 0, 0, 0 }
};

int
main(argc, argv)
int	argc;
char	**argv;
{
	SVCXPRT	*transp;
	int	c, longind;
	int	foreground = 0;

#ifndef HOSTS_ACCESS
	fprintf(stderr,
		"\n *** WARNING: This copy of ugidd has been compiled without\n"
		" *** support for host_access checking. This is very risky.\n"
		" *** Please consider recompiling it with access checking.\n");
	sleep(1);
#endif

	while ((c = getopt_long(argc, argv, "d", longopts, &longind)) != EOF) {
		switch (c) {
		case 'd':
			foreground = 1;
			enable_logging("ugid");
			break;
		default:
			usage();
		}
	}

        (void)pmap_unset(UGIDPROG, UGIDVERS);

        transp = svcudp_create(RPC_ANYSOCK);
        if (transp == NULL) {
                (void)fprintf(stderr, "cannot create udp service.\n");
                exit(1);
        }
        if (!svc_register(transp, UGIDPROG, UGIDVERS, ugidprog_1, IPPROTO_UDP)) {
                fprintf(stderr, "unable to register (UGIDPROG, UGIDVERS, UDP)\n");
                exit(1);
        }

        transp = svctcp_create(RPC_ANYSOCK, 0, 0);
        if (transp == NULL) {
                fprintf(stderr, "cannot create tcp service.\n");
                exit(1);
        }
        if (!svc_register(transp, UGIDPROG, UGIDVERS, ugidprog_1, IPPROTO_TCP)) {
                fprintf(stderr, "unable to register (UGIDPROG, UGIDVERS, TCP)\n");
                exit(1);
        }

	if (!foreground) {
		if ((c = fork()) > 0)
			exit(0);
		if (c < 0) {
			fprintf(stderr, "ugidd: cannot fork: %s\n",
						strerror(errno));
			exit(-1);
		}
		close(0);
		close(1);
		close(2);
#ifdef HAVE_SETSID
		setsid();
#else
		{
			int fd;

			if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
				ioctl(fd, TIOCNOTTY, (char *) NULL);
				close(fd);
			}
		}
#endif
	}

	log_open("ugidd", foreground);

	svc_run();
	Dprintf(L_ERROR, "svc_run returned\n");
	return 1;
}

static void
usage()
{
	fprintf(stderr, "rpc.ugidd: [-d]\n");
	exit (2);
}

static void
ugidprog_1(struct svc_req *rqstp, SVCXPRT *transp)
{
	union {
		int authenticate_1_arg;
		ugname name_uid_1_arg;
		ugname group_gid_1_arg;
		int uid_name_1_arg;
		int gid_group_1_arg;
	} argument;
	char		*result;
	xdrproc_t	xdr_argument, xdr_result;
	char		*(*local)();

	if (!client_checkaccess("rpc.ugidd", svc_getcaller(transp), 1))
		return;

	switch (rqstp->rq_proc) {
	case NULLPROC:
		svc_sendreply(transp, (xdrproc_t) xdr_void, (char *) NULL);
		return;

	case AUTHENTICATE:
		xdr_argument = (xdrproc_t) xdr_int;
		xdr_result   = (xdrproc_t) xdr_int;
		local = (char *(*)()) authenticate_1_svc;
		break;

	case NAME_UID:
		xdr_argument = (xdrproc_t) xdr_ugname;
		xdr_result   = (xdrproc_t) xdr_int;
		local = (char *(*)()) name_uid_1_svc;
		break;

	case GROUP_GID:
		xdr_argument = (xdrproc_t) xdr_ugname;
		xdr_result = (xdrproc_t) xdr_int;
		local = (char *(*)()) group_gid_1_svc;
		break;

	case UID_NAME:
		xdr_argument = (xdrproc_t) xdr_int;
		xdr_result = (xdrproc_t) xdr_ugname;
		local = (char *(*)()) uid_name_1_svc;
		break;

	case GID_GROUP:
		xdr_argument = (xdrproc_t) xdr_int;
		xdr_result = (xdrproc_t) xdr_ugname;
		local = (char *(*)()) gid_group_1_svc;
		break;

	default:
		svcerr_noproc(transp);
		return;
	}
	bzero((char *)&argument, sizeof(argument));
	if (!svc_getargs(transp, xdr_argument, &argument)) {
		svcerr_decode(transp);
		return;
	}
	result = (*local)(&argument, rqstp);
	if (result != NULL && !svc_sendreply(transp, xdr_result, result)) {
		svcerr_systemerr(transp);
	}
	if (!svc_freeargs(transp, xdr_argument, &argument)) {
		(void)fprintf(stderr, "unable to free arguments\n");
		exit(1);
	}
}

int *
authenticate_1_svc(argp, rqstp)
	int *argp;
	struct svc_req *rqstp;
{
	static int res;
	int	s;
	struct sockaddr_in	sendaddr, destaddr;
	int	dummy;
	short	lport;

	bzero(&res, sizeof res);
	destaddr = *svc_getcaller(rqstp->rq_xprt);
	destaddr.sin_port = htons(*argp);
	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
		goto bad;
	setsockopt(s, SOL_SOCKET, SO_LINGER, 0, 0);
	bzero((char *) &sendaddr, sizeof sendaddr);
	/* find a reserved port */
	lport = IPPORT_RESERVED - 1;
	sendaddr.sin_family = AF_INET;
	sendaddr.sin_addr.s_addr = INADDR_ANY;
	for (;;)
	{
		sendaddr.sin_port = htons((u_short)lport);
		if (bind(s, (struct sockaddr *)&sendaddr, sizeof sendaddr) >= 0)
			break;
		if (errno != EADDRINUSE && EADDRNOTAVAIL)
			goto bad;
		lport--;
		if (lport <= IPPORT_RESERVED / 2)
			/* give up */
			break;
	}
	if (sendto(s, &dummy, sizeof dummy, 0,
			(struct sockaddr *)&destaddr, sizeof destaddr) < 0)
		goto bad;

	close(s);
	res = 0;
	return (&res);
    bad:
	close(s);
	res = errno == 0 ? -1 : errno;
	return (&res);
}

int *
name_uid_1_svc(argp, rqstp)
	ugname *argp;
	struct svc_req *rqstp;
{
	static int res;
	struct passwd	*pw;

	bzero(&res, sizeof(res));
	if ((pw = getpwnam(*argp)) == NULL)
		res = NOBODY;
	else
		res = pw->pw_uid;

	return (&res);
}


int *
group_gid_1_svc(argp, rqstp)
	ugname *argp;
	struct svc_req *rqstp;
{
	static int res;
	struct group	*gr;

	bzero(&res, sizeof(res));
	if ((gr = getgrnam(*argp)) == NULL)
		res = NOBODY;
	else
		res = gr->gr_gid;

	return (&res);
}


ugname *
uid_name_1_svc(argp, rqstp)
	int *argp;
	struct svc_req *rqstp;
{
	static ugname res;
	struct passwd	*pw;

	bzero(&res, sizeof(res));
	if ((pw = getpwuid(*argp)) == NULL)
		res = "";
	else
		res = pw->pw_name;

	return (&res);
}


ugname *
gid_group_1_svc(argp, rqstp)
	int *argp;
	struct svc_req *rqstp;
{
	static ugname res;
	struct group	*gr;

	bzero(&res, sizeof(res));
	if ((gr = getgrgid(*argp)) == NULL)
		res = "";
	else
		res = gr->gr_name;

	return (&res);
}



#else /* ENABLE_UGID_DAEMON */

#include <stdio.h>

int
main(argc, argv)
	int	argc;
	char	**argv;
{
	fprintf(stderr, 
	"\nThis copy of the Universal NFS server has been compiled without\n");
	fprintf(stderr, "support for the ugidd RPC uid/gid map daemon.\n");
	fprintf(stderr, "This is a dummy program.\n");
	return 1;
}

#endif /* ENABLE_UGID_DAEMON */