summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/reparsed/reparsed.c
blob: 17027b0c78e02bb337b12ec3672144af513a4904 (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Reparsed daemon
 */

#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <memory.h>
#include <alloca.h>
#include <ucontext.h>
#include <errno.h>
#include <syslog.h>
#include <string.h>
#include <strings.h>
#include <door.h>
#include <wait.h>
#include <libintl.h>
#include <locale.h>
#include <sys/param.h>
#include <sys/systeminfo.h>
#include <sys/thread.h>
#include <rpc/xdr.h>
#include <priv.h>
#include <sys/fs_reparse.h>
#include <priv_utils.h>
#include <rpcsvc/daemon_utils.h>

#define	REPARSED_CMD_OPTS	"v"
#define	DOOR_RESULT_BUFSZ	(MAXPATHLEN + sizeof (reparsed_door_res_t))
#define	SAFETY_BUFFER		8*1024

static char *MyName;
static int verbose = 0;

static int start_reparsed_svcs();
static void daemonize(void);
static void reparsed_door_call_error(int error, int buflen);
static void reparsed_doorfunc(void *cookie, char *argp, size_t arg_size,
			door_desc_t *dp, uint_t n_desc);

static void
usage()
{
	syslog(LOG_ERR, "Usage: %s", MyName);
	syslog(LOG_ERR, "\t[-v]\t\tverbose error messages)");
	exit(1);
}

static void
warn_hup(int i)
{
	syslog(LOG_ERR, "SIGHUP received: ignored");
	(void) signal(SIGHUP, warn_hup);
}

/*
 * Processing for daemonization
 */
static void
daemonize(void)
{
	switch (fork()) {
	case -1:
		syslog(LOG_ERR, "reparsed: can't fork - errno %d", errno);
		exit(2);
		/* NOTREACHED */
	case 0:		/* child */
		break;

	default:	/* parent */
		_exit(0);
	}
	(void) chdir("/");

	/*
	 * Close stdin, stdout, and stderr.
	 * Open again to redirect input+output
	 */
	(void) close(0);
	(void) close(1);
	(void) close(2);
	(void) open("/dev/null", O_RDONLY);
	(void) open("/dev/null", O_WRONLY);
	(void) dup(1);
	(void) setsid();
}

int
main(int argc, char *argv[])
{
	pid_t pid;
	int c, error;
	struct rlimit rlset;
	char *defval;

	/*
	 * There is no check for non-global zone and Trusted Extensions.
	 * Reparsed works in both of these environments as long as the
	 * services that use reparsed are supported.
	 */

	MyName = argv[0];
	if (geteuid() != 0) {
		syslog(LOG_ERR, "%s must be run as root", MyName);
		exit(1);
	}

	while ((c = getopt(argc, argv, REPARSED_CMD_OPTS)) != EOF) {
		switch (c) {
		case 'v':
			verbose++;
			break;
		default:
			usage();
		}
	}

	daemonize();
	openlog(MyName, LOG_PID | LOG_NDELAY, LOG_DAEMON);

	(void) _create_daemon_lock(REPARSED, DAEMON_UID, DAEMON_GID);
	(void) enable_extended_FILE_stdio(-1, -1);
	switch (_enter_daemon_lock(REPARSED)) {
	case 0:
		break;
	case -1:
		syslog(LOG_ERR, "Error locking for %s", REPARSED);
		exit(2);
	default:
		/* daemon was already running */
		exit(0);
	}

	(void) signal(SIGHUP, warn_hup);

	/*
	 * Make the process a privilege aware daemon.
	 * Only "basic" privileges are required.
	 *
	 */
	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, 0, 0,
	    (char *)NULL) == -1) {
		syslog(LOG_ERR, "should be run with sufficient privileges");
		exit(3);
	}

	/*
	 * Clear basic privileges not required by reparsed.
	 */
	__fini_daemon_priv(PRIV_PROC_FORK, PRIV_PROC_EXEC, PRIV_PROC_SESSION,
	    PRIV_FILE_LINK_ANY, PRIV_PROC_INFO, (char *)NULL);

	return (start_reparsed_svcs());
}

static void
reparsed_door_call_error(int error, int buflen)
{
	reparsed_door_res_t rpd_res;

	memset(&rpd_res, 0, sizeof (reparsed_door_res_t));
	rpd_res.res_status = error;
	rpd_res.res_len = buflen;
	door_return((char *)&rpd_res, sizeof (reparsed_door_res_t), NULL, 0);

	(void) door_return(NULL, 0, NULL, 0);
	/* NOTREACHED */
}

/*
 *  reparsed_doorfunc
 *
 *  argp:  "service_type:service_data" string
 *  dp & n_desc: not used.
 */
static void
reparsed_doorfunc(void *cookie, char *argp, size_t arg_size,
    door_desc_t *dp, uint_t n_desc)
{
	int err;
	size_t bufsz;
	char *svc_type, *svc_data;
	char *cp, *buf, *sbuf, res_buf[DOOR_RESULT_BUFSZ];
	reparsed_door_res_t *resp;

	if ((argp == NULL) || (arg_size == 0)) {
		reparsed_door_call_error(EINVAL, 0);
		/* NOTREACHED */
	}

	if (verbose)
		syslog(LOG_NOTICE, "reparsed_door: [%s, %d]", argp, arg_size);

	if ((svc_type = strdup(argp)) == NULL) {
		reparsed_door_call_error(ENOMEM, 0);
		/* NOTREACHED */
	}

	/*
	 * Door argument string comes in "service_type:service_data" format.
	 * Need to break it into separate "service_type" and "service_data"
	 * string before passing them to reparse_deref() to process them.
	 */
	if ((cp = strchr(svc_type, ':')) == NULL) {
		free(svc_type);
		reparsed_door_call_error(EINVAL, 0);
		/* NOTREACHED */
	}
	*cp++ = '\0';
	svc_data = cp;

	/*
	 * Setup buffer for reparse_deref(). 'bufsz' is the actual
	 * buffer size to hold the result returned by reparse_deref().
	 */
	resp = (reparsed_door_res_t *)res_buf;
	buf = resp->res_data;
	bufsz = sizeof (res_buf) - sizeof (reparsed_door_res_t);

	/*
	 * reparse_deref() calls the service type plugin library to process
	 * the service data. The plugin library function should understand
	 * the context of the service data and should be the one to XDR the
	 * results before returning it to the caller.
	 */
	err = reparse_deref(svc_type, svc_data, buf, &bufsz);

	if (verbose)
		syslog(LOG_NOTICE,
		    "reparsed_deref(svc_type: %s, data: %s, size: %d) -> %d",
		    svc_type, svc_data, bufsz, err);

	switch (err) {
	case 0:
		break;

	case EOVERFLOW:
		/*
		 * bufsz was returned with size needed by reparse_deref().
		 *
		 * We cannot use malloc() here because door_return() never
		 * returns, and memory allocated by malloc() would get leaked.
		 */
		sbuf = alloca(bufsz + sizeof (reparsed_door_res_t));
		if (sbuf == NULL || stack_inbounds(buf) == 0 ||
		    stack_inbounds(buf + sizeof (reparsed_door_res_t) +
		    SAFETY_BUFFER - 1) == 0) {
			free(svc_type);
			reparsed_door_call_error(ENOMEM, 0);
			/* NOTREACHED */
		}

		resp = (reparsed_door_res_t *)sbuf;
		if ((err = reparse_deref(svc_type, svc_data, resp->res_data,
		    &bufsz)) == 0)
			break;

		/* fall through */

	default:
		free(svc_type);
		reparsed_door_call_error(err, 0);
		/* NOTREACHED */
	}

	free(svc_type);

	if (verbose)
		syslog(LOG_NOTICE, "reparsed_door_return <buf=%s> size=%d",
		    buf, bufsz);

	resp->res_status = 0;
	resp->res_len = bufsz;
	(void) door_return((char *)resp, bufsz + sizeof (reparsed_door_res_t),
	    NULL, 0);

	(void) door_return(NULL, 0, NULL, 0);
	/* NOTREACHED */
}

static int
start_reparsed_svcs()
{
	int doorfd;
	int dfd;

	if ((doorfd = door_create(reparsed_doorfunc, NULL,
	    DOOR_REFUSE_DESC|DOOR_NO_CANCEL)) == -1) {
		syslog(LOG_ERR, "Unable to create door");
		return (1);
	}

	/*
	 * Create a file system path for the door
	 */
	if ((dfd = open(REPARSED_DOOR, O_RDWR|O_CREAT|O_TRUNC,
	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) {
		syslog(LOG_ERR, "unable to open %s", REPARSED_DOOR);
		(void) close(doorfd);
		return (1);
	}

	/*
	 * Clean up any stale associations
	 */
	(void) fdetach(REPARSED_DOOR);

	/*
	 * Register in the kernel namespace for door_ki_open().
	 */
	if (fattach(doorfd, REPARSED_DOOR) == -1) {
		syslog(LOG_ERR, "Unable to fattach door %s", REPARSED_DOOR);
		(void) close(doorfd);
		(void) close(dfd);
		return (1);
	}
	(void) close(dfd);

	/*
	 * Wait for incoming calls
	 */
	/*CONSTCOND*/
	while (1)
		(void) pause();

	syslog(LOG_ERR, "Door server exited");
	return (10);
}