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 #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <assert.h> 30*7c478bd9Sstevel@tonic-gate #include <door.h> 31*7c478bd9Sstevel@tonic-gate #include <errno.h> 32*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 33*7c478bd9Sstevel@tonic-gate #include <pthread.h> 34*7c478bd9Sstevel@tonic-gate #include <signal.h> 35*7c478bd9Sstevel@tonic-gate #include <stdio.h> 36*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 38*7c478bd9Sstevel@tonic-gate #include <unistd.h> 39*7c478bd9Sstevel@tonic-gate #include <ucred.h> 40*7c478bd9Sstevel@tonic-gate 41*7c478bd9Sstevel@tonic-gate #include "repcache_protocol.h" 42*7c478bd9Sstevel@tonic-gate #include "configd.h" 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #define INVALID_RESULT ((uint32_t)-1U) 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate static int main_door_fd = -1; 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 49*7c478bd9Sstevel@tonic-gate static void 50*7c478bd9Sstevel@tonic-gate main_switcher(void *cookie, char *argp, size_t arg_size, door_desc_t *desc, 51*7c478bd9Sstevel@tonic-gate uint_t n_desc) 52*7c478bd9Sstevel@tonic-gate { 53*7c478bd9Sstevel@tonic-gate repository_door_request_t *request; 54*7c478bd9Sstevel@tonic-gate repository_door_response_t reply; 55*7c478bd9Sstevel@tonic-gate door_desc_t reply_desc; 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate thread_info_t *ti = thread_self(); 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate int send_desc = 0; 60*7c478bd9Sstevel@tonic-gate int fd; 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate thread_newstate(ti, TI_MAIN_DOOR_CALL); 63*7c478bd9Sstevel@tonic-gate ti->ti_main_door_request = (void *)argp; 64*7c478bd9Sstevel@tonic-gate 65*7c478bd9Sstevel@tonic-gate assert(cookie == REPOSITORY_DOOR_COOKIE); 66*7c478bd9Sstevel@tonic-gate 67*7c478bd9Sstevel@tonic-gate reply.rdr_status = INVALID_RESULT; 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate if (argp == DOOR_UNREF_DATA) { 70*7c478bd9Sstevel@tonic-gate backend_fini(); 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate exit(CONFIGD_EXIT_LOST_MAIN_DOOR); 73*7c478bd9Sstevel@tonic-gate } 74*7c478bd9Sstevel@tonic-gate 75*7c478bd9Sstevel@tonic-gate /* 76*7c478bd9Sstevel@tonic-gate * No file descriptors allowed 77*7c478bd9Sstevel@tonic-gate */ 78*7c478bd9Sstevel@tonic-gate assert(n_desc == 0); 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate /* 81*7c478bd9Sstevel@tonic-gate * first, we just check the version 82*7c478bd9Sstevel@tonic-gate */ 83*7c478bd9Sstevel@tonic-gate if (arg_size < offsetofend(repository_door_request_t, rdr_version)) { 84*7c478bd9Sstevel@tonic-gate reply.rdr_status = REPOSITORY_DOOR_FAIL_BAD_REQUEST; 85*7c478bd9Sstevel@tonic-gate goto fail; 86*7c478bd9Sstevel@tonic-gate } 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate /* LINTED alignment */ 89*7c478bd9Sstevel@tonic-gate request = (repository_door_request_t *)argp; 90*7c478bd9Sstevel@tonic-gate ti->ti_main_door_request = request; 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate if (request->rdr_version != REPOSITORY_DOOR_VERSION) { 93*7c478bd9Sstevel@tonic-gate reply.rdr_status = REPOSITORY_DOOR_FAIL_VERSION_MISMATCH; 94*7c478bd9Sstevel@tonic-gate goto fail; 95*7c478bd9Sstevel@tonic-gate } 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate /* 98*7c478bd9Sstevel@tonic-gate * Now, check that the argument is of the minimum required size 99*7c478bd9Sstevel@tonic-gate */ 100*7c478bd9Sstevel@tonic-gate if (arg_size < offsetofend(repository_door_request_t, rdr_request)) { 101*7c478bd9Sstevel@tonic-gate reply.rdr_status = REPOSITORY_DOOR_FAIL_BAD_REQUEST; 102*7c478bd9Sstevel@tonic-gate goto fail; 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate if (door_ucred(&ti->ti_ucred) != 0) { 106*7c478bd9Sstevel@tonic-gate reply.rdr_status = REPOSITORY_DOOR_FAIL_PERMISSION_DENIED; 107*7c478bd9Sstevel@tonic-gate goto fail; 108*7c478bd9Sstevel@tonic-gate } 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate switch (request->rdr_request) { 111*7c478bd9Sstevel@tonic-gate case REPOSITORY_DOOR_REQUEST_CONNECT: 112*7c478bd9Sstevel@tonic-gate fd = -1; 113*7c478bd9Sstevel@tonic-gate reply.rdr_status = create_connection(ti->ti_ucred, request, 114*7c478bd9Sstevel@tonic-gate arg_size, &fd); 115*7c478bd9Sstevel@tonic-gate if (reply.rdr_status != REPOSITORY_DOOR_SUCCESS) { 116*7c478bd9Sstevel@tonic-gate assert(fd == -1); 117*7c478bd9Sstevel@tonic-gate goto fail; 118*7c478bd9Sstevel@tonic-gate } 119*7c478bd9Sstevel@tonic-gate assert(fd != -1); 120*7c478bd9Sstevel@tonic-gate reply_desc.d_attributes = DOOR_DESCRIPTOR | DOOR_RELEASE; 121*7c478bd9Sstevel@tonic-gate reply_desc.d_data.d_desc.d_descriptor = fd; 122*7c478bd9Sstevel@tonic-gate send_desc = 1; 123*7c478bd9Sstevel@tonic-gate break; 124*7c478bd9Sstevel@tonic-gate 125*7c478bd9Sstevel@tonic-gate default: 126*7c478bd9Sstevel@tonic-gate reply.rdr_status = REPOSITORY_DOOR_FAIL_BAD_REQUEST; 127*7c478bd9Sstevel@tonic-gate goto fail; 128*7c478bd9Sstevel@tonic-gate } 129*7c478bd9Sstevel@tonic-gate 130*7c478bd9Sstevel@tonic-gate fail: 131*7c478bd9Sstevel@tonic-gate assert(reply.rdr_status != INVALID_RESULT); 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate thread_newstate(ti, TI_DOOR_RETURN); 134*7c478bd9Sstevel@tonic-gate ti->ti_main_door_request = NULL; 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate (void) door_return((char *)&reply, sizeof (reply), 137*7c478bd9Sstevel@tonic-gate &reply_desc, (send_desc)? 1:0); 138*7c478bd9Sstevel@tonic-gate (void) door_return(NULL, 0, NULL, 0); 139*7c478bd9Sstevel@tonic-gate } 140*7c478bd9Sstevel@tonic-gate 141*7c478bd9Sstevel@tonic-gate int 142*7c478bd9Sstevel@tonic-gate setup_main_door(const char *doorpath) 143*7c478bd9Sstevel@tonic-gate { 144*7c478bd9Sstevel@tonic-gate mode_t oldmask; 145*7c478bd9Sstevel@tonic-gate int fd; 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate int door_flags = DOOR_UNREF | DOOR_REFUSE_DESC; 148*7c478bd9Sstevel@tonic-gate #ifdef DOOR_NO_CANCEL 149*7c478bd9Sstevel@tonic-gate door_flags |= DOOR_NO_CANCEL; 150*7c478bd9Sstevel@tonic-gate #endif 151*7c478bd9Sstevel@tonic-gate if ((main_door_fd = door_create(main_switcher, REPOSITORY_DOOR_COOKIE, 152*7c478bd9Sstevel@tonic-gate door_flags)) < 0) { 153*7c478bd9Sstevel@tonic-gate perror("door_create"); 154*7c478bd9Sstevel@tonic-gate return (0); 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate #ifdef DOOR_PARAM_DATA_MIN 158*7c478bd9Sstevel@tonic-gate if (door_setparam(main_door_fd, DOOR_PARAM_DATA_MIN, 159*7c478bd9Sstevel@tonic-gate offsetofend(repository_door_request_t, rdr_request)) == -1 || 160*7c478bd9Sstevel@tonic-gate door_setparam(main_door_fd, DOOR_PARAM_DATA_MAX, 161*7c478bd9Sstevel@tonic-gate sizeof (repository_door_request_t)) == -1) { 162*7c478bd9Sstevel@tonic-gate perror("door_setparam"); 163*7c478bd9Sstevel@tonic-gate return (0); 164*7c478bd9Sstevel@tonic-gate } 165*7c478bd9Sstevel@tonic-gate #endif /* DOOR_PARAM_DATA_MIN */ 166*7c478bd9Sstevel@tonic-gate 167*7c478bd9Sstevel@tonic-gate /* 168*7c478bd9Sstevel@tonic-gate * Create the file if it doesn't exist. Ignore errors, since 169*7c478bd9Sstevel@tonic-gate * fattach(3C) will catch any real problems. 170*7c478bd9Sstevel@tonic-gate */ 171*7c478bd9Sstevel@tonic-gate oldmask = umask(000); /* disable umask temporarily */ 172*7c478bd9Sstevel@tonic-gate fd = open(doorpath, O_RDWR | O_CREAT | O_EXCL, 0644); 173*7c478bd9Sstevel@tonic-gate (void) umask(oldmask); 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate if (fd >= 0) 176*7c478bd9Sstevel@tonic-gate (void) close(fd); 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate if (fattach(main_door_fd, doorpath) < 0) { 179*7c478bd9Sstevel@tonic-gate if ((errno != EBUSY) || 180*7c478bd9Sstevel@tonic-gate (fdetach(doorpath) < 0) || 181*7c478bd9Sstevel@tonic-gate (fattach(main_door_fd, doorpath) < 0)) { 182*7c478bd9Sstevel@tonic-gate perror("fattach"); 183*7c478bd9Sstevel@tonic-gate (void) door_revoke(main_door_fd); 184*7c478bd9Sstevel@tonic-gate main_door_fd = -1; 185*7c478bd9Sstevel@tonic-gate return (0); 186*7c478bd9Sstevel@tonic-gate } 187*7c478bd9Sstevel@tonic-gate } 188*7c478bd9Sstevel@tonic-gate 189*7c478bd9Sstevel@tonic-gate return (1); 190*7c478bd9Sstevel@tonic-gate } 191