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 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25
26 #include <stdio.h>
27 #include <strings.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <door.h>
34 #include <errno.h>
35 #include <pthread.h>
36
37 #include <smbsrv/libsmb.h>
38 #include <smbsrv/libmlrpc.h>
39 #include "smbd.h"
40
41 static int smbd_opipe_fd = -1;
42 static int smbd_opipe_cookie = 0x50495045; /* PIPE */
43 static pthread_mutex_t smbd_opipe_mutex = PTHREAD_MUTEX_INITIALIZER;
44 static smbd_door_t smbd_opipe_sdh;
45
46 static void smbd_opipe_dispatch(void *, char *, size_t, door_desc_t *, uint_t);
47 static int smbd_opipe_exec_async(uint32_t);
48
49 /*
50 * Create the smbd opipe door service.
51 * Returns the door descriptor on success. Otherwise returns -1.
52 */
53 int
smbd_opipe_start(void)54 smbd_opipe_start(void)
55 {
56 (void) pthread_mutex_lock(&smbd_opipe_mutex);
57
58 if (smbd_opipe_fd != -1) {
59 (void) pthread_mutex_unlock(&smbd_opipe_mutex);
60 errno = EEXIST;
61 return (-1);
62 }
63
64 smbd_door_init(&smbd_opipe_sdh, "opipe");
65
66 errno = 0;
67 if ((smbd_opipe_fd = door_create(smbd_opipe_dispatch,
68 &smbd_opipe_cookie, (DOOR_UNREF | DOOR_REFUSE_DESC))) < 0) {
69 smbd_opipe_fd = -1;
70 }
71
72 (void) pthread_mutex_unlock(&smbd_opipe_mutex);
73 return (smbd_opipe_fd);
74 }
75
76 /*
77 * Stop the smbd opipe door service.
78 */
79 void
smbd_opipe_stop(void)80 smbd_opipe_stop(void)
81 {
82 (void) pthread_mutex_lock(&smbd_opipe_mutex);
83
84 smbd_door_fini(&smbd_opipe_sdh);
85
86 if (smbd_opipe_fd != -1) {
87 (void) door_revoke(smbd_opipe_fd);
88 smbd_opipe_fd = -1;
89 }
90
91 (void) pthread_mutex_unlock(&smbd_opipe_mutex);
92 }
93
94 /*
95 * Process smbd opipe requests.
96 */
97 /*ARGSUSED*/
98 static void
smbd_opipe_dispatch(void * cookie,char * argp,size_t arg_size,door_desc_t * dd,uint_t n_desc)99 smbd_opipe_dispatch(void *cookie, char *argp, size_t arg_size,
100 door_desc_t *dd, uint_t n_desc)
101 {
102 char buf[SMB_OPIPE_DOOR_BUFSIZE];
103 smb_doorhdr_t hdr;
104 size_t hdr_size;
105 uint8_t *data;
106 uint32_t datalen;
107
108 smbd_door_enter(&smbd_opipe_sdh);
109
110 if (!smbd_online())
111 smbd_door_return(&smbd_opipe_sdh, NULL, 0, NULL, 0);
112
113 bzero(&hdr, sizeof (smb_doorhdr_t));
114 hdr_size = xdr_sizeof(smb_doorhdr_xdr, &hdr);
115
116 if ((cookie != &smbd_opipe_cookie) || (argp == NULL) ||
117 (arg_size < hdr_size)) {
118 smbd_door_return(&smbd_opipe_sdh, NULL, 0, NULL, 0);
119 }
120
121 if (smb_doorhdr_decode(&hdr, (uint8_t *)argp, hdr_size) == -1)
122 smbd_door_return(&smbd_opipe_sdh, NULL, 0, NULL, 0);
123
124 if ((hdr.dh_magic != SMB_OPIPE_HDR_MAGIC) || (hdr.dh_fid == 0))
125 smbd_door_return(&smbd_opipe_sdh, NULL, 0, NULL, 0);
126
127 if (hdr.dh_datalen > SMB_OPIPE_DOOR_BUFSIZE)
128 hdr.dh_datalen = SMB_OPIPE_DOOR_BUFSIZE;
129
130 data = (uint8_t *)argp + hdr_size;
131 datalen = hdr.dh_datalen;
132
133 switch (hdr.dh_op) {
134 case SMB_OPIPE_OPEN:
135 hdr.dh_door_rc = ndr_pipe_open(hdr.dh_fid, data, datalen);
136
137 hdr.dh_datalen = 0;
138 hdr.dh_resid = 0;
139 datalen = hdr_size;
140 break;
141
142 case SMB_OPIPE_CLOSE:
143 hdr.dh_door_rc = ndr_pipe_close(hdr.dh_fid);
144
145 hdr.dh_datalen = 0;
146 hdr.dh_resid = 0;
147 datalen = hdr_size;
148 break;
149
150 case SMB_OPIPE_READ:
151 data = (uint8_t *)buf + hdr_size;
152 datalen = hdr.dh_datalen;
153
154 hdr.dh_door_rc = ndr_pipe_read(hdr.dh_fid, data, &datalen,
155 &hdr.dh_resid);
156
157 hdr.dh_datalen = datalen;
158 datalen += hdr_size;
159 break;
160
161 case SMB_OPIPE_WRITE:
162 hdr.dh_door_rc = ndr_pipe_write(hdr.dh_fid, data, datalen);
163
164 hdr.dh_datalen = 0;
165 hdr.dh_resid = 0;
166 datalen = hdr_size;
167 break;
168
169 case SMB_OPIPE_EXEC:
170 hdr.dh_door_rc = smbd_opipe_exec_async(hdr.dh_fid);
171
172 hdr.dh_datalen = 0;
173 hdr.dh_resid = 0;
174 datalen = hdr_size;
175 break;
176
177 default:
178 smbd_door_return(&smbd_opipe_sdh, NULL, 0, NULL, 0);
179 break;
180 }
181
182 (void) smb_doorhdr_encode(&hdr, (uint8_t *)buf, hdr_size);
183 smbd_door_return(&smbd_opipe_sdh, buf, datalen, NULL, 0);
184 }
185
186 /*
187 * On success, arg will be freed by the thread.
188 */
189 static int
smbd_opipe_exec_async(uint32_t fid)190 smbd_opipe_exec_async(uint32_t fid)
191 {
192 pthread_attr_t attr;
193 pthread_t tid;
194 uint32_t *arg;
195 int rc;
196
197 if ((arg = malloc(sizeof (uint32_t))) == NULL)
198 return (ENOMEM);
199
200 *arg = fid;
201
202 (void) pthread_attr_init(&attr);
203 (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
204 rc = pthread_create(&tid, &attr, ndr_pipe_transact, arg);
205 (void) pthread_attr_destroy(&attr);
206
207 if (rc != 0)
208 free(arg);
209 return (rc);
210 }
211