summaryrefslogtreecommitdiff
path: root/regress/sys/kern/getpeereid/getpeereid_test.c
blob: 8363d33143ba8d47d5d3c7210e82c701cb524493 (plain)
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
/* $OpenBSD: getpeereid_test.c,v 1.4 2017/03/08 19:28:47 deraadt Exp $ */
/* Written by Marc Espie in 2006 */
/* Public domain */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <err.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

char path[1024];
char *dir;

char *
check_id(int fd)
{
	uid_t sockuid, myuid;
	gid_t sockgid, mygid;
	static char problem[1024];

	if (getpeereid(fd, &sockuid, &sockgid) == -1) {
		snprintf(problem, sizeof problem, "getpeereid: %s", 
		    strerror(errno));
		return problem;
	}
	myuid = geteuid();
	mygid = getgid();
	if (myuid != sockuid) {
		snprintf(problem, sizeof problem, "uid discrepancy %ld vs %ld",
			(long)myuid, (long)sockuid);
		return problem;
	}
	if (mygid != sockgid) {
		snprintf(problem, sizeof problem, "gid discrepancy %ld vs %ld",
			(long)mygid, (long)sockgid);
		return problem;
	}
	return NULL;
}

void
client(struct sockaddr_un *sun)
{
	int s;
	int i;
	int r;
	char *problem;

	s = socket(AF_UNIX, SOCK_STREAM, 0);
	if (s == -1)
		err(1, "Bad socket");

	/* XXX make sure the server started alright */
	for (i = 0; i < 10; i++) {
		r = connect(s, (struct sockaddr *)sun, sizeof(*sun));
		if (r == 0) {
			problem = check_id(s);
			if (problem)
				errx(1, "%s", problem);
			exit(0);
		}
		sleep(5);
	}
	errx(1, "Could not connect after 10 tries");
}


void
server(struct sockaddr_un *sun)
{
	int s, fd;
	struct sockaddr_storage client_addr;
	socklen_t client_len;
	char *problem;

	s = socket(AF_UNIX, SOCK_STREAM, 0);
	if (s == -1)
		err(1, "Bad socket");

	if (bind(s, (struct sockaddr *)sun, sizeof(*sun)) != 0)
		err(1, "bind");
	if (listen(s, 5) != 0) {
		int saved_errno = errno;
		unlink(path);
		rmdir(dir);
		errc(1, saved_errno, "listen");
	}
	fd = accept(s, (struct sockaddr *)&client_addr, &client_len);
	if (fd == -1) {
		int saved_errno = errno;
		unlink(path);
		rmdir(dir);
		errc(1, saved_errno, "accept");
	}
	problem = check_id(fd);
	if (problem)  {
		unlink(path);
		rmdir(dir);
		errx(1, "%s", problem);
	}
	unlink(path);
	rmdir(dir);
}



int
main()
{
	pid_t pid;
	struct sockaddr_un sun;
	char dir_template[] = "/tmp/peer.XXXXXX";

	dir = mkdtemp(dir_template);
	if (!dir)
		err(1, "mkdtemp");
	snprintf(path, sizeof path, "%s/%s", dir, "socket");

	memset(&sun, 0, sizeof(struct sockaddr_un));
	if (strlcpy(sun.sun_path, path, sizeof(sun.sun_path)) >=
	    sizeof(sun.sun_path))
		errx(1, "Memory error");
	sun.sun_family = AF_UNIX;

	/* let's make those two rendez-vous, a bit artificial */
	pid = fork();
	if (pid == -1)
		err(1, "can't fork");
	if (pid == 0) {
		client(&sun);
		exit(0);
	} else {
		int status;

		server(&sun);
		waitpid(pid, &status, 0);
		if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
			printf("getpeereid test okay\n");
			exit(0);
		} else
			errx(1, "Problem with child\n");
	}
}