xref: /original-bsd/sbin/mount_portal/pt_file.c (revision 13deef58)
1 /*
2  * Copyright (c) 1992 The Regents of the University of California
3  * Copyright (c) 1990, 1992 Jan-Simon Pendry
4  * All rights reserved.
5  *
6  * This code is derived from software donated to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * %sccs.include.redist.c%
10  *
11  *	@(#)pt_file.c	5.1 (Berkeley) 07/13/92
12  *
13  * $Id: pt_file.c,v 1.1 1992/05/25 21:43:09 jsp Exp jsp $
14  */
15 
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <sys/param.h>
23 #include <sys/syslog.h>
24 
25 #include "portald.h"
26 
27 int portal_file(pcr, key, v, so, fdp)
28 struct portal_cred *pcr;
29 char *key;
30 char **v;
31 int so;
32 int *fdp;
33 {
34 	int fd;
35 	int gid;
36 	char pbuf[MAXPATHLEN];
37 	int error;
38 	int gidset[NGROUPS];
39 	int i;
40 
41 	pbuf[0] = '/';
42 	strcpy(pbuf+1, key + (v[1] ? strlen(v[1]) : 0));
43 
44 #ifdef DEBUG
45 	printf("path = %s, uid = %d, gid = %d\n", pbuf, pcr->pcr_uid, pcr->pcr_groups[0]);
46 #endif
47 
48 	for (i = 0; i < pcr->pcr_ngroups; i++)
49 		gidset[i] = pcr->pcr_groups[i];
50 
51 	if (setgroups(pcr->pcr_ngroups, gidset) < 0)
52 		return (errno);
53 
54 	if (seteuid(pcr->pcr_uid) < 0)
55 		return (errno);
56 
57 	fd = open(pbuf, O_RDWR|O_CREAT, 0666);
58 	if (fd < 0)
59 		error = errno;
60 	else
61 		error = 0;
62 
63 	if (seteuid((uid_t) 0) < 0) {	/* XXX - should reset gidset too */
64 		error = errno;
65 		syslog(LOG_ERR, "setcred: %s", strerror(error));
66 		if (fd >= 0) {
67 			(void) close(fd);
68 			fd = -1;
69 		}
70 	}
71 
72 	if (error == 0)
73 		*fdp = fd;
74 
75 #ifdef DEBUG
76 	fprintf(stderr, "pt_file returns *fdp = %d, error = %d\n", *fdp, error);
77 #endif
78 
79 	return (error);
80 }
81