xref: /illumos-gate/usr/src/cmd/svc/configd/maindoor.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <assert.h>
28*7c478bd9Sstevel@tonic-gate #include <door.h>
29*7c478bd9Sstevel@tonic-gate #include <errno.h>
30*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
31*7c478bd9Sstevel@tonic-gate #include <pthread.h>
32*7c478bd9Sstevel@tonic-gate #include <signal.h>
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
36*7c478bd9Sstevel@tonic-gate #include <unistd.h>
37*7c478bd9Sstevel@tonic-gate #include <ucred.h>
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate #include "repcache_protocol.h"
40*7c478bd9Sstevel@tonic-gate #include "configd.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #define	INVALID_RESULT		((uint32_t)-1U)
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static int		main_door_fd = -1;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
47*7c478bd9Sstevel@tonic-gate static void
main_switcher(void * cookie,char * argp,size_t arg_size,door_desc_t * desc,uint_t n_desc)48*7c478bd9Sstevel@tonic-gate main_switcher(void *cookie, char *argp, size_t arg_size, door_desc_t *desc,
49*7c478bd9Sstevel@tonic-gate     uint_t n_desc)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	repository_door_request_t *request;
52*7c478bd9Sstevel@tonic-gate 	repository_door_response_t reply;
53*7c478bd9Sstevel@tonic-gate 	door_desc_t reply_desc;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	thread_info_t *ti = thread_self();
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	int send_desc = 0;
58*7c478bd9Sstevel@tonic-gate 	int fd;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	thread_newstate(ti, TI_MAIN_DOOR_CALL);
61*7c478bd9Sstevel@tonic-gate 	ti->ti_main_door_request = (void *)argp;
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	assert(cookie == REPOSITORY_DOOR_COOKIE);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	reply.rdr_status = INVALID_RESULT;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if (argp == DOOR_UNREF_DATA) {
68*7c478bd9Sstevel@tonic-gate 		backend_fini();
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 		exit(CONFIGD_EXIT_LOST_MAIN_DOOR);
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	/*
74*7c478bd9Sstevel@tonic-gate 	 * No file descriptors allowed
75*7c478bd9Sstevel@tonic-gate 	 */
76*7c478bd9Sstevel@tonic-gate 	assert(n_desc == 0);
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	/*
79*7c478bd9Sstevel@tonic-gate 	 * first, we just check the version
80*7c478bd9Sstevel@tonic-gate 	 */
81*7c478bd9Sstevel@tonic-gate 	if (arg_size < offsetofend(repository_door_request_t, rdr_version)) {
82*7c478bd9Sstevel@tonic-gate 		reply.rdr_status = REPOSITORY_DOOR_FAIL_BAD_REQUEST;
83*7c478bd9Sstevel@tonic-gate 		goto fail;
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	/* LINTED alignment */
87*7c478bd9Sstevel@tonic-gate 	request = (repository_door_request_t *)argp;
88*7c478bd9Sstevel@tonic-gate 	ti->ti_main_door_request = request;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	if (request->rdr_version != REPOSITORY_DOOR_VERSION) {
91*7c478bd9Sstevel@tonic-gate 		reply.rdr_status = REPOSITORY_DOOR_FAIL_VERSION_MISMATCH;
92*7c478bd9Sstevel@tonic-gate 		goto fail;
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	/*
96*7c478bd9Sstevel@tonic-gate 	 * Now, check that the argument is of the minimum required size
97*7c478bd9Sstevel@tonic-gate 	 */
98*7c478bd9Sstevel@tonic-gate 	if (arg_size < offsetofend(repository_door_request_t, rdr_request)) {
99*7c478bd9Sstevel@tonic-gate 		reply.rdr_status = REPOSITORY_DOOR_FAIL_BAD_REQUEST;
100*7c478bd9Sstevel@tonic-gate 		goto fail;
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	if (door_ucred(&ti->ti_ucred) != 0) {
104*7c478bd9Sstevel@tonic-gate 		reply.rdr_status = REPOSITORY_DOOR_FAIL_PERMISSION_DENIED;
105*7c478bd9Sstevel@tonic-gate 		goto fail;
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	switch (request->rdr_request) {
109*7c478bd9Sstevel@tonic-gate 	case REPOSITORY_DOOR_REQUEST_CONNECT:
110*7c478bd9Sstevel@tonic-gate 		fd = -1;
111*7c478bd9Sstevel@tonic-gate 		reply.rdr_status = create_connection(ti->ti_ucred, request,
112*7c478bd9Sstevel@tonic-gate 		    arg_size, &fd);
113*7c478bd9Sstevel@tonic-gate 		if (reply.rdr_status != REPOSITORY_DOOR_SUCCESS) {
114*7c478bd9Sstevel@tonic-gate 			assert(fd == -1);
115*7c478bd9Sstevel@tonic-gate 			goto fail;
116*7c478bd9Sstevel@tonic-gate 		}
117*7c478bd9Sstevel@tonic-gate 		assert(fd != -1);
118*7c478bd9Sstevel@tonic-gate 		reply_desc.d_attributes = DOOR_DESCRIPTOR | DOOR_RELEASE;
119*7c478bd9Sstevel@tonic-gate 		reply_desc.d_data.d_desc.d_descriptor = fd;
120*7c478bd9Sstevel@tonic-gate 		send_desc = 1;
121*7c478bd9Sstevel@tonic-gate 		break;
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	default:
124*7c478bd9Sstevel@tonic-gate 		reply.rdr_status = REPOSITORY_DOOR_FAIL_BAD_REQUEST;
125*7c478bd9Sstevel@tonic-gate 		goto fail;
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate fail:
129*7c478bd9Sstevel@tonic-gate 	assert(reply.rdr_status != INVALID_RESULT);
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	thread_newstate(ti, TI_DOOR_RETURN);
132*7c478bd9Sstevel@tonic-gate 	ti->ti_main_door_request = NULL;
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	(void) door_return((char *)&reply, sizeof (reply),
135*7c478bd9Sstevel@tonic-gate 	    &reply_desc, (send_desc)? 1:0);
136*7c478bd9Sstevel@tonic-gate 	(void) door_return(NULL, 0, NULL, 0);
137*7c478bd9Sstevel@tonic-gate }
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate int
setup_main_door(const char * doorpath)140*7c478bd9Sstevel@tonic-gate setup_main_door(const char *doorpath)
141*7c478bd9Sstevel@tonic-gate {
142*7c478bd9Sstevel@tonic-gate 	mode_t oldmask;
143*7c478bd9Sstevel@tonic-gate 	int fd;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	int door_flags = DOOR_UNREF | DOOR_REFUSE_DESC;
146*7c478bd9Sstevel@tonic-gate #ifdef DOOR_NO_CANCEL
147*7c478bd9Sstevel@tonic-gate 	door_flags |= DOOR_NO_CANCEL;
148*7c478bd9Sstevel@tonic-gate #endif
149*7c478bd9Sstevel@tonic-gate 	if ((main_door_fd = door_create(main_switcher, REPOSITORY_DOOR_COOKIE,
150*7c478bd9Sstevel@tonic-gate 	    door_flags)) < 0) {
151*7c478bd9Sstevel@tonic-gate 		perror("door_create");
152*7c478bd9Sstevel@tonic-gate 		return (0);
153*7c478bd9Sstevel@tonic-gate 	}
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate #ifdef DOOR_PARAM_DATA_MIN
156*7c478bd9Sstevel@tonic-gate 	if (door_setparam(main_door_fd, DOOR_PARAM_DATA_MIN,
157*7c478bd9Sstevel@tonic-gate 	    offsetofend(repository_door_request_t, rdr_request)) == -1 ||
158*7c478bd9Sstevel@tonic-gate 	    door_setparam(main_door_fd, DOOR_PARAM_DATA_MAX,
159*7c478bd9Sstevel@tonic-gate 	    sizeof (repository_door_request_t)) == -1) {
160*7c478bd9Sstevel@tonic-gate 		perror("door_setparam");
161*7c478bd9Sstevel@tonic-gate 		return (0);
162*7c478bd9Sstevel@tonic-gate 	}
163*7c478bd9Sstevel@tonic-gate #endif /* DOOR_PARAM_DATA_MIN */
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	/*
166*7c478bd9Sstevel@tonic-gate 	 * Create the file if it doesn't exist.  Ignore errors, since
167*7c478bd9Sstevel@tonic-gate 	 * fattach(3C) will catch any real problems.
168*7c478bd9Sstevel@tonic-gate 	 */
169*7c478bd9Sstevel@tonic-gate 	oldmask = umask(000);		/* disable umask temporarily */
170*7c478bd9Sstevel@tonic-gate 	fd = open(doorpath, O_RDWR | O_CREAT | O_EXCL, 0644);
171*7c478bd9Sstevel@tonic-gate 	(void) umask(oldmask);
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	if (fd >= 0)
174*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	if (fattach(main_door_fd, doorpath) < 0) {
177*7c478bd9Sstevel@tonic-gate 		if ((errno != EBUSY) ||
178*7c478bd9Sstevel@tonic-gate 		    (fdetach(doorpath) < 0) ||
179*7c478bd9Sstevel@tonic-gate 		    (fattach(main_door_fd, doorpath) < 0)) {
180*7c478bd9Sstevel@tonic-gate 			perror("fattach");
181*7c478bd9Sstevel@tonic-gate 			(void) door_revoke(main_door_fd);
182*7c478bd9Sstevel@tonic-gate 			main_door_fd = -1;
183*7c478bd9Sstevel@tonic-gate 			return (0);
184*7c478bd9Sstevel@tonic-gate 		}
185*7c478bd9Sstevel@tonic-gate 	}
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	return (1);
188*7c478bd9Sstevel@tonic-gate }
189