xref: /original-bsd/sbin/mount_portal/pt_file.c (revision 27393bdf)
1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
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	8.3 (Berkeley) 07/03/94
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 <string.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <sys/types.h>
23 #include <sys/param.h>
24 #include <sys/syslog.h>
25 
26 #include "portald.h"
27 
28 int portal_file(pcr, key, v, so, fdp)
29 struct portal_cred *pcr;
30 char *key;
31 char **v;
32 int so;
33 int *fdp;
34 {
35 	int fd;
36 	char pbuf[MAXPATHLEN];
37 	int error;
38 	gid_t 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