summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/smbclnt/smbiod-svc/smbiod-svc.c
blob: d99e04ba8d5c847ebe43f2bb12d6442225464f10 (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
 * 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 (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * SMBFS I/O Daemon (SMF service)
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/note.h>
#include <sys/queue.h>

#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <synch.h>
#include <time.h>
#include <unistd.h>
#include <ucred.h>
#include <wait.h>
#include <priv_utils.h>
#include <err.h>
#include <door.h>
#include <libscf.h>
#include <locale.h>
#include <thread.h>
#include <assert.h>

#include <netsmb/smb_lib.h>

static boolean_t d_flag = B_FALSE;

/* Keep a list of child processes. */
typedef struct _child {
	LIST_ENTRY(_child) list;
	pid_t pid;
	uid_t uid;
} child_t;
static LIST_HEAD(, _child) child_list = { 0 };
mutex_t	cl_mutex = DEFAULTMUTEX;

static const char smbiod_path[] = "/usr/lib/smbfs/smbiod";
static const char door_path[] = SMBIOD_SVC_DOOR;

void svc_dispatch(void *cookie, char *argp, size_t argsz,
    door_desc_t *dp, uint_t n_desc);
static int cmd_start(uid_t uid, gid_t gid);
static int new_child(uid_t uid, gid_t gid);
static void svc_sigchld(void);
static void child_gone(uid_t, pid_t, int);
static void svc_cleanup(void);

static child_t *
child_find_by_pid(pid_t pid)
{
	child_t *cp;

	assert(MUTEX_HELD(&cl_mutex));
	LIST_FOREACH(cp, &child_list, list) {
		if (cp->pid == pid)
			return (cp);
	}
	return (NULL);
}

static child_t *
child_find_by_uid(uid_t uid)
{
	child_t *cp;

	assert(MUTEX_HELD(&cl_mutex));
	LIST_FOREACH(cp, &child_list, list) {
		if (cp->uid == uid)
			return (cp);
	}
	return (NULL);
}

/*
 * Find out if the service is already running.
 * Return: true, false.
 */
static boolean_t
already_running(void)
{
	door_info_t info;
	int fd, rc;

	if ((fd = open(door_path, O_RDONLY)) < 0)
		return (B_FALSE);

	rc = door_info(fd, &info);
	close(fd);
	if (rc < 0)
		return (B_FALSE);

	return (B_TRUE);
}

/*
 * This function will fork off a child process,
 * from which only the child will return.
 *
 * The parent exit status is taken as the SMF start method
 * success or failure, so the parent waits (via pipe read)
 * for the child to finish initialization before it exits.
 * Use SMF error codes only on exit.
 */
static int
daemonize_init(void)
{
	int pid, st;
	int pfds[2];

	chdir("/");

	if (pipe(pfds) < 0) {
		perror("pipe");
		exit(SMF_EXIT_ERR_FATAL);
	}
	if ((pid = fork1()) == -1) {
		perror("fork");
		exit(SMF_EXIT_ERR_FATAL);
	}

	/*
	 * If we're the parent process, wait for either the child to send us
	 * the appropriate exit status over the pipe or for the read to fail
	 * (presumably with 0 for EOF if our child terminated abnormally).
	 * If the read fails, exit with either the child's exit status if it
	 * exited or with SMF_EXIT_ERR_FATAL if it died from a fatal signal.
	 */
	if (pid != 0) {
		/* parent */
		close(pfds[1]);
		if (read(pfds[0], &st, sizeof (st)) == sizeof (st))
			_exit(st);
		if (waitpid(pid, &st, 0) == pid && WIFEXITED(st))
			_exit(WEXITSTATUS(st));
		_exit(SMF_EXIT_ERR_FATAL);
	}

	/* child */
	close(pfds[0]);

	return (pfds[1]);
}

static void
daemonize_fini(int pfd, int rc)
{
	/* Tell parent we're ready. */
	(void) write(pfd, &rc, sizeof (rc));
	close(pfd);
}

int
main(int argc, char **argv)
{
	sigset_t oldmask, tmpmask;
	struct sigaction sa;
	struct rlimit rl;
	int door_fd = -1, tmp_fd = -1, pfd = -1;
	int c, sig;
	int rc = SMF_EXIT_ERR_FATAL;
	boolean_t created = B_FALSE, attached = B_FALSE;

	/* set locale and text domain for i18n */
	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	while ((c = getopt(argc, argv, "d")) != -1) {
		switch (c) {
		case 'd':
			/* Do debug messages. */
			d_flag = B_TRUE;
			break;
		default:
			fprintf(stderr, "Usage: %s [-d]\n", argv[0]);
			return (SMF_EXIT_ERR_CONFIG);
		}
	}

	if (already_running()) {
		fprintf(stderr, "%s: already running", argv[0]);
		return (rc);
	}

	/*
	 * Raise the fd limit to max
	 * errors here are non-fatal
	 */
	if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
		fprintf(stderr, "getrlimit failed, err %d\n", errno);
	} else if (rl.rlim_cur < rl.rlim_max) {
		rl.rlim_cur = rl.rlim_max;
		if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
			fprintf(stderr, "setrlimit "
			    "RLIMIT_NOFILE %d, err %d",
			    (int)rl.rlim_cur, errno);
	}

	/*
	 * Want all signals blocked, as we're doing
	 * synchronous delivery via sigwait below.
	 */
	sigfillset(&tmpmask);
	sigprocmask(SIG_BLOCK, &tmpmask, &oldmask);

	/*
	 * Do want SIGCHLD, and will waitpid().
	 */
	sa.sa_flags = SA_NOCLDSTOP;
	sa.sa_handler = SIG_DFL;
	sigemptyset(&sa.sa_mask);
	sigaction(SIGCHLD, &sa, NULL);

	/*
	 * Daemonize, unless debugging.
	 */
	if (d_flag) {
		/* debug: run in foregound (not a service) */
		putenv("SMBFS_DEBUG=1");
	} else {
		/* Non-debug: start daemon in the background. */
		pfd = daemonize_init();
	}

	/*
	 * Create directory for all smbiod doors.
	 */
	if ((mkdir(SMBIOD_RUNDIR, 0755) < 0) && errno != EEXIST) {
		perror(SMBIOD_RUNDIR);
		goto out;
	}

	/*
	 * Create a file for the main service door.
	 */
	unlink(door_path);
	tmp_fd = open(door_path, O_RDWR|O_CREAT|O_EXCL, 0644);
	if (tmp_fd < 0) {
		perror(door_path);
		goto out;
	}
	close(tmp_fd);
	tmp_fd = -1;
	created = B_TRUE;

	/* Setup the door service. */
	door_fd = door_create(svc_dispatch, NULL,
	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL);
	if (door_fd == -1) {
		perror("svc door_create");
		goto out;
	}
	fdetach(door_path);
	if (fattach(door_fd, door_path) < 0) {
		fprintf(stderr, "%s: fattach failed, %s\n",
		    door_path, strerror(errno));
		goto out;
	}
	attached = B_TRUE;

	/*
	 * Initializations done.  Tell start method we're up.
	 */
	rc = SMF_EXIT_OK;
	if (pfd != -1) {
		daemonize_fini(pfd, rc);
		pfd = -1;
	}

	/*
	 * Main thread just waits for signals.
	 */
again:
	sig = sigwait(&tmpmask);
	if (d_flag)
		fprintf(stderr, "main: sig=%d\n", sig);
	switch (sig) {
	case SIGINT:
	case SIGTERM:
		/*
		 * The whole process contract gets a SIGTERM
		 * at once.  Give children a chance to exit
		 * so we can do normal SIGCHLD cleanup.
		 * Prevent new door_open calls.
		 */
		fdetach(door_path);
		attached = B_FALSE;
		alarm(2);
		goto again;
	case SIGALRM:
		break;	/* normal termination */
	case SIGCHLD:
		svc_sigchld();
		goto again;
	case SIGCONT:
		goto again;
	default:
		/* Unexpected signal. */
		fprintf(stderr, "svc_main: unexpected sig=%d\n", sig);
		break;
	}

out:
	if (attached)
		fdetach(door_path);
	if (door_fd != -1)
		door_revoke(door_fd);
	if (created)
		unlink(door_path);

	/* NB: door threads gone now. */
	svc_cleanup();

	/* If startup error, report to parent. */
	if (pfd != -1)
		daemonize_fini(pfd, rc);

	return (rc);
}

/*ARGSUSED*/
void
svc_dispatch(void *cookie, char *argp, size_t argsz,
    door_desc_t *dp, uint_t n_desc)
{
	ucred_t *ucred = NULL;
	uid_t uid;
	gid_t gid;
	int32_t cmd, rc;

	/*
	 * Allow a NULL arg call to check if this
	 * daemon is running.  Just return zero.
	 */
	if (argp == NULL) {
		rc = 0;
		goto out;
	}

	/*
	 * Get the caller's credentials.
	 * (from client side of door)
	 */
	if (door_ucred(&ucred) != 0) {
		rc = EACCES;
		goto out;
	}
	uid = ucred_getruid(ucred);
	gid = ucred_getrgid(ucred);

	/*
	 * Arg is just an int command code.
	 * Reply is also an int.
	 */
	if (argsz != sizeof (cmd)) {
		rc = EINVAL;
		goto out;
	}
	bcopy(argp, &cmd, sizeof (cmd));
	switch (cmd) {
	case SMBIOD_START:
		rc = cmd_start(uid, gid);
		break;
	default:
		rc = EINVAL;
		goto out;
	}

out:
	if (ucred != NULL)
		ucred_free(ucred);

	door_return((void *)&rc, sizeof (rc), NULL, 0);
}

/*
 * Start a per-user smbiod, if not already running.
 */
int
cmd_start(uid_t uid, gid_t gid)
{
	char door_file[64];
	child_t *cp;
	int pid, fd = -1;

	mutex_lock(&cl_mutex);
	cp = child_find_by_uid(uid);
	if (cp != NULL) {
		/* This UID already has an IOD. */
		mutex_unlock(&cl_mutex);
		if (d_flag) {
			fprintf(stderr, "cmd_start: uid %d"
			    " already has an iod\n", uid);
		}
		return (0);
	}

	/*
	 * OK, create a new child.
	 */
	cp = malloc(sizeof (*cp));
	if (cp == NULL) {
		mutex_unlock(&cl_mutex);
		return (ENOMEM);
	}
	cp->pid = 0; /* update below */
	cp->uid = uid;
	LIST_INSERT_HEAD(&child_list, cp, list);
	mutex_unlock(&cl_mutex);

	/*
	 * The child will not have permission to create or
	 * destroy files in SMBIOD_RUNDIR so do that here.
	 */
	snprintf(door_file, sizeof (door_file),
	    SMBIOD_USR_DOOR, cp->uid);
	unlink(door_file);
	fd = open(door_file, O_RDWR|O_CREAT|O_EXCL, 0600);
	if (fd < 0) {
		perror(door_file);
		goto errout;
	}
	if (fchown(fd, uid, gid) < 0) {
		perror(door_file);
		goto errout;
	}
	close(fd);
	fd = -1;

	if ((pid = fork1()) == -1) {
		perror("fork");
		goto errout;
	}
	if (pid == 0) {
		(void) new_child(uid, gid);
		_exit(1);
	}
	/* parent */
	cp->pid = pid;

	if (d_flag) {
		fprintf(stderr, "cmd_start: uid %d new iod, pid %d\n",
		    uid, pid);
	}

	return (0);

errout:
	if (fd != -1)
		close(fd);
	mutex_lock(&cl_mutex);
	LIST_REMOVE(cp, list);
	mutex_unlock(&cl_mutex);
	free(cp);
	return (errno);
}

/*
 * Assume the passed credentials (from the door client),
 * drop any extra privileges, and exec the per-user iod.
 */
static int
new_child(uid_t uid, gid_t gid)
{
	char *argv[2];
	int flags, rc;

	flags = PU_RESETGROUPS | PU_LIMITPRIVS | PU_INHERITPRIVS;
	rc = __init_daemon_priv(flags, uid, gid, PRIV_NET_ACCESS, NULL);
	if (rc != 0)
		return (errno);

	argv[0] = "smbiod";
	argv[1] = NULL;
	(void) execv(smbiod_path, argv);
	return (errno);
}

static void
svc_sigchld(void)
{
	child_t *cp;
	pid_t pid;
	int err, status, found = 0;

	mutex_lock(&cl_mutex);

	while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {

		found++;
		if (d_flag)
			fprintf(stderr, "svc_sigchld: pid %d\n", (int)pid);

		cp = child_find_by_pid(pid);
		if (cp == NULL) {
			fprintf(stderr, "Unknown pid %d\n", (int)pid);
			continue;
		}
		child_gone(cp->uid, cp->pid, status);
		LIST_REMOVE(cp, list);
		free(cp);
	}
	err = errno;

	mutex_unlock(&cl_mutex);

	/* ECHILD is the normal end of loop. */
	if (pid < 0 && err != ECHILD)
		fprintf(stderr, "svc_sigchld: waitpid err %d\n", err);
	if (found == 0)
		fprintf(stderr, "svc_sigchld: no children?\n");
}

static void
child_gone(uid_t uid, pid_t pid, int status)
{
	char door_file[64];
	int x;

	if (d_flag)
		fprintf(stderr, "child_gone: uid %d pid %d\n",
		    uid, (int)pid);

	snprintf(door_file, sizeof (door_file),
	    SMBIOD_RUNDIR "/%d", uid);
	unlink(door_file);

	if (WIFEXITED(status)) {
		x = WEXITSTATUS(status);
		if (x != 0) {
			fprintf(stderr,
			    "uid %d, pid %d exit %d",
			    uid, (int)pid, x);
		}
	}
	if (WIFSIGNALED(status)) {
		x = WTERMSIG(status);
		fprintf(stderr,
		    "uid %d, pid %d signal %d",
		    uid, (int)pid, x);
	}
}

/*
 * Final cleanup before exit.  Unlink child doors, etc.
 * Called while single threaded, so no locks needed here.
 * The list is normally empty by now due to svc_sigchld
 * calls during shutdown.  But in case there were any
 * straglers, do cleanup here.  Don't bother freeing any
 * list elements here, as we're exiting.
 */
static void
svc_cleanup(void)
{
	child_t *cp;

	LIST_FOREACH(cp, &child_list, list) {
		child_gone(cp->uid, cp->pid, 0);
	}
}