1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright 2016 Joyent, Inc.
26 */
27
28 /*
29 * The dlmgmtd daemon is started by the datalink-management SMF service.
30 * This daemon is used to manage <link name, linkid> mapping and the
31 * persistent datalink configuration.
32 *
33 * Today, the <link name, linkid> mapping and the persistent configuration
34 * of datalinks is kept in /etc/dladm/datalink.conf, and the daemon keeps
35 * a copy of the datalinks in the memory (see dlmgmt_id_avl and
36 * dlmgmt_name_avl). The active <link name, linkid> mapping is kept in
37 * /etc/svc/volatile/dladm cache file, so that the mapping can be recovered
38 * when dlmgmtd exits for some reason (e.g., when dlmgmtd is accidentally
39 * killed).
40 */
41
42 #include <assert.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <priv.h>
46 #include <signal.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <strings.h>
50 #include <syslog.h>
51 #include <zone.h>
52 #include <sys/dld.h>
53 #include <sys/dld_ioc.h>
54 #include <sys/param.h>
55 #include <sys/stat.h>
56 #include <unistd.h>
57 #include <libdladm_impl.h>
58 #include <libdlmgmt.h>
59 #include "dlmgmt_impl.h"
60
61 const char *progname;
62 boolean_t debug;
63 static int pfds[2];
64 /*
65 * This file descriptor to DLMGMT_DOOR cannot be in the libdladm
66 * handle because the door isn't created when the handle is created.
67 */
68 static int dlmgmt_door_fd = -1;
69
70 /*
71 * This libdladm handle is global so that dlmgmt_upcall_linkprop_init() can
72 * pass to libdladm. The handle is opened with "ALL" privileges, before
73 * privileges are dropped in dlmgmt_drop_privileges(). It is not able to open
74 * DLMGMT_DOOR at that time as it hasn't been created yet. This door in the
75 * handle is opened in the first call to dladm_door_fd().
76 */
77 dladm_handle_t dld_handle = NULL;
78
79 static void dlmgmtd_exit(int);
80 static int dlmgmt_init();
81 static void dlmgmt_fini();
82 static int dlmgmt_set_privileges();
83
84 static int
85 dlmgmt_set_doorfd(boolean_t start)
86 {
87 dld_ioc_door_t did;
88 int err = 0;
89
90 assert(dld_handle != NULL);
91
92 did.did_start_door = start;
93
94 if (ioctl(dladm_dld_fd(dld_handle), DLDIOC_DOORSERVER, &did) == -1)
95 err = errno;
96
97 return (err);
98 }
99
100 static int
101 dlmgmt_door_init(void)
102 {
103 int err = 0;
104
105 if ((dlmgmt_door_fd = door_create(dlmgmt_handler, NULL,
106 DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
107 err = errno;
108 dlmgmt_log(LOG_ERR, "door_create() failed: %s",
109 strerror(err));
110 return (err);
111 }
112 return (err);
113 }
114
115 static void
116 dlmgmt_door_fini(void)
117 {
118 if (dlmgmt_door_fd == -1)
119 return;
120
121 if (door_revoke(dlmgmt_door_fd) == -1) {
122 dlmgmt_log(LOG_WARNING, "door_revoke(%s) failed: %s",
123 DLMGMT_DOOR, strerror(errno));
124 }
125 (void) dlmgmt_set_doorfd(B_FALSE);
126 dlmgmt_door_fd = -1;
127 }
128
129 static int
130 dlmgmt_door_attach(zoneid_t zoneid, char *rootdir)
131 {
132 int fd;
133 int err = 0;
134 char doorpath[MAXPATHLEN];
135 struct stat statbuf;
136
137 /* Handle running in a non-native branded zone (i.e. has /native) */
138 (void) snprintf(doorpath, sizeof (doorpath), "%s/native%s",
139 rootdir, DLMGMT_TMPFS_DIR);
140 if (stat(doorpath, &statbuf) == 0) {
141 (void) snprintf(doorpath, sizeof (doorpath), "%s/native%s",
142 rootdir, DLMGMT_DOOR);
143 } else {
144 (void) snprintf(doorpath, sizeof (doorpath), "%s%s",
145 rootdir, DLMGMT_DOOR);
146 }
147
148 /*
149 * Create the door file for dlmgmtd.
150 */
151 if ((fd = open(doorpath, O_CREAT|O_RDONLY, 0644)) == -1) {
152 err = errno;
153 dlmgmt_log(LOG_ERR, "open(%s) failed: %s", doorpath,
154 strerror(err));
155 return (err);
156 }
157 (void) close(fd);
158 if (chown(doorpath, UID_DLADM, GID_NETADM) == -1)
159 return (errno);
160
161 /*
162 * fdetach first in case a previous daemon instance exited
163 * ungracefully.
164 */
165 (void) fdetach(doorpath);
166 if (fattach(dlmgmt_door_fd, doorpath) != 0) {
167 err = errno;
168 dlmgmt_log(LOG_ERR, "fattach(%s) failed: %s", doorpath,
169 strerror(err));
170 } else if (zoneid == GLOBAL_ZONEID) {
171 if ((err = dlmgmt_set_doorfd(B_TRUE)) != 0) {
172 dlmgmt_log(LOG_ERR, "cannot set kernel doorfd: %s",
173 strerror(err));
174 }
175 }
176
177 return (err);
178 }
179
180 /*
181 * Create the /etc/svc/volatile/dladm/ directory if it doesn't exist, load the
182 * datalink.conf data for this zone, and create/attach the door rendezvous
183 * file.
184 */
185 int
186 dlmgmt_zone_init(zoneid_t zoneid)
187 {
188 char rootdir[MAXPATHLEN], tmpfsdir[MAXPATHLEN];
189 int err;
190 struct stat statbuf;
191
192 if (zoneid == GLOBAL_ZONEID) {
193 rootdir[0] = '\0';
194 } else if (zone_getattr(zoneid, ZONE_ATTR_ROOT, rootdir,
195 sizeof (rootdir)) < 0) {
196 return (errno);
197 }
198
199 /*
200 * Create the DLMGMT_TMPFS_DIR directory.
201 */
202 (void) snprintf(tmpfsdir, sizeof (tmpfsdir), "%s%s", rootdir,
203 DLMGMT_TMPFS_DIR);
204 if (stat(tmpfsdir, &statbuf) < 0) {
205 if (mkdir(tmpfsdir, (mode_t)0755) < 0) {
206 /*
207 * Handle running in a non-native branded zone
208 * (i.e. has /native)
209 */
210 (void) snprintf(tmpfsdir, sizeof (tmpfsdir),
211 "%s/native%s", rootdir, DLMGMT_TMPFS_DIR);
212 if (mkdir(tmpfsdir, (mode_t)0755) < 0)
213 return (errno);
214 }
215 } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
216 return (ENOTDIR);
217 }
218
219 if ((chmod(tmpfsdir, 0755) < 0) ||
220 (chown(tmpfsdir, UID_DLADM, GID_NETADM) < 0)) {
221 return (EPERM);
222 }
223
224 if ((err = dlmgmt_db_init(zoneid, rootdir)) != 0)
225 return (err);
226 return (dlmgmt_door_attach(zoneid, rootdir));
227 }
228
229 /*
230 * Initialize each running zone.
231 */
232 static int
233 dlmgmt_allzones_init(void)
234 {
235 int i;
236 zoneid_t *zids = NULL;
237 uint_t nzids, nzids_saved;
238
239 if (zone_list(NULL, &nzids) != 0)
240 return (errno);
241 again:
242 nzids *= 2;
243 if ((zids = malloc(nzids * sizeof (zoneid_t))) == NULL)
244 return (errno);
245 nzids_saved = nzids;
246 if (zone_list(zids, &nzids) != 0) {
247 free(zids);
248 return (errno);
249 }
250 if (nzids > nzids_saved) {
251 free(zids);
252 goto again;
253 }
254
255 for (i = 0; i < nzids; i++) {
256 int res;
257 zone_status_t status;
258
259 /*
260 * Skip over zones that have gone away or are going down
261 * since we got the list. Process all zones in the list,
262 * logging errors for any that failed.
263 */
264 if (zone_getattr(zids[i], ZONE_ATTR_STATUS, &status,
265 sizeof (status)) < 0)
266 continue;
267 switch (status) {
268 case ZONE_IS_SHUTTING_DOWN:
269 case ZONE_IS_EMPTY:
270 case ZONE_IS_DOWN:
271 case ZONE_IS_DYING:
272 case ZONE_IS_DEAD:
273 /* FALLTHRU */
274 continue;
275 default:
276 break;
277 }
278 if ((res = dlmgmt_zone_init(zids[i])) != 0) {
279 (void) fprintf(stderr, "zone (%ld) init error %s",
280 zids[i], strerror(res));
281 dlmgmt_log(LOG_ERR, "zone (%d) init error %s",
282 zids[i], strerror(res));
283 }
284 }
285 free(zids);
286 return (0);
287 }
288
289 static int
290 dlmgmt_init(void)
291 {
292 int err;
293 char *fmri, *c;
294 char filename[MAXPATHLEN];
295
296 if (dladm_open(&dld_handle) != DLADM_STATUS_OK) {
297 dlmgmt_log(LOG_ERR, "dladm_open() failed");
298 return (EPERM);
299 }
300
301 if (signal(SIGTERM, dlmgmtd_exit) == SIG_ERR ||
302 signal(SIGINT, dlmgmtd_exit) == SIG_ERR) {
303 err = errno;
304 dlmgmt_log(LOG_ERR, "signal() for SIGTERM/INT failed: %s",
305 strerror(err));
306 return (err);
307 }
308
309 /*
310 * First derive the name of the cache file from the FMRI name. This
311 * cache name is used to keep active datalink configuration.
312 */
313 if (debug) {
314 (void) snprintf(cachefile, MAXPATHLEN, "%s/%s%s",
315 DLMGMT_TMPFS_DIR, progname, ".debug.cache");
316 } else {
317 if ((fmri = getenv("SMF_FMRI")) == NULL) {
318 dlmgmt_log(LOG_ERR, "dlmgmtd is an smf(7) managed "
319 "service and should not be run from the command "
320 "line.");
321 return (EINVAL);
322 }
323
324 /*
325 * The FMRI name is in the form of
326 * svc:/service/service:instance. We need to remove the
327 * prefix "svc:/" and replace '/' with '-'. The cache file
328 * name is in the form of "service:instance.cache".
329 */
330 if ((c = strchr(fmri, '/')) != NULL)
331 c++;
332 else
333 c = fmri;
334 (void) snprintf(filename, MAXPATHLEN, "%s.cache", c);
335 c = filename;
336 while ((c = strchr(c, '/')) != NULL)
337 *c = '-';
338
339 (void) snprintf(cachefile, MAXPATHLEN, "%s/%s",
340 DLMGMT_TMPFS_DIR, filename);
341 }
342
343 dlmgmt_linktable_init();
344 if ((err = dlmgmt_door_init()) != 0)
345 goto done;
346
347 /*
348 * Load datalink configuration and create dlmgmtd door files for all
349 * currently running zones.
350 */
351 if ((err = dlmgmt_allzones_init()) != 0)
352 dlmgmt_door_fini();
353
354 done:
355 if (err != 0)
356 dlmgmt_linktable_fini();
357 return (err);
358 }
359
360 static void
361 dlmgmt_fini(void)
362 {
363 dlmgmt_door_fini();
364 dlmgmt_linktable_fini();
365 if (dld_handle != NULL) {
366 dladm_close(dld_handle);
367 dld_handle = NULL;
368 }
369 }
370
371 /*
372 * This is called by the child process to inform the parent process to
373 * exit with the given return value.
374 */
375 static void
376 dlmgmt_inform_parent_exit(int rv)
377 {
378 if (debug)
379 return;
380
381 if (write(pfds[1], &rv, sizeof (int)) != sizeof (int)) {
382 dlmgmt_log(LOG_WARNING,
383 "dlmgmt_inform_parent_exit() failed: %s", strerror(errno));
384 (void) close(pfds[1]);
385 exit(EXIT_FAILURE);
386 }
387 (void) close(pfds[1]);
388 }
389
390 /*ARGSUSED*/
391 static void
392 dlmgmtd_exit(int signo)
393 {
394 (void) close(pfds[1]);
395 dlmgmt_fini();
396 exit(EXIT_FAILURE);
397 }
398
399 static void
400 usage(void)
401 {
402 (void) fprintf(stderr, "Usage: %s [-d]\n", progname);
403 exit(EXIT_FAILURE);
404 }
405
406 /*
407 * Restrict privileges to only those needed.
408 */
409 int
410 dlmgmt_drop_privileges(void)
411 {
412 priv_set_t *pset;
413 priv_ptype_t ptype;
414 zoneid_t zoneid = getzoneid();
415 int err = 0;
416
417 if ((pset = priv_allocset()) == NULL)
418 return (errno);
419
420 /*
421 * The global zone needs PRIV_PROC_FORK so that it can fork() when it
422 * issues db ops in non-global zones, PRIV_SYS_CONFIG to post
423 * sysevents, and PRIV_SYS_DL_CONFIG to initialize link properties in
424 * dlmgmt_upcall_linkprop_init().
425 *
426 * We remove non-basic privileges from the permitted (and thus
427 * effective) set. When executing in a non-global zone, dlmgmtd
428 * only needs to read and write to files that it already owns.
429 */
430 priv_basicset(pset);
431 (void) priv_delset(pset, PRIV_PROC_EXEC);
432 (void) priv_delset(pset, PRIV_PROC_INFO);
433 (void) priv_delset(pset, PRIV_PROC_SESSION);
434 (void) priv_delset(pset, PRIV_FILE_LINK_ANY);
435 if (zoneid == GLOBAL_ZONEID) {
436 ptype = PRIV_EFFECTIVE;
437 if (priv_addset(pset, PRIV_SYS_CONFIG) == -1 ||
438 priv_addset(pset, PRIV_SYS_DL_CONFIG) == -1)
439 err = errno;
440 } else {
441 (void) priv_delset(pset, PRIV_PROC_FORK);
442 ptype = PRIV_PERMITTED;
443 }
444 if (err == 0 && setppriv(PRIV_SET, ptype, pset) == -1)
445 err = errno;
446 done:
447 priv_freeset(pset);
448 return (err);
449 }
450
451 int
452 dlmgmt_elevate_privileges(void)
453 {
454 priv_set_t *privset;
455 int err = 0;
456
457 if ((privset = priv_str_to_set("zone", ",", NULL)) == NULL)
458 return (errno);
459 if (setppriv(PRIV_SET, PRIV_EFFECTIVE, privset) == -1)
460 err = errno;
461 priv_freeset(privset);
462 return (err);
463 }
464
465 /*
466 * Set the uid of this daemon to the "dladm" user and drop privileges to only
467 * those needed.
468 */
469 static int
470 dlmgmt_set_privileges(void)
471 {
472 int err;
473
474 (void) setgroups(0, NULL);
475 if (setegid(GID_NETADM) == -1 || seteuid(UID_DLADM) == -1)
476 err = errno;
477 else
478 err = dlmgmt_drop_privileges();
479 done:
480 return (err);
481 }
482
483 /*
484 * Keep the pfds fd open, close other fds.
485 */
486 /*ARGSUSED*/
487 static int
488 closefunc(void *arg, int fd)
489 {
490 if (fd != pfds[1])
491 (void) close(fd);
492 return (0);
493 }
494
495 static boolean_t
496 dlmgmt_daemonize(void)
497 {
498 pid_t pid;
499 int rv;
500
501 if (pipe(pfds) < 0) {
502 (void) fprintf(stderr, "%s: pipe() failed: %s\n",
503 progname, strerror(errno));
504 exit(EXIT_FAILURE);
505 }
506
507 if ((pid = fork()) == -1) {
508 (void) fprintf(stderr, "%s: fork() failed: %s\n",
509 progname, strerror(errno));
510 exit(EXIT_FAILURE);
511 } else if (pid > 0) { /* Parent */
512 (void) close(pfds[1]);
513
514 /*
515 * Read the child process's return value from the pfds.
516 * If the child process exits unexpected, read() returns -1.
517 */
518 if (read(pfds[0], &rv, sizeof (int)) != sizeof (int)) {
519 (void) kill(pid, SIGKILL);
520 rv = EXIT_FAILURE;
521 }
522
523 (void) close(pfds[0]);
524 exit(rv);
525 }
526
527 /* Child */
528 (void) close(pfds[0]);
529 (void) setsid();
530
531 /*
532 * Close all files except pfds[1].
533 */
534 (void) fdwalk(closefunc, NULL);
535 (void) chdir("/");
536 openlog(progname, LOG_PID, LOG_DAEMON);
537 return (B_TRUE);
538 }
539
540 int
541 main(int argc, char *argv[])
542 {
543 int opt, err;
544
545 progname = strrchr(argv[0], '/');
546 if (progname != NULL)
547 progname++;
548 else
549 progname = argv[0];
550
551 /*
552 * Process options.
553 */
554 while ((opt = getopt(argc, argv, "d")) != EOF) {
555 switch (opt) {
556 case 'd':
557 debug = B_TRUE;
558 break;
559 default:
560 usage();
561 }
562 }
563
564 if (!debug && !dlmgmt_daemonize())
565 return (EXIT_FAILURE);
566
567 if ((err = dlmgmt_init()) != 0) {
568 dlmgmt_log(LOG_ERR, "unable to initialize daemon: %s",
569 strerror(err));
570 goto child_out;
571 } else if ((err = dlmgmt_set_privileges()) != 0) {
572 dlmgmt_log(LOG_ERR, "unable to set daemon privileges: %s",
573 strerror(err));
574 dlmgmt_fini();
575 goto child_out;
576 }
577
578 /*
579 * Inform the parent process that it can successfully exit.
580 */
581 dlmgmt_inform_parent_exit(EXIT_SUCCESS);
582
583 for (;;)
584 (void) pause();
585
586 child_out:
587 /* return from main() forcibly exits an MT process */
588 dlmgmt_inform_parent_exit(EXIT_FAILURE);
589 return (EXIT_FAILURE);
590 }