summaryrefslogtreecommitdiff
path: root/usr/src/cmd/dlmgmtd/dlmgmt_main.c
blob: 658078e6240313e4a9c1685fede7474f795b3ba5 (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
/*
 * 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 2010 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * The dlmgmtd daemon is started by the datalink-management SMF service.
 * This daemon is used to manage <link name, linkid> mapping and the
 * persistent datalink configuration.
 *
 * Today, the <link name, linkid> mapping and the persistent configuration
 * of datalinks is kept in /etc/dladm/datalink.conf, and the daemon keeps
 * a copy of the datalinks in the memory (see dlmgmt_id_avl and
 * dlmgmt_name_avl). The active <link name, linkid> mapping is kept in
 * /etc/svc/volatile/dladm cache file, so that the mapping can be recovered
 * when dlmgmtd exits for some reason (e.g., when dlmgmtd is accidentally
 * killed).
 */

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <priv.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <syslog.h>
#include <zone.h>
#include <sys/dld.h>
#include <sys/dld_ioc.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libdladm_impl.h>
#include <libdlmgmt.h>
#include "dlmgmt_impl.h"

const char		*progname;
boolean_t		debug;
static int		pfds[2];
/*
 * This file descriptor to DLMGMT_DOOR cannot be in the libdladm
 * handle because the door isn't created when the handle is created.
 */
static int		dlmgmt_door_fd = -1;

/*
 * This libdladm handle is global so that dlmgmt_upcall_linkprop_init() can
 * pass to libdladm.  The handle is opened with "ALL" privileges, before
 * privileges are dropped in dlmgmt_drop_privileges().  It is not able to open
 * DLMGMT_DOOR at that time as it hasn't been created yet.  This door in the
 * handle is opened in the first call to dladm_door_fd().
 */
dladm_handle_t		dld_handle = NULL;

static void		dlmgmtd_exit(int);
static int		dlmgmt_init();
static void		dlmgmt_fini();
static int		dlmgmt_set_privileges();

static int
dlmgmt_set_doorfd(boolean_t start)
{
	dld_ioc_door_t did;
	int err = 0;

	assert(dld_handle != NULL);

	did.did_start_door = start;

	if (ioctl(dladm_dld_fd(dld_handle), DLDIOC_DOORSERVER, &did) == -1)
		err = errno;

	return (err);
}

static int
dlmgmt_door_init(void)
{
	int err = 0;

	if ((dlmgmt_door_fd = door_create(dlmgmt_handler, NULL,
	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
		err = errno;
		dlmgmt_log(LOG_ERR, "door_create() failed: %s",
		    strerror(err));
		return (err);
	}
	return (err);
}

static void
dlmgmt_door_fini(void)
{
	if (dlmgmt_door_fd == -1)
		return;

	if (door_revoke(dlmgmt_door_fd) == -1) {
		dlmgmt_log(LOG_WARNING, "door_revoke(%s) failed: %s",
		    DLMGMT_DOOR, strerror(errno));
	}
	(void) dlmgmt_set_doorfd(B_FALSE);
	dlmgmt_door_fd = -1;
}

int
dlmgmt_door_attach(zoneid_t zoneid, char *rootdir)
{
	int	fd;
	int	err = 0;
	char	doorpath[MAXPATHLEN];

	(void) snprintf(doorpath, sizeof (doorpath), "%s%s", rootdir,
	    DLMGMT_DOOR);

	/*
	 * Create the door file for dlmgmtd.
	 */
	if ((fd = open(doorpath, O_CREAT|O_RDONLY, 0644)) == -1) {
		err = errno;
		dlmgmt_log(LOG_ERR, "open(%s) failed: %s", doorpath,
		    strerror(err));
		return (err);
	}
	(void) close(fd);
	if (chown(doorpath, UID_DLADM, GID_NETADM) == -1)
		return (errno);

	/*
	 * fdetach first in case a previous daemon instance exited
	 * ungracefully.
	 */
	(void) fdetach(doorpath);
	if (fattach(dlmgmt_door_fd, doorpath) != 0) {
		err = errno;
		dlmgmt_log(LOG_ERR, "fattach(%s) failed: %s", doorpath,
		    strerror(err));
	} else if (zoneid == GLOBAL_ZONEID) {
		if ((err = dlmgmt_set_doorfd(B_TRUE)) != 0) {
			dlmgmt_log(LOG_ERR, "cannot set kernel doorfd: %s",
			    strerror(err));
		}
	}

	return (err);
}

/*
 * Create the /etc/svc/volatile/dladm/ directory if it doesn't exist, load the
 * datalink.conf data for this zone, and create/attach the door rendezvous
 * file.
 */
int
dlmgmt_zone_init(zoneid_t zoneid)
{
	char	rootdir[MAXPATHLEN], tmpfsdir[MAXPATHLEN];
	int	err;
	struct stat statbuf;

	if (zoneid == GLOBAL_ZONEID) {
		rootdir[0] = '\0';
	} else if (zone_getattr(zoneid, ZONE_ATTR_ROOT, rootdir,
	    sizeof (rootdir)) < 0) {
		return (errno);
	}

	/*
	 * Create the DLMGMT_TMPFS_DIR directory.
	 */
	(void) snprintf(tmpfsdir, sizeof (tmpfsdir), "%s%s", rootdir,
	    DLMGMT_TMPFS_DIR);
	if (stat(tmpfsdir, &statbuf) < 0) {
		if (mkdir(tmpfsdir, (mode_t)0755) < 0)
			return (errno);
	} else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
		return (ENOTDIR);
	}

	if ((chmod(tmpfsdir, 0755) < 0) ||
	    (chown(tmpfsdir, UID_DLADM, GID_NETADM) < 0)) {
		return (EPERM);
	}

	if ((err = dlmgmt_db_init(zoneid)) != 0)
		return (err);
	return (dlmgmt_door_attach(zoneid, rootdir));
}

/*
 * Initialize each running zone.
 */
static int
dlmgmt_allzones_init(void)
{
	int		err, i;
	zoneid_t	*zids = NULL;
	uint_t		nzids, nzids_saved;

	if (zone_list(NULL, &nzids) != 0)
		return (errno);
again:
	nzids *= 2;
	if ((zids = malloc(nzids * sizeof (zoneid_t))) == NULL)
		return (errno);
	nzids_saved = nzids;
	if (zone_list(zids, &nzids) != 0) {
		free(zids);
		return (errno);
	}
	if (nzids > nzids_saved) {
		free(zids);
		goto again;
	}

	for (i = 0; i < nzids; i++) {
		if ((err = dlmgmt_zone_init(zids[i])) != 0)
			break;
	}
	free(zids);
	return (err);
}

static int
dlmgmt_init(void)
{
	int	err;
	char	*fmri, *c;
	char	filename[MAXPATHLEN];

	if (dladm_open(&dld_handle) != DLADM_STATUS_OK) {
		dlmgmt_log(LOG_ERR, "dladm_open() failed");
		return (EPERM);
	}

	if (signal(SIGTERM, dlmgmtd_exit) == SIG_ERR ||
	    signal(SIGINT, dlmgmtd_exit) == SIG_ERR) {
		err = errno;
		dlmgmt_log(LOG_ERR, "signal() for SIGTERM/INT failed: %s",
		    strerror(err));
		return (err);
	}

	/*
	 * First derive the name of the cache file from the FMRI name. This
	 * cache name is used to keep active datalink configuration.
	 */
	if (debug) {
		(void) snprintf(cachefile, MAXPATHLEN, "%s/%s%s",
		    DLMGMT_TMPFS_DIR, progname, ".debug.cache");
	} else {
		if ((fmri = getenv("SMF_FMRI")) == NULL) {
			dlmgmt_log(LOG_ERR, "dlmgmtd is an smf(7) managed "
			    "service and should not be run from the command "
			    "line.");
			return (EINVAL);
		}

		/*
		 * The FMRI name is in the form of
		 * svc:/service/service:instance.  We need to remove the
		 * prefix "svc:/" and replace '/' with '-'.  The cache file
		 * name is in the form of "service:instance.cache".
		 */
		if ((c = strchr(fmri, '/')) != NULL)
			c++;
		else
			c = fmri;
		(void) snprintf(filename, MAXPATHLEN, "%s.cache", c);
		c = filename;
		while ((c = strchr(c, '/')) != NULL)
			*c = '-';

		(void) snprintf(cachefile, MAXPATHLEN, "%s/%s",
		    DLMGMT_TMPFS_DIR, filename);
	}

	dlmgmt_linktable_init();
	if ((err = dlmgmt_door_init()) != 0)
		goto done;

	/*
	 * Load datalink configuration and create dlmgmtd door files for all
	 * currently running zones.
	 */
	if ((err = dlmgmt_allzones_init()) != 0)
		dlmgmt_door_fini();

done:
	if (err != 0)
		dlmgmt_linktable_fini();
	return (err);
}

static void
dlmgmt_fini(void)
{
	dlmgmt_door_fini();
	dlmgmt_linktable_fini();
	if (dld_handle != NULL) {
		dladm_close(dld_handle);
		dld_handle = NULL;
	}
}

/*
 * This is called by the child process to inform the parent process to
 * exit with the given return value.
 */
static void
dlmgmt_inform_parent_exit(int rv)
{
	if (debug)
		return;

	if (write(pfds[1], &rv, sizeof (int)) != sizeof (int)) {
		dlmgmt_log(LOG_WARNING,
		    "dlmgmt_inform_parent_exit() failed: %s", strerror(errno));
		(void) close(pfds[1]);
		exit(EXIT_FAILURE);
	}
	(void) close(pfds[1]);
}

/*ARGSUSED*/
static void
dlmgmtd_exit(int signo)
{
	(void) close(pfds[1]);
	dlmgmt_fini();
	exit(EXIT_FAILURE);
}

static void
usage(void)
{
	(void) fprintf(stderr, "Usage: %s [-d]\n", progname);
	exit(EXIT_FAILURE);
}

/*
 * Restrict privileges to only those needed.
 */
int
dlmgmt_drop_privileges(void)
{
	priv_set_t	*pset;
	priv_ptype_t	ptype;
	zoneid_t	zoneid = getzoneid();
	int		err = 0;

	if ((pset = priv_allocset()) == NULL)
		return (errno);

	/*
	 * The global zone needs PRIV_PROC_FORK so that it can fork() when it
	 * issues db ops in non-global zones, PRIV_SYS_CONFIG to post
	 * sysevents, and PRIV_SYS_DL_CONFIG to initialize link properties in
	 * dlmgmt_upcall_linkprop_init().
	 *
	 * We remove non-basic privileges from the permitted (and thus
	 * effective) set.  When executing in a non-global zone, dlmgmtd
	 * only needs to read and write to files that it already owns.
	 */
	priv_basicset(pset);
	(void) priv_delset(pset, PRIV_PROC_EXEC);
	(void) priv_delset(pset, PRIV_PROC_INFO);
	(void) priv_delset(pset, PRIV_PROC_SESSION);
	(void) priv_delset(pset, PRIV_FILE_LINK_ANY);
	if (zoneid == GLOBAL_ZONEID) {
		ptype = PRIV_EFFECTIVE;
		if (priv_addset(pset, PRIV_SYS_CONFIG) == -1 ||
		    priv_addset(pset, PRIV_SYS_DL_CONFIG) == -1)
			err = errno;
	} else {
		(void) priv_delset(pset, PRIV_PROC_FORK);
		ptype = PRIV_PERMITTED;
	}
	if (err == 0 && setppriv(PRIV_SET, ptype, pset) == -1)
		err = errno;
done:
	priv_freeset(pset);
	return (err);
}

int
dlmgmt_elevate_privileges(void)
{
	priv_set_t	*privset;
	int		err = 0;

	if ((privset = priv_str_to_set("zone", ",", NULL)) == NULL)
		return (errno);
	if (setppriv(PRIV_SET, PRIV_EFFECTIVE, privset) == -1)
		err = errno;
	priv_freeset(privset);
	return (err);
}

/*
 * Set the uid of this daemon to the "dladm" user and drop privileges to only
 * those needed.
 */
static int
dlmgmt_set_privileges(void)
{
	int err;

	(void) setgroups(0, NULL);
	if (setegid(GID_NETADM) == -1 || seteuid(UID_DLADM) == -1)
		err = errno;
	else
		err = dlmgmt_drop_privileges();
done:
	return (err);
}

/*
 * Keep the pfds fd open, close other fds.
 */
/*ARGSUSED*/
static int
closefunc(void *arg, int fd)
{
	if (fd != pfds[1])
		(void) close(fd);
	return (0);
}

static boolean_t
dlmgmt_daemonize(void)
{
	pid_t pid;
	int rv;

	if (pipe(pfds) < 0) {
		(void) fprintf(stderr, "%s: pipe() failed: %s\n",
		    progname, strerror(errno));
		exit(EXIT_FAILURE);
	}

	if ((pid = fork()) == -1) {
		(void) fprintf(stderr, "%s: fork() failed: %s\n",
		    progname, strerror(errno));
		exit(EXIT_FAILURE);
	} else if (pid > 0) { /* Parent */
		(void) close(pfds[1]);

		/*
		 * Read the child process's return value from the pfds.
		 * If the child process exits unexpected, read() returns -1.
		 */
		if (read(pfds[0], &rv, sizeof (int)) != sizeof (int)) {
			(void) kill(pid, SIGKILL);
			rv = EXIT_FAILURE;
		}

		(void) close(pfds[0]);
		exit(rv);
	}

	/* Child */
	(void) close(pfds[0]);
	(void) setsid();

	/*
	 * Close all files except pfds[1].
	 */
	(void) fdwalk(closefunc, NULL);
	(void) chdir("/");
	openlog(progname, LOG_PID, LOG_DAEMON);
	return (B_TRUE);
}

int
main(int argc, char *argv[])
{
	int opt, err;

	progname = strrchr(argv[0], '/');
	if (progname != NULL)
		progname++;
	else
		progname = argv[0];

	/*
	 * Process options.
	 */
	while ((opt = getopt(argc, argv, "d")) != EOF) {
		switch (opt) {
		case 'd':
			debug = B_TRUE;
			break;
		default:
			usage();
		}
	}

	if (!debug && !dlmgmt_daemonize())
		return (EXIT_FAILURE);

	if ((err = dlmgmt_init()) != 0) {
		dlmgmt_log(LOG_ERR, "unable to initialize daemon: %s",
		    strerror(err));
		goto child_out;
	} else if ((err = dlmgmt_set_privileges()) != 0) {
		dlmgmt_log(LOG_ERR, "unable to set daemon privileges: %s",
		    strerror(err));
		dlmgmt_fini();
		goto child_out;
	}

	/*
	 * Inform the parent process that it can successfully exit.
	 */
	dlmgmt_inform_parent_exit(EXIT_SUCCESS);

	for (;;)
		(void) pause();

child_out:
	/* return from main() forcibly exits an MT process */
	dlmgmt_inform_parent_exit(EXIT_FAILURE);
	return (EXIT_FAILURE);
}