xref: /illumos-gate/usr/src/cmd/smbsrv/smbd/smbd_share_doorsvc.c (revision 24b9abbad58fdd63dad716fd35a99a7944c4e3eb)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * LanMan share door server
30  */
31 
32 #include <door.h>
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <syslog.h>
39 #include <string.h>
40 #include <strings.h>
41 #include <pthread.h>
42 
43 #include <smbsrv/libsmb.h>
44 
45 #include <smbsrv/smb_share.h>
46 #include <smbsrv/smbinfo.h>
47 
48 #define	SMB_SHARE_DSRV_VERSION	1
49 #define	SMB_SHARE_DSRV_COOKIE	((void*)(0xdeadbeef^SMB_SHARE_DSRV_VERSION))
50 
51 static int smb_share_dsrv_fd = -1;
52 static pthread_mutex_t smb_share_dsrv_mtx = PTHREAD_MUTEX_INITIALIZER;
53 
54 static void smb_share_dsrv_dispatch(void *, char *, size_t, door_desc_t *,
55     uint_t);
56 static int smb_share_dsrv_enum(smb_enumshare_info_t *esi);
57 
58 /*
59  * smb_share_dsrv_start
60  *
61  * Start the LanMan share door service.
62  * Returns 0 on success. Otherwise, -1.
63  */
64 int
65 smb_share_dsrv_start(void)
66 {
67 	int	newfd;
68 
69 	(void) pthread_mutex_lock(&smb_share_dsrv_mtx);
70 
71 	if (smb_share_dsrv_fd != -1) {
72 		syslog(LOG_ERR, "smb_share_dsrv_start: duplicate");
73 		(void) pthread_mutex_unlock(&smb_share_dsrv_mtx);
74 		return (smb_share_dsrv_fd);
75 	}
76 
77 	if ((smb_share_dsrv_fd = door_create(smb_share_dsrv_dispatch,
78 	    SMB_SHARE_DSRV_COOKIE, (DOOR_UNREF | DOOR_REFUSE_DESC))) < 0) {
79 		syslog(LOG_ERR, "smb_share_dsrv_start: door_create: %s",
80 		    strerror(errno));
81 		(void) pthread_mutex_unlock(&smb_share_dsrv_mtx);
82 		return (-1);
83 	}
84 
85 	(void) unlink(SMB_SHARE_DNAME);
86 
87 	if ((newfd = creat(SMB_SHARE_DNAME, 0644)) < 0) {
88 		syslog(LOG_ERR, "smb_share_dsrv_start: open: %s",
89 		    strerror(errno));
90 		(void) door_revoke(smb_share_dsrv_fd);
91 		smb_share_dsrv_fd = -1;
92 		(void) pthread_mutex_unlock(&smb_share_dsrv_mtx);
93 		return (-1);
94 	}
95 
96 	(void) close(newfd);
97 	(void) fdetach(SMB_SHARE_DNAME);
98 
99 	if (fattach(smb_share_dsrv_fd, SMB_SHARE_DNAME) < 0) {
100 		syslog(LOG_ERR, "smb_share_dsrv_start: fattach: %s",
101 		    strerror(errno));
102 		(void) door_revoke(smb_share_dsrv_fd);
103 		smb_share_dsrv_fd = -1;
104 		(void) pthread_mutex_unlock(&smb_share_dsrv_mtx);
105 		return (-1);
106 	}
107 
108 	(void) pthread_mutex_unlock(&smb_share_dsrv_mtx);
109 	return (smb_share_dsrv_fd);
110 }
111 
112 /*
113  * smb_share_dsrv_stop
114  *
115  * Stop the LanMan share door service.
116  */
117 void
118 smb_share_dsrv_stop(void)
119 {
120 	(void) pthread_mutex_lock(&smb_share_dsrv_mtx);
121 
122 	if (smb_share_dsrv_fd != -1) {
123 		(void) fdetach(SMB_SHARE_DNAME);
124 		(void) door_revoke(smb_share_dsrv_fd);
125 		smb_share_dsrv_fd = -1;
126 	}
127 
128 	(void) pthread_mutex_unlock(&smb_share_dsrv_mtx);
129 }
130 
131 /*
132  * smb_share_dsrv_dispatch
133  *
134  * This function with which the LMSHARE door is associated
135  * will invoke the appropriate CIFS share management function
136  * based on the request type of the door call.
137  */
138 /*ARGSUSED*/
139 static void
140 smb_share_dsrv_dispatch(void *cookie, char *ptr, size_t size, door_desc_t *dp,
141     uint_t n_desc)
142 {
143 	uint32_t rc;
144 	int req_type;
145 	char buf[SMB_SHARE_DSIZE];
146 	unsigned int used;
147 	smb_dr_ctx_t *dec_ctx;
148 	smb_dr_ctx_t *enc_ctx;
149 	unsigned int dec_status;
150 	unsigned int enc_status;
151 	char *sharename, *sharename2;
152 	smb_share_t lmshr_info;
153 	smb_shrlist_t lmshr_list;
154 	smb_enumshare_info_t esi;
155 	int offset;
156 
157 	if ((cookie != SMB_SHARE_DSRV_COOKIE) || (ptr == NULL) ||
158 	    (size < sizeof (uint32_t))) {
159 		(void) door_return(NULL, 0, NULL, 0);
160 	}
161 
162 	dec_ctx = smb_dr_decode_start(ptr, size);
163 	enc_ctx = smb_dr_encode_start(buf, sizeof (buf));
164 	req_type = smb_dr_get_uint32(dec_ctx);
165 
166 	switch (req_type) {
167 	case SMB_SHROP_NUM_SHARES:
168 		if ((dec_status = smb_dr_decode_finish(dec_ctx)) != 0)
169 			goto decode_error;
170 
171 		rc = smb_shr_count();
172 		smb_dr_put_int32(enc_ctx, SMB_SHARE_DSUCCESS);
173 		smb_dr_put_uint32(enc_ctx, rc);
174 		break;
175 
176 	case SMB_SHROP_DELETE:
177 		sharename = smb_dr_get_string(dec_ctx);
178 
179 		if ((dec_status = smb_dr_decode_finish(dec_ctx)) != 0) {
180 			smb_dr_free_string(sharename);
181 			goto decode_error;
182 		}
183 
184 		rc = smb_shr_del(sharename, 0);
185 		smb_dr_put_int32(enc_ctx, SMB_SHARE_DSUCCESS);
186 		smb_dr_put_uint32(enc_ctx, rc);
187 		smb_dr_free_string(sharename);
188 		break;
189 
190 	case SMB_SHROP_RENAME:
191 		sharename = smb_dr_get_string(dec_ctx);
192 		sharename2 = smb_dr_get_string(dec_ctx);
193 
194 		if ((dec_status = smb_dr_decode_finish(dec_ctx)) != 0) {
195 			smb_dr_free_string(sharename);
196 			smb_dr_free_string(sharename2);
197 			goto decode_error;
198 		}
199 
200 		rc = smb_shr_ren(sharename, sharename2, 0);
201 		smb_dr_put_int32(enc_ctx, SMB_SHARE_DSUCCESS);
202 		smb_dr_put_uint32(enc_ctx, rc);
203 		smb_dr_free_string(sharename);
204 		smb_dr_free_string(sharename2);
205 		break;
206 
207 	case SMB_SHROP_GETINFO:
208 		sharename = smb_dr_get_string(dec_ctx);
209 		if ((dec_status = smb_dr_decode_finish(dec_ctx)) != 0) {
210 			smb_dr_free_string(sharename);
211 			goto decode_error;
212 		}
213 
214 		rc = smb_shr_get(sharename, &lmshr_info);
215 		smb_dr_put_int32(enc_ctx, SMB_SHARE_DSUCCESS);
216 		smb_dr_put_uint32(enc_ctx, rc);
217 		smb_dr_put_share(enc_ctx, &lmshr_info);
218 		smb_dr_free_string(sharename);
219 		break;
220 
221 	case SMB_SHROP_ADD:
222 		smb_dr_get_share(dec_ctx, &lmshr_info);
223 		if ((dec_status = smb_dr_decode_finish(dec_ctx)) != 0)
224 			goto decode_error;
225 
226 		rc = smb_shr_add(&lmshr_info, 0);
227 		smb_dr_put_int32(enc_ctx, SMB_SHARE_DSUCCESS);
228 		smb_dr_put_uint32(enc_ctx, rc);
229 		smb_dr_put_share(enc_ctx, &lmshr_info);
230 		break;
231 
232 	case SMB_SHROP_SETINFO:
233 		smb_dr_get_share(dec_ctx, &lmshr_info);
234 		if ((dec_status = smb_dr_decode_finish(dec_ctx)) != 0)
235 			goto decode_error;
236 
237 		rc = smb_shr_set(&lmshr_info, 0);
238 		smb_dr_put_int32(enc_ctx, SMB_SHARE_DSUCCESS);
239 		smb_dr_put_uint32(enc_ctx, rc);
240 		break;
241 
242 	case SMB_SHROP_LIST:
243 		offset = smb_dr_get_int32(dec_ctx);
244 		if ((dec_status = smb_dr_decode_finish(dec_ctx)) != 0)
245 			goto decode_error;
246 
247 		smb_shr_list(offset, &lmshr_list);
248 		smb_dr_put_int32(enc_ctx, SMB_SHARE_DSUCCESS);
249 		smb_dr_put_shrlist(enc_ctx, &lmshr_list);
250 		break;
251 
252 	case SMB_SHROP_ENUM:
253 		esi.es_bufsize = smb_dr_get_ushort(dec_ctx);
254 		esi.es_username = smb_dr_get_string(dec_ctx);
255 		if ((dec_status = smb_dr_decode_finish(dec_ctx)) != 0) {
256 			smb_dr_free_string(esi.es_username);
257 			goto decode_error;
258 		}
259 
260 		rc = smb_share_dsrv_enum(&esi);
261 
262 		smb_dr_free_string(esi.es_username);
263 
264 		smb_dr_put_int32(enc_ctx, SMB_SHARE_DSUCCESS);
265 		smb_dr_put_uint32(enc_ctx, rc);
266 		if (rc == NERR_Success) {
267 			smb_dr_put_ushort(enc_ctx, esi.es_ntotal);
268 			smb_dr_put_ushort(enc_ctx, esi.es_nsent);
269 			smb_dr_put_ushort(enc_ctx, esi.es_datasize);
270 			smb_dr_put_buf(enc_ctx,
271 			    (unsigned char *)esi.es_buf, esi.es_bufsize);
272 			free(esi.es_buf);
273 		}
274 		break;
275 
276 	default:
277 		dec_status = smb_dr_decode_finish(dec_ctx);
278 		goto decode_error;
279 	}
280 
281 	if ((enc_status = smb_dr_encode_finish(enc_ctx, &used)) != 0) {
282 		enc_ctx = smb_dr_encode_start(buf, sizeof (buf));
283 		smb_dr_put_int32(enc_ctx, SMB_SHARE_DERROR);
284 		smb_dr_put_uint32(enc_ctx, enc_status);
285 		(void) smb_dr_encode_finish(enc_ctx, &used);
286 	}
287 
288 	(void) door_return(buf, used, NULL, 0);
289 	return;
290 
291 decode_error:
292 	smb_dr_put_int32(enc_ctx, SMB_SHARE_DERROR);
293 	smb_dr_put_uint32(enc_ctx, dec_status);
294 	(void) smb_dr_encode_finish(enc_ctx, &used);
295 	(void) door_return(buf, used, NULL, 0);
296 }
297 
298 /*
299  * smb_share_dsrv_enum
300  *
301  * This function builds a response for a NetShareEnum RAP request which
302  * originates from smbsrv kernel module. A response buffer is allocated
303  * with the specified size in esi->es_bufsize. List of shares is scanned
304  * twice. In the first round the total number of shares which their OEM
305  * name is shorter than 13 chars (esi->es_ntotal) and also the number of
306  * shares that fit in the given buffer are calculated. In the second
307  * round the shares data are encoded in the buffer.
308  *
309  * The data associated with each share has two parts, a fixed size part and
310  * a variable size part which is share's comment. The outline of the response
311  * buffer is so that fixed part for all the shares will appear first and follows
312  * with the comments for all those shares and that's why the data cannot be
313  * encoded in one round without unnecessarily complicating the code.
314  */
315 static int
316 smb_share_dsrv_enum(smb_enumshare_info_t *esi)
317 {
318 	smb_shriter_t shi;
319 	smb_share_t *si;
320 	int remained;
321 	uint16_t infolen = 0;
322 	uint16_t cmntlen = 0;
323 	uint16_t sharelen;
324 	uint16_t clen;
325 	uint32_t cmnt_offs;
326 	smb_msgbuf_t info_mb;
327 	smb_msgbuf_t cmnt_mb;
328 	boolean_t autohome_added = B_FALSE;
329 
330 	esi->es_ntotal = esi->es_nsent = 0;
331 
332 	if ((esi->es_buf = malloc(esi->es_bufsize)) == NULL)
333 		return (NERR_InternalError);
334 
335 	bzero(esi->es_buf, esi->es_bufsize);
336 	remained = esi->es_bufsize;
337 
338 	/* Do the necessary calculations in the first round */
339 	smb_shr_iterinit(&shi, SMB_SHRF_ALL);
340 
341 	while ((si = smb_shr_iterate(&shi)) != NULL) {
342 		if (si->shr_flags & SMB_SHRF_LONGNAME)
343 			continue;
344 
345 		if ((si->shr_flags & SMB_SHRF_AUTOHOME) && !autohome_added) {
346 			if (strcasecmp(esi->es_username, si->shr_name) == 0)
347 				autohome_added = B_TRUE;
348 			else
349 				continue;
350 		}
351 
352 		esi->es_ntotal++;
353 
354 		if (remained <= 0)
355 			continue;
356 
357 		clen = strlen(si->shr_cmnt) + 1;
358 		sharelen = SHARE_INFO_1_SIZE + clen;
359 
360 		if (sharelen <= remained) {
361 			infolen += SHARE_INFO_1_SIZE;
362 			cmntlen += clen;
363 		}
364 
365 		remained -= sharelen;
366 	}
367 
368 	esi->es_datasize = infolen + cmntlen;
369 
370 	smb_msgbuf_init(&info_mb, (uint8_t *)esi->es_buf, infolen, 0);
371 	smb_msgbuf_init(&cmnt_mb, (uint8_t *)esi->es_buf + infolen, cmntlen, 0);
372 	cmnt_offs = infolen;
373 
374 	/* Encode the data in the second round */
375 	smb_shr_iterinit(&shi, SMB_SHRF_ALL);
376 	autohome_added = B_FALSE;
377 
378 	while ((si = smb_shr_iterate(&shi)) != NULL) {
379 		if (si->shr_flags & SMB_SHRF_LONGNAME)
380 			continue;
381 
382 		if ((si->shr_flags & SMB_SHRF_AUTOHOME) && !autohome_added) {
383 			if (strcasecmp(esi->es_username, si->shr_name) == 0)
384 				autohome_added = B_TRUE;
385 			else
386 				continue;
387 		}
388 
389 		if (smb_msgbuf_encode(&info_mb, "13c.wl",
390 		    si->shr_oemname, si->shr_type, cmnt_offs) < 0)
391 			break;
392 
393 		if (smb_msgbuf_encode(&cmnt_mb, "s", si->shr_cmnt) < 0)
394 			break;
395 
396 		cmnt_offs += strlen(si->shr_cmnt) + 1;
397 		esi->es_nsent++;
398 	}
399 
400 	smb_msgbuf_term(&info_mb);
401 	smb_msgbuf_term(&cmnt_mb);
402 
403 	return (NERR_Success);
404 }
405