1 file changed, 47 insertions(+), 22 deletions(-)
utils/xuserset.c |   69 ++++++++++++++++++++++++++++++++++++------------------


# HG changeset patch
# User Abhishek Kulkarni <[EMAIL PROTECTED]>
# Date 1226339852 25200
# Node ID a955190ac5b2ea7266ffc275212423a9205e46c3
# Parent  93bad2e749f5322b566e208727c185a457aebff9
this patch adds the "addgroup" and "delgroup" commands to xuserset.
a user can be assigned to a group using:
xuserset addgroup {-a|nodeset} uname gname

changed the usage message to reflect these new changes.
added -p switch for xuserset (specify different port)
this patch also adds better input arguments validation to xuserset.
xuserset now just passes the path to the adminkey to the libxcpu user functions
since the key handling logic has been moved in to the libxcpu functions.

Signed-off-by: Abhishek Kulkarni <[EMAIL PROTECTED]>

diff --git a/utils/xuserset.c b/utils/xuserset.c
--- a/utils/xuserset.c
+++ b/utils/xuserset.c
@@ -47,27 +47,26 @@
 #include "xcpu.h"
 
 extern int spc_chatty;
-static Xkey *adminkey;
 
 void
 usage(char *name)
 {
-	fprintf(stderr, "usage: %s [-h] add [-A admin_keyfile] {-a | nodeset} user uid group key\n", name);
-	fprintf(stderr, "       %s [-h] delete [-A admin_keyfile] {-a | nodeset} user\n", name);
-	fprintf(stderr, "       %s [-h] flush [-A admin_keyfile] {-a | nodeset}\n", name);
+	fprintf(stderr, "usage: %s [-h] add [-A adminkey] {-a | nodeset} user uid group key\n", name);
+	fprintf(stderr, "       %s      delete [-A adminkey] {-a | nodeset} user\n", name);
+	fprintf(stderr, "       %s      flush [-A adminkey] {-a | nodeset}\n\n", name);
+	fprintf(stderr, "       %s      addgroup [-A adminkey] {-a | nodeset} group\n", name);
+	fprintf(stderr, "       %s      delgroup [-A adminkey] {-a | nodeset} group\n", name);
 	fprintf(stderr, "where: \n");
-	fprintf(stderr, "\t-h prints this message\n");
 	fprintf(stderr, "\tnodeset is the set of nodes to issue the command to\n");
 	fprintf(stderr, "\tif -a is used, contact statfs and get a list of all nodes that are up\n");
 	fprintf(stderr, "\tuser is the user name of the user\n");
 	fprintf(stderr, "\tuid is the numeric user id of the user on the remote machine\n");
 	fprintf(stderr, "\tgroup is the group this user belongs to\n");
 	fprintf(stderr, "\tkey is the public key of that user (usually id_rsa.pub)\n");
-	fprintf(stderr, "\n\tadmin_keyfile defaults to /etc/xcpu/admin_key\n");
+	fprintf(stderr, "\n\tadminkey is the private key of the admin user (usually /etc/xcpu/admin_key)\n");
 
 	fprintf(stderr, "\nexample: %s add 192.168.19.2 root 0 xcpu-admin ~/.ssh/id_rsa.pub\n", name);
 	fprintf(stderr, "\t: %s delete 192.168.19.2 root\n", name);
-	fprintf(stderr, "note: group should be created with xgroupset before xuserset is executed\n");
 	exit(1);
 }
 
@@ -97,29 +96,34 @@
 int
 main(int argc, char **argv)
 {
-	int c, ecode;
+	int c, ecode, port = STAT_PORT;
 	u32 userid;
-	char cmd[7], ukeypath[256], userkey[4096];
+	char cmd[9], ukeypath[256], userkey[4096];
 	char *nodeset, *ename, *username, *groupname;
-	char *adminkeyfile = "/etc/xcpu/admin_key";
+        char *adminkey = NULL, *end;
 	Xpnodeset *nds, *nds2;
 	int allflag = 0, passwdfile = 0;
 	struct passwd *pw;
 	struct group *gr;
 
-	while((c = getopt(argc, argv, "aA:dhu")) != -1) {
+	while((c = getopt(argc, argv, "aA:dhup")) != -1) {
 		switch(c) {
 		case 'd':
 			spc_chatty = 1;
 			break;
 		case 'A':
-			adminkeyfile = strdup(optarg);
+			adminkey = strdup(optarg);
 			break;
 		case 'a':
 			allflag++;
 			break;
 		case 'u':
 			passwdfile++;
+			break;
+		case 'p':
+			port = strtol(optarg, &end, 10);
+			if (*end != '\0')
+				usage(argv[0]);
 			break;
 		case 'h':
 		default:
@@ -130,24 +134,30 @@
 	if (argc < 2)
 		usage(argv[0]);
 
-	snprintf(cmd, 7 * sizeof(char), "%s", argv[optind++]);
+	snprintf(cmd, 9 * sizeof(char), "%s", argv[optind++]);
 	if (!strcmp("add", cmd) && !passwdfile) {
 		if (argc < 7)
 			usage(argv[0]);
 	} else if (!strcmp("delete", cmd) && !passwdfile) {
 		if (argc < 4)
 			usage(argv[0]);
+	} else if (!strcmp("addgroup", cmd) || !strcmp("delgroup", cmd)) {
+		if (argc < 5)
+			usage(argv[0]);		
 	} else if (!strcmp("flush", cmd) || passwdfile)
 		;
 	else
 		usage(argv[0]);
 
-	if (!allflag && ((nodeset = getenv("NODES")) == NULL))
+	if (!allflag && ((nodeset = getenv("NODES")) == NULL)) {
+		if ((argc - optind) < 1 && !passwdfile)
+			usage(argv[0]);
 		nodeset = argv[optind++];
+	}
 
 	if (allflag) {
 		char statserver[32];
-		sprintf(statserver, "localhost!%d", STAT_PORT);
+		sprintf(statserver, "localhost!%d", port);
 		nds = xp_nodeset_list(NULL);
 		if(nds == NULL)
 			nds = xp_nodeset_list(statserver);
@@ -168,10 +178,6 @@
 	if (!nds)
 		goto error;
 
-	adminkey = xauth_privkey_create(adminkeyfile);
-	if (!adminkey)
-		goto error;
-
 	if (!strcmp("flush", cmd)) {
 		if (xp_user_flush(nds, adminkey) < 0)
 			goto error;
@@ -182,11 +188,13 @@
 				xp_user_del(nds, adminkey, pw->pw_name);
 			endpwent();
 		} else {
+			if ((argc - optind) < 1)
+				usage(argv[0]);
 			username = argv[optind++];
 			if (xp_user_del(nds, adminkey, username) < 0)
-				goto error;
+				return -1;
 		}
-	} else { /* user add */
+	} else if (!strcmp("add", cmd)) {
 		if (passwdfile) {
 			setpwent();
 			while ((pw = getpwent()) != NULL) {
@@ -206,6 +214,8 @@
 			}
 			endpwent();
 		} else {
+			if ((argc - optind) < 4)
+				usage(argv[0]);
 			username = argv[optind++];
 			userid = strtol(argv[optind++], NULL, 10);
 			groupname = argv[optind++];
@@ -217,7 +227,22 @@
 			
 			if (xp_user_add(nds, adminkey, username, userid,
 					groupname, userkey) < 0)
-				goto error;
+				return -1;
+		}
+	} else { /* group operations */
+		if ((argc - optind) < 2)
+			usage(argv[0]);
+		username = argv[optind++];
+		groupname = argv[optind++];
+
+		if (!strcmp("addgroup", cmd)) {
+			if (xp_user_add_group(nds, adminkey, username,
+					      groupname) < 0)
+				return -1;
+		} else { /* !strcmp("delgroup", cmd) */			
+			if (xp_user_del_group(nds, adminkey, username,
+					      groupname) < 0)
+				return -1;
 		}
 	}
 	return 0;

Reply via email to