xuserset and xgroupset now use the new libxcpu functions to do the user
and group operations respectively.

i also added a -u option to xgroupset and xuserset.
this reads from the groupfile or passwd file and performs the operations
on all the groups and users.

for instance, 

./xgroupset add -a -u

will add all the groups from /etc/group to all the nodes in the
cluster...

./xuserset del -a -u

will delete all the users in /etc/passwd from the userpool on the
nodes...

so on and so forth.

this kinda makes "flush" operation redundant but some might prefer flush
over this.

Signed-off-by: Abhishek Kulkarni <[EMAIL PROTECTED]>
Index: utils/xgroupset.c
===================================================================
--- utils/xgroupset.c	(revision 715)
+++ utils/xgroupset.c	(working copy)
@@ -36,9 +36,7 @@
 #include <signal.h>
 #include <dirent.h>
 #include <signal.h>
-#include <regex.h>
-#include <math.h>
-#include <pwd.h>
+#include <grp.h>
 
 #include "spfs.h"
 #include "spclient.h"
@@ -48,77 +46,30 @@
 #include "xcpu.h"
 
 extern int spc_chatty;
-
-static char *groupname;
-static u32 gid;
-static char *groupname;
 static Xkey *adminkey;
-static Spuser adminuser = {
-	.uname = "xcpu-admin",
-	.uid = 65530,
-};
-enum {
-    Add,
-    Delete,
-};
-static int groupcmd;
 
 void
 usage(char *name)
 {
-	fprintf(stderr, "usage: %s [-h] {add | delete} [-A adminkey] {-a | nodeset} gname gid [gname gid ...]\n", name);
+	fprintf(stderr, "usage: %s [-h] add [-A adminkey] {-a | nodeset} {-u | gname gid [gname gid ...]}\n", name);
+	fprintf(stderr, "       %s [-h] delete [-A adminkey] {-a | nodeset} {-u | gname [gname ...]}\n", name);
+	fprintf(stderr, "       %s [-h] flush [-A adminkey] {-a | nodeset}\n", name);
 	exit(1);
 }
 
-static int
-setgroup(Xpnode *nd, void *cba)
-{
-	int buflen, ret, bufsize = 2048;
-	char *buf;
-	Spcfsys *fs;
-	Spcfid *fid;
-	fs = xp_node_mount(nd, &adminuser, adminkey);
-	if (!fs)
-		return -1;
-
-	buf = malloc(sizeof(*buf) * bufsize);
-	if(groupcmd == Add)
-		snprintf(buf, bufsize, "group-add %s %d\n", groupname, gid);
-	else
-		snprintf(buf, bufsize, "group-del %s\n", groupname);
-	
-	buflen = strlen(buf);
-
-	fid = spc_open(fs, "ctl", Owrite);
-	if (!fid) {
-		free(buf);
-		spc_umount(fs);
-		return -1;
-	}
-	ret = spc_write(fid, (u8 *) buf, buflen, 0);
-	if (ret < 0) {
-		free(buf);
-		spc_umount(fs);
-		return ret;
-	}
-
-	spc_close(fid);
-	free(buf);
-	return 0;
-}
-
 int
 main(int argc, char **argv)
 {
 	int c, ecode;
 	char cmd[7];
-	char *nodeset, *ename;
+	u32 gid;
+	char *nodeset, *ename, *groupname;
 	char *adminkeyfile = "/etc/xcpu/admin_key";
-	struct passwd *pw;
 	Xpnodeset *nds, *nds2;
-	int allflag = 0;
+	int allflag = 0, groupfile = 0;
+	struct group *gr;
 
-	while((c = getopt(argc, argv, "aA:dh")) != -1) {
+	while((c = getopt(argc, argv, "aA:dhu")) != -1) {
 		switch(c) {
 		case 'd':
 			spc_chatty = 1;
@@ -129,51 +80,38 @@
 		case 'a':
 			allflag++;
 			break;
+		case 'u':
+			groupfile++;
+			break;
 		case 'h':
 		default:
 			usage(argv[0]);
 		}
 	}
 
-	if (argc < 4)
+	if (argc < 2)
 		usage(argv[0]);
 
-	snprintf(cmd, 7 * sizeof(char), "%s", argv[optind++]);   
-	if ((strcmp("add", cmd) && strcmp("delete", cmd))
-	    || (!strcmp("add", cmd) && argc < 5))
-		usage(argv[0]);
-	
-	if(!strcmp("add", cmd)) 
-		groupcmd = Add;
+	snprintf(cmd, 7 * sizeof(char), "%s", argv[optind++]);
+	if (!strcmp("add", cmd) && !groupfile) {
+		if (argc < 5)
+			usage(argv[0]);
+	} else if (!strcmp("delete", cmd) && !groupfile) {
+		if (argc < 4)
+			usage(argv[0]);
+	} else if (!strcmp("flush", cmd) || groupfile)
+		;
 	else
-		groupcmd = Delete;
-    
-	if (! allflag)
+		usage(argv[0]);
+ 
+	if (!allflag && ((nodeset = getenv("NODES")) == NULL))
 		nodeset = argv[optind++];
-    
-	groupname = argv[optind++];
-	if(groupcmd == Add)
-		gid = strtol(argv[optind++], NULL, 10);
-	
-	adminkey = xauth_privkey_create(adminkeyfile);
-	if (!adminkey)
-		goto lerror;
 
-	setpwent();
-	while ((pw = getpwent()) != NULL) {
-		if(!strcmp(pw->pw_name, adminuser.uname)) {
-			adminuser.uid = pw->pw_uid;
-			break;
-		}
-	}
-	endpwent(); 
-
-
 	if (allflag) {
 		char statserver[32];
 		sprintf(statserver, "localhost!%d", STAT_PORT);
 		nds = xp_nodeset_list(NULL);
-		if(nds == NULL)
+		if (nds == NULL)
 			nds = xp_nodeset_list(statserver);
 		if (nds != NULL) {
 			nds2 = xp_nodeset_create();
@@ -186,24 +124,52 @@
 				}
 			} /* if filter is unsuccessful just use the full set */
 		}
-	} else {
+	} else
 		nds = xp_nodeset_from_string(nodeset);
-	}
 
 	if (!nds)
-		goto lerror;
+		goto error;
 
-	if (xp_nodeset_iterate(nds, setgroup, NULL) > 0)
-		goto rerror;
+	adminkey = xauth_privkey_create(adminkeyfile);
+	if (!adminkey)
+		goto error;
 
+	if (!strcmp("flush", cmd)) {
+		if (xp_group_flush(nds, adminkey) < 0)
+			goto error;
+	} else {
+		if (!strcmp("delete", cmd)) {			
+			if (groupfile) {
+				setgrent();
+				while ((gr = getgrent()) != NULL) {
+					if (xp_group_del(nds, adminkey, gr->gr_name) < 0)
+						goto error;
+				}
+				endgrent();
+			} else {
+				groupname = argv[optind++];
+				if (xp_group_del(nds, adminkey, groupname) < 0)
+					goto error;
+			}
+		} else { /* group add */
+			if (groupfile) {
+				setgrent();
+				while ((gr = getgrent()) != NULL) {
+					if (xp_group_add(nds, adminkey, gr->gr_name, gr->gr_gid) < 0)
+						goto error;
+				}
+				endgrent();
+			} else {
+				groupname = argv[optind++];
+				gid = strtol(argv[optind++], NULL, 10);
+				if (xp_group_add(nds, adminkey, groupname, gid) < 0)
+					goto error;
+			}
+		}
+	}
 	return 0;
-
-lerror:
+error:
 	sp_rerror(&ename, &ecode);
 	fprintf(stderr, "Error: %s\n", ename);
-	return 1;
-
-rerror:
-	xp_nodeerror_print(argv[0]);
-	return 1;
+	return -1;
 }
Index: utils/xuserset.c
===================================================================
--- utils/xuserset.c	(revision 715)
+++ utils/xuserset.c	(working copy)
@@ -36,9 +36,8 @@
 #include <signal.h>
 #include <dirent.h>
 #include <signal.h>
-#include <regex.h>
-#include <math.h>
 #include <pwd.h>
+#include <grp.h>
 
 #include "spfs.h"
 #include "spclient.h"
@@ -48,27 +47,14 @@
 #include "xcpu.h"
 
 extern int spc_chatty;
-
-static char *username;
-static u32 userid;
-static char *groupname;
 static Xkey *adminkey;
-static Spuser adminuser = {
-	.uname = "xcpu-admin",
-	.uid = 65530,
-};
-static char userkey[4096];
-enum {
-    Add,
-    Delete,
-};
-static int usercmd;
 
 void
 usage(char *name)
 {
 	fprintf(stderr, "usage: %s [-h] add [-A admin_keyfile] {-a | nodeset} user uid group key\n", name);
-	fprintf(stderr, "usage: %s [-h] delete [-A admin_keyfile] {-a | nodeset} user\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, "where: \n");
 	fprintf(stderr, "\t-h prints this message\n");
 	fprintf(stderr, "\tnodeset is the set of nodes to issue the command to\n");
@@ -85,77 +71,41 @@
 	exit(1);
 }
 
-static int
-setuser(Xpnode *nd, void *cba)
+static inline
+int get_user_key(char *keypath, char *key, int keysize)
 {
-	int bufsize = 2048, buflen, ret;
-	char *buf, *qkey;
-	Spcfsys *fs;
-	Spcfid *fid;
-	Spwstat wst;
+	int fd, n;
+	char *s;
 
-	fs = xp_node_mount(nd, &adminuser, adminkey);
-	if (!fs)
+	fd = open(keypath, O_RDONLY);
+	if (fd < 0)
 		return -1;
 
-	buf = malloc(sizeof(*buf) * bufsize);
-	if(usercmd == Add) {
-		qkey = quotestrdup(userkey);
-		snprintf(buf, bufsize, "user-add %s %d %s %s\n", username, userid, groupname, qkey);
-		free(qkey);
-	} else 
-		snprintf(buf, bufsize, "user-del %s\n", username);
-	
-	buflen = strlen(buf);
-
-	fid = spc_open(fs, "ctl", Owrite);
-	if (!fid) {
-		free(buf);
-		spc_umount(fs);
+	n = read(fd, key, keysize-1);
+	if (n < 0)
 		return -1;
-	}
-	ret = spc_write(fid, (u8 *) buf, buflen, 0);
-	if (ret < 0) {
-		free(buf);
-		spc_umount(fs);
-		return ret;
-	}
 
-	spc_close(fid);	
-	wst.type = ~0;
-	wst.dev = ~0;
-	wst.qid.type = ~0;
-	wst.qid.version = ~0;
-	wst.qid.path = ~0;
-	wst.mode = ~0;
-	wst.atime = ~0;
-	wst.mtime = ~0;
-	wst.name = NULL;
-	wst.length = ~0;
-	wst.uid = (usercmd == Add) ? username : adminuser.uname;
-	wst.gid = (usercmd == Add) ? groupname : adminuser.uname;
-	wst.muid = NULL;
-	wst.extension = NULL;
-	wst.n_uid = (usercmd == Add) ? userid : adminuser.uid;
-	wst.n_gid = ~0;
-	wst.n_muid = ~0;
-		
-	ret = spc_wstat(fs, "clone", &wst);
-	spc_umount(fs);
-	free(buf);
-	return ret;
+	s = strchr(key, '\n');
+	if (s)
+		*s = '\0';
+	else
+		*(key+n) = '\0';
+	close(fd);
+	return 0;
 }
 
 int
 main(int argc, char **argv)
 {
-	int c, fd, n, ecode;
-	char *s, cmd[7];
-	char *nodeset, *ename;
+	int c, ecode;
+	u32 userid;
+	char cmd[7], ukeypath[256], userkey[4096];
+	char *nodeset, *ename, *username, *groupname;
 	char *adminkeyfile = "/etc/xcpu/admin_key";
-	struct passwd *pw;
 	Xpnodeset *nds, *nds2;
-	int allflag = 0;
+	int allflag = 0, passwdfile = 0;
+	struct passwd *pw;
+	struct group *gr;
 
 	while((c = getopt(argc, argv, "aA:dh")) != -1) {
 		switch(c) {
@@ -168,64 +118,33 @@
 		case 'a':
 			allflag++;
 			break;
-
+		case 'u':
+			passwdfile++;
+			break;
 		case 'h':
 		default:
 			usage(argv[0]);
 		}
 	}
 
-	snprintf(cmd, 7 * sizeof(char), "%s", argv[optind++]);   
-	if ((strcmp("add", cmd) && strcmp("delete", cmd))
-	    || (!strcmp("add", cmd) && argc < 7) 
-	    || (!strcmp("delete", cmd) && argc < 4))
+	if (argc < 2)
 		usage(argv[0]);
-	
-	if(!strcmp("add", cmd)) 
-		usercmd = Add;
+
+	snprintf(cmd, 7 * 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("flush", cmd) || passwdfile)
+		;
 	else
-		usercmd = Delete;
+		usage(argv[0]);
 
-	if (! allflag)
+	if (!allflag && ((nodeset = getenv("NODES")) == NULL))
 		nodeset = argv[optind++];
-	username = argv[optind++];
 
-	if(usercmd == Add) {
-		userid = strtol(argv[optind++], NULL, 10);
-		groupname = argv[optind++];
-		fd = open(argv[optind], O_RDONLY);
-		if (fd < 0) {
-			sp_suerror(argv[optind-1], errno);
-			goto lerror;
-		}
-
-		n = read(fd, userkey, sizeof(userkey) - 1);
-		if (n < 0) {
-			sp_uerror(errno);
-			goto lerror;
-		}
-		s = strchr(userkey, '\n');
-		if (s)
-			*s = '\0';
-		else
-			userkey[n] = '\0';
-		
-		close(fd);
-	}	
-	adminkey = xauth_privkey_create(adminkeyfile);
-	if (!adminkey)
-		goto lerror;
-
-	setpwent();
-	while ((pw = getpwent()) != NULL) {
-		if(!strcmp(pw->pw_name, adminuser.uname)) {
-			adminuser.uid = pw->pw_uid;
-			break;
-		}
-	}
-	endpwent(); 
-
-
 	if (allflag) {
 		char statserver[32];
 		sprintf(statserver, "localhost!%d", STAT_PORT);
@@ -243,24 +162,71 @@
 				}
 			} /* if filter is unsuccessful just use the full set */
 		}
-	} else {
+	} else
 		nds = xp_nodeset_from_string(nodeset);
-	}
+
 	if (!nds)
-		goto lerror;
+		goto error;
 
-	if (xp_nodeset_iterate(nds, setuser, NULL) > 0)
-		goto rerror;
-	
-	return 0;
+	adminkey = xauth_privkey_create(adminkeyfile);
+	if (!adminkey)
+		goto error;
 
-lerror:
+	if (!strcmp("flush", cmd)) {
+		if (xp_user_flush(nds, adminkey) < 0)
+			goto error;
+	} else {
+		if (!strcmp("delete", cmd)) {
+			if (passwdfile) {
+				setpwent();
+				while ((pw = getpwent()) != NULL) {
+					if (xp_user_del(nds, adminkey, pw->pw_name) < 0)
+						goto error;
+				}
+				endpwent();
+			} else {
+				username = argv[optind++];
+				if (xp_user_del(nds, adminkey, username) < 0)
+					goto error;
+			}
+		} else { /* user add */
+			if (passwdfile) {
+				setpwent();
+				while ((pw = getpwent()) != NULL) {
+					snprintf(ukeypath, sizeof(ukeypath), "%s/.ssh/id_rsa.pub", pw->pw_dir);
+					if (get_user_key(ukeypath, userkey, sizeof(userkey)) < 0) {
+						sp_suerror("get_user_key", errno);
+						continue;
+					}
+
+					if ((gr = getgrgid(pw->pw_gid)) == NULL) {
+						sp_suerror("get_user_key", errno);
+						continue;
+					}
+
+					if (xp_user_add(nds, adminkey, pw->pw_name, pw->pw_uid,
+							gr->gr_name, userkey) < 0)
+						goto error;
+				}
+				endpwent();
+			} else {
+				userid = strtol(argv[optind++], NULL, 10);
+				groupname = argv[optind++];
+
+				if (get_user_key(argv[optind++], userkey, sizeof(userkey)) < 0) {
+					sp_suerror("get_user_key", errno);
+					goto error;
+				}
+
+				if (xp_user_add(nds, adminkey, username, userid,
+						groupname, userkey) < 0)
+					goto error;
+			}
+		}
+	}
+	return 0;
+error:
 	sp_rerror(&ename, &ecode);
 	fprintf(stderr, "Error: %s\n", ename);
 	return 1;
-	
-rerror:
-	xp_nodeerror_print(argv[0]);
-	return 1;
 }
-

Reply via email to