summaryrefslogtreecommitdiff
path: root/usr/src/cmd/smbsrv/smbd/smbd_doorsvc.c
blob: 4434dc2b29cf743a6eb06a42415fbadbecd3a6d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2019 Nexenta Systems, Inc.  All rights reserved.
 */

#include <sys/list.h>
#include <assert.h>
#include <alloca.h>
#include <door.h>
#include <errno.h>
#include <syslog.h>
#include <unistd.h>
#include <stdio.h>
#include <synch.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <strings.h>
#include <note.h>
#include <smbsrv/smb_door.h>
#include <smbsrv/smb_xdr.h>
#include <smbsrv/smb_token.h>
#include <smbsrv/libmlsvc.h>
#include <smbsrv/libsmbns.h>
#include "smbd.h"

/*
 * The list contains asynchronous requests that have been initiated
 * but have not yet been collected (via smbd_dop_async_response).
 */
typedef struct smbd_doorsvc {
	mutex_t		sd_mutex;
	cond_t		sd_cv;
	list_t		sd_async_list;
	uint32_t	sd_async_count;
} smbd_doorsvc_t;

static int smbd_dop_null(smbd_arg_t *);
static int smbd_dop_async_response(smbd_arg_t *);
static int smbd_dop_user_auth_logon(smbd_arg_t *);
static int smbd_dop_user_nonauth_logon(smbd_arg_t *);
static int smbd_dop_user_auth_logoff(smbd_arg_t *);
static int smbd_dop_lookup_sid(smbd_arg_t *);
static int smbd_dop_lookup_name(smbd_arg_t *);
static int smbd_dop_join(smbd_arg_t *);
static int smbd_dop_get_dcinfo(smbd_arg_t *);
static int smbd_dop_vss_get_count(smbd_arg_t *);
static int smbd_dop_vss_get_snapshots(smbd_arg_t *);
static int smbd_dop_vss_map_gmttoken(smbd_arg_t *);
static int smbd_dop_ads_find_host(smbd_arg_t *);
static int smbd_dop_quota_query(smbd_arg_t *);
static int smbd_dop_quota_set(smbd_arg_t *);
static int smbd_dop_dfs_get_referrals(smbd_arg_t *);
static int smbd_dop_shr_hostaccess(smbd_arg_t *);
static int smbd_dop_shr_exec(smbd_arg_t *);
static int smbd_dop_notify_dc_changed(smbd_arg_t *);

typedef int (*smbd_dop_t)(smbd_arg_t *);

typedef struct smbd_doorop {
	smb_dopcode_t	opcode;
	smbd_dop_t	op;
} smbd_doorop_t;

smbd_doorop_t smbd_doorops[] = {
	{ SMB_DR_NULL,			smbd_dop_null },
	{ SMB_DR_ASYNC_RESPONSE,	smbd_dop_async_response },
	{ SMB_DR_USER_AUTH_LOGON,	smbd_dop_user_auth_logon },
	{ SMB_DR_USER_NONAUTH_LOGON,	smbd_dop_user_nonauth_logon },
	{ SMB_DR_USER_AUTH_LOGOFF,	smbd_dop_user_auth_logoff },
	{ SMB_DR_LOOKUP_SID,		smbd_dop_lookup_sid },
	{ SMB_DR_LOOKUP_NAME,		smbd_dop_lookup_name },
	{ SMB_DR_JOIN,			smbd_dop_join },
	{ SMB_DR_GET_DCINFO,		smbd_dop_get_dcinfo },
	{ SMB_DR_VSS_GET_COUNT,		smbd_dop_vss_get_count },
	{ SMB_DR_VSS_GET_SNAPSHOTS,	smbd_dop_vss_get_snapshots },
	{ SMB_DR_VSS_MAP_GMTTOKEN,	smbd_dop_vss_map_gmttoken },
	{ SMB_DR_ADS_FIND_HOST,		smbd_dop_ads_find_host },
	{ SMB_DR_QUOTA_QUERY,		smbd_dop_quota_query },
	{ SMB_DR_QUOTA_SET,		smbd_dop_quota_set },
	{ SMB_DR_DFS_GET_REFERRALS,	smbd_dop_dfs_get_referrals },
	{ SMB_DR_SHR_HOSTACCESS,	smbd_dop_shr_hostaccess },
	{ SMB_DR_SHR_EXEC,		smbd_dop_shr_exec },
	{ SMB_DR_NOTIFY_DC_CHANGED,	smbd_dop_notify_dc_changed },
	{ SMB_DR_LOOKUP_LSID,		smbd_dop_lookup_sid },
	{ SMB_DR_LOOKUP_LNAME,		smbd_dop_lookup_name }
};

static int smbd_ndoorop = (sizeof (smbd_doorops) / sizeof (smbd_doorops[0]));

static smbd_doorsvc_t smbd_doorsvc;
static int smbd_door_fd = -1;
static int smbd_door_cookie = 0x534D4244;	/* SMBD */
static smbd_door_t smbd_door_sdh;
static char *smbd_door_name = NULL;

static void smbd_door_dispatch(void *, char *, size_t, door_desc_t *, uint_t);
static int smbd_door_dispatch_async(smbd_arg_t *);
static void smbd_door_release_async(smbd_arg_t *);

/*
 * Start the smbd door service.  Create and bind to a door.
 * Returns 0 on success. Otherwise, -1.
 */
int
smbd_door_start(void)
{
	int	newfd;

	(void) mutex_lock(&smbd_doorsvc.sd_mutex);

	if (smbd_door_fd != -1) {
		(void) fprintf(stderr, "smb_doorsrv_start: already started");
		(void) mutex_unlock(&smbd_doorsvc.sd_mutex);
		return (-1);
	}

	smbd_door_name = getenv("SMBD_DOOR_NAME");
	if (smbd_door_name == NULL)
		smbd_door_name = SMBD_DOOR_NAME;

	smbd_door_init(&smbd_door_sdh, "doorsrv");

	list_create(&smbd_doorsvc.sd_async_list, sizeof (smbd_arg_t),
	    offsetof(smbd_arg_t, lnd));
	smbd_doorsvc.sd_async_count = 0;

	if ((smbd_door_fd = door_create(smbd_door_dispatch,
	    &smbd_door_cookie, DOOR_UNREF)) < 0) {
		(void) fprintf(stderr, "smb_doorsrv_start: door_create: %s",
		    strerror(errno));
		smbd_door_fd = -1;
		(void) mutex_unlock(&smbd_doorsvc.sd_mutex);
		return (-1);
	}

	(void) unlink(smbd_door_name);

	if ((newfd = creat(smbd_door_name, 0644)) < 0) {
		(void) fprintf(stderr, "smb_doorsrv_start: open: %s",
		    strerror(errno));
		(void) door_revoke(smbd_door_fd);
		smbd_door_fd = -1;
		(void) mutex_unlock(&smbd_doorsvc.sd_mutex);
		return (-1);
	}

	(void) close(newfd);
	(void) fdetach(smbd_door_name);

	if (fattach(smbd_door_fd, smbd_door_name) < 0) {
		(void) fprintf(stderr, "smb_doorsrv_start: fattach: %s",
		    strerror(errno));
		(void) door_revoke(smbd_door_fd);
		smbd_door_fd = -1;
		(void) mutex_unlock(&smbd_doorsvc.sd_mutex);
		return (-1);
	}

	(void) mutex_unlock(&smbd_doorsvc.sd_mutex);
	return (smbd_door_fd);
}

/*
 * Stop the smbd door service.
 */
void
smbd_door_stop(void)
{
	(void) mutex_lock(&smbd_doorsvc.sd_mutex);

	smbd_door_fini(&smbd_door_sdh);

	if (smbd_door_name)
		(void) fdetach(smbd_door_name);

	if (smbd_door_fd != -1) {
		(void) door_revoke(smbd_door_fd);
		smbd_door_fd = -1;
	}

	(void) mutex_unlock(&smbd_doorsvc.sd_mutex);
}

/*ARGSUSED*/
static void
smbd_door_dispatch(void *cookie, char *argp, size_t arg_size, door_desc_t *dp,
    uint_t n_desc)
{
	smbd_arg_t	dop_arg;
	smb_doorhdr_t	*hdr;
	size_t		hdr_size;
	char		*rbuf = NULL;

	smbd_door_enter(&smbd_door_sdh);

	if (!smbd_online())
		smbd_door_return(&smbd_door_sdh, NULL, 0, NULL, 0);

	bzero(&dop_arg, sizeof (smbd_arg_t));
	hdr = &dop_arg.hdr;
	hdr_size = xdr_sizeof(smb_doorhdr_xdr, hdr);

	if ((cookie != &smbd_door_cookie) || (argp == NULL) ||
	    (arg_size < hdr_size)) {
		smbd_door_return(&smbd_door_sdh, NULL, 0, NULL, 0);
	}

	if (smb_doorhdr_decode(hdr, (uint8_t *)argp, hdr_size) == -1) {
		syslog(LOG_DEBUG, "smbd_door_dispatch: header decode failed");
		smbd_door_return(&smbd_door_sdh, NULL, 0, NULL, 0);
	}

	if ((hdr->dh_magic != SMB_DOOR_HDR_MAGIC) || (hdr->dh_txid == 0)) {
		syslog(LOG_DEBUG, "smbd_door_dispatch: invalid header");
		smbd_door_return(&smbd_door_sdh, NULL, 0, NULL, 0);
	}

	dop_arg.opname = smb_doorhdr_opname(hdr->dh_op);
	dop_arg.data = argp + hdr_size;
	dop_arg.datalen = hdr->dh_datalen;

	if (hdr->dh_op == SMB_DR_ASYNC_RESPONSE) {
		/*
		 * ASYNC_RESPONSE is used to collect the response
		 * to an async call; it cannot be an async call.
		 */
		hdr->dh_flags &= ~SMB_DF_ASYNC;
	}

	if (hdr->dh_flags & SMB_DF_ASYNC) {
		if (smbd_door_dispatch_async(&dop_arg) == 0)
			hdr->dh_door_rc = SMB_DOP_SUCCESS;
		else
			hdr->dh_door_rc = SMB_DOP_NOT_CALLED;
	} else {
		(void) smbd_door_dispatch_op(&dop_arg);
	}

	if ((rbuf = (char *)alloca(dop_arg.rsize + hdr_size)) == NULL) {
		errno = ENOMEM;
		syslog(LOG_DEBUG, "smbd_door_dispatch[%s]: alloca %m",
		    dop_arg.opname);
		smbd_door_return(&smbd_door_sdh, NULL, 0, NULL, 0);
	}

	if (dop_arg.rbuf != NULL) {
		(void) memcpy(rbuf + hdr_size, dop_arg.rbuf, dop_arg.rsize);
		free(dop_arg.rbuf);
	}

	hdr->dh_datalen = dop_arg.rsize;
	(void) smb_doorhdr_encode(hdr, (uint8_t *)rbuf, hdr_size);
	dop_arg.rsize += hdr_size;

	smbd_door_return(&smbd_door_sdh, rbuf, dop_arg.rsize, NULL, 0);
	/*NOTREACHED*/
}

/*
 * Launch a thread to process an asynchronous door call.
 */
static int
smbd_door_dispatch_async(smbd_arg_t *req_arg)
{
	smbd_arg_t	*arg = NULL;
	char		*data = NULL;
	pthread_attr_t	attr;
	pthread_t	tid;
	int		rc;

	if ((req_arg->hdr.dh_flags & SMB_DF_ASYNC) == 0) {
		errno = EINVAL;
		return (-1);
	}

	if ((arg = malloc(sizeof (smbd_arg_t))) == NULL) {
		syslog(LOG_DEBUG, "smbd_door_dispatch_async[%s]: %m",
		    req_arg->opname);
		return (-1);
	}

	(void) memcpy(arg, req_arg, sizeof (smbd_arg_t));
	arg->data = NULL;

	if (req_arg->datalen != 0) {
		if ((data = malloc(req_arg->datalen)) == NULL) {
			free(arg);
			syslog(LOG_DEBUG, "smbd_door_dispatch_async[%s]: %m",
			    req_arg->opname);
			return (-1);
		}

		(void) memcpy(data, req_arg->data, req_arg->datalen);
		arg->data = data;
	}

	(void) mutex_lock(&smbd_doorsvc.sd_mutex);
	arg->magic = SMBD_ARG_MAGIC;
	list_insert_tail(&smbd_doorsvc.sd_async_list, arg);
	++smbd_doorsvc.sd_async_count;
	(void) mutex_unlock(&smbd_doorsvc.sd_mutex);

	(void) pthread_attr_init(&attr);
	(void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	rc = pthread_create(&tid, &attr, smbd_door_dispatch_op, arg);
	(void) pthread_attr_destroy(&attr);

	if (rc != 0) {
		(void) mutex_lock(&smbd_doorsvc.sd_mutex);
		smbd_door_release_async(arg);
		(void) mutex_unlock(&smbd_doorsvc.sd_mutex);
	}

	return (rc);
}

/*
 * Remove an entry from the async response pending list and free
 * the arg and associated data.
 *
 * Must only be called while holding the smbd_doorsvc mutex.
 */
static void
smbd_door_release_async(smbd_arg_t *arg)
{
	if (arg != NULL) {
		assert(arg->magic == SMBD_ARG_MAGIC);
		arg->magic = (uint32_t)~SMBD_ARG_MAGIC;

		list_remove(&smbd_doorsvc.sd_async_list, arg);
		--smbd_doorsvc.sd_async_count;
		free(arg->data);
		arg->data = NULL;
		free(arg);
	}
}

/*
 * All door calls are processed here: synchronous or asynchronous:
 * - synchronous calls are invoked by direct function call
 * - asynchronous calls are invoked from a launched thread
 *
 * If the kernel has attempted to collect a response before the op
 * has completed, the arg will have been marked as response_abort
 * and we can discard the response data and release the arg.
 *
 * We send a notification when asynchronous (ASYNC) door calls
 * from the kernel (SYSSPACE) have completed.
 */
void *
smbd_door_dispatch_op(void *thread_arg)
{
	smbd_arg_t	*arg = (smbd_arg_t *)thread_arg;
	smbd_doorop_t	*doorop;
	smb_doorhdr_t	*hdr;
	int		i;

	if ((!smbd_online()) || arg == NULL)
		return (NULL);

	hdr = &arg->hdr;
	arg->opname = smb_doorhdr_opname(hdr->dh_op);

	for (i = 0; i < smbd_ndoorop; ++i) {
		doorop = &smbd_doorops[i];

		if (hdr->dh_op == doorop->opcode) {
			hdr->dh_door_rc = doorop->op(arg);
			hdr->dh_status = arg->status;

			if ((hdr->dh_flags & SMB_DF_SYSSPACE) &&
			    (hdr->dh_flags & SMB_DF_ASYNC)) {
				assert(hdr->dh_op != SMB_DR_ASYNC_RESPONSE);

				(void) mutex_lock(&smbd_doorsvc.sd_mutex);
				if (arg->response_abort) {
					free(arg->rbuf);
					arg->rbuf = NULL;
					smbd_door_release_async(arg);
				} else {
					arg->response_ready = B_TRUE;
				}
				(void) mutex_unlock(&smbd_doorsvc.sd_mutex);

				(void) smb_kmod_event_notify(hdr->dh_txid);
			}

			return (NULL);
		}
	}

	syslog(LOG_ERR, "smbd_door_dispatch_op[%s]: invalid op %u",
	    arg->opname, hdr->dh_op);
	return (NULL);
}

/*
 * Wrapper for door_return.  smbd_door_enter() increments a reference count
 * when a door call is dispatched and smbd_door_return() decrements the
 * reference count when it completes.
 *
 * The reference counting is used in smbd_door_fini() to wait for active
 * calls to complete before closing the door.
 */
void
smbd_door_init(smbd_door_t *sdh, const char *name)
{
	(void) strlcpy(sdh->sd_name, name, sizeof (sdh->sd_name));
}

void
smbd_door_enter(smbd_door_t *sdh)
{
	(void) mutex_lock(&sdh->sd_mutex);
	++sdh->sd_ncalls;
	(void) mutex_unlock(&sdh->sd_mutex);
}

/*
 * We have two calls to door_return because the first call (with data)
 * can fail, which can leave the door call blocked here.  The second
 * call (with NULL) is guaranteed to unblock and return to the caller.
 */
void
smbd_door_return(smbd_door_t *sdh, char *data_ptr, size_t data_size,
    door_desc_t *desc_ptr, uint_t num_desc)
{
	(void) mutex_lock(&sdh->sd_mutex);

	if (sdh->sd_ncalls == 0)
		syslog(LOG_ERR, "smbd_door_return[%s]: unexpected count=0",
		    sdh->sd_name);
	else
		--sdh->sd_ncalls;

	(void) cond_broadcast(&sdh->sd_cv);
	(void) mutex_unlock(&sdh->sd_mutex);

	(void) door_return(data_ptr, data_size, desc_ptr, num_desc);
	(void) door_return(NULL, 0, NULL, 0);
	/* NOTREACHED */
}

/*
 * A door service is about to terminate.
 * Give active requests a small grace period to complete.
 */
void
smbd_door_fini(smbd_door_t *sdh)
{
	timestruc_t	delay;
	int		rc = 0;

	(void) mutex_lock(&sdh->sd_mutex);

	while (rc != ETIME && sdh->sd_ncalls != 0) {
		delay.tv_sec = 1;
		delay.tv_nsec = 0;
		rc = cond_reltimedwait(&sdh->sd_cv, &sdh->sd_mutex, &delay);
	}

	if (sdh->sd_ncalls != 0)
		syslog(LOG_NOTICE, "smbd_door_fini[%s]: %d remaining",
		    sdh->sd_name, sdh->sd_ncalls);

	(void) mutex_unlock(&sdh->sd_mutex);
}

/*
 * Null door operation: always returns success.
 * Assumes no request or response data.
 */
/*ARGSUSED*/
static int
smbd_dop_null(smbd_arg_t *arg)
{
	return (SMB_DOP_SUCCESS);
}

/*
 * Async response handler: setup the rbuf and rsize for the specified
 * transaction.  This function is used by the kernel to collect the
 * response half of an asynchronous door call.
 *
 * If a door client attempts to collect a response before the op has
 * completed (!response_ready), mark the arg as response_abort and
 * set an error.  The response will be discarded when the op completes.
 */
static int
smbd_dop_async_response(smbd_arg_t *rsp_arg)
{
	list_t		*arg_list = &smbd_doorsvc.sd_async_list;
	smbd_arg_t	*arg;

	(void) mutex_lock(&smbd_doorsvc.sd_mutex);
	arg = list_head(arg_list);

	while (arg != NULL) {
		assert(arg->magic == SMBD_ARG_MAGIC);

		if (arg->hdr.dh_txid == rsp_arg->hdr.dh_txid) {
			if (!arg->response_ready) {
				arg->response_abort = B_TRUE;
				rsp_arg->hdr.dh_door_rc = SMB_DOP_NOT_CALLED;
				syslog(LOG_NOTICE, "doorsvc[%s]: %u not ready",
				    arg->opname, arg->hdr.dh_txid);
				break;
			}

			rsp_arg->rbuf = arg->rbuf;
			rsp_arg->rsize = arg->rsize;
			arg->rbuf = NULL;
			arg->rsize = 0;
			smbd_door_release_async(arg);
			break;
		}

		arg = list_next(arg_list, arg);
	}

	(void) mutex_unlock(&smbd_doorsvc.sd_mutex);
	return (SMB_DOP_SUCCESS);
}

static int
smbd_dop_user_nonauth_logon(smbd_arg_t *arg)
{
	uint32_t	sid = 0;

	if (smb_common_decode(arg->data, arg->datalen,
	    xdr_uint32_t, &sid) != 0)
		return (SMB_DOP_DECODE_ERROR);

	smbd_user_nonauth_logon(sid);
	return (SMB_DOP_SUCCESS);
}

static int
smbd_dop_user_auth_logoff(smbd_arg_t *arg)
{
	uint32_t	sid = 0;

	if (smb_common_decode(arg->data, arg->datalen,
	    xdr_uint32_t, &sid) != 0)
		return (SMB_DOP_DECODE_ERROR);

	smbd_user_auth_logoff(sid);
	return (SMB_DOP_SUCCESS);
}

/*
 * Obtains an access token on successful user authentication.
 */
static int
smbd_dop_user_auth_logon(smbd_arg_t *arg)
{
	_NOTE(ARGUNUSED(arg))

	/* No longer used */
	return (SMB_DOP_EMPTYBUF);
}

/*
 * SMB_DR_LOOKUP_NAME,
 * SMB_DR_LOOKUP_LNAME (local-only, for idmap)
 */
static int
smbd_dop_lookup_name(smbd_arg_t *arg)
{
	smb_domain_t	dinfo;
	smb_account_t	ainfo;
	lsa_account_t	acct;
	char		buf[MAXNAMELEN];

	bzero(&acct, sizeof (lsa_account_t));

	if (smb_common_decode(arg->data, arg->datalen,
	    lsa_account_xdr, &acct) != 0)
		return (SMB_DOP_DECODE_ERROR);

	if (*acct.a_domain == '\0')
		(void) snprintf(buf, MAXNAMELEN, "%s", acct.a_name);
	else if (strchr(acct.a_domain, '.') != NULL)
		(void) snprintf(buf, MAXNAMELEN, "%s@%s", acct.a_name,
		    acct.a_domain);
	else
		(void) snprintf(buf, MAXNAMELEN, "%s\\%s", acct.a_domain,
		    acct.a_name);

	switch (arg->hdr.dh_op) {
	case SMB_DR_LOOKUP_NAME:
		acct.a_status = lsa_lookup_name(buf, acct.a_sidtype, &ainfo);
		break;

	case SMB_DR_LOOKUP_LNAME:
		/*
		 * Basically for idmap.  Don't call out to AD.
		 */
		acct.a_status = lsa_lookup_lname(buf, acct.a_sidtype, &ainfo);
		break;

	default:
		assert(!"arg->hdr.dh_op");
		acct.a_status = NT_STATUS_INTERNAL_ERROR;
		break;
	}

	if (acct.a_status == NT_STATUS_SUCCESS) {
		acct.a_sidtype = ainfo.a_type;
		smb_sid_tostr(ainfo.a_sid, acct.a_sid);
		(void) strlcpy(acct.a_name, ainfo.a_name, MAXNAMELEN);

		if (smb_domain_lookup_name(ainfo.a_domain, &dinfo))
			(void) strlcpy(acct.a_domain, dinfo.di_fqname,
			    MAXNAMELEN);
		else
			(void) strlcpy(acct.a_domain, ainfo.a_domain,
			    MAXNAMELEN);
		smb_account_free(&ainfo);
	}

	arg->rbuf = smb_common_encode(&acct, lsa_account_xdr, &arg->rsize);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

/*
 * SMB_DR_LOOKUP_SID,
 * SMB_DR_LOOKUP_LSID (local-only, for idmap)
 */
static int
smbd_dop_lookup_sid(smbd_arg_t *arg)
{
	smb_domain_t	dinfo;
	smb_account_t	ainfo;
	lsa_account_t	acct;
	smb_sid_t	*sid;

	bzero(&acct, sizeof (lsa_account_t));

	if (smb_common_decode(arg->data, arg->datalen,
	    lsa_account_xdr, &acct) != 0)
		return (SMB_DOP_DECODE_ERROR);

	sid = smb_sid_fromstr(acct.a_sid);

	switch (arg->hdr.dh_op) {
	case SMB_DR_LOOKUP_SID:
		acct.a_status = lsa_lookup_sid(sid, &ainfo);
		break;

	case SMB_DR_LOOKUP_LSID:
		/*
		 * Basically for idmap.  Don't call out to AD.
		 */
		acct.a_status = lsa_lookup_lsid(sid, &ainfo);
		break;

	default:
		assert(!"arg->hdr.dh_op");
		acct.a_status = NT_STATUS_INTERNAL_ERROR;
		break;
	}

	smb_sid_free(sid);

	if (acct.a_status == NT_STATUS_SUCCESS) {
		acct.a_sidtype = ainfo.a_type;
		smb_sid_tostr(ainfo.a_sid, acct.a_sid);
		(void) strlcpy(acct.a_name, ainfo.a_name, MAXNAMELEN);

		if (smb_domain_lookup_name(ainfo.a_domain, &dinfo))
			(void) strlcpy(acct.a_domain, dinfo.di_fqname,
			    MAXNAMELEN);
		else
			(void) strlcpy(acct.a_domain, ainfo.a_domain,
			    MAXNAMELEN);

		smb_account_free(&ainfo);
	}

	arg->rbuf = smb_common_encode(&acct, lsa_account_xdr, &arg->rsize);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

static int
smbd_dop_join(smbd_arg_t *arg)
{
	smb_joininfo_t	jdi;
	smb_joinres_t	jdres;

	bzero(&jdi, sizeof (smb_joininfo_t));
	bzero(&jdres, sizeof (smb_joinres_t));

	if (smb_common_decode(arg->data, arg->datalen,
	    smb_joininfo_xdr, &jdi) != 0)
		return (SMB_DOP_DECODE_ERROR);

	smbd_join(&jdi, &jdres);

	arg->rbuf = smb_common_encode(&jdres, smb_joinres_xdr, &arg->rsize);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

static int
smbd_dop_get_dcinfo(smbd_arg_t *arg)
{
	smb_domainex_t	dxi;

	if (!smb_domain_getinfo(&dxi))
		return (SMB_DOP_EMPTYBUF);

	arg->rbuf = smb_string_encode(dxi.d_dci.dc_name, &arg->rsize);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

/*
 * Return the number of snapshots for a dataset
 */
static int
smbd_dop_vss_get_count(smbd_arg_t *arg)
{
	smb_string_t	path;
	uint32_t	count;

	bzero(&path, sizeof (smb_string_t));
	arg->rbuf = NULL;

	if (smb_string_decode(&path, arg->data, arg->datalen) != 0)
		return (SMB_DOP_DECODE_ERROR);

	if (smbd_vss_get_count(path.buf, &count) == 0)
		arg->rbuf = smb_common_encode(&count, xdr_uint32_t,
		    &arg->rsize);

	xdr_free(smb_string_xdr, (char *)&path);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

/*
 * Return the count and list of snapshots.
 * The list is in @GMT token format.
 */
static int
smbd_dop_vss_get_snapshots(smbd_arg_t *arg)
{
	char				**gmtp;
	smb_gmttoken_query_t		request;
	smb_gmttoken_response_t		reply;
	uint_t				i;

	bzero(&request, sizeof (smb_gmttoken_query_t));
	bzero(&reply, sizeof (smb_gmttoken_response_t));

	if (smb_common_decode(arg->data, arg->datalen,
	    smb_gmttoken_query_xdr, &request) != 0)
		return (SMB_DOP_DECODE_ERROR);

	reply.gtr_gmttokens.gtr_gmttokens_val = malloc(request.gtq_count *
	    sizeof (char *));
	bzero(reply.gtr_gmttokens.gtr_gmttokens_val, request.gtq_count *
	    sizeof (char *));

	if (reply.gtr_gmttokens.gtr_gmttokens_val == NULL) {
		xdr_free(smb_gmttoken_query_xdr, (char *)&request);
		return (SMB_DOP_EMPTYBUF);
	}

	smbd_vss_get_snapshots(request.gtq_path, request.gtq_count,
	    &reply.gtr_count,
	    &reply.gtr_gmttokens.gtr_gmttokens_len,
	    reply.gtr_gmttokens.gtr_gmttokens_val);

	arg->rbuf = smb_common_encode(&reply, smb_gmttoken_response_xdr,
	    &arg->rsize);
	if (arg->rbuf == NULL) {
		xdr_free(smb_gmttoken_query_xdr, (char *)&request);
		return (SMB_DOP_ENCODE_ERROR);
	}

	for (i = 0, gmtp = reply.gtr_gmttokens.gtr_gmttokens_val;
	    (i < request.gtq_count); i++) {
		if (*gmtp)
			free(*gmtp);
		gmtp++;
	}

	free(reply.gtr_gmttokens.gtr_gmttokens_val);
	xdr_free(smb_gmttoken_query_xdr, (char *)&request);
	return (SMB_DOP_SUCCESS);
}

/*
 * Return the name of the snapshot that matches the dataset path
 * and @GMT token.
 */
static int
smbd_dop_vss_map_gmttoken(smbd_arg_t *arg)
{
	char			*snapname;
	smb_gmttoken_snapname_t	request;

	bzero(&request, sizeof (smb_gmttoken_snapname_t));

	if (smb_common_decode(arg->data, arg->datalen,
	    smb_gmttoken_snapname_xdr, &request) != 0) {
		xdr_free(smb_gmttoken_snapname_xdr, (char *)&request);
		return (SMB_DOP_DECODE_ERROR);
	}

	if ((snapname = malloc(MAXPATHLEN)) == NULL) {
		xdr_free(smb_gmttoken_snapname_xdr, (char *)&request);
		return (SMB_DOP_ENCODE_ERROR);
	}

	if ((smbd_vss_map_gmttoken(request.gts_path, request.gts_gmttoken,
	    request.gts_toktime, snapname) != 0)) {
		*snapname = '\0';
	}

	arg->rbuf = smb_string_encode(snapname, &arg->rsize);
	xdr_free(smb_gmttoken_snapname_xdr, (char *)&request);
	free(snapname);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

static int
smbd_dop_ads_find_host(smbd_arg_t *arg)
{
	smb_ads_host_info_t	*hinfo = NULL;
	char			*hostname = "";
	smb_string_t		fqdn;

	bzero(&fqdn, sizeof (smb_string_t));

	if (smb_string_decode(&fqdn, arg->data, arg->datalen) != 0)
		return (SMB_DOP_DECODE_ERROR);

	if ((hinfo = smb_ads_find_host(fqdn.buf)) != NULL)
		hostname = hinfo->name;

	xdr_free(smb_string_xdr, (char *)&fqdn);

	arg->rbuf = smb_string_encode(hostname, &arg->rsize);
	free(hinfo);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

/*
 * Query the list of user/group quota entries for a given filesystem.
 */
static int
smbd_dop_quota_query(smbd_arg_t *arg)
{
	smb_quota_query_t	request;
	smb_quota_response_t	reply;
	uint32_t		status;

	bzero(&request, sizeof (smb_quota_query_t));
	bzero(&reply, sizeof (smb_quota_response_t));

	if (smb_common_decode(arg->data, arg->datalen,
	    smb_quota_query_xdr, &request) != 0)
		return (SMB_DOP_DECODE_ERROR);

	status = smb_quota_query(&request, &reply);
	reply.qr_status = status;

	arg->rbuf = smb_common_encode(&reply, smb_quota_response_xdr,
	    &arg->rsize);

	xdr_free(smb_quota_query_xdr, (char *)&request);
	smb_quota_free(&reply);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

/*
 * Set a list of user/group quota entries for a given filesystem.
 */
static int
smbd_dop_quota_set(smbd_arg_t *arg)
{
	smb_quota_set_t	request;
	uint32_t	status = 0;

	bzero(&request, sizeof (smb_quota_set_t));

	if (smb_common_decode(arg->data, arg->datalen,
	    smb_quota_set_xdr, &request) != 0)
		return (SMB_DOP_DECODE_ERROR);

	status = smb_quota_set(&request);

	arg->rbuf = smb_common_encode(&status, xdr_uint32_t, &arg->rsize);
	xdr_free(smb_quota_set_xdr, (char *)&request);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

static int
smbd_dop_dfs_get_referrals(smbd_arg_t *arg)
{
	dfs_referral_query_t	request;
	dfs_referral_response_t	reply;

	bzero(&request, sizeof (request));
	bzero(&reply, sizeof (reply));

	if (smb_common_decode(arg->data, arg->datalen,
	    dfs_referral_query_xdr, &request) != 0)
		return (SMB_DOP_DECODE_ERROR);

	reply.rp_status = dfs_get_referrals((const char *)request.rq_path,
	    request.rq_type, &reply.rp_referrals);

	if (reply.rp_status != ERROR_SUCCESS)
		bzero(&reply.rp_referrals, sizeof (dfs_info_t));

	arg->rbuf = smb_common_encode(&reply, dfs_referral_response_xdr,
	    &arg->rsize);

	if (reply.rp_status == ERROR_SUCCESS)
		dfs_info_free(&reply.rp_referrals);

	xdr_free(dfs_referral_query_xdr, (char *)&request);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

static int
smbd_dop_shr_hostaccess(smbd_arg_t *arg)
{
	smb_shr_hostaccess_query_t request;
	uint32_t reply;

	bzero(&request, sizeof (request));
	bzero(&reply, sizeof (reply));

	if (smb_common_decode(arg->data, arg->datalen,
	    smb_shr_hostaccess_query_xdr, &request) != 0)
		return (SMB_DOP_DECODE_ERROR);

	reply = smb_shr_hostaccess(&request.shq_ipaddr, request.shq_none,
	    request.shq_ro, request.shq_rw, request.shq_flag);

	arg->rbuf = smb_common_encode(&reply, xdr_uint32_t, &arg->rsize);

	xdr_free(smb_shr_hostaccess_query_xdr, (char *)&request);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

static int
smbd_dop_shr_exec(smbd_arg_t *arg)
{
	smb_shr_execinfo_t request;
	int reply;

	bzero(&request, sizeof (request));
	bzero(&reply, sizeof (reply));

	if (smb_common_decode(arg->data, arg->datalen,
	    smb_shr_execinfo_xdr, &request) != 0)
		return (SMB_DOP_DECODE_ERROR);

	reply = smb_shr_exec(&request);

	if (reply != 0)
		syslog(LOG_NOTICE, "Failed to execute %s command",
		    (request.e_type == SMB_EXEC_MAP) ? "map" : "unmap");

	arg->rbuf = smb_common_encode(&reply, xdr_int, &arg->rsize);

	xdr_free(smb_shr_execinfo_xdr, (char *)&request);

	if (arg->rbuf == NULL)
		return (SMB_DOP_ENCODE_ERROR);
	return (SMB_DOP_SUCCESS);
}

/* ARGSUSED */
static int
smbd_dop_notify_dc_changed(smbd_arg_t *arg)
{

	smbd_dc_monitor_refresh();

	return (SMB_DOP_SUCCESS);
}