summaryrefslogtreecommitdiffstats
path: root/src/plugin.c
blob: b4a662d8f02630824e969853786838ca3592ba1a (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
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
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#include <rpc/rpc.h>
#include <rpc/pmap_clnt.h>
#include <rpcsvc/yp_prot.h>

#include <nspr.h>
#include <secport.h>
#include <plarenas.h>
#include <dirsrv/slapi-plugin.h>

#include "nis.h"
#include "plugin.h"
#include "portmap.h"
#include "schema.h"
#include "stream.h"

#define PACKAGE_VERSION "0.0"

/* the module initialization function */
static Slapi_PluginDesc
plugin_description = {
	.spd_id = "nis-plugin",
	.spd_vendor = "hamdingers.org",
	.spd_version = PACKAGE_VERSION,
	.spd_description = "NIS Service Plugin",
};

static void *
listener_thread(void *p)
{
	struct plugin_state *state = p;
	struct pollfd fds[4];
	int client, reqsize, i;
	char dgram[65536];
	struct sockaddr_storage client_addr;
	socklen_t client_addrlen;
	for (;;) {
		/* Set up for polling. */
		memset(&fds, 0, sizeof(fds));
		for (i = 0; i < state->n_listeners; i++) {
			fds[i].fd = state->listenfd[i];
			fds[i].events = POLLIN;
		}
		switch (poll(fds, state->n_listeners, -1) != -1) {
		case -1:
			return NULL;
			break;
		case 0:
			continue;
		default:
			/* Iterate over listening sockets with work for us. */
			for (i = 0; i < state->n_listeners; i++) {
				if ((fds[i].revents & POLLIN) == 0) {
					continue;
				}
				switch (state->sock_type[i]) {
				case SOCK_DGRAM:
					client_addrlen = sizeof(client_addr);
					reqsize = recvfrom(fds[i].fd,
							   dgram,
							   sizeof(dgram),
							   0,
							   (struct sockaddr *) &client_addr,
							   &client_addrlen);
					slapi_log_error(SLAPI_LOG_PLUGIN,
							plugin_description.spd_id,
							"datagram request\n");
					nis_process_request(state, fds[i].fd,
							    (struct sockaddr *)
							    &client_addr,
							    client_addrlen,
							    dgram, reqsize);
					break;
				case SOCK_STREAM:
					client = accept(fds[i].fd, NULL, NULL);
					if (client != -1) {
						slapi_log_error(SLAPI_LOG_PLUGIN,
								plugin_description.spd_id,
								"new stream request\n");
						stream_client_start(state,
								    client);
					}
					break;
				default:
					slapi_log_error(SLAPI_LOG_PLUGIN,
							plugin_description.spd_id,
							"unknown request\n");
					break;
				}
			}
		}
	}
	return state;
}

/* Start the plugin's work thread. */
static int
plugin_start(Slapi_PBlock *pb)
{
	struct plugin_state *state;
	const char *pname;
	int i, protocol;
	slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state);
	slapi_log_error(SLAPI_LOG_PLUGIN, "plugin_start",
			"plugin starting\n");
	/* Register the listener sockets with the portmapper. */
	if (state->resvport != -1) {
		for (i = 0; i < state->n_listeners; i++) {
			switch (state->sock_type[i]) {
			case SOCK_DGRAM:
				protocol = IPPROTO_UDP;
				pname = "UDP";
				break;
			case SOCK_STREAM:
				protocol = IPPROTO_TCP;
				pname = "TCP";
				break;
			default:
				protocol = IPPROTO_IP;
				pname = "IP";
				break;
			}
			if (protocol == IPPROTO_IP) {
				continue;
			}
			if (!portmap_register(state->resvport,
					      YPPROG, YPVERS, protocol,
					      state->listenport[i])) {
				slapi_log_error(SLAPI_LOG_PLUGIN,
						plugin_description.spd_id,
						"error registering %s service "
						"with portmap\n", pname);
			}
		}
	}
	/* Start a new listening thread to handle incoming traffic.  FIXME:
	 * switch to using NSPR's threading facilities. */
	if (pthread_create(&state->tid, NULL, &listener_thread, state) != 0) {
		slapi_log_error(SLAPI_LOG_PLUGIN,
				plugin_description.spd_id,
				"error starting listener thread\n");
		return -1;
	}
	slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id,
			"plugin startup completed\n");
	return 0;
}

/* Stop the plugin's work thread. */
static int
plugin_close(Slapi_PBlock *pb)
{
	struct plugin_state *state;
	slapi_pblock_get(pb, SLAPI_PLUGIN_PRIVATE, &state);
	if (state->resvport != -1) {
		/* Clear our registration with the portmapper. */
		portmap_unregister(state->resvport, YPPROG, YPVERS);
		close(state->resvport);
	}
	return 0;
}

static int
setup_listener_sockets(struct plugin_state **lstate)
{
	int sockfd = -1, err, i;
	struct plugin_state *state;
	PLArenaPool *arena = NULL;

	arena = PORT_NewArena(sizeof(double));
	if (arena == NULL) {
		goto failed;
	}
	state = PORT_ArenaZAlloc(arena, sizeof(*state));
	if (state == NULL) {
		goto failed;
	}
	state->arena = arena;

	/* Bind to a privileged port so that the portmapper will let us
	 * register after we've dropped to a non-root account.  We could reuse
	 * the datagram listening socket we'll create later. */
	state->resvport = socket(PF_INET, SOCK_DGRAM, 0);
	if (bindresvport(state->resvport, NULL) != 0) {
		slapi_log_error(SLAPI_LOG_PLUGIN,
				plugin_description.spd_id,
				"error setting up portmap client socket, %d\n",
				(int)getuid());
		close(state->resvport);
		state->resvport = -1;
	}

	/* Privileged port, datagram and stream, IPv4 and IPv6. */
	for (i = 0; i < 4; i++) {
		int sock_pf, sock_type, one = 1;
		struct sockaddr *addr;
		socklen_t addrlen;
		struct sockaddr_storage ss;
		struct sockaddr_in *addr4 = (struct sockaddr_in *) &ss;
		struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) &ss;
		const char *sock_desc;
		u_int16_t *port;
		memset(&ss, 0, sizeof(ss));
		switch (i) {
		case 0:
			sock_pf = PF_INET;
			sock_type = SOCK_DGRAM;
			sock_desc = "udp";
			addr4->sin_family = AF_INET;
			port = &addr4->sin_port;
			break;
		case 1:
			sock_pf = PF_INET;
			sock_type = SOCK_STREAM;
			sock_desc = "tcp";
			addr4->sin_family = AF_INET;
			port = &addr4->sin_port;
			break;
		case 2:
			sock_pf = PF_INET6;
			sock_type = SOCK_DGRAM;
			sock_desc = "udp6";
			addr6->sin6_family = AF_INET6;
			port = &addr6->sin6_port;
			break;
		case 3:
			sock_pf = PF_INET6;
			sock_type = SOCK_STREAM;
			sock_desc = "tcp6";
			addr6->sin6_family = AF_INET6;
			port = &addr6->sin6_port;
			break;
		}
		sockfd = socket(sock_pf, sock_type, 0);
		if (sockfd == -1) {
			slapi_log_error(SLAPI_LOG_PLUGIN,
					plugin_description.spd_id,
					"error creating a %s socket\n",
					sock_desc);
			continue;
		}
		if (setsockopt(sockfd, IPPROTO_IP, SO_REUSEADDR,
			       &one, sizeof(one)) != 0) {
			slapi_log_error(SLAPI_LOG_PLUGIN,
					plugin_description.spd_id,
					"error marking %s socket for reuse\n",
					sock_desc);
		}
		if (bindresvport(sockfd, (struct sockaddr_in *) &ss) != 0) {
			slapi_log_error(SLAPI_LOG_PLUGIN,
					plugin_description.spd_id,
					"error binding %s socket to a "
					"privileged port: %s\n",
					sock_desc, strerror(errno));
			close(sockfd);
			continue;
		}
		if ((sock_type == SOCK_STREAM) && (listen(sockfd, 128) == -1)) {
			slapi_log_error(SLAPI_LOG_PLUGIN,
					plugin_description.spd_id,
					"error marking %s socket for "
					"listening: %s\n", sock_desc,
					strerror(errno));
			close(sockfd);
			continue;
		}
		if (i == 0) {
			state->resvport = sockfd;
		}
		state->listenfd[state->n_listeners] = sockfd;
		state->listenport[state->n_listeners] = ntohs(*port);
		state->sock_pf[state->n_listeners] = sock_pf;
		state->sock_type[state->n_listeners] = sock_type;
		slapi_log_error(SLAPI_LOG_PLUGIN,
				plugin_description.spd_id,
				"listening on port %d for %s%s clients\n",
				state->listenport[state->n_listeners],
				sock_type == SOCK_STREAM ? "tcp" : "udp",
				sock_pf == PF_INET6 ? "6" : "");
		state->n_listeners++;
	}
	slapi_log_error(SLAPI_LOG_PLUGIN,
			plugin_description.spd_id,
			"set up %d listening sockets\n", state->n_listeners);
	*lstate = state;
	return 0;
failed:
	err = errno;
	if (arena != NULL) {
		PORT_FreeArena(arena, PR_TRUE);
	}
	errno = err;
	return -1;
}

int
nis_plugin_init(Slapi_PBlock *pb)
{
	struct plugin_state *state = NULL;
	if (setup_listener_sockets(&state) == -1) {
		slapi_log_error(SLAPI_LOG_PLUGIN, plugin_description.spd_id,
				"error setting up listening sockets\n");
		return -1;
	}
	slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_03);
	slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, &plugin_description);
	slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN, &plugin_start);
	slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN, &plugin_close);
	slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &state->plugin_identity);
	state->plugin_desc = &plugin_description;
	slapi_pblock_set(pb, SLAPI_PLUGIN_PRIVATE, state);
	slapi_log_error(SLAPI_LOG_PLUGIN, state->plugin_desc->spd_id,
			"registered plugin hooks\n");
	return 0;
}