summaryrefslogtreecommitdiff
path: root/bin/nshd-tester.c
blob: 110819d3afe8331c0d107db229db301d317f8e4e (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Copyright (C) 2015 Luke Shumaker <lukeshu@sbcglobal.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <error.h>
#include <errno.h>
#include <stdarg.h>
#include <signal.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>

#define _(s) s

const char *xgetenv(const char *name, const char *unset) {
	const char *val = getenv(name);
	if (!val)
		val = unset;
	return val;
}

char *xasprintf(const char *format, ...) {
	va_list arg;
	int len;
	char *str = NULL;

	va_start(arg, format);
	len = vasprintf(&str, format, arg);
	va_end(arg);

	if (len < 0)
		error(EXIT_FAILURE, errno, _("Could not allocate memory in vasprintf"));
	
	return str;
}

#define xasprintfa(...) (__extension__ ({ char *heap = xasprintf(__VA_ARGS__); char *stack = strdupa(heap); free(heap); stack; }))

int pid = -1;
void
sigchld_handler(int sig __attribute__((__unused__))) {
	int status;
	pid = waitpid(pid, &status, WNOHANG);
	int exited = WEXITSTATUS(status);
	error(exited, 0, "%ld exited with status %d", (long)pid, exited);
	exit(0);
}

union addr {
	struct sockaddr gen;
	struct sockaddr_un un;
};

int new_unix_sock(const char *filename, int type) {
	union addr addr;
	addr.un.sun_family = AF_UNIX;
	strcpy(addr.un.sun_path, filename);
	
	int sock = socket(AF_UNIX, type, 0);
	if (sock < 0)
		error(EXIT_FAILURE, errno, "socket(%d, %d)", AF_UNIX, type);
	unlink(filename);
	if (bind(sock, &addr.gen, sizeof(addr)))
		error(EXIT_FAILURE, errno, "bind(%d, sockaddr(\"%s\"))", sock, filename);
	switch (type) {
	case SOCK_STREAM:
	case SOCK_SEQPACKET:
		if (listen(sock, 5))
			error(EXIT_FAILURE, errno, "listen(%d /* \"%s\" */, %d)", sock, filename, 5);
		break;
	case SOCK_DGRAM:
		break;
	default:
		error(EXIT_FAILURE, errno, "new_unix_sock: Unrecognized type: %d", type);
	}
	return sock;
}

char *cmdname = "nshd_runner";
const char *notify_sockname = "notify.sock";
const char *nslcd_sockname = "nslcd.sock";
void cleanup(void) {
	if (nslcd_sockname)
		unlink(nslcd_sockname);
	if (notify_sockname)
		unlink(notify_sockname);
	fprintf(stderr, "%s: Exiting\n", cmdname);
}

int main(int argc, char *argv[]) {
	cmdname = argv[0];
	if (argc != 2) {
		error(2, 0, _("Usage: %s NSHD_PROGRAM"), argv[0]);
	}

	atexit(&cleanup);	
	int nslcd_sock  = new_unix_sock(nslcd_sockname , SOCK_STREAM);
	int notify_sock = new_unix_sock(notify_sockname, SOCK_DGRAM );
	
	struct sigaction act;
	sigemptyset(&act.sa_mask);
	act.sa_flags = SA_RESTART;
	act.sa_handler = sigchld_handler;
	if (sigaction(SIGCHLD, &act, 0))
		error(EXIT_FAILURE, errno, "sigaction");


	pid = fork();
	switch (pid) {
	case -1:
		error(EXIT_FAILURE, errno, "fork");
	case 0:
		close(notify_sock);
		dup2(nslcd_sock, 3);
		if (nslcd_sock != 3)
			close(nslcd_sock);
		pid = getpid();
		setenv("LISTEN_PID", xasprintfa("%ld", (long)pid), 1);
		setenv("LISTEN_FDS", "1", 1);
		setenv("NOTIFY_SOCKET", notify_sockname, 1);
		execl(argv[1], argv[1], NULL);
		error(EXIT_FAILURE, errno, "execl");
	}

	while (1) {
		union addr client_addr;
		socklen_t  client_size;
		char buf[4097];
		ssize_t bytes_read = recvfrom(notify_sock, buf, sizeof(buf)-1, 0, &client_addr.gen, &client_size);
		if (bytes_read < 1)
			error(EXIT_FAILURE, errno, "recvfrom");
		if (buf[bytes_read-1] != '\n') {
			buf[bytes_read] = '\n';
			bytes_read++;
		}
		ssize_t bytes_written = 0;
		while (bytes_written < bytes_read) {
			ssize_t n = write(2, &(buf[bytes_written]), bytes_read-bytes_written);
			if (n < 0) {
				bytes_written = -1;
				break;
			}
			bytes_written += n;
		}
		if (bytes_written < 0)
			error(EXIT_FAILURE, errno, "write");
	}
	error(EXIT_FAILURE, 0, "not reached");
	return EXIT_FAILURE;
}