1 /*
2    Unix SMB/Netbios implementation.
3    VFS module to get and set posix acls
4    Copyright (C) Volker Lendecke 2006
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/smbd.h"
23 #include "modules/vfs_posixacl.h"
24 
25 /* prototypes for static functions first - for clarity */
26 
27 static bool smb_ace_to_internal(acl_entry_t posix_ace,
28 				struct smb_acl_entry *ace);
29 static struct smb_acl_t *smb_acl_to_internal(acl_t acl, TALLOC_CTX *mem_ctx);
30 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm);
31 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl);
32 
33 
34 /* public functions - the api */
35 
posixacl_sys_acl_get_file(vfs_handle_struct * handle,const struct smb_filename * smb_fname,SMB_ACL_TYPE_T type,TALLOC_CTX * mem_ctx)36 SMB_ACL_T posixacl_sys_acl_get_file(vfs_handle_struct *handle,
37 				const struct smb_filename *smb_fname,
38 				SMB_ACL_TYPE_T type,
39 				TALLOC_CTX *mem_ctx)
40 {
41 	struct smb_acl_t *result;
42 	acl_type_t acl_type;
43 	acl_t acl;
44 
45 	switch(type) {
46 	case SMB_ACL_TYPE_ACCESS:
47 		acl_type = ACL_TYPE_ACCESS;
48 		break;
49 	case SMB_ACL_TYPE_DEFAULT:
50 		acl_type = ACL_TYPE_DEFAULT;
51 		break;
52 	default:
53 		errno = EINVAL;
54 		return NULL;
55 	}
56 
57 	acl = acl_get_file(smb_fname->base_name, acl_type);
58 
59 	if (acl == NULL) {
60 		return NULL;
61 	}
62 
63 	result = smb_acl_to_internal(acl, mem_ctx);
64 	acl_free(acl);
65 	return result;
66 }
67 
posixacl_sys_acl_get_fd(vfs_handle_struct * handle,files_struct * fsp,TALLOC_CTX * mem_ctx)68 SMB_ACL_T posixacl_sys_acl_get_fd(vfs_handle_struct *handle,
69 				  files_struct *fsp, TALLOC_CTX *mem_ctx)
70 {
71 	struct smb_acl_t *result;
72 	acl_t acl = acl_get_fd(fsp->fh->fd);
73 
74 	if (acl == NULL) {
75 		return NULL;
76 	}
77 
78 	result = smb_acl_to_internal(acl, mem_ctx);
79 	acl_free(acl);
80 	return result;
81 }
82 
posixacl_sys_acl_set_file(vfs_handle_struct * handle,const struct smb_filename * smb_fname,SMB_ACL_TYPE_T type,SMB_ACL_T theacl)83 int posixacl_sys_acl_set_file(vfs_handle_struct *handle,
84 			      const struct smb_filename *smb_fname,
85 			      SMB_ACL_TYPE_T type,
86 			      SMB_ACL_T theacl)
87 {
88 	int res;
89 	acl_type_t acl_type;
90 	acl_t acl;
91 
92 	DEBUG(10, ("Calling acl_set_file: %s, %d\n",
93 			smb_fname->base_name,
94 			type));
95 
96 	switch(type) {
97 	case SMB_ACL_TYPE_ACCESS:
98 		acl_type = ACL_TYPE_ACCESS;
99 		break;
100 	case SMB_ACL_TYPE_DEFAULT:
101 		acl_type = ACL_TYPE_DEFAULT;
102 		break;
103 	default:
104 		errno = EINVAL;
105 		return -1;
106 	}
107 
108 	if ((acl = smb_acl_to_posix(theacl)) == NULL) {
109 		return -1;
110 	}
111 	res = acl_set_file(smb_fname->base_name, acl_type, acl);
112 	if (res != 0) {
113 		DEBUG(10, ("acl_set_file failed: %s\n", strerror(errno)));
114 	}
115 	acl_free(acl);
116 	return res;
117 }
118 
posixacl_sys_acl_set_fd(vfs_handle_struct * handle,files_struct * fsp,SMB_ACL_T theacl)119 int posixacl_sys_acl_set_fd(vfs_handle_struct *handle,
120 			    files_struct *fsp,
121 			    SMB_ACL_T theacl)
122 {
123 	int res;
124 	acl_t acl = smb_acl_to_posix(theacl);
125 	if (acl == NULL) {
126 		return -1;
127 	}
128 	res =  acl_set_fd(fsp->fh->fd, acl);
129 	acl_free(acl);
130 	return res;
131 }
132 
posixacl_sys_acl_delete_def_file(vfs_handle_struct * handle,const struct smb_filename * smb_fname)133 int posixacl_sys_acl_delete_def_file(vfs_handle_struct *handle,
134 				const struct smb_filename *smb_fname)
135 {
136 	return acl_delete_def_file(smb_fname->base_name);
137 }
138 
139 
140 /* private functions */
141 
smb_ace_to_internal(acl_entry_t posix_ace,struct smb_acl_entry * ace)142 static bool smb_ace_to_internal(acl_entry_t posix_ace,
143 				struct smb_acl_entry *ace)
144 {
145 	acl_tag_t tag;
146 	acl_permset_t permset;
147 
148 	if (acl_get_tag_type(posix_ace, &tag) != 0) {
149 		DEBUG(0, ("smb_acl_get_tag_type failed\n"));
150 		return False;
151 	}
152 
153 	switch(tag) {
154 	case ACL_USER:
155 		ace->a_type = SMB_ACL_USER;
156 		break;
157 	case ACL_USER_OBJ:
158 		ace->a_type = SMB_ACL_USER_OBJ;
159 		break;
160 	case ACL_GROUP:
161 		ace->a_type = SMB_ACL_GROUP;
162 		break;
163 	case ACL_GROUP_OBJ:
164 		ace->a_type = SMB_ACL_GROUP_OBJ;
165 		break;
166 	case ACL_OTHER:
167 		ace->a_type = SMB_ACL_OTHER;
168 		break;
169 	case ACL_MASK:
170 		ace->a_type = SMB_ACL_MASK;
171 		break;
172 #ifdef HAVE_ACL_EVERYONE
173 	case ACL_EVERYONE:
174 		DEBUG(1, ("ACL tag type ACL_EVERYONE. FreeBSD with ZFS? Use 'vfs objects = zfsacl'\n"));
175 		return false;
176 #endif
177 	default:
178 		DEBUG(0, ("unknown tag type %d\n", (unsigned int)tag));
179 		return False;
180 	}
181 	switch(ace->a_type) {
182 	case SMB_ACL_USER: {
183 		uid_t *puid = (uid_t *)acl_get_qualifier(posix_ace);
184 		if (puid == NULL) {
185 			DEBUG(0, ("smb_acl_get_qualifier failed\n"));
186 			return False;
187 		}
188 		ace->info.user.uid = *puid;
189 		acl_free(puid);
190 		break;
191 	}
192 
193 	case SMB_ACL_GROUP: {
194 		gid_t *pgid = (uid_t *)acl_get_qualifier(posix_ace);
195 		if (pgid == NULL) {
196 			DEBUG(0, ("smb_acl_get_qualifier failed\n"));
197 			return False;
198 		}
199 		ace->info.group.gid = *pgid;
200 		acl_free(pgid);
201 		break;
202 	}
203 	default:
204 		break;
205 	}
206 	if (acl_get_permset(posix_ace, &permset) != 0) {
207 		DEBUG(0, ("smb_acl_get_mode failed\n"));
208 		return False;
209 	}
210 	ace->a_perm = 0;
211 #ifdef HAVE_ACL_GET_PERM_NP
212 	ace->a_perm |= (acl_get_perm_np(permset, ACL_READ) ? SMB_ACL_READ : 0);
213 	ace->a_perm |= (acl_get_perm_np(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
214 	ace->a_perm |= (acl_get_perm_np(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
215 #else
216 	ace->a_perm |= (acl_get_perm(permset, ACL_READ) ? SMB_ACL_READ : 0);
217 	ace->a_perm |= (acl_get_perm(permset, ACL_WRITE) ? SMB_ACL_WRITE : 0);
218 	ace->a_perm |= (acl_get_perm(permset, ACL_EXECUTE) ? SMB_ACL_EXECUTE : 0);
219 #endif
220 	return True;
221 }
222 
smb_acl_to_internal(acl_t acl,TALLOC_CTX * mem_ctx)223 static struct smb_acl_t *smb_acl_to_internal(acl_t acl, TALLOC_CTX *mem_ctx)
224 {
225 	struct smb_acl_t *result = sys_acl_init(mem_ctx);
226 	int entry_id = ACL_FIRST_ENTRY;
227 	acl_entry_t e;
228 	if (result == NULL) {
229 		return NULL;
230 	}
231 	while (acl_get_entry(acl, entry_id, &e) == 1) {
232 
233 		entry_id = ACL_NEXT_ENTRY;
234 
235 		result->acl = talloc_realloc(result, result->acl,
236 					     struct smb_acl_entry, result->count+1);
237 		if (result->acl == NULL) {
238 			TALLOC_FREE(result);
239 			DEBUG(0, ("talloc_realloc failed\n"));
240 			errno = ENOMEM;
241 			return NULL;
242 		}
243 
244 		if (!smb_ace_to_internal(e, &result->acl[result->count])) {
245 			TALLOC_FREE(result);
246 			return NULL;
247 		}
248 
249 		result->count += 1;
250 	}
251 	return result;
252 }
253 
smb_acl_set_mode(acl_entry_t entry,SMB_ACL_PERM_T perm)254 static int smb_acl_set_mode(acl_entry_t entry, SMB_ACL_PERM_T perm)
255 {
256         int ret;
257         acl_permset_t permset;
258 
259 	if ((ret = acl_get_permset(entry, &permset)) != 0) {
260 		return ret;
261 	}
262         if ((ret = acl_clear_perms(permset)) != 0) {
263                 return ret;
264 	}
265         if ((perm & SMB_ACL_READ) &&
266 	    ((ret = acl_add_perm(permset, ACL_READ)) != 0)) {
267 		return ret;
268 	}
269         if ((perm & SMB_ACL_WRITE) &&
270 	    ((ret = acl_add_perm(permset, ACL_WRITE)) != 0)) {
271 		return ret;
272 	}
273         if ((perm & SMB_ACL_EXECUTE) &&
274 	    ((ret = acl_add_perm(permset, ACL_EXECUTE)) != 0)) {
275 		return ret;
276 	}
277         return acl_set_permset(entry, permset);
278 }
279 
smb_acl_to_posix(const struct smb_acl_t * acl)280 static acl_t smb_acl_to_posix(const struct smb_acl_t *acl)
281 {
282 	acl_t result;
283 	int i;
284 
285 	result = acl_init(acl->count);
286 	if (result == NULL) {
287 		DEBUG(10, ("acl_init failed\n"));
288 		return NULL;
289 	}
290 
291 	for (i=0; i<acl->count; i++) {
292 		const struct smb_acl_entry *entry = &acl->acl[i];
293 		acl_entry_t e;
294 		acl_tag_t tag;
295 
296 		if (acl_create_entry(&result, &e) != 0) {
297 			DEBUG(1, ("acl_create_entry failed: %s\n",
298 				  strerror(errno)));
299 			goto fail;
300 		}
301 
302 		switch (entry->a_type) {
303 		case SMB_ACL_USER:
304 			tag = ACL_USER;
305 			break;
306 		case SMB_ACL_USER_OBJ:
307 			tag = ACL_USER_OBJ;
308 			break;
309 		case SMB_ACL_GROUP:
310 			tag = ACL_GROUP;
311 			break;
312 		case SMB_ACL_GROUP_OBJ:
313 			tag = ACL_GROUP_OBJ;
314 			break;
315 		case SMB_ACL_OTHER:
316 			tag = ACL_OTHER;
317 			break;
318 		case SMB_ACL_MASK:
319 			tag = ACL_MASK;
320 			break;
321 		default:
322 			DEBUG(1, ("Unknown tag value %d\n", entry->a_type));
323 			goto fail;
324 		}
325 
326 		if (acl_set_tag_type(e, tag) != 0) {
327 			DEBUG(10, ("acl_set_tag_type(%d) failed: %s\n",
328 				   tag, strerror(errno)));
329 			goto fail;
330 		}
331 
332 		switch (entry->a_type) {
333 		case SMB_ACL_USER:
334 			if (acl_set_qualifier(e, &entry->info.user.uid) != 0) {
335 				DEBUG(1, ("acl_set_qualifiier failed: %s\n",
336 					  strerror(errno)));
337 				goto fail;
338 			}
339 			break;
340 		case SMB_ACL_GROUP:
341 			if (acl_set_qualifier(e, &entry->info.group.gid) != 0) {
342 				DEBUG(1, ("acl_set_qualifiier failed: %s\n",
343 					  strerror(errno)));
344 				goto fail;
345 			}
346 			break;
347 		default: 	/* Shut up, compiler! :-) */
348 			break;
349 		}
350 
351 		if (smb_acl_set_mode(e, entry->a_perm) != 0) {
352 			goto fail;
353 		}
354 	}
355 
356 	if (acl_valid(result) != 0) {
357 		char *acl_string = sys_acl_to_text(acl, NULL);
358 		DEBUG(0, ("smb_acl_to_posix: ACL %s is invalid for set (%s)\n",
359 			  acl_string, strerror(errno)));
360 		SAFE_FREE(acl_string);
361 		goto fail;
362 	}
363 
364 	return result;
365 
366  fail:
367 	if (result != NULL) {
368 		acl_free(result);
369 	}
370 	return NULL;
371 }
372 
373 /* VFS operations structure */
374 
375 static struct vfs_fn_pointers posixacl_fns = {
376 	.sys_acl_get_file_fn = posixacl_sys_acl_get_file,
377 	.sys_acl_get_fd_fn = posixacl_sys_acl_get_fd,
378 	.sys_acl_blob_get_file_fn = posix_sys_acl_blob_get_file,
379 	.sys_acl_blob_get_fd_fn = posix_sys_acl_blob_get_fd,
380 	.sys_acl_set_file_fn = posixacl_sys_acl_set_file,
381 	.sys_acl_set_fd_fn = posixacl_sys_acl_set_fd,
382 	.sys_acl_delete_def_file_fn = posixacl_sys_acl_delete_def_file,
383 };
384 
385 static_decl_vfs;
vfs_posixacl_init(TALLOC_CTX * ctx)386 NTSTATUS vfs_posixacl_init(TALLOC_CTX *ctx)
387 {
388 	return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "posixacl",
389 				&posixacl_fns);
390 }
391