xref: /titanic_52/usr/src/lib/pyzfs/common/ioctl.c (revision 23a9f846c20554daf41a34c0f67d0184f9bb792f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <Python.h>
27 #include <sys/zfs_ioctl.h>
28 #include <sys/fs/zfs.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #include <libnvpair.h>
32 #include <idmap.h>
33 #include <zone.h>
34 #include <libintl.h>
35 #include <libzfs.h>
36 #include <directory.h>
37 #include "zfs_prop.h"
38 
39 static PyObject *ZFSError;
40 static int zfsdevfd;
41 
42 #ifdef __lint
43 #define	dgettext(x, y) y
44 #endif
45 
46 #define	_(s) dgettext(TEXT_DOMAIN, s)
47 
48 extern int sid_to_id(char *sid, boolean_t user, uid_t *id);
49 
50 /*PRINTFLIKE1*/
51 static void
52 seterr(char *fmt, ...)
53 {
54 	char errstr[1024];
55 	va_list v;
56 
57 	va_start(v, fmt);
58 	(void) vsnprintf(errstr, sizeof (errstr), fmt, v);
59 	va_end(v);
60 
61 	PyErr_SetObject(ZFSError, Py_BuildValue("is", errno, errstr));
62 }
63 
64 static char cmdstr[HIS_MAX_RECORD_LEN];
65 
66 static int
67 ioctl_with_cmdstr(int ioc, zfs_cmd_t *zc)
68 {
69 	int err;
70 
71 	if (cmdstr[0])
72 		zc->zc_history = (uint64_t)(uintptr_t)cmdstr;
73 	err = ioctl(zfsdevfd, ioc, zc);
74 	cmdstr[0] = '\0';
75 	return (err);
76 }
77 
78 static PyObject *
79 nvl2py(nvlist_t *nvl)
80 {
81 	PyObject *pyo;
82 	nvpair_t *nvp;
83 
84 	pyo = PyDict_New();
85 
86 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp;
87 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
88 		PyObject *pyval;
89 		char *sval;
90 		uint64_t ival;
91 		boolean_t bval;
92 		nvlist_t *nval;
93 
94 		switch (nvpair_type(nvp)) {
95 		case DATA_TYPE_STRING:
96 			(void) nvpair_value_string(nvp, &sval);
97 			pyval = Py_BuildValue("s", sval);
98 			break;
99 
100 		case DATA_TYPE_UINT64:
101 			(void) nvpair_value_uint64(nvp, &ival);
102 			pyval = Py_BuildValue("K", ival);
103 			break;
104 
105 		case DATA_TYPE_NVLIST:
106 			(void) nvpair_value_nvlist(nvp, &nval);
107 			pyval = nvl2py(nval);
108 			break;
109 
110 		case DATA_TYPE_BOOLEAN:
111 			Py_INCREF(Py_None);
112 			pyval = Py_None;
113 			break;
114 
115 		case DATA_TYPE_BOOLEAN_VALUE:
116 			(void) nvpair_value_boolean_value(nvp, &bval);
117 			pyval = Py_BuildValue("i", bval);
118 			break;
119 
120 		default:
121 			PyErr_SetNone(PyExc_ValueError);
122 			Py_DECREF(pyo);
123 			return (NULL);
124 		}
125 
126 		PyDict_SetItemString(pyo, nvpair_name(nvp), pyval);
127 		Py_DECREF(pyval);
128 	}
129 
130 	return (pyo);
131 }
132 
133 static nvlist_t *
134 dict2nvl(PyObject *d)
135 {
136 	nvlist_t *nvl;
137 	int err;
138 	PyObject *key, *value;
139 	int pos = 0;
140 
141 	if (!PyDict_Check(d)) {
142 		PyErr_SetObject(PyExc_ValueError, d);
143 		return (NULL);
144 	}
145 
146 	err = nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0);
147 	assert(err == 0);
148 
149 	while (PyDict_Next(d, &pos, &key, &value)) {
150 		char *keystr = PyString_AsString(key);
151 		if (keystr == NULL) {
152 			PyErr_SetObject(PyExc_KeyError, key);
153 			nvlist_free(nvl);
154 			return (NULL);
155 		}
156 
157 		if (PyDict_Check(value)) {
158 			nvlist_t *valnvl = dict2nvl(value);
159 			err = nvlist_add_nvlist(nvl, keystr, valnvl);
160 			nvlist_free(valnvl);
161 		} else if (value == Py_None) {
162 			err = nvlist_add_boolean(nvl, keystr);
163 		} else if (PyString_Check(value)) {
164 			char *valstr = PyString_AsString(value);
165 			err = nvlist_add_string(nvl, keystr, valstr);
166 		} else if (PyInt_Check(value)) {
167 			uint64_t valint = PyInt_AsUnsignedLongLongMask(value);
168 			err = nvlist_add_uint64(nvl, keystr, valint);
169 		} else if (PyBool_Check(value)) {
170 			boolean_t valbool = value == Py_True ? B_TRUE : B_FALSE;
171 			err = nvlist_add_boolean_value(nvl, keystr, valbool);
172 		} else {
173 			PyErr_SetObject(PyExc_ValueError, value);
174 			nvlist_free(nvl);
175 			return (NULL);
176 		}
177 		assert(err == 0);
178 	}
179 
180 	return (nvl);
181 }
182 
183 static PyObject *
184 fakepropval(uint64_t value)
185 {
186 	PyObject *d = PyDict_New();
187 	PyDict_SetItemString(d, "value", Py_BuildValue("K", value));
188 	return (d);
189 }
190 
191 static void
192 add_ds_props(zfs_cmd_t *zc, PyObject *nvl)
193 {
194 	dmu_objset_stats_t *s = &zc->zc_objset_stats;
195 	PyDict_SetItemString(nvl, "numclones",
196 	    fakepropval(s->dds_num_clones));
197 	PyDict_SetItemString(nvl, "issnap",
198 	    fakepropval(s->dds_is_snapshot));
199 	PyDict_SetItemString(nvl, "inconsistent",
200 	    fakepropval(s->dds_inconsistent));
201 }
202 
203 /* On error, returns NULL but does not set python exception. */
204 static PyObject *
205 ioctl_with_dstnv(int ioc, zfs_cmd_t *zc)
206 {
207 	int nvsz = 2048;
208 	void *nvbuf;
209 	PyObject *pynv = NULL;
210 
211 again:
212 	nvbuf = malloc(nvsz);
213 	zc->zc_nvlist_dst_size = nvsz;
214 	zc->zc_nvlist_dst = (uintptr_t)nvbuf;
215 
216 	if (ioctl(zfsdevfd, ioc, zc) == 0) {
217 		nvlist_t *nvl;
218 
219 		errno = nvlist_unpack(nvbuf, zc->zc_nvlist_dst_size, &nvl, 0);
220 		if (errno == 0) {
221 			pynv = nvl2py(nvl);
222 			nvlist_free(nvl);
223 		}
224 	} else if (errno == ENOMEM) {
225 		free(nvbuf);
226 		nvsz = zc->zc_nvlist_dst_size;
227 		goto again;
228 	}
229 	free(nvbuf);
230 	return (pynv);
231 }
232 
233 static PyObject *
234 py_next_dataset(PyObject *self, PyObject *args)
235 {
236 	int ioc;
237 	uint64_t cookie;
238 	zfs_cmd_t zc = { 0 };
239 	int snaps;
240 	char *name;
241 	PyObject *nvl;
242 	PyObject *ret = NULL;
243 
244 	if (!PyArg_ParseTuple(args, "siK", &name, &snaps, &cookie))
245 		return (NULL);
246 
247 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
248 	zc.zc_cookie = cookie;
249 
250 	if (snaps)
251 		ioc = ZFS_IOC_SNAPSHOT_LIST_NEXT;
252 	else
253 		ioc = ZFS_IOC_DATASET_LIST_NEXT;
254 
255 	nvl = ioctl_with_dstnv(ioc, &zc);
256 	if (nvl) {
257 		add_ds_props(&zc, nvl);
258 		ret = Py_BuildValue("sKO", zc.zc_name, zc.zc_cookie, nvl);
259 		Py_DECREF(nvl);
260 	} else if (errno == ESRCH) {
261 		PyErr_SetNone(PyExc_StopIteration);
262 	} else {
263 		if (snaps)
264 			seterr(_("cannot get snapshots of %s"), name);
265 		else
266 			seterr(_("cannot get child datasets of %s"), name);
267 	}
268 	return (ret);
269 }
270 
271 static PyObject *
272 py_dataset_props(PyObject *self, PyObject *args)
273 {
274 	zfs_cmd_t zc = { 0 };
275 	int snaps;
276 	char *name;
277 	PyObject *nvl;
278 
279 	if (!PyArg_ParseTuple(args, "s", &name))
280 		return (NULL);
281 
282 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
283 
284 	nvl = ioctl_with_dstnv(ZFS_IOC_OBJSET_STATS, &zc);
285 	if (nvl) {
286 		add_ds_props(&zc, nvl);
287 	} else {
288 		seterr(_("cannot access dataset %s"), name);
289 	}
290 	return (nvl);
291 }
292 
293 static PyObject *
294 py_get_fsacl(PyObject *self, PyObject *args)
295 {
296 	zfs_cmd_t zc = { 0 };
297 	char *name;
298 	PyObject *nvl;
299 
300 	if (!PyArg_ParseTuple(args, "s", &name))
301 		return (NULL);
302 
303 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
304 
305 	nvl = ioctl_with_dstnv(ZFS_IOC_GET_FSACL, &zc);
306 	if (nvl == NULL)
307 		seterr(_("cannot get permissions on %s"), name);
308 
309 	return (nvl);
310 }
311 
312 static PyObject *
313 py_set_fsacl(PyObject *self, PyObject *args)
314 {
315 	int un;
316 	size_t nvsz;
317 	zfs_cmd_t zc = { 0 };
318 	char *name, *nvbuf;
319 	PyObject *dict, *file;
320 	nvlist_t *nvl;
321 	int err;
322 
323 	if (!PyArg_ParseTuple(args, "siO!", &name, &un,
324 	    &PyDict_Type, &dict))
325 		return (NULL);
326 
327 	nvl = dict2nvl(dict);
328 	if (nvl == NULL)
329 		return (NULL);
330 
331 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
332 	assert(err == 0);
333 	nvbuf = malloc(nvsz);
334 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
335 	assert(err == 0);
336 
337 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
338 	zc.zc_nvlist_src_size = nvsz;
339 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
340 	zc.zc_perm_action = un;
341 
342 	err = ioctl_with_cmdstr(ZFS_IOC_SET_FSACL, &zc);
343 	free(nvbuf);
344 	if (err) {
345 		seterr(_("cannot set permissions on %s"), name);
346 		return (NULL);
347 	}
348 
349 	Py_RETURN_NONE;
350 }
351 
352 static PyObject *
353 py_userspace_many(PyObject *self, PyObject *args)
354 {
355 	zfs_cmd_t zc = { 0 };
356 	zfs_userquota_prop_t type;
357 	char *name, *propname;
358 	int bufsz = 1<<20;
359 	void *buf;
360 	PyObject *dict, *file;
361 	int error;
362 
363 	if (!PyArg_ParseTuple(args, "ss", &name, &propname))
364 		return (NULL);
365 
366 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++)
367 		if (strcmp(propname, zfs_userquota_prop_prefixes[type]) == 0)
368 			break;
369 	if (type == ZFS_NUM_USERQUOTA_PROPS) {
370 		PyErr_SetString(PyExc_KeyError, propname);
371 		return (NULL);
372 	}
373 
374 	dict = PyDict_New();
375 	buf = malloc(bufsz);
376 
377 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
378 	zc.zc_objset_type = type;
379 	zc.zc_cookie = 0;
380 
381 	while (1) {
382 		zfs_useracct_t *zua = buf;
383 
384 		zc.zc_nvlist_dst = (uintptr_t)buf;
385 		zc.zc_nvlist_dst_size = bufsz;
386 
387 		error = ioctl(zfsdevfd, ZFS_IOC_USERSPACE_MANY, &zc);
388 		if (error || zc.zc_nvlist_dst_size == 0)
389 			break;
390 
391 		while (zc.zc_nvlist_dst_size > 0) {
392 			PyObject *pykey, *pyval;
393 
394 			pykey = Py_BuildValue("sI",
395 			    zua->zu_domain, zua->zu_rid);
396 			pyval = Py_BuildValue("K", zua->zu_space);
397 			PyDict_SetItem(dict, pykey, pyval);
398 			Py_DECREF(pykey);
399 			Py_DECREF(pyval);
400 
401 			zua++;
402 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
403 		}
404 	}
405 
406 	free(buf);
407 
408 	if (error != 0) {
409 		Py_DECREF(dict);
410 		seterr(_("cannot get %s property on %s"), propname, name);
411 		return (NULL);
412 	}
413 
414 	return (dict);
415 }
416 
417 static PyObject *
418 py_userspace_upgrade(PyObject *self, PyObject *args)
419 {
420 	zfs_cmd_t zc = { 0 };
421 	char *name;
422 	int error;
423 
424 	if (!PyArg_ParseTuple(args, "s", &name))
425 		return (NULL);
426 
427 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
428 	error = ioctl(zfsdevfd, ZFS_IOC_USERSPACE_UPGRADE, &zc);
429 
430 	if (error != 0) {
431 		seterr(_("cannot initialize user accounting information on %s"),
432 		    name);
433 		return (NULL);
434 	}
435 
436 	Py_RETURN_NONE;
437 }
438 
439 static PyObject *
440 py_sid_to_id(PyObject *self, PyObject *args)
441 {
442 	char *sid;
443 	int err, isuser;
444 	uid_t id;
445 
446 	if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
447 		return (NULL);
448 
449 	err = sid_to_id(sid, isuser, &id);
450 	if (err) {
451 		PyErr_SetString(PyExc_KeyError, sid);
452 		return (NULL);
453 	}
454 
455 	return (Py_BuildValue("I", id));
456 }
457 
458 /*
459  * Translate the sid string ("S-1-...") to the user@domain name, if
460  * possible.
461  */
462 static PyObject *
463 py_sid_to_name(PyObject *self, PyObject *args)
464 {
465 	int isuser;
466 	char *name, *sid;
467 	directory_error_t e;
468 	uint64_t classes;
469 	PyObject *ret;
470 
471 	if (!PyArg_ParseTuple(args, "si", &sid, &isuser))
472 		return (NULL);
473 	e = directory_name_from_sid(NULL, sid, &name, &classes);
474 	if (e != NULL) {
475 		directory_error_free(e);
476 		PyErr_SetString(PyExc_KeyError, sid);
477 		return (NULL);
478 	}
479 	if (name == NULL) {
480 		PyErr_SetString(PyExc_KeyError, sid);
481 		return (NULL);
482 	}
483 	if (isuser) {
484 		if (!(classes & DIRECTORY_CLASS_USER)) {
485 			free(name);
486 			PyErr_SetString(PyExc_KeyError, sid);
487 			return (NULL);
488 		}
489 	} else {
490 		if (!(classes & DIRECTORY_CLASS_GROUP)) {
491 			free(name);
492 			PyErr_SetString(PyExc_KeyError, sid);
493 			return (NULL);
494 		}
495 	}
496 
497 	ret = PyString_FromString(name);
498 	free(name);
499 	return (ret);
500 }
501 
502 static PyObject *
503 py_isglobalzone(PyObject *self, PyObject *args)
504 {
505 	return (Py_BuildValue("i", getzoneid() == GLOBAL_ZONEID));
506 }
507 
508 static PyObject *
509 py_set_cmdstr(PyObject *self, PyObject *args)
510 {
511 	char *str;
512 
513 	if (!PyArg_ParseTuple(args, "s", &str))
514 		return (NULL);
515 
516 	(void) strlcpy(cmdstr, str, sizeof (cmdstr));
517 
518 	Py_RETURN_NONE;
519 }
520 
521 static PyObject *
522 py_get_proptable(PyObject *self, PyObject *args)
523 {
524 	zprop_desc_t *t = zfs_prop_get_table();
525 	PyObject *d = PyDict_New();
526 	zfs_prop_t i;
527 
528 	for (i = 0; i < ZFS_NUM_PROPS; i++) {
529 		zprop_desc_t *p = &t[i];
530 		PyObject *tuple;
531 		static const char *typetable[] =
532 		    {"number", "string", "index"};
533 		static const char *attrtable[] =
534 		    {"default", "readonly", "inherit", "onetime"};
535 		PyObject *indextable;
536 
537 		if (p->pd_proptype == PROP_TYPE_INDEX) {
538 			const zprop_index_t *it = p->pd_table;
539 			indextable = PyDict_New();
540 			int j;
541 			for (j = 0; it[j].pi_name; j++) {
542 				PyDict_SetItemString(indextable,
543 				    it[j].pi_name,
544 				    Py_BuildValue("K", it[j].pi_value));
545 			}
546 		} else {
547 			Py_INCREF(Py_None);
548 			indextable = Py_None;
549 		}
550 
551 		tuple = Py_BuildValue("sissKsissiiO",
552 		    p->pd_name, p->pd_propnum, typetable[p->pd_proptype],
553 		    p->pd_strdefault, p->pd_numdefault,
554 		    attrtable[p->pd_attr], p->pd_types,
555 		    p->pd_values, p->pd_colname,
556 		    p->pd_rightalign, p->pd_visible, indextable);
557 		PyDict_SetItemString(d, p->pd_name, tuple);
558 		Py_DECREF(tuple);
559 	}
560 
561 	return (d);
562 }
563 
564 static PyMethodDef zfsmethods[] = {
565 	{"next_dataset", py_next_dataset, METH_VARARGS,
566 	    "Get next child dataset or snapshot."},
567 	{"get_fsacl", py_get_fsacl, METH_VARARGS, "Get allowed permissions."},
568 	{"set_fsacl", py_set_fsacl, METH_VARARGS, "Set allowed permissions."},
569 	{"userspace_many", py_userspace_many, METH_VARARGS,
570 	    "Get user space accounting."},
571 	{"userspace_upgrade", py_userspace_upgrade, METH_VARARGS,
572 	    "Upgrade fs to enable user space accounting."},
573 	{"set_cmdstr", py_set_cmdstr, METH_VARARGS,
574 	    "Set command string for history logging."},
575 	{"dataset_props", py_dataset_props, METH_VARARGS,
576 	    "Get dataset properties."},
577 	{"get_proptable", py_get_proptable, METH_NOARGS,
578 	    "Get property table."},
579 	/* Below are not really zfs-specific: */
580 	{"sid_to_id", py_sid_to_id, METH_VARARGS, "Map SID to UID/GID."},
581 	{"sid_to_name", py_sid_to_name, METH_VARARGS,
582 	    "Map SID to name@domain."},
583 	{"isglobalzone", py_isglobalzone, METH_NOARGS,
584 	    "Determine if this is the global zone."},
585 	{NULL, NULL, 0, NULL}
586 };
587 
588 void
589 initioctl(void)
590 {
591 	PyObject *zfs_ioctl = Py_InitModule("zfs.ioctl", zfsmethods);
592 	PyObject *zfs_util = PyImport_ImportModule("zfs.util");
593 	PyObject *devfile;
594 
595 	if (zfs_util == NULL)
596 		return;
597 
598 	ZFSError = PyObject_GetAttrString(zfs_util, "ZFSError");
599 	devfile = PyObject_GetAttrString(zfs_util, "dev");
600 	zfsdevfd = PyObject_AsFileDescriptor(devfile);
601 
602 	zfs_prop_init();
603 }
604