5749 libbe to provide library interface to validate BE name
authorTim Knitter <tim.knitter@sun.com>
Wed, 17 Dec 2008 09:37:59 -0700
changeset 397 b0d5a99fe64f
parent 396 bfa6f94f02b2
child 398 036d60d0e488
5749 libbe to provide library interface to validate BE name
usr/src/cmd/beadm/beadm.py
usr/src/cmd/beadm/messages.py
usr/src/lib/libbe/be_utils.c
usr/src/lib/libbe/libbe_priv.h
usr/src/lib/libbe_pymod/libbe.c
--- a/usr/src/cmd/beadm/beadm.py	Tue Jan 06 11:12:28 2009 -0700
+++ b/usr/src/cmd/beadm/beadm.py	Wed Dec 17 09:37:59 2008 -0700
@@ -19,7 +19,7 @@
 #
 # CDDL HEADER END
 #
-# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 # Use is subject to license terms.
 #
 # beadm - The Boot Environment Administration tool. Use this CLI to
@@ -90,6 +90,10 @@
 		usage()
 	
 	be = BootEnvironment()
+
+	if lb.beVerifyBEName(opts[0]) != 0:
+		msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1)
+		return 1
 		
 	rc = lb.beActivate(opts[0])
 	if rc == 0:
@@ -177,7 +181,7 @@
 	# Check that all info provided from the user is legitimate.
 	if (verifyCreateOptionsArgs(be) != 0):
 		usage()
-	
+
 	if initBELog("create", be) != 0:
 		return 1
 	
@@ -188,6 +192,10 @@
 		# Create a snapshot
 		rc = createSnapshot(be)
 	else:
+		if lb.beVerifyBEName(be.trgtBeNameOrSnapshot[0]) != 0:
+			msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1)
+			return 1
+
 		# Create a BE based on a snapshot
 		if be.srcBeNameOrSnapshot != None and \
 		    be.srcBeNameOrSnapshot.find("@") != -1:
@@ -255,6 +263,10 @@
 		msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1)
 		usage()
 
+	if lb.beVerifyBEName(be.trgtBeNameOrSnapshot[0]) != 0:
+		msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1)
+		return 1
+		
 	# Get the 'active' BE and the 'active on boot' BE.
 	beActive, beActiveOnBoot = \
 	    getActiveBEAndActiveOnBootBE(be.trgtBeNameOrSnapshot[0])
@@ -365,7 +377,7 @@
 	dontDisplayHeaders = False
 	beName = None
 	beList = None
-	
+
 	# Counters for detecting multiple options.
 	# e.g. beadm list -a -a newbe
 	numAOpts = 0; numDOpts = 0; numSOpts = 0; numHOpts = 0
@@ -400,6 +412,9 @@
 
 	if len(be.trgtBeNameOrSnapshot) == 1:
 		beName = be.trgtBeNameOrSnapshot[0]
+		if lb.beVerifyBEName(beName) != 0:
+			msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1)
+			return 1
 
 	if (listAllAttrs == "-a" and (listDatasets == "-d" \
 	    or listSnapshots == "-s")):
@@ -502,6 +517,10 @@
 			    mountpoint, -1)
 			return 1
 
+	if lb.beVerifyBEName(beName_mntPoint[0]) != 0:
+		msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1)
+		return 1
+
 	rc = lb.beMount(beName_mntPoint[0], mountpoint)
 	if rc == 0:
 		return 0
@@ -577,6 +596,10 @@
 		msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1)
 		usage()
 
+	if lb.beVerifyBEName(args[0]) != 0:
+		msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1)
+		return 1
+
 	rc = lb.beUnmount(args[0], force_unmount)
 	if rc == 0:
 		return 0
@@ -636,6 +659,14 @@
 		msg.printMsg(msg.Msgs.BEADM_ERR_OPT_ARGS, None, -1)
 		usage()
 
+	if lb.beVerifyBEName(beNames[0]) != 0:
+		msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1)
+		return 1
+
+	if lb.beVerifyBEName(beNames[1]) != 0:
+		msg.printMsg(msg.Msgs.BEADM_ERR_BENAME, None, -1)
+		return 1
+
 	rc = lb.beRename(beNames[0], beNames[1])
 
 	if rc == 0:
--- a/usr/src/cmd/beadm/messages.py	Tue Jan 06 11:12:28 2009 -0700
+++ b/usr/src/cmd/beadm/messages.py	Wed Dec 17 09:37:59 2008 -0700
@@ -19,7 +19,7 @@
 #
 # CDDL HEADER END
 #
-# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 # Use is subject to license terms.
 #
 # beadm - The Boot Environment Administration tool.
@@ -62,13 +62,14 @@
 	BEADM_ERR_SNAP_DOES_NOT_EXISTS,
 	BEADM_ERR_UNMOUNT,
 	BEADM_ERR_UNMOUNT_ACTIVE,
+	BEADM_ERR_BENAME,
 	BEADM_MSG_ACTIVE_ON_BOOT,
 	BEADM_MSG_DESTROY,
 	BEADM_MSG_DESTROY_NO,
 	BEADM_MSG_BE_CREATE_START,
 	BEADM_MSG_BE_CREATE_SUCCESS,
 	BEADM_MSG_FREE_FORMAT,
-	) = range(37)
+	) = range(38)
 
 	# Indices corresponding to message numbers for libbe that we are
 	# interested in expanding messages.
@@ -131,7 +132,6 @@
 	BE_ERR_ZONES_UNMOUNT
 	) = range(4000, 4057)
 
-
 	# Error message dictionaries.
 	mBeadmErr = {}
 	mBeadmOut = {}
@@ -170,6 +170,8 @@
 	mBeadmErr[BEADM_ERR_UNMOUNT_ACTIVE] = "%s is the currently active BE.\nIt cannot be unmounted unless another BE is the currently active BE."
 	mBeadmErr[BE_ERR_ZONES_UNMOUNT] = "Unable to destroy one of %(0)s's zone BE's.\nUse 'beadm destroy -fF %(1)s' or 'zfs -f destroy <dataset>'."
 	mBeadmErr[BEADM_ERR_PERMISSIONS] = "You have insufficient privileges to execute this command.\nEither use 'pfexec' to execute the command or become superuser."	
+	mBeadmErr[BEADM_ERR_BENAME] = "The BE name provided is invalid.\nPlease check it and try again."
+
 	# Catchall
 	mBeadmErr[BEADM_MSG_FREE_FORMAT] = "%s"
 
--- a/usr/src/lib/libbe/be_utils.c	Tue Jan 06 11:12:28 2009 -0700
+++ b/usr/src/lib/libbe/be_utils.c	Wed Dec 17 09:37:59 2008 -0700
@@ -20,7 +20,7 @@
  */
 
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -348,7 +348,6 @@
 
 	be_make_root_ds(be_root_pool, be_name, be_root_ds, sizeof (be_root_ds));
 
-
 	/*
 	 * Iterate through menu first to make sure the BE doesn't already
 	 * have an entry in the menu.
@@ -2033,8 +2032,12 @@
 	if (*c != '\0')
 		return (B_FALSE);
 
-	/* The BE name must comply with a zfs dataset filesystem name */
-	if (!zfs_name_valid(be_name, ZFS_TYPE_FILESYSTEM))
+	/*
+	 * The BE name must comply with a zfs dataset filesystem. We also
+	 * verify its length to be < BE_NAME_MAX_LEN.
+	 */
+	if (!zfs_name_valid(be_name, ZFS_TYPE_FILESYSTEM) ||
+	    strlen(be_name) > BE_NAME_MAX_LEN)
 		return (B_FALSE);
 
 	return (B_TRUE);
--- a/usr/src/lib/libbe/libbe_priv.h	Tue Jan 06 11:12:28 2009 -0700
+++ b/usr/src/lib/libbe/libbe_priv.h	Wed Dec 17 09:37:59 2008 -0700
@@ -20,7 +20,7 @@
  */
 
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -59,6 +59,9 @@
 #define	BE_ZONE_SUPPORTED_BRANDS	"ipkg"
 #define	BE_ZONE_SUPPORTED_BRANDS_DELIM	" "
 
+/* Maximum length for the BE name. */
+#define	BE_NAME_MAX_LEN		64
+
 typedef struct be_transaction_data {
 	char		*obe_name;	/* Original BE name */
 	char		*obe_root_ds;	/* Original BE root dataset */
--- a/usr/src/lib/libbe_pymod/libbe.c	Tue Jan 06 11:12:28 2009 -0700
+++ b/usr/src/lib/libbe_pymod/libbe.c	Wed Dec 17 09:37:59 2008 -0700
@@ -20,7 +20,7 @@
  */
 
 /*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
+ * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  */
 
@@ -560,6 +560,7 @@
 	if (beAttrs == NULL) {
 		return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
 	}
+
 	ret = be_rename(beAttrs);
 	nvlist_free(beAttrs);
 	return (Py_BuildValue("i", ret));
@@ -714,7 +715,8 @@
  * Description: Convert Python args to boolean and call libbe_print_errors to
  *			turn on/off error output for the library.
  * Parameter:
- *		set_do_print - Boolean that turns library error
+ *   args -     pointer to a python object containing:
+ *		print_errors - Boolean that turns library error
  *			       printing on or off.
  * Parameters:
  *   args -     pointer to a python object containing:
@@ -739,13 +741,14 @@
 }
 
 /*
- * Function:    beMapLibbeErrorToString
+ * Function:    beGetErrDesc
  * Description: Convert Python args to an int and call be_err_to_str to
  *			map an error code to an error string.
  * Parameter:
+ *   args -     pointer to a python object containing:
  *		errCode - value to map to an error string.
  *
- * Returns error string or NULL
+ * Returns: error string or NULL
  * Scope:
  *      Public
  */
@@ -773,6 +776,34 @@
 	return (Py_BuildValue("s", beErrStr));
 }
 
+/*
+ * Function:    beVerifyBEName
+ * Description: Call be_valid_be_name() to verify the BE name.
+ * Parameter:
+ *   args -     pointer to a python object containing:
+ *		string - value to map to a string.
+ *
+ * Returns:  0 for success or 1 for failure
+ * Scope:
+ *      Public
+ */
+
+PyObject *
+beVerifyBEName(PyObject *self, PyObject *args)
+{
+	char	*string = NULL;
+
+	if (!PyArg_ParseTuple(args, "s", &string)) {
+		return (Py_BuildValue("i", 1));
+	}
+
+	if (be_valid_be_name(string)) {
+		return (Py_BuildValue("i", 0));
+	} else {
+		return (Py_BuildValue("i", 1));
+	}
+}
+
 /* ~~~~~~~~~~~~~~~~~ */
 /* Private Functions */
 /* ~~~~~~~~~~~~~~~~~ */
@@ -1041,6 +1072,8 @@
 	    "Enable/disable error printing."},
 	{"beGetErrDesc", (PyCFunction)beGetErrDesc, METH_VARARGS,
 	    "Map Error codes to strings."},
+	{"beVerifyBEName", (PyCFunction)beVerifyBEName, METH_VARARGS,
+	    "Verify BE name."},
 	{NULL, NULL, 0, NULL}
 };