summaryrefslogtreecommitdiffstats
path: root/source3/smbd/pysmbd.c
blob: e1f889dad30b8d7f65e810a15e96a3c15ff15232 (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
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
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
/*
   Unix SMB/CIFS implementation.
   Set NT and POSIX ACLs and other VFS operations from Python

   Copyrigyt (C) Andrew Bartlett 2012
   Copyright (C) Jeremy Allison 1994-2009.
   Copyright (C) Andreas Gruenbacher 2002.
   Copyright (C) Simo Sorce <idra@samba.org> 2009.
   Copyright (C) Simo Sorce 2002
   Copyright (C) Eric Lorimer 2002

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "lib/replace/system/python.h"
#include "includes.h"
#include "python/py3compat.h"
#include "python/modules.h"
#include "smbd/smbd.h"
#include "libcli/util/pyerrors.h"
#include "librpc/rpc/pyrpc_util.h"
#include <pytalloc.h>
#include "system/filesys.h"
#include "passdb.h"
#include "secrets.h"
#include "auth.h"

extern const struct generic_mapping file_generic_mapping;

#undef  DBGC_CLASS
#define DBGC_CLASS DBGC_ACLS

#ifdef O_DIRECTORY
#define DIRECTORY_FLAGS O_RDONLY|O_DIRECTORY
#else
/* POSIX allows us to open a directory with O_RDONLY. */
#define DIRECTORY_FLAGS O_RDONLY
#endif


static connection_struct *get_conn_tos(
	const char *service,
	const struct auth_session_info *session_info)
{
	struct conn_struct_tos *c = NULL;
	int snum = -1;
	NTSTATUS status;
	char *cwd = NULL;
	struct smb_filename cwd_fname = {0};
	int ret;

	if (!posix_locking_init(false)) {
		PyErr_NoMemory();
		return NULL;
	}

	if (service) {
		snum = lp_servicenumber(service);
		if (snum == -1) {
			PyErr_SetString(PyExc_RuntimeError, "unknown service");
			return NULL;
		}
	}

	/*
	 * Make sure that session unix info is filled,
	 * which is required by vfs operations.
	 */
	if (session_info->unix_info == NULL) {
		PyErr_SetString(PyExc_RuntimeError,
				"Session unix info not initialized");
		return NULL;
	}
	if (session_info->unix_info->unix_name == NULL) {
		PyErr_SetString(PyExc_RuntimeError,
				"Session unix info not available");
		return NULL;
	}

	status = create_conn_struct_tos(NULL,
					snum,
					"/",
					session_info,
					&c);
	PyErr_NTSTATUS_IS_ERR_RAISE(status);

	/* Ignore read-only and share restrictions */
	c->conn->read_only = false;
	c->conn->share_access = SEC_RIGHTS_FILE_ALL;

	/* Provided by libreplace if not present. Always mallocs. */
	cwd = get_current_dir_name();
	if (cwd == NULL) {
		PyErr_NoMemory();
		return NULL;
	}

	cwd_fname.base_name = cwd;
	/*
	 * We need to call vfs_ChDir() to initialize
	 * conn->cwd_fsp correctly. Change directory
	 * to current directory (so no change for process).
	 */
	ret = vfs_ChDir(c->conn, &cwd_fname);
	if (ret != 0) {
		status = map_nt_error_from_unix(errno);
		SAFE_FREE(cwd);
		PyErr_NTSTATUS_IS_ERR_RAISE(status);
	}

	SAFE_FREE(cwd);

	return c->conn;
}

static int set_sys_acl_conn(const char *fname,
				 SMB_ACL_TYPE_T acltype,
				 SMB_ACL_T theacl, connection_struct *conn)
{
	int ret;
	struct smb_filename *smb_fname = NULL;
	TALLOC_CTX *frame = talloc_stackframe();
	NTSTATUS status;

	smb_fname = synthetic_smb_fname_split(frame,
					fname,
					lp_posix_pathnames());
	if (smb_fname == NULL) {
		TALLOC_FREE(frame);
		return -1;
	}

	status = openat_pathref_fsp(conn->cwd_fsp, smb_fname);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(frame);
		errno = map_errno_from_nt_status(status);
		return -1;
	}

	ret = SMB_VFS_SYS_ACL_SET_FD(smb_fname->fsp, acltype, theacl);

	status = fd_close(smb_fname->fsp);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(frame);
		errno = map_errno_from_nt_status(status);
		return -1;
	}

	TALLOC_FREE(frame);
	return ret;
}


static NTSTATUS init_files_struct(TALLOC_CTX *mem_ctx,
				  const char *fname,
				  struct connection_struct *conn,
				  int flags,
				  struct files_struct **_fsp)
{
	struct vfs_open_how how = { .flags = flags, .mode = 0644 };
	struct smb_filename *smb_fname = NULL;
	int fd;
	mode_t saved_umask;
	struct files_struct *fsp;
	struct files_struct *fspcwd = NULL;
	NTSTATUS status;

	fsp = talloc_zero(mem_ctx, struct files_struct);
	if (fsp == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	fsp->fh = fd_handle_create(fsp);
	if (fsp->fh == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	fsp->conn = conn;

	smb_fname = synthetic_smb_fname_split(fsp,
					      fname,
					      lp_posix_pathnames());
	if (smb_fname == NULL) {
		return NT_STATUS_NO_MEMORY;
	}

	fsp->fsp_name = smb_fname;

	status = vfs_at_fspcwd(fsp, conn, &fspcwd);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/*
	 * we want total control over the permissions on created files,
	 * so set our umask to 0 (this matters if flags contains O_CREAT)
	 */
	saved_umask = umask(0);

	fd = SMB_VFS_OPENAT(conn,
			    fspcwd,
			    smb_fname,
			    fsp,
			    &how);

	umask(saved_umask);

	if (fd == -1) {
		int err = errno;
		if (err == ENOENT) {
			return NT_STATUS_OBJECT_NAME_NOT_FOUND;
		}
		return NT_STATUS_INVALID_PARAMETER;
	}
	fsp_set_fd(fsp, fd);

	status = vfs_stat_fsp(fsp);
	if (!NT_STATUS_IS_OK(status)) {
		/* If we have an fd, this stat should succeed. */
		DEBUG(0,("Error doing fstat on open file %s (%s)\n",
			 smb_fname_str_dbg(smb_fname),
			 nt_errstr(status) ));
		return status;
	}

	fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
	fsp->vuid = UID_FIELD_INVALID;
	fsp->file_pid = 0;
	fsp->fsp_flags.can_lock = true;
	fsp->fsp_flags.can_read = true;
	fsp->fsp_flags.can_write = true;
	fsp->print_file = NULL;
	fsp->fsp_flags.modified = false;
	fsp->sent_oplock_break = NO_BREAK_SENT;
	fsp->fsp_flags.is_directory = S_ISDIR(smb_fname->st.st_ex_mode);

	*_fsp = fsp;

	return NT_STATUS_OK;
}

static NTSTATUS set_nt_acl_conn(const char *fname,
				uint32_t security_info_sent, const struct security_descriptor *sd,
				connection_struct *conn)
{
	TALLOC_CTX *frame = talloc_stackframe();
	struct files_struct *fsp = NULL;
	NTSTATUS status = NT_STATUS_OK;

	/* first, try to open it as a file with flag O_RDWR */
	status = init_files_struct(frame,
				   fname,
				   conn,
				   O_RDWR,
				   &fsp);
	if (!NT_STATUS_IS_OK(status) && errno == EISDIR) {
		/* if fail, try to open as dir */
		status = init_files_struct(frame,
					   fname,
					   conn,
					   DIRECTORY_FLAGS,
					   &fsp);
	}

	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("init_files_struct failed: %s\n",
			nt_errstr(status));
		if (fsp != NULL) {
			fd_close(fsp);
		}
		TALLOC_FREE(frame);
		return status;
	}

	status = SMB_VFS_FSET_NT_ACL(metadata_fsp(fsp), security_info_sent, sd);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("set_nt_acl_conn: fset_nt_acl returned %s.\n", nt_errstr(status)));
	}

	fd_close(fsp);

	TALLOC_FREE(frame);
	return status;
}

static NTSTATUS get_nt_acl_conn(TALLOC_CTX *mem_ctx,
				const char *fname,
				connection_struct *conn,
				uint32_t security_info_wanted,
				struct security_descriptor **sd)
{
	TALLOC_CTX *frame = talloc_stackframe();
	NTSTATUS status;
	struct smb_filename *smb_fname =  NULL;

	smb_fname = synthetic_smb_fname_split(frame,
					fname,
					lp_posix_pathnames());

	if (smb_fname == NULL) {
		TALLOC_FREE(frame);
		return NT_STATUS_NO_MEMORY;
	}

	status = openat_pathref_fsp(conn->cwd_fsp, smb_fname);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(frame);
		return status;
	}

	status = SMB_VFS_FGET_NT_ACL(metadata_fsp(smb_fname->fsp),
				security_info_wanted,
				mem_ctx,
				sd);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("fget_nt_acl_at returned %s.\n",
			nt_errstr(status));
	}

	status = fd_close(smb_fname->fsp);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(frame);
		return status;
	}

	TALLOC_FREE(frame);

	return status;
}

static int set_acl_entry_perms(SMB_ACL_ENTRY_T entry, mode_t perm_mask)
{
	SMB_ACL_PERMSET_T perms = NULL;

	if (sys_acl_get_permset(entry, &perms) != 0) {
		return -1;
	}

	if (sys_acl_clear_perms(perms) != 0) {
		return -1;
	}

	if ((perm_mask & SMB_ACL_READ) != 0 &&
	    sys_acl_add_perm(perms, SMB_ACL_READ) != 0) {
		return -1;
	}

	if ((perm_mask & SMB_ACL_WRITE) != 0 &&
	    sys_acl_add_perm(perms, SMB_ACL_WRITE) != 0) {
		return -1;
	}

	if ((perm_mask & SMB_ACL_EXECUTE) != 0 &&
	    sys_acl_add_perm(perms, SMB_ACL_EXECUTE) != 0) {
		return -1;
	}

	if (sys_acl_set_permset(entry, perms) != 0) {
		return -1;
	}

	return 0;
}

static SMB_ACL_T make_simple_acl(TALLOC_CTX *mem_ctx,
			gid_t gid,
			mode_t chmod_mode)
{
	mode_t mode = SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE;

	mode_t mode_user = (chmod_mode & 0700) >> 6;
	mode_t mode_group = (chmod_mode & 070) >> 3;
	mode_t mode_other = chmod_mode &  07;
	SMB_ACL_ENTRY_T entry;
	SMB_ACL_T acl = sys_acl_init(mem_ctx);

	if (!acl) {
		return NULL;
	}

	if (sys_acl_create_entry(&acl, &entry) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	if (sys_acl_set_tag_type(entry, SMB_ACL_USER_OBJ) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	if (set_acl_entry_perms(entry, mode_user) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	if (sys_acl_create_entry(&acl, &entry) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP_OBJ) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	if (set_acl_entry_perms(entry, mode_group) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	if (sys_acl_create_entry(&acl, &entry) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	if (sys_acl_set_tag_type(entry, SMB_ACL_OTHER) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	if (set_acl_entry_perms(entry, mode_other) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	if (gid != -1) {
		if (sys_acl_create_entry(&acl, &entry) != 0) {
			TALLOC_FREE(acl);
			return NULL;
		}

		if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP) != 0) {
			TALLOC_FREE(acl);
			return NULL;
		}

		if (sys_acl_set_qualifier(entry, &gid) != 0) {
			TALLOC_FREE(acl);
			return NULL;
		}

		if (set_acl_entry_perms(entry, mode_group) != 0) {
			TALLOC_FREE(acl);
			return NULL;
		}
	}

	if (sys_acl_create_entry(&acl, &entry) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	if (sys_acl_set_tag_type(entry, SMB_ACL_MASK) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	if (set_acl_entry_perms(entry, mode) != 0) {
		TALLOC_FREE(acl);
		return NULL;
	}

	return acl;
}

/*
  set a simple ACL on a file, as a test
 */
static PyObject *py_smbd_set_simple_acl(PyObject *self, PyObject *args, PyObject *kwargs)
{
	const char * const kwnames[] = {
		"fname",
		"mode",
		"session_info",
		"gid",
		"service",
		NULL
	};
	char *fname, *service = NULL;
	PyObject *py_session = Py_None;
	struct auth_session_info *session_info = NULL;
	int ret;
	int mode, gid = -1;
	SMB_ACL_T acl;
	TALLOC_CTX *frame;
	connection_struct *conn;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|iz",
					 discard_const_p(char *, kwnames),
					 &fname,
					 &mode,
					 &py_session,
					 &gid,
					 &service))
		return NULL;

	if (!py_check_dcerpc_type(py_session,
				  "samba.dcerpc.auth",
				  "session_info")) {
		return NULL;
	}
	session_info = pytalloc_get_type(py_session,
					 struct auth_session_info);
	if (session_info == NULL) {
		PyErr_Format(PyExc_TypeError,
			     "Expected auth_session_info for session_info argument got %s",
			     pytalloc_get_name(py_session));
		return NULL;
	}

	frame = talloc_stackframe();

	acl = make_simple_acl(frame, gid, mode);
	if (acl == NULL) {
		TALLOC_FREE(frame);
		return NULL;
	}

	conn = get_conn_tos(service, session_info);
	if (!conn) {
		TALLOC_FREE(frame);
		return NULL;
	}

	ret = set_sys_acl_conn(fname, SMB_ACL_TYPE_ACCESS, acl, conn);

	if (ret != 0) {
		TALLOC_FREE(frame);
		errno = ret;
		return PyErr_SetFromErrno(PyExc_OSError);
	}

	TALLOC_FREE(frame);

	Py_RETURN_NONE;
}

/*
  chown a file
 */
static PyObject *py_smbd_chown(PyObject *self, PyObject *args, PyObject *kwargs)
{
	const char * const kwnames[] = {
		"fname",
		"uid",
		"gid",
		"session_info",
		"service",
		NULL
	};
	connection_struct *conn;
	int ret;
	NTSTATUS status;
	char *fname, *service = NULL;
	PyObject *py_session = Py_None;
	struct auth_session_info *session_info = NULL;
	int uid, gid;
	TALLOC_CTX *frame;
	struct files_struct *fsp = NULL;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siiO|z",
					 discard_const_p(char *, kwnames),
					 &fname,
					 &uid,
					 &gid,
					 &py_session,
					 &service))
		return NULL;

	if (!py_check_dcerpc_type(py_session,
				  "samba.dcerpc.auth",
				  "session_info")) {
		return NULL;
	}
	session_info = pytalloc_get_type(py_session,
					 struct auth_session_info);
	if (session_info == NULL) {
		PyErr_Format(PyExc_TypeError,
			     "Expected auth_session_info for session_info argument got %s",
			     pytalloc_get_name(py_session));
		return NULL;
	}

	frame = talloc_stackframe();

	conn = get_conn_tos(service, session_info);
	if (!conn) {
		TALLOC_FREE(frame);
		return NULL;
	}

	/* first, try to open it as a file with flag O_RDWR */
	status = init_files_struct(frame,
				   fname,
				   conn,
				   O_RDWR,
				   &fsp);
	if (!NT_STATUS_IS_OK(status) && errno == EISDIR) {
		/* if fail, try to open as dir */
		status = init_files_struct(frame,
					   fname,
					   conn,
					   DIRECTORY_FLAGS,
					   &fsp);
	}

	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("init_files_struct failed: %s\n",
			nt_errstr(status));
		if (fsp != NULL) {
			fd_close(fsp);
		}
		TALLOC_FREE(frame);
		/*
		 * The following macro raises a python
		 * error then returns NULL.
		 */
		PyErr_NTSTATUS_IS_ERR_RAISE(status);
	}

	ret = SMB_VFS_FCHOWN(fsp, uid, gid);
	if (ret != 0) {
		int saved_errno = errno;
		fd_close(fsp);
		TALLOC_FREE(frame);
		errno = saved_errno;
		return PyErr_SetFromErrno(PyExc_OSError);
	}

	fd_close(fsp);
	TALLOC_FREE(frame);

	Py_RETURN_NONE;
}

/*
  unlink a file
 */
static PyObject *py_smbd_unlink(PyObject *self, PyObject *args, PyObject *kwargs)
{
	const char * const kwnames[] = {
		"fname",
		"session_info",
		"service",
		NULL
	};
	connection_struct *conn;
	int ret;
	struct smb_filename *smb_fname = NULL;
	struct smb_filename *parent_fname = NULL;
	struct smb_filename *at_fname = NULL;
	PyObject *py_session = Py_None;
	struct auth_session_info *session_info = NULL;
	char *fname, *service = NULL;
	TALLOC_CTX *frame;
	NTSTATUS status;

	frame = talloc_stackframe();

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|z",
					 discard_const_p(char *, kwnames),
					 &fname,
					 &py_session ,
					 &service)) {
		TALLOC_FREE(frame);
		return NULL;
	}

	if (!py_check_dcerpc_type(py_session,
				  "samba.dcerpc.auth",
				  "session_info")) {
		TALLOC_FREE(frame);
		return NULL;
	}
	session_info = pytalloc_get_type(py_session,
					 struct auth_session_info);
	if (session_info == NULL) {
		PyErr_Format(PyExc_TypeError,
			     "Expected auth_session_info for session_info argument got %s",
			     pytalloc_get_name(py_session));
		TALLOC_FREE(frame);
		return NULL;
	}

	conn = get_conn_tos(service, session_info);
	if (!conn) {
		TALLOC_FREE(frame);
		return NULL;
	}

	smb_fname = synthetic_smb_fname_split(frame,
					fname,
					lp_posix_pathnames());
	if (smb_fname == NULL) {
		TALLOC_FREE(frame);
		return PyErr_NoMemory();
	}

	status = parent_pathref(frame,
				conn->cwd_fsp,
				smb_fname,
				&parent_fname,
				&at_fname);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(frame);
		return PyErr_NoMemory();
	}

	ret = SMB_VFS_UNLINKAT(conn,
			parent_fname->fsp,
			at_fname,
			0);
	if (ret != 0) {
		TALLOC_FREE(frame);
		errno = ret;
		return PyErr_SetFromErrno(PyExc_OSError);
	}

	TALLOC_FREE(frame);

	Py_RETURN_NONE;
}

/*
  check if we have ACL support
 */
static PyObject *py_smbd_have_posix_acls(PyObject *self,
		PyObject *Py_UNUSED(ignored))
{
#ifdef HAVE_POSIX_ACLS
	return PyBool_FromLong(true);
#else
	return PyBool_FromLong(false);
#endif
}

/*
  set the NT ACL on a file
 */
static PyObject *py_smbd_set_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
{
	const char * const kwnames[] = {
		"fname",
		"security_info_sent",
		"sd",
		"session_info",
		"service",
		NULL
	};

	NTSTATUS status;
	char *fname, *service = NULL;
	int security_info_sent;
	PyObject *py_sd;
	struct security_descriptor *sd;
	PyObject *py_session = Py_None;
	struct auth_session_info *session_info = NULL;
	connection_struct *conn;
	TALLOC_CTX *frame;

	frame = talloc_stackframe();

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siOO|z",
				         discard_const_p(char *, kwnames),
					 &fname,
					 &security_info_sent,
					 &py_sd,
					 &py_session,
					 &service)) {
		TALLOC_FREE(frame);
		return NULL;
	}

	if (!py_check_dcerpc_type(py_sd, "samba.dcerpc.security", "descriptor")) {
		TALLOC_FREE(frame);
		return NULL;
	}

	if (!py_check_dcerpc_type(py_session,
				  "samba.dcerpc.auth",
				  "session_info")) {
		TALLOC_FREE(frame);
		return NULL;
	}
	session_info = pytalloc_get_type(py_session,
					 struct auth_session_info);
	if (session_info == NULL) {
		PyErr_Format(PyExc_TypeError,
			     "Expected auth_session_info for session_info argument got %s",
			     pytalloc_get_name(py_session));
		return NULL;
	}

	conn = get_conn_tos(service, session_info);
	if (!conn) {
		TALLOC_FREE(frame);
		return NULL;
	}

	sd = pytalloc_get_type(py_sd, struct security_descriptor);

	status = set_nt_acl_conn(fname, security_info_sent, sd, conn);
	TALLOC_FREE(frame);
	if (NT_STATUS_IS_ERR(status)) {
		if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
			/*
			 * This will show up as a FileNotFoundError in python.
			 */
			PyErr_SetFromErrnoWithFilename(PyExc_OSError, fname);
		} else {
			PyErr_SetNTSTATUS(status);
		}
		return NULL;
	}

	Py_RETURN_NONE;
}

/*
  Return the NT ACL on a file
 */
static PyObject *py_smbd_get_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
{
	const char * const kwnames[] = {
		"fname",
		"security_info_wanted",
		"session_info",
		"service",
		NULL
	};
	char *fname, *service = NULL;
	int security_info_wanted;
	PyObject *py_sd;
	struct security_descriptor *sd;
	TALLOC_CTX *frame = talloc_stackframe();
	PyObject *py_session = Py_None;
	struct auth_session_info *session_info = NULL;
	connection_struct *conn;
	NTSTATUS status;
	int ret = 1;

	ret = PyArg_ParseTupleAndKeywords(args,
					  kwargs,
					  "siO|z",
					  discard_const_p(char *, kwnames),
					  &fname,
					  &security_info_wanted,
					  &py_session,
					  &service);
	if (!ret) {
		TALLOC_FREE(frame);
		return NULL;
	}

	if (!py_check_dcerpc_type(py_session,
				  "samba.dcerpc.auth",
				  "session_info")) {
		TALLOC_FREE(frame);
		return NULL;
	}
	session_info = pytalloc_get_type(py_session,
					 struct auth_session_info);
	if (session_info == NULL) {
		PyErr_Format(
			PyExc_TypeError,
			"Expected auth_session_info for "
			"session_info argument got %s",
			pytalloc_get_name(py_session));
		TALLOC_FREE(frame);
		return NULL;
	}

	conn = get_conn_tos(service, session_info);
	if (!conn) {
		TALLOC_FREE(frame);
		return NULL;
	}

	status = get_nt_acl_conn(frame, fname, conn, security_info_wanted, &sd);
	if (NT_STATUS_IS_ERR(status)) {
		if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
			/*
			 * This will show up as a FileNotFoundError in python,
			 * from which samba-tool can at least produce a short
			 * message containing the problematic filename.
			 */
			PyErr_SetFromErrnoWithFilename(PyExc_OSError, fname);
		} else {
			PyErr_SetNTSTATUS(status);
		}
		TALLOC_FREE(frame);
		return NULL;
	}

	py_sd = py_return_ndr_struct("samba.dcerpc.security", "descriptor", sd, sd);

	TALLOC_FREE(frame);

	return py_sd;
}

/*
  set the posix (or similar) ACL on a file
 */
static PyObject *py_smbd_set_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
{
	const char * const kwnames[] = {
		"fname",
		"acl_type",
		"acl",
		"session_info",
		"service",
		NULL
	};
	TALLOC_CTX *frame = talloc_stackframe();
	int ret;
	char *fname, *service = NULL;
	PyObject *py_acl;
	PyObject *py_session = Py_None;
	struct auth_session_info *session_info = NULL;
	struct smb_acl_t *acl;
	int acl_type;
	connection_struct *conn;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siOO|z",
					 discard_const_p(char *, kwnames),
					 &fname,
					 &acl_type,
					 &py_acl,
					 &py_session,
					 &service)) {
		TALLOC_FREE(frame);
		return NULL;
	}

	if (!py_check_dcerpc_type(py_acl, "samba.dcerpc.smb_acl", "t")) {
		TALLOC_FREE(frame);
		return NULL;
	}

	if (!py_check_dcerpc_type(py_session,
				  "samba.dcerpc.auth",
				  "session_info")) {
		TALLOC_FREE(frame);
		return NULL;
	}
	session_info = pytalloc_get_type(py_session,
					 struct auth_session_info);
	if (session_info == NULL) {
		PyErr_Format(PyExc_TypeError,
			     "Expected auth_session_info for session_info argument got %s",
			     pytalloc_get_name(py_session));
		TALLOC_FREE(frame);
		return NULL;
	}

	conn = get_conn_tos(service, session_info);
	if (!conn) {
		TALLOC_FREE(frame);
		return NULL;
	}

	acl = pytalloc_get_type(py_acl, struct smb_acl_t);

	ret = set_sys_acl_conn(fname, acl_type, acl, conn);
	if (ret != 0) {
		TALLOC_FREE(frame);
		errno = ret;
		return PyErr_SetFromErrno(PyExc_OSError);
	}

	TALLOC_FREE(frame);
	Py_RETURN_NONE;
}

/*
  Return the posix (or similar) ACL on a file
 */
static PyObject *py_smbd_get_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
{
	const char * const kwnames[] = {
		"fname",
		"acl_type",
		"session_info",
		"service",
		NULL
	};
	char *fname;
	PyObject *py_acl;
	PyObject *py_session = Py_None;
	struct auth_session_info *session_info = NULL;
	struct smb_acl_t *acl;
	int acl_type;
	TALLOC_CTX *frame = talloc_stackframe();
	connection_struct *conn;
	char *service = NULL;
	struct smb_filename *smb_fname = NULL;
	NTSTATUS status;

	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|z",
					 discard_const_p(char *, kwnames),
					 &fname,
					 &acl_type,
					 &py_session,
					 &service)) {
		TALLOC_FREE(frame);
		return NULL;
	}

	if (!py_check_dcerpc_type(py_session,
				  "samba.dcerpc.auth",
				  "session_info")) {
		TALLOC_FREE(frame);
		return NULL;
	}
	session_info = pytalloc_get_type(py_session,
					 struct auth_session_info);
	if (session_info == NULL) {
		PyErr_Format(PyExc_TypeError,
			     "Expected auth_session_info for session_info argument got %s",
			     pytalloc_get_name(py_session));
		TALLOC_FREE(frame);
		return NULL;
	}

	conn = get_conn_tos(service, session_info);
	if (!conn) {
		TALLOC_FREE(frame);
		return NULL;
	}

	smb_fname = synthetic_smb_fname_split(frame,
					fname,
					lp_posix_pathnames());
	if (smb_fname == NULL) {
		TALLOC_FREE(frame);
		return NULL;
	}

	status = openat_pathref_fsp(conn->cwd_fsp, smb_fname);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(frame);
		PyErr_SetNTSTATUS(status);
		return NULL;
	}

	acl = SMB_VFS_SYS_ACL_GET_FD(smb_fname->fsp, acl_type, frame);
	if (!acl) {
		TALLOC_FREE(frame);
		return PyErr_SetFromErrno(PyExc_OSError);
	}

	status = fd_close(smb_fname->fsp);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(frame);
		PyErr_SetNTSTATUS(status);
		return NULL;
	}

	py_acl = py_return_ndr_struct("samba.dcerpc.smb_acl", "t", acl, acl);

	TALLOC_FREE(frame);

	return py_acl;
}

static PyObject *py_smbd_mkdir(PyObject *self, PyObject *args, PyObject *kwargs)
{
	const char * const kwnames[] = {
		"fname",
		"session_info",
		"service",
		NULL
	};
	char *fname, *service = NULL;
	PyObject *py_session = Py_None;
	struct auth_session_info *session_info = NULL;
	TALLOC_CTX *frame = talloc_stackframe();
	struct connection_struct *conn = NULL;
	struct smb_filename *smb_fname = NULL;
	struct smb_filename *parent_fname = NULL;
	struct smb_filename *base_name = NULL;
	NTSTATUS status;
	int ret;
	mode_t saved_umask;

	if (!PyArg_ParseTupleAndKeywords(args,
					 kwargs,
					 "sO|z",
					 discard_const_p(char *,
							 kwnames),
					 &fname,
					 &py_session,
					 &service)) {
		TALLOC_FREE(frame);
		return NULL;
	}

	if (!py_check_dcerpc_type(py_session,
				  "samba.dcerpc.auth",
				  "session_info")) {
		TALLOC_FREE(frame);
		return NULL;
	}
	session_info = pytalloc_get_type(py_session,
					 struct auth_session_info);
	if (session_info == NULL) {
		PyErr_Format(PyExc_TypeError,
			     "Expected auth_session_info for session_info argument got %s",
			     pytalloc_get_name(py_session));
		TALLOC_FREE(frame);
		return NULL;
	}

	conn = get_conn_tos(service, session_info);
	if (!conn) {
		TALLOC_FREE(frame);
		return NULL;
	}

	smb_fname = synthetic_smb_fname(talloc_tos(),
					fname,
					NULL,
					NULL,
					0,
					lp_posix_pathnames() ?
					SMB_FILENAME_POSIX_PATH : 0);

	if (smb_fname == NULL) {
		TALLOC_FREE(frame);
		return NULL;
	}

	status = parent_pathref(talloc_tos(),
				conn->cwd_fsp,
				smb_fname,
				&parent_fname,
				&base_name);
	if (!NT_STATUS_IS_OK(status)) {
		TALLOC_FREE(frame);
		return NULL;
	}

	/* we want total control over the permissions on created files,
	   so set our umask to 0 */
	saved_umask = umask(0);

	ret = SMB_VFS_MKDIRAT(conn,
			parent_fname->fsp,
			base_name,
			00755);

	umask(saved_umask);

	if (ret == -1) {
		DBG_ERR("mkdirat error=%d (%s)\n", errno, strerror(errno));
		TALLOC_FREE(frame);
		return NULL;
	}

	TALLOC_FREE(frame);
	Py_RETURN_NONE;
}


/*
  Create an empty file
 */
static PyObject *py_smbd_create_file(PyObject *self, PyObject *args, PyObject *kwargs)
{
	const char * const kwnames[] = {
		"fname",
		"session_info",
		"service",
		NULL
	};
	char *fname, *service = NULL;
	PyObject *py_session = Py_None;
	struct auth_session_info *session_info = NULL;
	TALLOC_CTX *frame = talloc_stackframe();
	struct connection_struct *conn = NULL;
	struct files_struct *fsp = NULL;
	NTSTATUS status;

	if (!PyArg_ParseTupleAndKeywords(args,
					 kwargs,
					 "sO|z",
					 discard_const_p(char *,
							 kwnames),
					 &fname,
					 &py_session,
					 &service)) {
		TALLOC_FREE(frame);
		return NULL;
	}

	if (!py_check_dcerpc_type(py_session,
				  "samba.dcerpc.auth",
				  "session_info")) {
		TALLOC_FREE(frame);
		return NULL;
	}
	session_info = pytalloc_get_type(py_session,
					 struct auth_session_info);
	if (session_info == NULL) {
		PyErr_Format(PyExc_TypeError,
			     "Expected auth_session_info for session_info argument got %s",
			     pytalloc_get_name(py_session));
		TALLOC_FREE(frame);
		return NULL;
	}

	conn = get_conn_tos(service, session_info);
	if (!conn) {
		TALLOC_FREE(frame);
		return NULL;
	}

	status = init_files_struct(frame,
				   fname,
				   conn,
				   O_CREAT|O_EXCL|O_RDWR,
				   &fsp);
	if (!NT_STATUS_IS_OK(status)) {
		DBG_ERR("init_files_struct failed: %s\n",
			nt_errstr(status));
	} else if (fsp != NULL) {
		fd_close(fsp);
	}

	TALLOC_FREE(frame);
	PyErr_NTSTATUS_NOT_OK_RAISE(status);
	Py_RETURN_NONE;
}


static PyMethodDef py_smbd_methods[] = {
	{ "have_posix_acls",
		(PyCFunction)py_smbd_have_posix_acls, METH_NOARGS,
		NULL },
	{ "set_simple_acl",
		PY_DISCARD_FUNC_SIG(PyCFunction, py_smbd_set_simple_acl),
		METH_VARARGS|METH_KEYWORDS,
		NULL },
	{ "set_nt_acl",
		PY_DISCARD_FUNC_SIG(PyCFunction, py_smbd_set_nt_acl),
		METH_VARARGS|METH_KEYWORDS,
		NULL },
	{ "get_nt_acl",
		PY_DISCARD_FUNC_SIG(PyCFunction, py_smbd_get_nt_acl),
		METH_VARARGS|METH_KEYWORDS,
		NULL },
	{ "get_sys_acl",
		PY_DISCARD_FUNC_SIG(PyCFunction, py_smbd_get_sys_acl),
		METH_VARARGS|METH_KEYWORDS,
		NULL },
	{ "set_sys_acl",
		PY_DISCARD_FUNC_SIG(PyCFunction, py_smbd_set_sys_acl),
		METH_VARARGS|METH_KEYWORDS,
		NULL },
	{ "chown",
		PY_DISCARD_FUNC_SIG(PyCFunction, py_smbd_chown),
		METH_VARARGS|METH_KEYWORDS,
		NULL },
	{ "unlink",
		PY_DISCARD_FUNC_SIG(PyCFunction, py_smbd_unlink),
		METH_VARARGS|METH_KEYWORDS,
		NULL },
	{ "mkdir",
		PY_DISCARD_FUNC_SIG(PyCFunction, py_smbd_mkdir),
		METH_VARARGS|METH_KEYWORDS,
		NULL },
	{ "create_file",
		PY_DISCARD_FUNC_SIG(PyCFunction, py_smbd_create_file),
		METH_VARARGS|METH_KEYWORDS,
		NULL },
	{0}
};

void initsmbd(void);

static struct PyModuleDef moduledef = {
    PyModuleDef_HEAD_INIT,
    .m_name = "smbd",
    .m_doc = "Python bindings for the smbd file server.",
    .m_size = -1,
    .m_methods = py_smbd_methods,
};

MODULE_INIT_FUNC(smbd)
{
	PyObject *m = NULL;

	m = PyModule_Create(&moduledef);
	return m;
}