smbd: Simplify downgrade_lease
[bbaumbach/samba-autobuild/.git] / source3 / smbd / pysmbd.c
1 /*
2    Unix SMB/CIFS implementation.
3    Set NT and POSIX ACLs and other VFS operations from Python
4
5    Copyrigyt (C) Andrew Bartlett 2012
6    Copyright (C) Jeremy Allison 1994-2009.
7    Copyright (C) Andreas Gruenbacher 2002.
8    Copyright (C) Simo Sorce <idra@samba.org> 2009.
9    Copyright (C) Simo Sorce 2002
10    Copyright (C) Eric Lorimer 2002
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include <Python.h>
27 #include "includes.h"
28 #include "python/py3compat.h"
29 #include "smbd/smbd.h"
30 #include "libcli/util/pyerrors.h"
31 #include "librpc/rpc/pyrpc_util.h"
32 #include <pytalloc.h>
33 #include "system/filesys.h"
34
35 extern const struct generic_mapping file_generic_mapping;
36
37 #undef  DBGC_CLASS
38 #define DBGC_CLASS DBGC_ACLS
39
40 #ifdef O_DIRECTORY
41 #define DIRECTORY_FLAGS O_RDONLY|O_DIRECTORY
42 #else
43 /* POSIX allows us to open a directory with O_RDONLY. */
44 #define DIRECTORY_FLAGS O_RDONLY
45 #endif
46
47
48 static connection_struct *get_conn_tos(
49         const char *service,
50         const struct auth_session_info *session_info)
51 {
52         struct conn_struct_tos *c = NULL;
53         int snum = -1;
54         NTSTATUS status;
55
56         if (!posix_locking_init(false)) {
57                 PyErr_NoMemory();
58                 return NULL;
59         }
60
61         if (service) {
62                 snum = lp_servicenumber(service);
63                 if (snum == -1) {
64                         PyErr_SetString(PyExc_RuntimeError, "unknown service");
65                         return NULL;
66                 }
67         }
68
69         status = create_conn_struct_tos(NULL,
70                                         snum,
71                                         "/",
72                                         session_info,
73                                         &c);
74         PyErr_NTSTATUS_IS_ERR_RAISE(status);
75
76         /* Ignore read-only and share restrictions */
77         c->conn->read_only = false;
78         c->conn->share_access = SEC_RIGHTS_FILE_ALL;
79         return c->conn;
80 }
81
82 static int set_sys_acl_conn(const char *fname,
83                                  SMB_ACL_TYPE_T acltype,
84                                  SMB_ACL_T theacl, connection_struct *conn)
85 {
86         int ret;
87         struct smb_filename *smb_fname = NULL;
88         mode_t saved_umask;
89
90         TALLOC_CTX *frame = talloc_stackframe();
91
92         /* we want total control over the permissions on created files,
93            so set our umask to 0 */
94         saved_umask = umask(0);
95
96         smb_fname = synthetic_smb_fname_split(frame,
97                                         fname,
98                                         lp_posix_pathnames());
99         if (smb_fname == NULL) {
100                 TALLOC_FREE(frame);
101                 umask(saved_umask);
102                 return -1;
103         }
104
105         ret = SMB_VFS_SYS_ACL_SET_FILE( conn, smb_fname, acltype, theacl);
106
107         umask(saved_umask);
108
109         TALLOC_FREE(frame);
110         return ret;
111 }
112
113
114 static NTSTATUS init_files_struct(TALLOC_CTX *mem_ctx,
115                                   const char *fname,
116                                   struct connection_struct *conn,
117                                   int flags,
118                                   struct files_struct **_fsp)
119 {
120         struct smb_filename *smb_fname = NULL;
121         int ret;
122         mode_t saved_umask;
123         struct files_struct *fsp;
124
125         fsp = talloc_zero(mem_ctx, struct files_struct);
126         if (fsp == NULL) {
127                 return NT_STATUS_NO_MEMORY;
128         }
129         fsp->fh = talloc(fsp, struct fd_handle);
130         if (fsp->fh == NULL) {
131                 return NT_STATUS_NO_MEMORY;
132         }
133         fsp->conn = conn;
134
135         /* we want total control over the permissions on created files,
136            so set our umask to 0 */
137         saved_umask = umask(0);
138
139         smb_fname = synthetic_smb_fname_split(fsp,
140                                               fname,
141                                               lp_posix_pathnames());
142         if (smb_fname == NULL) {
143                 umask(saved_umask);
144                 return NT_STATUS_NO_MEMORY;
145         }
146
147         fsp->fsp_name = smb_fname;
148         fsp->fh->fd = SMB_VFS_OPEN(conn, smb_fname, fsp, flags, 00644);
149         if (fsp->fh->fd == -1) {
150                 umask(saved_umask);
151                 return NT_STATUS_INVALID_PARAMETER;
152         }
153
154         ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
155         if (ret == -1) {
156                 /* If we have an fd, this stat should succeed. */
157                 DEBUG(0,("Error doing fstat on open file %s (%s)\n",
158                          smb_fname_str_dbg(smb_fname),
159                          strerror(errno) ));
160                 umask(saved_umask);
161                 return map_nt_error_from_unix(errno);
162         }
163
164         fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
165         fsp->vuid = UID_FIELD_INVALID;
166         fsp->file_pid = 0;
167         fsp->can_lock = True;
168         fsp->can_read = True;
169         fsp->can_write = True;
170         fsp->print_file = NULL;
171         fsp->modified = False;
172         fsp->sent_oplock_break = NO_BREAK_SENT;
173         fsp->is_directory = S_ISDIR(smb_fname->st.st_ex_mode);
174
175         *_fsp = fsp;
176
177         return NT_STATUS_OK;
178 }
179
180 static NTSTATUS set_nt_acl_conn(const char *fname,
181                                 uint32_t security_info_sent, const struct security_descriptor *sd,
182                                 connection_struct *conn)
183 {
184         TALLOC_CTX *frame = talloc_stackframe();
185         struct files_struct *fsp = NULL;
186         NTSTATUS status = NT_STATUS_OK;
187
188         /* first, try to open it as a file with flag O_RDWR */
189         status = init_files_struct(frame,
190                                    fname,
191                                    conn,
192                                    O_RDWR,
193                                    &fsp);
194         if (!NT_STATUS_IS_OK(status) && errno == EISDIR) {
195                 /* if fail, try to open as dir */
196                 status = init_files_struct(frame,
197                                            fname,
198                                            conn,
199                                            DIRECTORY_FLAGS,
200                                            &fsp);
201         }
202
203         if (!NT_STATUS_IS_OK(status)) {
204                 printf("open: error=%d (%s)\n", errno, strerror(errno));
205                 SMB_VFS_CLOSE(fsp);
206                 TALLOC_FREE(frame);
207                 return status;
208         }
209
210         status = SMB_VFS_FSET_NT_ACL(fsp, security_info_sent, sd);
211         if (!NT_STATUS_IS_OK(status)) {
212                 DEBUG(0,("set_nt_acl_no_snum: fset_nt_acl returned %s.\n", nt_errstr(status)));
213         }
214
215         SMB_VFS_CLOSE(fsp);
216
217         TALLOC_FREE(frame);
218         return status;
219 }
220
221 static NTSTATUS get_nt_acl_conn(TALLOC_CTX *mem_ctx,
222                                 const char *fname,
223                                 connection_struct *conn,
224                                 uint32_t security_info_wanted,
225                                 struct security_descriptor **sd)
226 {
227         TALLOC_CTX *frame = talloc_stackframe();
228         NTSTATUS status;
229         struct smb_filename *smb_fname = synthetic_smb_fname(talloc_tos(),
230                                         fname,
231                                         NULL,
232                                         NULL,
233                                         lp_posix_pathnames() ?
234                                                 SMB_FILENAME_POSIX_PATH : 0);
235
236         if (smb_fname == NULL) {
237                 TALLOC_FREE(frame);
238                 return NT_STATUS_NO_MEMORY;
239         }
240
241         status = SMB_VFS_GET_NT_ACL(conn,
242                                 smb_fname,
243                                 security_info_wanted,
244                                 mem_ctx,
245                                 sd);
246         if (!NT_STATUS_IS_OK(status)) {
247                 DEBUG(0,("get_nt_acl_conn: get_nt_acl returned %s.\n", nt_errstr(status)));
248         }
249
250         TALLOC_FREE(frame);
251
252         return status;
253 }
254
255 static int set_acl_entry_perms(SMB_ACL_ENTRY_T entry, mode_t perm_mask)
256 {
257         SMB_ACL_PERMSET_T perms = NULL;
258
259         if (sys_acl_get_permset(entry, &perms) != 0) {
260                 return -1;
261         }
262
263         if (sys_acl_clear_perms(perms) != 0) {
264                 return -1;
265         }
266
267         if ((perm_mask & SMB_ACL_READ) != 0 &&
268             sys_acl_add_perm(perms, SMB_ACL_READ) != 0) {
269                 return -1;
270         }
271
272         if ((perm_mask & SMB_ACL_WRITE) != 0 &&
273             sys_acl_add_perm(perms, SMB_ACL_WRITE) != 0) {
274                 return -1;
275         }
276
277         if ((perm_mask & SMB_ACL_EXECUTE) != 0 &&
278             sys_acl_add_perm(perms, SMB_ACL_EXECUTE) != 0) {
279                 return -1;
280         }
281
282         if (sys_acl_set_permset(entry, perms) != 0) {
283                 return -1;
284         }
285
286         return 0;
287 }
288
289 static SMB_ACL_T make_simple_acl(TALLOC_CTX *mem_ctx,
290                         gid_t gid,
291                         mode_t chmod_mode)
292 {
293         mode_t mode = SMB_ACL_READ|SMB_ACL_WRITE|SMB_ACL_EXECUTE;
294
295         mode_t mode_user = (chmod_mode & 0700) >> 6;
296         mode_t mode_group = (chmod_mode & 070) >> 3;
297         mode_t mode_other = chmod_mode &  07;
298         SMB_ACL_ENTRY_T entry;
299         SMB_ACL_T acl = sys_acl_init(mem_ctx);
300
301         if (!acl) {
302                 return NULL;
303         }
304
305         if (sys_acl_create_entry(&acl, &entry) != 0) {
306                 TALLOC_FREE(acl);
307                 return NULL;
308         }
309
310         if (sys_acl_set_tag_type(entry, SMB_ACL_USER_OBJ) != 0) {
311                 TALLOC_FREE(acl);
312                 return NULL;
313         }
314
315         if (set_acl_entry_perms(entry, mode_user) != 0) {
316                 TALLOC_FREE(acl);
317                 return NULL;
318         }
319
320         if (sys_acl_create_entry(&acl, &entry) != 0) {
321                 TALLOC_FREE(acl);
322                 return NULL;
323         }
324
325         if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP_OBJ) != 0) {
326                 TALLOC_FREE(acl);
327                 return NULL;
328         }
329
330         if (set_acl_entry_perms(entry, mode_group) != 0) {
331                 TALLOC_FREE(acl);
332                 return NULL;
333         }
334
335         if (sys_acl_create_entry(&acl, &entry) != 0) {
336                 TALLOC_FREE(acl);
337                 return NULL;
338         }
339
340         if (sys_acl_set_tag_type(entry, SMB_ACL_OTHER) != 0) {
341                 TALLOC_FREE(acl);
342                 return NULL;
343         }
344
345         if (set_acl_entry_perms(entry, mode_other) != 0) {
346                 TALLOC_FREE(acl);
347                 return NULL;
348         }
349
350         if (gid != -1) {
351                 if (sys_acl_create_entry(&acl, &entry) != 0) {
352                         TALLOC_FREE(acl);
353                         return NULL;
354                 }
355
356                 if (sys_acl_set_tag_type(entry, SMB_ACL_GROUP) != 0) {
357                         TALLOC_FREE(acl);
358                         return NULL;
359                 }
360
361                 if (sys_acl_set_qualifier(entry, &gid) != 0) {
362                         TALLOC_FREE(acl);
363                         return NULL;
364                 }
365
366                 if (set_acl_entry_perms(entry, mode_group) != 0) {
367                         TALLOC_FREE(acl);
368                         return NULL;
369                 }
370         }
371
372         if (sys_acl_create_entry(&acl, &entry) != 0) {
373                 TALLOC_FREE(acl);
374                 return NULL;
375         }
376
377         if (sys_acl_set_tag_type(entry, SMB_ACL_MASK) != 0) {
378                 TALLOC_FREE(acl);
379                 return NULL;
380         }
381
382         if (set_acl_entry_perms(entry, mode) != 0) {
383                 TALLOC_FREE(acl);
384                 return NULL;
385         }
386
387         return acl;
388 }
389
390 /*
391   set a simple ACL on a file, as a test
392  */
393 static PyObject *py_smbd_set_simple_acl(PyObject *self, PyObject *args, PyObject *kwargs)
394 {
395         const char * const kwnames[] = { "fname", "mode", "gid", "service", NULL };
396         char *fname, *service = NULL;
397         int ret;
398         int mode, gid = -1;
399         SMB_ACL_T acl;
400         TALLOC_CTX *frame;
401         connection_struct *conn;
402
403         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|iz",
404                                          discard_const_p(char *, kwnames),
405                                          &fname, &mode, &gid, &service))
406                 return NULL;
407
408         frame = talloc_stackframe();
409
410         acl = make_simple_acl(frame, gid, mode);
411         if (acl == NULL) {
412                 TALLOC_FREE(frame);
413                 return NULL;
414         }
415
416         conn = get_conn_tos(service, NULL);
417         if (!conn) {
418                 TALLOC_FREE(frame);
419                 return NULL;
420         }
421
422         ret = set_sys_acl_conn(fname, SMB_ACL_TYPE_ACCESS, acl, conn);
423
424         if (ret != 0) {
425                 TALLOC_FREE(frame);
426                 errno = ret;
427                 return PyErr_SetFromErrno(PyExc_OSError);
428         }
429
430         TALLOC_FREE(frame);
431
432         Py_RETURN_NONE;
433 }
434
435 /*
436   chown a file
437  */
438 static PyObject *py_smbd_chown(PyObject *self, PyObject *args, PyObject *kwargs)
439 {
440         const char * const kwnames[] = { "fname", "uid", "gid", "service", NULL };
441         connection_struct *conn;
442         int ret;
443
444         char *fname, *service = NULL;
445         int uid, gid;
446         TALLOC_CTX *frame;
447         mode_t saved_umask;
448         struct smb_filename *smb_fname = NULL;
449
450         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sii|z",
451                                          discard_const_p(char *, kwnames),
452                                          &fname, &uid, &gid, &service))
453                 return NULL;
454
455         frame = talloc_stackframe();
456
457         conn = get_conn_tos(service, NULL);
458         if (!conn) {
459                 TALLOC_FREE(frame);
460                 return NULL;
461         }
462
463         /* we want total control over the permissions on created files,
464            so set our umask to 0 */
465         saved_umask = umask(0);
466
467         smb_fname = synthetic_smb_fname(talloc_tos(),
468                                         fname,
469                                         NULL,
470                                         NULL,
471                                         lp_posix_pathnames() ?
472                                                 SMB_FILENAME_POSIX_PATH : 0);
473         if (smb_fname == NULL) {
474                 umask(saved_umask);
475                 TALLOC_FREE(frame);
476                 errno = ENOMEM;
477                 return PyErr_SetFromErrno(PyExc_OSError);
478         }
479
480         ret = SMB_VFS_CHOWN(conn, smb_fname, uid, gid);
481         if (ret != 0) {
482                 umask(saved_umask);
483                 TALLOC_FREE(frame);
484                 errno = ret;
485                 return PyErr_SetFromErrno(PyExc_OSError);
486         }
487
488         umask(saved_umask);
489
490         TALLOC_FREE(frame);
491
492         Py_RETURN_NONE;
493 }
494
495 /*
496   unlink a file
497  */
498 static PyObject *py_smbd_unlink(PyObject *self, PyObject *args, PyObject *kwargs)
499 {
500         const char * const kwnames[] = { "fname", "service", NULL };
501         connection_struct *conn;
502         int ret;
503         struct smb_filename *smb_fname = NULL;
504         char *fname, *service = NULL;
505         TALLOC_CTX *frame;
506
507         frame = talloc_stackframe();
508
509         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|z",
510                                          discard_const_p(char *, kwnames),
511                                          &fname, &service)) {
512                 TALLOC_FREE(frame);
513                 return NULL;
514         }
515
516         conn = get_conn_tos(service, NULL);
517         if (!conn) {
518                 TALLOC_FREE(frame);
519                 return NULL;
520         }
521
522         smb_fname = synthetic_smb_fname_split(frame,
523                                         fname,
524                                         lp_posix_pathnames());
525         if (smb_fname == NULL) {
526                 TALLOC_FREE(frame);
527                 return PyErr_NoMemory();
528         }
529
530         ret = SMB_VFS_UNLINK(conn, smb_fname);
531         if (ret != 0) {
532                 TALLOC_FREE(frame);
533                 errno = ret;
534                 return PyErr_SetFromErrno(PyExc_OSError);
535         }
536
537         TALLOC_FREE(frame);
538
539         Py_RETURN_NONE;
540 }
541
542 /*
543   check if we have ACL support
544  */
545 static PyObject *py_smbd_have_posix_acls(PyObject *self)
546 {
547 #ifdef HAVE_POSIX_ACLS
548         return PyBool_FromLong(true);
549 #else
550         return PyBool_FromLong(false);
551 #endif
552 }
553
554 /*
555   set the NT ACL on a file
556  */
557 static PyObject *py_smbd_set_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
558 {
559         const char * const kwnames[] = {
560                 "fname", "security_info_sent", "sd",
561                 "service", "session_info", NULL };
562
563         NTSTATUS status;
564         char *fname, *service = NULL;
565         int security_info_sent;
566         PyObject *py_sd;
567         struct security_descriptor *sd;
568         PyObject *py_session = Py_None;
569         struct auth_session_info *session_info = NULL;
570         connection_struct *conn;
571         TALLOC_CTX *frame;
572
573         frame = talloc_stackframe();
574
575         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|zO",
576                                          discard_const_p(char *, kwnames),
577                                          &fname, &security_info_sent, &py_sd,
578                                          &service, &py_session)) {
579                 TALLOC_FREE(frame);
580                 return NULL;
581         }
582
583         if (!py_check_dcerpc_type(py_sd, "samba.dcerpc.security", "descriptor")) {
584                 TALLOC_FREE(frame);
585                 return NULL;
586         }
587
588         if (py_session != Py_None) {
589                 if (!py_check_dcerpc_type(py_session,
590                                           "samba.dcerpc.auth",
591                                           "session_info")) {
592                         TALLOC_FREE(frame);
593                         return NULL;
594                 }
595                 session_info = pytalloc_get_type(py_session,
596                                                  struct auth_session_info);
597                 if (!session_info) {
598                         PyErr_Format(PyExc_TypeError,
599                                      "Expected auth_session_info for session_info argument got %s",
600                                      talloc_get_name(pytalloc_get_ptr(py_session)));
601                         return NULL;
602                 }
603         }
604
605         conn = get_conn_tos(service, session_info);
606         if (!conn) {
607                 TALLOC_FREE(frame);
608                 return NULL;
609         }
610
611         sd = pytalloc_get_type(py_sd, struct security_descriptor);
612
613         status = set_nt_acl_conn(fname, security_info_sent, sd, conn);
614         TALLOC_FREE(frame);
615         PyErr_NTSTATUS_IS_ERR_RAISE(status);
616
617         Py_RETURN_NONE;
618 }
619
620 /*
621   Return the NT ACL on a file
622  */
623 static PyObject *py_smbd_get_nt_acl(PyObject *self, PyObject *args, PyObject *kwargs)
624 {
625         const char * const kwnames[] = { "fname", "security_info_wanted", "service", NULL };
626         char *fname, *service = NULL;
627         int security_info_wanted;
628         PyObject *py_sd;
629         struct security_descriptor *sd;
630         TALLOC_CTX *frame = talloc_stackframe();
631         connection_struct *conn;
632         NTSTATUS status;
633
634         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z", discard_const_p(char *, kwnames),
635                                          &fname, &security_info_wanted, &service)) {
636                 TALLOC_FREE(frame);
637                 return NULL;
638         }
639
640         conn = get_conn_tos(service, NULL);
641         if (!conn) {
642                 TALLOC_FREE(frame);
643                 return NULL;
644         }
645
646         status = get_nt_acl_conn(frame, fname, conn, security_info_wanted, &sd);
647         PyErr_NTSTATUS_IS_ERR_RAISE(status);
648
649         py_sd = py_return_ndr_struct("samba.dcerpc.security", "descriptor", sd, sd);
650
651         TALLOC_FREE(frame);
652
653         return py_sd;
654 }
655
656 /*
657   set the posix (or similar) ACL on a file
658  */
659 static PyObject *py_smbd_set_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
660 {
661         const char * const kwnames[] = { "fname", "acl_type", "acl", "service", NULL };
662         TALLOC_CTX *frame = talloc_stackframe();
663         int ret;
664         char *fname, *service = NULL;
665         PyObject *py_acl;
666         struct smb_acl_t *acl;
667         int acl_type;
668         connection_struct *conn;
669
670         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|z",
671                                          discard_const_p(char *, kwnames),
672                                          &fname, &acl_type, &py_acl, &service)) {
673                 TALLOC_FREE(frame);
674                 return NULL;
675         }
676
677         if (!py_check_dcerpc_type(py_acl, "samba.dcerpc.smb_acl", "t")) {
678                 TALLOC_FREE(frame);
679                 return NULL;
680         }
681
682         conn = get_conn_tos(service, NULL);
683         if (!conn) {
684                 TALLOC_FREE(frame);
685                 return NULL;
686         }
687
688         acl = pytalloc_get_type(py_acl, struct smb_acl_t);
689
690         ret = set_sys_acl_conn(fname, acl_type, acl, conn);
691         if (ret != 0) {
692                 TALLOC_FREE(frame);
693                 errno = ret;
694                 return PyErr_SetFromErrno(PyExc_OSError);
695         }
696
697         TALLOC_FREE(frame);
698         Py_RETURN_NONE;
699 }
700
701 /*
702   Return the posix (or similar) ACL on a file
703  */
704 static PyObject *py_smbd_get_sys_acl(PyObject *self, PyObject *args, PyObject *kwargs)
705 {
706         const char * const kwnames[] = { "fname", "acl_type", "service", NULL };
707         char *fname;
708         PyObject *py_acl;
709         struct smb_acl_t *acl;
710         int acl_type;
711         TALLOC_CTX *frame = talloc_stackframe();
712         connection_struct *conn;
713         char *service = NULL;
714         struct smb_filename *smb_fname = NULL;
715
716         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "si|z",
717                                          discard_const_p(char *, kwnames),
718                                          &fname, &acl_type, &service)) {
719                 TALLOC_FREE(frame);
720                 return NULL;
721         }
722
723         conn = get_conn_tos(service, NULL);
724         if (!conn) {
725                 TALLOC_FREE(frame);
726                 return NULL;
727         }
728
729         smb_fname = synthetic_smb_fname_split(frame,
730                                         fname,
731                                         lp_posix_pathnames());
732         if (smb_fname == NULL) {
733                 TALLOC_FREE(frame);
734                 return NULL;
735         }
736         acl = SMB_VFS_SYS_ACL_GET_FILE( conn, smb_fname, acl_type, frame);
737         if (!acl) {
738                 TALLOC_FREE(frame);
739                 return PyErr_SetFromErrno(PyExc_OSError);
740         }
741
742         py_acl = py_return_ndr_struct("samba.dcerpc.smb_acl", "t", acl, acl);
743
744         TALLOC_FREE(frame);
745
746         return py_acl;
747 }
748
749 static PyObject *py_smbd_mkdir(PyObject *self, PyObject *args, PyObject *kwargs)
750 {
751         const char * const kwnames[] = { "fname", "service", NULL };
752         char *fname, *service = NULL;
753         TALLOC_CTX *frame = talloc_stackframe();
754         struct connection_struct *conn = NULL;
755         struct smb_filename *smb_fname = NULL;
756
757         if (!PyArg_ParseTupleAndKeywords(args,
758                                          kwargs,
759                                          "s|z",
760                                          discard_const_p(char *,
761                                                          kwnames),
762                                          &fname,
763                                          &service)) {
764                 TALLOC_FREE(frame);
765                 return NULL;
766         }
767
768         conn = get_conn_tos(service, NULL);
769         if (!conn) {
770                 TALLOC_FREE(frame);
771                 return NULL;
772         }
773
774         smb_fname = synthetic_smb_fname(talloc_tos(),
775                                         fname,
776                                         NULL,
777                                         NULL,
778                                         lp_posix_pathnames() ?
779                                         SMB_FILENAME_POSIX_PATH : 0);
780
781         if (smb_fname == NULL) {
782                 TALLOC_FREE(frame);
783                 return NULL;
784         }
785
786
787         if (SMB_VFS_MKDIR(conn, smb_fname, 00755) == -1) {
788                 DBG_ERR("mkdir error=%d (%s)\n", errno, strerror(errno));
789                 TALLOC_FREE(frame);
790                 return NULL;
791         }
792
793         TALLOC_FREE(frame);
794         Py_RETURN_NONE;
795 }
796
797
798 /*
799   Create an empty file
800  */
801 static PyObject *py_smbd_create_file(PyObject *self, PyObject *args, PyObject *kwargs)
802 {
803         const char * const kwnames[] = { "fname", "service", NULL };
804         char *fname, *service = NULL;
805         TALLOC_CTX *frame = talloc_stackframe();
806         struct connection_struct *conn = NULL;
807         struct files_struct *fsp = NULL;
808         NTSTATUS status;
809
810         if (!PyArg_ParseTupleAndKeywords(args,
811                                          kwargs,
812                                          "s|z",
813                                          discard_const_p(char *,
814                                                          kwnames),
815                                          &fname,
816                                          &service)) {
817                 TALLOC_FREE(frame);
818                 return NULL;
819         }
820
821         conn = get_conn_tos(service, NULL);
822         if (!conn) {
823                 TALLOC_FREE(frame);
824                 return NULL;
825         }
826
827         status = init_files_struct(frame,
828                                    fname,
829                                    conn,
830                                    O_CREAT|O_EXCL|O_RDWR,
831                                    &fsp);
832         if (!NT_STATUS_IS_OK(status)) {
833                 DBG_ERR("init_files_struct failed: %s\n",
834                         nt_errstr(status));
835         }
836
837         TALLOC_FREE(frame);
838         Py_RETURN_NONE;
839 }
840
841
842 static PyMethodDef py_smbd_methods[] = {
843         { "have_posix_acls",
844                 (PyCFunction)py_smbd_have_posix_acls, METH_NOARGS,
845                 NULL },
846         { "set_simple_acl",
847                 (PyCFunction)py_smbd_set_simple_acl, METH_VARARGS|METH_KEYWORDS,
848                 NULL },
849         { "set_nt_acl",
850                 (PyCFunction)py_smbd_set_nt_acl, METH_VARARGS|METH_KEYWORDS,
851                 NULL },
852         { "get_nt_acl",
853                 (PyCFunction)py_smbd_get_nt_acl, METH_VARARGS|METH_KEYWORDS,
854                 NULL },
855         { "get_sys_acl",
856                 (PyCFunction)py_smbd_get_sys_acl, METH_VARARGS|METH_KEYWORDS,
857                 NULL },
858         { "set_sys_acl",
859                 (PyCFunction)py_smbd_set_sys_acl, METH_VARARGS|METH_KEYWORDS,
860                 NULL },
861         { "chown",
862                 (PyCFunction)py_smbd_chown, METH_VARARGS|METH_KEYWORDS,
863                 NULL },
864         { "unlink",
865                 (PyCFunction)py_smbd_unlink, METH_VARARGS|METH_KEYWORDS,
866                 NULL },
867         { "mkdir",
868                 (PyCFunction)py_smbd_mkdir, METH_VARARGS|METH_KEYWORDS,
869                 NULL },
870         { "create_file",
871                 (PyCFunction)py_smbd_create_file, METH_VARARGS|METH_KEYWORDS,
872                 NULL },
873         { NULL }
874 };
875
876 void initsmbd(void);
877
878 static struct PyModuleDef moduledef = {
879     PyModuleDef_HEAD_INIT,
880     .m_name = "smbd",
881     .m_doc = "Python bindings for the smbd file server.",
882     .m_size = -1,
883     .m_methods = py_smbd_methods,
884 };
885
886 MODULE_INIT_FUNC(smbd)
887 {
888         PyObject *m = NULL;
889
890         m = PyModule_Create(&moduledef);
891         return m;
892 }