|
root / base / usr / src / cmd / smbsrv / fksmbd
fksmbd Plain Text 1239 lines 29.9 KB
   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
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238

#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
#

# set -o emacs

setenv SMBD_DOOR_NAME /tmp/fksmbd_door
setenv SMB_SHARE_DNAME /tmp/fksmbshare_door

# fksmbd is always 32-bit
setenv LD_LIBRARY_PATH ${ROOT}/usr/lib/smbsrv:${ROOT}/usr/lib:${ROOT}/lib

echo 'Do one of: attach ${PID}'
echo 'or: debug ${ROOT}/usr/lib/smbsrv/fksmbd'

#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2021 Tintri by DDN, Inc. All rights reserved.
#

# gdb script (it allows python :)

python
import os
gdb.execute('file' + os.environ['ROOT'] + '/usr/lib/smbsrv/fksmbd')
gdb.execute('set env ' + 'LD_LIBRARY_PATH ' + \
	os.environ['ROOT'] + '/lib:' + \
	os.environ['ROOT'] + '/usr/lib')
end

echo break debug_enter\n
#
# 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 2015 Nexenta Systems, Inc.  All rights reserved.
#


PROG=	fksmbd
OBJS_SMBD=			\
	smbd_authsvc.o		\
	smbd_doorsvc.o		\
	smbd_join.o		\
	smbd_krb5lookup.o	\
	smbd_krb5ssp.o		\
	smbd_logon.o		\
	smbd_main.o		\
	smbd_nicmon.o		\
	smbd_ntlmssp.o		\
	smbd_pipesvc.o		\
	smbd_share_doorsvc.o	\
	smbd_spool.o		\
	smbd_vss.o		\


OBJS_LOCAL =			\
	fksmbd_door.o		\
	fksmbd_kmod.o		\
	fksmbd_ksock.o		\
	fksmbd_log.o		\
	fksmbd_shr.o

OBJS=	${OBJS_SMBD} ${OBJS_LOCAL}
SRCS=	${OBJS_SMBD:%.o=../smbd/%.c} \
	${OBJS_LOCAL:.o=.c}

include ../../Makefile.cmd
include ../../Makefile.ctf

# Hammerhead: Include mech_krb5 definitions for direct linking.
# libkrb5.so is a filter library; GNU ld cannot resolve symbols through it.
include $(SRC)/lib/gss_mechs/mech_krb5/Makefile.mech_krb5

# Note: need our sys includes _before_ ENVCPPFLAGS, proto etc.
CPPFLAGS.first += -I../../../lib/smbsrv/libfksmbsrv/common

INCS += -I../smbd
INCS +=	-I../../../uts/common
INCS +=	-I../../../uts/common/smbsrv
INCS +=	-I../../../common/smbsrv

# Should not have to do this, but the Kerberos includes are a mess.
INCS += -I $(ROOT)/usr/include/kerberosv5

CSTD=	$(CSTD_GNU99)

CFLAGS += $(CCVERBOSE)
CFLAGS64 += $(CCVERBOSE)
# Hammerhead: smbd_krb5ssp.c uses pointer-to-int casts
CERRWARN += -Wno-pointer-to-int-cast
CPPFLAGS += -D_REENTRANT
CPPFLAGS += -Dsyslog=smb_syslog
CPPFLAGS += -D_LARGEFILE64_SOURCE=1
CPPFLAGS += -DFKSMBD
# Always debug here
CPPFLAGS += -DDEBUG
CPPFLAGS += $(INCS)

LDFLAGS += $(ZNOLAZYLOAD)
LDFLAGS += '-R$$ORIGIN' '-R$$ORIGIN/..'
LDLIBS += -L$(ROOT)/usr/lib/smbsrv
LDLIBS += -lfksmbsrv -lfakekernel
# prefer to keep libs ordered by dependence
LDLIBS += -lmlsvc -lmlrpc -lsmbns -lsmb -lsmbfs -lgss
LDLIBS += -lzfs -lbsm -lscf -lcmdutils -lsocket -lnsl -lumem
# Hammerhead: Link directly against mech_krb5.so instead of filter lib -lkrb5
$(PROG) : LDLIBS += $(KMECHLIB)
$(PROG) : LDFLAGS += $(KRUNPATH) -Wl,-rpath-link,$(ROOT_KLIBDIR)

ROOTSMBDDIR = $(ROOTLIB)/smbsrv
ROOTSMBDFILE = $(PROG:%=$(ROOTSMBDDIR)/%)

.KEEP_STATE:

all: $(PROG)

$(PROG): $(OBJS)
	$(LINK.c) -o $(PROG) $(OBJS) $(LDLIBS)
	$(POST_PROCESS)

clean:
	-$(RM) $(OBJS)

include ../../Makefile.targ

install: all $(ROOTSMBDFILE)

%.o: ../smbd/%.c
	$(COMPILE.c) $<
	$(POST_PROCESS_O)

$(ROOTSMBDDIR)/%: %
	$(INS.file)

#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
#

This directory builds a program linking all of the SMB server code
into a user-level process.  The result is not a fully functional
SMB server but is very useful for some kinds of development work.

The architecture of this roughly parallels the in-kernel version,
where the fksmbd program corresponds to the real smbd, the library
libfksmbsrv corresponds to the smbsrv kernel module, and all the
required kernel interfaces are simulated in libfakekernel.

Just as with the kernel code, there are mdb modules that know
how to walk data structures in libfksmbsrv, etc.

For debugging, etc. it's easiest to run this as a normal user,
i.e. yourself (not root); but before you can do that, there are
some prerequisites to take care of:

  a: Install ../bind-helper in /usr/lib/smbsrv/.
     It needs to be either setuid root (easiest) or added to
     the exec_attr as described in the top of bind-helper.c
     (This must be in /usr/lib/smbsrv, not the proto area.)

  b: Setup an SMB account for guest, i.e.
     Add pam_smb_passwd.so.1 to pam.conf if not already there
     (other password required pam_smb_passwd.so.1 nowarn)
     % useradd guest
     % smbadm enable-user guest
     % passwd guest    (sets the SMB p/w hash too)
     Make sure guest shows in /var/smb/smbpasswd

  c: chown/chmod the directories: /var/smb /var/run/smb
     to yourself, mode 755.  Reboot will chown these
     back to root, so repeat this after a reboot.

Now you can run fksmbd from the proto area using this script:
  ./Run.sh -df

You can also run it under dbx (see the .dbxrc file).
To run it under mdb (with mdb modules build here):
  mdb -L $ROOT/usr/lib/mdb/proc:/usr/lib/mdb/proc ...
where ... is one of: fksmbd, core.nnn, -p $PID

There are also some dtrace scripts in here, and in ../dtrace
for watching either all activity or only selected areas.
Run these like:  dtrace -s Watch-all.d -p $PID -o output

These two (from over in ../dtrace) also work with fksmbd:
	dtrace -s smbd-authsvc.d -p `pgrep fksmbd`  -o output
	dtrace -s smbd-pipesvc.d -p `pgrep fksmbd`  -o output


Here are a couple simple, handy tests you can try:

smbclient -L localhost -U guest%guest

smbclient //localhost/test -U guest%guest -c dir

smbtorture //localhost/test -U guest%guest base
#!/bin/sh

#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2013-2021 Tintri by DDN, Inc. All rights reserved.
#

# Helper program to run fksmbd (user-space smbd for debugging)
# using binaries from the proto area.

[ -n "$ROOT" ] || {
  echo "Need a bldenv to set ROOT=..."
  exit 1;
}

if [[ ! -w /var/smb || ! -w /var/run/smb ]]
then
  echo "Need to create/chown/chmod /var/smb /var/run/smb"
  echo "mkdir -p /var/run/smb"
  echo "chown -R $USER /var/smb /var/run/smb"
  echo "chmod -R a+rw  /var/smb /var/run/smb"
  exit 1;
fi

if [[ ! -r /var/smb/smbpasswd ]]
then
  echo "Need readable /var/smb/smbpasswd, i.e."
  echo 'chgrp staff /var/smb/smbpasswd'
  echo 'chmod 440   /var/smb/smbpasswd'
  exit 1;
fi

if [[ -e /var/smb/.pwd.lock && ! -w /var/smb/.pwd.lock ]]
then
  echo "Need to cleanup /var/smb/.pwd.lock, i.e."
  echo "rm -f /var/smb/.pwd.lock"
  exit 1;
fi

# OK, setup env. to run it.

export SMBD_DOOR_NAME="/tmp/fksmbd_door"
export SMB_SHARE_DNAME="/tmp/fksmbshare_door"

export PATH_PKCS11_CONF=$ROOT/etc/crypto/pkcs11.conf

LD_LIBRARY_PATH=$ROOT/usr/lib/smbsrv:$ROOT/usr/lib:$ROOT/usr/lib:$ROOT/lib
export LD_LIBRARY_PATH

# Enable everything, for debugging
export SMB_MAX_PROTOCOL=311
export SMB_SIGNING=require

# normally runs with cwd=/ but this is more careful
cd /var/smb

# run with the passed options
exec $ROOT/usr/lib/smbsrv/fksmbd "$@"
#!/usr/sbin/dtrace -s
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
 */

/*
 * User-level dtrace for fksmbd
 * Usage: dtrace -s Watch-all.d -p $PID
 */

self int trace;
self int mask;

/*
 * Trace almost everything
 */
pid$target:fksmbd::entry,
pid$target:libfksmbsrv.so.1::entry,
pid$target:libmlsvc.so.1::entry,
pid$target:libmlrpc.so.2::entry,
pid$target:libsmbns.so.1::entry,
pid$target:libsmb.so.1::entry
{
	self->trace++;
}

/*
 * If traced and not masked, print entry/return
 */
pid$target:fksmbd::entry,
pid$target:libfksmbsrv.so.1::entry,
pid$target:libmlsvc.so.1::entry,
pid$target:libmlrpc.so.2::entry,
pid$target:libsmbns.so.1::entry,
pid$target:libsmb.so.1::entry
/self->trace > 0 && self->mask == 0/
{
	printf("\t0x%x", arg0);
	printf("\t0x%x", arg1);
	printf("\t0x%x", arg2);
	printf("\t0x%x", arg3);
	printf("\t0x%x", arg4);
	printf("\t0x%x", arg5);
}

/*
 * Mask (don't print) all function calls below these functions.
 * These make many boring, repetitive function calls like
 * smb_mbtowc, mbc_marshal_...
 */
pid$target::fop__getxvattr:entry,
pid$target::fop__setxvattr:entry,
pid$target::smb_mbc_vdecodef:entry,
pid$target::smb_mbc_vencodef:entry,
pid$target::smb_msgbuf_decode:entry,
pid$target::smb_msgbuf_encode:entry,
pid$target::smb_strlwr:entry,
pid$target::smb_strupr:entry,
pid$target::smb_wcequiv_strlen:entry
{
	self->mask++;
}

/*
 * Now inverses of above, unwind order.
 */

pid$target::fop__getxvattr:return,
pid$target::fop__setxvattr:return,
pid$target::smb_mbc_vdecodef:return,
pid$target::smb_mbc_vencodef:return,
pid$target::smb_msgbuf_decode:return,
pid$target::smb_msgbuf_encode:return,
pid$target::smb_strlwr:return,
pid$target::smb_strupr:return,
pid$target::smb_wcequiv_strlen:return
{
	self->mask--;
}

pid$target:fksmbd::return,
pid$target:libfksmbsrv.so.1::return,
pid$target:libmlsvc.so.1::return,
pid$target:libmlrpc.so.2::return,
pid$target:libsmbns.so.1::return,
pid$target:libsmb.so.1::return
/self->trace > 0 && self->mask == 0/
{
	printf("\t0x%x", arg1);
}

pid$target:fksmbd::return,
pid$target:libfksmbsrv.so.1::return,
pid$target:libmlsvc.so.1::return,
pid$target:libmlrpc.so.2::return,
pid$target:libsmbns.so.1::return,
pid$target:libsmb.so.1::return
{
	self->trace--;
}

/*
 * fksmb dtrace provder
 */

fksmb$target:::smb_start
{
	this->pn = copyinstr(arg0);
	this->sr = (userland pid`smb_request_t *)arg1;

	printf(" %s mid=0x%x uid=0x%x tid=0x%x\n",
	    this->pn,
	    this->sr->smb_mid,
	    this->sr->smb_uid,
	    this->sr->smb_tid);
}

fksmb$target:::smb_done
{
	this->pn = copyinstr(arg0);
	this->sr = (userland pid`smb_request_t *)arg1;

	printf(" %s mid=0x%x status=0x%x\n",
	    this->pn,
	    this->sr->smb_mid,
	    this->sr->smb_error.status);
}

fksmb$target:::smb2_start
{
	this->pn = copyinstr(arg0);
	this->sr = (userland pid`smb_request_t *)arg1;

	printf(" %s mid=0x%x uid=0x%x tid=0x%x\n",
	    this->pn,
	    this->sr->smb2_messageid,
	    this->sr->smb2_ssnid,
	    this->sr->smb_tid);
}

fksmb$target:::smb2_done
{
	this->pn = copyinstr(arg0);
	this->sr = (userland pid`smb_request_t *)arg1;

	printf(" %s mid=0x%x status=0x%x\n",
	    this->pn,
	    this->sr->smb2_messageid,
	    this->sr->smb2_status);
}
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
 */

/*
 * Example using the "fksmb$pid" dtrace provider.
 * Traces all SMB commands using the probes:
 *	start, done
 * all of which have two args:
 *	args[0]: char * (probe-name)
 *	args[1]: ulong (struct smb_request *)
 *
 * Note: the "userland" type classifier causes dtrace to
 * automatically copyin the struct for us.  (Nice!)
 */

fksmb$target:::smb_start
{
	this->pn = copyinstr(arg0);
	this->sr = (userland pid`smb_request_t *)arg1;

	printf(" %s mid=0x%x uid=0x%x tid=0x%x\n",
	    this->pn,
	    this->sr->smb_mid,
	    this->sr->smb_uid,
	    this->sr->smb_tid);
}

fksmb$target:::smb_done
{
	this->pn = copyinstr(arg0);
	this->sr = (userland pid`smb_request_t *)arg1;

	printf(" %s mid=0x%x status=0x%x\n",
	    this->pn,
	    this->sr->smb_mid,
	    this->sr->smb_error.status);
}

fksmb$target:::smb2_start
{
	this->pn = copyinstr(arg0);
	this->sr = (userland pid`smb_request_t *)arg1;

	printf(" %s mid=0x%x uid=0x%x tid=0x%x\n",
	    this->pn,
	    this->sr->smb2_messageid,
	    this->sr->smb2_ssnid,
	    this->sr->smb_tid);
}

fksmb$target:::smb2_done
{
	this->pn = copyinstr(arg0);
	this->sr = (userland pid`smb_request_t *)arg1;

	printf(" %s mid=0x%x status=0x%x\n",
	    this->pn,
	    this->sr->smb2_messageid,
	    this->sr->smb2_status);
}
/*
 * 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 2013 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 <umem.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"


/*
 * Special version of smb_door_dispatch() for the
 * "fake" smbsrv (running in user space).
 *
 * This is called via function pointer from
 * smbsrv: smb_kdoor_upcall()
 *
 * The args and response go RPC encoded, just so we can
 * borrow some of the common doorsvc code, even though
 * there's no need for RPC encoding in this scenario.
 */
int
fksmbd_door_dispatch(smb_doorarg_t *da)
{

	smbd_arg_t	dop_arg;
	smb_doorhdr_t	*hdr;
	char		*rbuf = NULL;
	char		*argp = da->da_arg.data_ptr;
	size_t		arg_size = da->da_arg.data_size;
	size_t		hdr_size, rsize;

	/*
	 * Decode
	 *
	 * da->da_arg.data_ptr  = (arg data, xdr encoded)
	 * da->da_arg.data_size = (arg data len)
	 */

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

	if ((argp == NULL) || (arg_size < hdr_size)) {
		syslog(LOG_DEBUG, "fksmbd_door_dispatch: bad args");
		return (-1);
	}

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

	if ((hdr->dh_magic != SMB_DOOR_HDR_MAGIC) ||
	    (hdr->dh_flags != SMB_DF_FAKE_KERNEL)) {
		syslog(LOG_DEBUG, "fksmbd_door_dispatch: invalid header");
		return (-1);
	}

	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 not used here.
		 */
		syslog(LOG_DEBUG, "fksmbd_door_dispatch: ASYNC?");
		return (-1);
	}

	/*
	 * Dispatch
	 *
	 * Call the common smbd_doorsvc.c code.
	 */
	(void) smbd_door_dispatch_op(&dop_arg);

	/*
	 * Encode
	 *
	 * da->da_arg.rbuf  = (return data buf)
	 * da->da_arg.rsize = (return data size)
	 *
	 * Note that the return data buffer initially
	 * points to the same buffer as the args.
	 * If that's not large enough, umem_alloc.
	 */

	rsize = dop_arg.rsize + hdr_size;
	rbuf = umem_alloc(rsize, UMEM_DEFAULT);
	if (rbuf == NULL) {
		syslog(LOG_DEBUG, "fksmbd_door_dispatch[%s]: alloc %m",
		    dop_arg.opname);
		return (-1);
	}

	/* Copy caller's return data after the header. */
	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);

	/* Let's update da->da_hdr too. */
	da->da_hdr = *hdr;

	/*
	 * Was door_return()
	 * NB: The "fake kernel" smbsrv code will umem_free rbuf.
	 */
	da->da_arg.rbuf = rbuf;
	da->da_arg.rsize = rsize;

	return (0);
}
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2017 Nexenta Systems, Inc.  All rights reserved.
 */

/*
 * These replace NODIRECT functions of the same name in
 * $SRC/lib/smbsrv/libsmb/common/smb_kmod.c including:
 *	smb_kmod_bind, smb_kmod_ioctl, smb_kmod_isbound,
 *	smb_kmod_start, smb_kmod_stop, smb_kmod_unbind.
 *
 * For all the other smb_kmod_... functions, we can just use the
 * libsmb code because those all call smb_kmod_ioctl, for which
 * we have an override here.
 *
 * The replacment functions here just call the libfksmbsrv code
 * directly where the real (in-kernel) versions would be entered
 * via the driver framework (open, close, ioctl).  Aside from that,
 * the call sequences are intentionally the same (where possible).
 * In particular, that makes it possible to debug startup/teardown
 * problems in the user-space version of this code.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioccom.h>
#include <sys/param.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <note.h>

#include <smbsrv/smbinfo.h>
#include <smbsrv/smb_ioctl.h>
#include "smbd.h"

boolean_t smbdrv_opened = B_FALSE;

/*
 * We want to adjust a few things in the standard configuration
 * passed to the "fake" version of the smbsrv kernel module.
 *
 * Reduce the maximum number of connections and workers, just for
 * convenience while debugging.  (Don't want hundreds of threads.)
 */
static void
fksmbd_adjust_config(smb_ioc_header_t *ioc_hdr)
{
	smb_ioc_cfg_t *ioc = (smb_ioc_cfg_t *)ioc_hdr;
	char *s;

	ioc->maxconnections = 10;
	ioc->maxworkers = 20;
	smbd_report("maxconnections=%d, maxworkers=%d",
	    ioc->maxconnections, ioc->maxworkers);

	if ((s = getenv("SMB_MAX_PROTOCOL")) != NULL) {
		ioc->max_protocol = strtol(s, NULL, 16);
		smbd_report("max_protocol=0x%x", ioc->max_protocol);
	}

	if ((s = getenv("SMB_MIN_PROTOCOL")) != NULL) {
		ioc->min_protocol = strtol(s, NULL, 16);
		smbd_report("min_protocol=0x%x", ioc->min_protocol);
	}

	if ((s = getenv("SMB_SIGNING")) != NULL) {
		ioc->signing_enable = 0;
		ioc->signing_required = 0;
		switch (s[0]) {
		case 'e':
			ioc->signing_enable = 1;
			break;
		case 'r':
			ioc->signing_enable = 1;
			ioc->signing_required = 1;
			break;
		default:
			smbd_report("env SMB_SIGNING invalid");
			break;
		}
	}
	smbd_report("signing: enable=%d, required=%d",
	    ioc->signing_enable, ioc->signing_required);
}

boolean_t
smb_kmod_isbound(void)
{
	return (smbdrv_opened);
}

int
smb_kmod_bind(boolean_t smbd)
{
	int rc;

	if (!smbd)
		return (ENXIO);

	if (smbdrv_opened) {
		smbdrv_opened = B_FALSE;
		(void) fksmbsrv_drv_close();
	}

	rc = fksmbsrv_drv_open();
	if (rc == 0)
		smbdrv_opened = B_TRUE;

	return (rc);
}

void
smb_kmod_unbind(void)
{
	if (smbdrv_opened) {
		smbdrv_opened = B_FALSE;
		(void) fksmbsrv_drv_close();
	}
}

int
smb_kmod_ioctl(int cmd, smb_ioc_header_t *ioc, uint32_t len)
{
	int rc;

	_NOTE(ARGUNUSED(len));

	if (!smbdrv_opened)
		return (EBADF);

	if (cmd == SMB_IOC_CONFIG)
		fksmbd_adjust_config(ioc);

	rc = fksmbsrv_drv_ioctl(cmd, ioc);
	return (rc);
}

/* ARGSUSED */
int
smb_kmod_start(int opipe, int lmshr, int udoor)
{
	smb_ioc_start_t ioc;
	int rc;

	bzero(&ioc, sizeof (ioc));

	/* These three are unused */
	ioc.opipe = -1;
	ioc.lmshrd = -1;
	ioc.udoor = -1;

	/* These are the "door" dispatch callbacks */
	ioc.lmshr_func = NULL; /* not used */
	ioc.opipe_func = NULL; /* not used */
	ioc.udoor_func = (void *)fksmbd_door_dispatch;

	rc = smb_kmod_ioctl(SMB_IOC_START, &ioc.hdr, sizeof (ioc));
	return (rc);
}

void
smb_kmod_stop(void)
{
	smb_ioc_header_t ioc;

	bzero(&ioc, sizeof (ioc));
	(void) smb_kmod_ioctl(SMB_IOC_STOP, &ioc, sizeof (ioc));
}
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
 */

/*
 * fork/exec a privileged helper to do the bind.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/note.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int
ksocket_bind_helper(int fd, struct sockaddr *addr, uint_t addrlen)
{
	char familystr[8];
	char portstr[12];
	char addrstr[INET6_ADDRSTRLEN];
	char *argv[6];
	const char *p;
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	struct sockaddr_in  *sin  = (struct sockaddr_in *)addr;
	/* LINTED E_BAD_PTR_CAST_ALIGN */
	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr;
	int pid, err, stat;
	_NOTE(ARGUNUSED(addrlen));

	(void) snprintf(familystr, sizeof (familystr), "%d", addr->sa_family);
	switch (addr->sa_family) {
	case AF_INET:
		(void) snprintf(portstr, sizeof (portstr), "%d",
		    ntohs(sin->sin_port));
		p = inet_ntop(AF_INET, &sin->sin_addr,
		    addrstr, sizeof (addrstr));
		break;
	case AF_INET6:
		(void) snprintf(portstr, sizeof (portstr), "%d",
		    ntohs(sin6->sin6_port));
		p = inet_ntop(AF_INET6, &sin6->sin6_addr,
		    addrstr, sizeof (addrstr));
		break;
	default:
		p = NULL;
		break;
	}
	if (p == NULL) {
		err = errno;
		(void) fprintf(stdout, "ksocket_bind_helper, inet_ntop %s\n",
		    strerror(err));
		return (err);
	}

	(void) fprintf(stdout, "ksocket_bind_helper, "
	    "family=%s addr=%s port=%s\n",
	    familystr, addrstr, portstr);

	argv[0] = "/usr/bin/pfexec";
	argv[1] = "/usr/lib/smbsrv/bind-helper";
	argv[2] = familystr;
	argv[3] = addrstr;
	argv[4] = portstr;
	argv[5] = NULL;

	pid = vfork();
	if (pid == -1) {
		err = errno;
		perror("fork");
		return (err);
	}
	if (pid == 0) {
		(void) dup2(fd, 0);
		(void) execv(argv[0], argv);
		err = errno;
		perror("execv");
		return (err);
	}
	err = waitpid(pid, &stat, 0);
	if (err == -1) {
		err = errno;
		perror("waitpid");
		return (err);
	}
	if (WIFEXITED(stat)) {
		err = WEXITSTATUS(stat);
		if (err == 0)
			return (0);
		(void) fprintf(stderr, "helper exit %d\n", err);
	}
	return (EACCES);
}
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
 */

#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <syslog.h>
#include <sys/strlog.h>

#include <smbsrv/smbinfo.h>
#include <smbsrv/smb_ioctl.h>
#include "smbd.h"

#include <libfakekernel/fakekernel.h>

static const char *pri_name[LOG_DEBUG+1] = {
	"emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"
};


/*
 * Provide a replacement for libsmb:smb_vsyslog() that just
 * prints the messages to stdout for "fksmbd" debugging.
 */
void
smb_vsyslog(int pri, const char *fmt, va_list ap)
{
	int save_errno = errno;
	char buf[SMBD_LOG_MSGSIZE];
	char *newfmt;

	pri &= LOG_PRIMASK;

	if (smbd.s_debug == 0 && pri > LOG_INFO)
		return;

	newfmt = smb_syslog_fmt_m(buf, sizeof (buf), fmt, save_errno);

	flockfile(stdout);
	(void) fprintf(stdout, "fksmbd.%s: ", pri_name[pri]);
	/* LINTED E_SEC_PRINTF_VAR_FMT */
	(void) vfprintf(stdout, newfmt, ap);
	(void) fprintf(stdout, "\n");
	funlockfile(stdout);

	(void) fflush(stdout);
}

/*
 * Provide a real function (one that prints something) to replace
 * the stub in libfakekernel.  This prints cmn_err() messages.
 */
void
fakekernel_putlog(char *msg, size_t len, int flags)
{

	/*
	 * [CE_CONT, CE_NOTE, CE_WARN, CE_PANIC] maps to
	 * [SL_NOTE, SL_NOTE, SL_WARN, SL_FATAL]
	 */
	if (smbd.s_debug == 0 && (flags & SL_NOTE))
		return;
	(void) fwrite(msg, 1, len, stdout);
	(void) fflush(stdout);
}

/*
 * Initialization function called at the start of fksmbd:main().
 * Call an empty function in both of libfksmbsrv, libfakekernel,
 * just to force them to load so we can set breakpoints in them
 * without debugger forceload tricks.  This also avoids elfchk
 * complaints from libfakekernel, which we don't call directly
 * except for here.
 */
void
fksmbd_init(void)
{
	fksmbsrv_drv_load();
	fakekernel_init();
}
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2014-2021 Tintri by DDN, Inc. All rights reserved.
 */

/*
 * Replace the smb_shr_load() function in libmlsvc, because
 * fksmbd doesn't want the real shares known by libshare,
 * instead preferring its own (fake) list of shares.
 */

#include <sys/types.h>


#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <syslog.h>
#include <libshare.h>
#include <unistd.h>
#include <note.h>

#include <smbsrv/libsmb.h>
#include <smbsrv/libsmbns.h>
#include <smbsrv/libmlsvc.h>
#include <smbsrv/smb_share.h>
#include <smbsrv/smb.h>

#define	SMB_SHRF_ENCRYPT	0x8000	// SMB2_SHAREFLAG_ENCRYPT_DATA?

static void
new_share(char *name, char *path, char *comment, int flags)
{
	smb_share_t si;

	bzero(&si, sizeof (si));
	(void) strlcpy(si.shr_name, name, MAXNAMELEN);
	(void) strlcpy(si.shr_path, path, MAXPATHLEN);
	(void) strlcpy(si.shr_cmnt, comment, SMB_SHARE_CMNT_MAX);
	if (flags & SMB_SHRF_ENCRYPT) {
		flags &= ~SMB_SHRF_ENCRYPT;
		si.shr_encrypt = SMB_CONFIG_REQUIRED;
	}
	si.shr_flags = flags;


	if (smb_shr_add(&si) != 0) {
		syslog(LOG_ERR, "failed to add test share: %s",
		    si.shr_name);
	}
}

/*
 * This function loads a list of shares from a text file, where
 * each line of the file contains:
 * name path comment
 *
 * This is only for fksmbd, for testing.
 */
void
shr_load_file(char *shr_file)
{
	char linebuf[1024];
	FILE *fp;
	char *p;
	char *name, *path, *comment;

	fp = fopen(shr_file, "r");
	if (fp == NULL) {
		perror(shr_file);
		return;
	}

	while ((p = fgets(linebuf, sizeof (linebuf), fp)) != NULL) {

		name = p;
		p = strpbrk(p, " \t\n");
		if (p == NULL)
			continue;
		*p++ = '\0';

		path = p;
		p = strpbrk(p, " \t\n");
		if (p == NULL)
			comment = "";
		else {
			*p++ = '\0';

			comment = p;
			p = strchr(p, '\n');
			if (p != NULL)
				*p++ = '\0';
		}
		new_share(name, path, comment, 0);
	}
	(void) fclose(fp);
}

/*ARGSUSED*/
void *
smb_shr_load(void *args)
{
	char *shr_file;
	_NOTE(ARGUNUSED(args))

	/*
	 * Not loading the real shares in fksmbd because that
	 * tries to enable the network/smb/server service.
	 * Also, we won't generally have access to everything
	 * in the real shares, because fksmbd runs (only) with
	 * the credentials of the user who runs it.
	 */
	new_share("test", "/var/smb/test", "fksmbd test share",
	    SMB_SHRF_GUEST_OK);
	new_share("testca", "/var/smb/test", "fksmbd test CA share",
	    SMB_SHRF_CA);
	new_share("teste", "/var/smb/test", "fksmbd test encrypted share",
	    SMB_SHRF_ENCRYPT);

	/* Allow creating lots of shares for testing. */
	shr_file = getenv("FKSMBD_SHARE_FILE");
	if (shr_file != NULL)
		shr_load_file(shr_file);

	return (NULL);
}

void
smb_shr_load_execinfo()
{
}

void
smb_shr_unload()
{
}