s3: OneFS unmappable sids support.
[samba.git] / source3 / modules / onefs_system.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Support for OneFS system interfaces.
4  *
5  * Copyright (C) Tim Prouty, 2008
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "onefs.h"
22
23 #include <ifs/ifs_syscalls.h>
24 #include <isi_acl/isi_acl_util.h>
25
26 /*
27  * Initialize the sm_lock struct before passing it to ifs_createfile.
28  */
29 static void smlock_init(connection_struct *conn, struct sm_lock *sml,
30     bool isexe, uint32_t access_mask, uint32_t share_access,
31     uint32_t create_options)
32 {
33         sml->sm_type.doc = false;
34         sml->sm_type.isexe = isexe;
35         sml->sm_type.statonly = is_stat_open(access_mask);
36         sml->sm_type.access_mask = access_mask;
37         sml->sm_type.share_access = share_access;
38
39         /*
40          * private_options was previously used for DENY_DOS/DENY_FCB checks in
41          * the kernel, but are now properly handled by fcb_or_dos_open. In
42          * these cases, ifs_createfile will return a sharing violation, which
43          * gives fcb_or_dos_open the chance to open a duplicate file handle.
44          */
45         sml->sm_type.private_options = 0;
46
47         /* 1 second delay is handled in onefs_open.c by deferring the open */
48         sml->sm_timeout = timeval_set(0, 0);
49 }
50
51 static void smlock_dump(int debuglevel, const struct sm_lock *sml)
52 {
53         if (sml == NULL) {
54                 DEBUG(debuglevel, ("sml == NULL\n"));
55                 return;
56         }
57
58         DEBUG(debuglevel,
59               ("smlock: doc=%s, isexec=%s, statonly=%s, access_mask=0x%x, "
60                "share_access=0x%x, private_options=0x%x timeout=%d/%d\n",
61                sml->sm_type.doc ? "True" : "False",
62                sml->sm_type.isexe ? "True" : "False",
63                sml->sm_type.statonly ? "True" : "False",
64                sml->sm_type.access_mask,
65                sml->sm_type.share_access,
66                sml->sm_type.private_options,
67                (int)sml->sm_timeout.tv_sec,
68                (int)sml->sm_timeout.tv_usec));
69 }
70
71 /*
72  * Return string value of onefs oplock types.
73  */
74 static const char *onefs_oplock_str(enum oplock_type onefs_oplock_type)
75 {
76         switch (onefs_oplock_type) {
77         case OPLOCK_NONE:
78                 return "OPLOCK_NONE";
79         case OPLOCK_EXCLUSIVE:
80                 return "OPLOCK_EXCLUSIVE";
81         case OPLOCK_BATCH:
82                 return "OPLOCK_BATCH";
83         case OPLOCK_SHARED:
84                 return "OPLOCK_SHARED";
85         default:
86                 break;
87         }
88         return "UNKNOWN";
89 }
90
91 /*
92  * Convert from onefs to samba oplock.
93  */
94 static int onefs_oplock_to_samba_oplock(enum oplock_type onefs_oplock)
95 {
96         switch (onefs_oplock) {
97         case OPLOCK_NONE:
98                 return NO_OPLOCK;
99         case OPLOCK_EXCLUSIVE:
100                 return EXCLUSIVE_OPLOCK;
101         case OPLOCK_BATCH:
102                 return BATCH_OPLOCK;
103         case OPLOCK_SHARED:
104                 return LEVEL_II_OPLOCK;
105         default:
106                 DEBUG(0, ("unknown oplock type %d found\n", onefs_oplock));
107                 break;
108         }
109         return NO_OPLOCK;
110 }
111
112 /*
113  * Convert from samba to onefs oplock.
114  */
115 static enum oplock_type onefs_samba_oplock_to_oplock(int samba_oplock_type)
116 {
117         if (BATCH_OPLOCK_TYPE(samba_oplock_type)) return OPLOCK_BATCH;
118         if (EXCLUSIVE_OPLOCK_TYPE(samba_oplock_type)) return OPLOCK_EXCLUSIVE;
119         if (LEVEL_II_OPLOCK_TYPE(samba_oplock_type)) return OPLOCK_SHARED;
120         return OPLOCK_NONE;
121 }
122
123 /**
124  * External interface to ifs_createfile
125  */
126 int onefs_sys_create_file(connection_struct *conn,
127                           int base_fd,
128                           const char *path,
129                           uint32_t access_mask,
130                           uint32_t open_access_mask,
131                           uint32_t share_access,
132                           uint32_t create_options,
133                           int flags,
134                           mode_t mode,
135                           int oplock_request,
136                           uint64_t id,
137                           struct security_descriptor *sd,
138                           uint32_t dos_flags,
139                           int *granted_oplock)
140 {
141         struct sm_lock sml, *psml = NULL;
142         enum oplock_type onefs_oplock;
143         enum oplock_type onefs_granted_oplock = OPLOCK_NONE;
144         struct ifs_security_descriptor ifs_sd = {}, *pifs_sd = NULL;
145         int secinfo = 0;
146         int ret_fd = -1;
147         uint32_t onefs_dos_attributes;
148
149         /* Setup security descriptor and get secinfo. */
150         if (sd != NULL) {
151                 NTSTATUS status;
152
153                 secinfo = (get_sec_info(sd) & IFS_SEC_INFO_KNOWN_MASK);
154
155                 status = onefs_samba_sd_to_sd(secinfo, sd, &ifs_sd, SNUM(conn));
156
157                 if (!NT_STATUS_IS_OK(status)) {
158                         DEBUG(1, ("SD initialization failure: %s",
159                                   nt_errstr(status)));
160                         errno = EINVAL;
161                         goto out;
162                 }
163
164                 pifs_sd = &ifs_sd;
165         }
166
167         onefs_oplock = onefs_samba_oplock_to_oplock(oplock_request);
168
169         /* Temporary until oplock work is added to vfs_onefs */
170         onefs_oplock = OPLOCK_NONE;
171
172         /* Convert samba dos flags to UF_DOS_* attributes. */
173         onefs_dos_attributes = dos_attributes_to_stat_dos_flags(dos_flags);
174
175         DEBUG(10,("onefs_sys_create_file: base_fd = %d, "
176                   "open_access_mask = 0x%x, flags = 0x%x, mode = 0x%x, "
177                   "desired_oplock = %s, id = 0x%x, secinfo = 0x%x, sd = %p, "
178                   "dos_attributes = 0x%x, path = %s\n", base_fd,
179                   (unsigned int)open_access_mask,
180                   (unsigned int)flags,
181                   (unsigned int)mode,
182                   onefs_oplock_str(onefs_oplock),
183                   (unsigned int)id,
184                   (unsigned int)secinfo, sd,
185                   (unsigned int)onefs_dos_attributes, path));
186
187         /* Initialize smlock struct for files/dirs but not internal opens */
188         if (!(oplock_request & INTERNAL_OPEN_ONLY)) {
189                 smlock_init(conn, &sml, is_executable(path), access_mask,
190                     share_access, create_options);
191                 psml = &sml;
192         }
193
194         smlock_dump(10, psml);
195
196         ret_fd = ifs_createfile(base_fd, path,
197             (enum ifs_ace_rights)open_access_mask, flags & ~O_ACCMODE, mode,
198             onefs_oplock, id, psml, secinfo, pifs_sd, onefs_dos_attributes,
199             &onefs_granted_oplock);
200
201         DEBUG(10,("onefs_sys_create_file(%s): ret_fd = %d, "
202                   "onefs_granted_oplock = %s\n",
203                   ret_fd < 0 ? strerror(errno) : "success", ret_fd,
204                   onefs_oplock_str(onefs_granted_oplock)));
205
206         if (granted_oplock) {
207                 *granted_oplock =
208                     onefs_oplock_to_samba_oplock(onefs_granted_oplock);
209         }
210
211  out:
212         aclu_free_sd(pifs_sd, false);
213
214         return ret_fd;
215 }