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