1 file changed, 49 insertions(+), 113 deletions(-)
utils/xgetent.c |  162 ++++++++++++++++---------------------------------------


# HG changeset patch
# User Abhishek Kulkarni <[EMAIL PROTECTED]>
# Date 1226340326 25200
# Node ID 16f112a860d446fcf705837be163b7ef943c4aaa
# Parent  a68b59fb85899367fd3d712749cc305c592af10b
xgetent now just calls xp_getpwent or xp_getgrent to fetch the list of the users/groups
since the "reading pwent/grent" logic has been moved inside libxcpu.

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

diff --git a/utils/xgetent.c b/utils/xgetent.c
--- a/utils/xgetent.c
+++ b/utils/xgetent.c
@@ -37,6 +37,7 @@
 #include <signal.h>
 #include <regex.h>
 #include <math.h>
+#include <pwd.h>
 
 #include "spfs.h"
 #include "spclient.h"
@@ -46,99 +47,42 @@
 #include "xcpu.h"
 
 extern int spc_chatty;
-static Spuser *user;
-static Xkey *ukey;
 
-static int
-init_user(void)
+void usage(char *name) {
+	fprintf(stderr, "usage: %s [-h] [-A adminkey] [-d] [-p port] <passwd|group> {-a | nodeset} \n", name);
+	exit(1);
+}
+
+int
+read_pwent(Xpnode *node, void *adminkey)
 {
-	char *homepath, keypath[128];
-
-	user = sp_unix_users->uid2user(sp_unix_users, geteuid());
-	if (!user)
+	char **pwent = NULL;
+	int i, n;
+	n = xp_getpwent(node, (char *)adminkey, &pwent);
+	if (n < 0)
 		return -1;
 
-	homepath = getenv("HOME");
-	snprintf(keypath, sizeof(keypath), "%s/.ssh/id_rsa", homepath);
-	ukey = xauth_privkey_create(keypath);
-	if (!ukey)
-		return -1;
-
+	printf("Password Database From Node: %s\n", node->name);
+	for (i = 0; i < n; i++)
+		printf("%s\n", pwent[i]);
+	free(pwent);
 	return 0;
 }
 
-void usage() {
-	fprintf(stderr, "usage: xgetent [-dap] <passwd|group> host,...\n");
-	exit(1);
-}
-
-int read_pwent(Xpnode *node, void *cba) {
-	Spcfsys *fs;
-	Spcfid *fid;
-	char *buf;
-	int n, off, ret, bufsize = 8192;
-
-	ret = 0;
-	buf = malloc(sizeof(*buf) * bufsize);
-	fs = xp_node_mount(node, user, ukey);
-	if (!fs)
+int
+read_grent(Xpnode *node, void *adminkey)
+{
+	char **grent = NULL;
+	int i, n;
+	n = xp_getgrent(node, (char *)adminkey, &grent);
+	if (n < 0)
 		return -1;
 
-	fid = spc_open(fs, "pwent", Oread);
-	if (!fid) {
-		free(buf);
-		spc_umount(fs);
-		return -1;
-	}
-	
-	printf("\nPassword Database From Node: %s\n", node->name);
-	off = 0;
-	while ((n = spc_read(fid, (u8 *) buf, bufsize, off)) > 0) {
-		printf("%s", buf);
-		off += n;
-	}
-	
-	if (n < 0)
-		ret = -1;
-	
-	spc_close(fid);
-	spc_umount(fs);
-	free(buf);
-	return ret;
-}
-int read_grent(Xpnode *node, void *cba) {
-	Spcfsys *fs;
-	Spcfid *fid;
-	char *buf;
-	int n, off, ret, bufsize = 8192;
-
-	ret = 0;
-	buf = malloc(sizeof(*buf) * bufsize);
-	fs = xp_node_mount(node, user, ukey);
-	if (!fs)
-		return -1;
-
-	fid = spc_open(fs, "grent", Oread);
-	if (!fid) {
-		free(buf);
-		spc_umount(fs);
-		return -1;
-	}
-
-	printf("\nGroup Database From Node: %s\n", node->name);	
-	off = 0;
-	while ((n = spc_read(fid, (u8 *) buf, bufsize, off)) > 0) {
-		printf("%s", buf);
-		off += n;
-	}
-
-	if (n < 0)
-		ret = -1;
-	
-	spc_close(fid);
-	spc_umount(fs);
-	free(buf);
-	return ret;
+	printf("Group Database From Node: %s\n", node->name);
+	for (i = 0; i < n; i++)
+		printf("%s\n", grent[i]);
+	free(grent);
+	return 0;
 }
 
 int
@@ -146,15 +90,19 @@
 {
 	int c, ecode;
 	int allflag = 0;
-	char *ename, db[7];
+	char db[7], *end, *ename;
+	char *adminkey = NULL;
 	Xpnodeset *nds, *nds2;
 	int port = STAT_PORT;
-	char *end;
 
-	while ((c = getopt(argc, argv, "+dap:")) != -1) {
+	while ((c = getopt(argc, argv, "+dA:ap:h")) != -1) {
 		switch (c) {
 		case 'd':
 			spc_chatty = 1;
+			break;
+
+		case 'A':
+			adminkey = strdup(optarg);
 			break;
 
 		case 'a':
@@ -164,20 +112,20 @@
 		case 'p':
 			port = strtol(optarg, &end, 10);
 			if (*end != '\0')
-				usage();
+				usage(argv[0]);
 			break;
+		case 'h':
 		default:
-			usage();
+			usage(argv[0]);
 		}
 	}
 
 	if ((!allflag && argc - optind != 2 ) || (allflag && argc - optind != 1)) 
-		usage();
-
+		usage(argv[0]);
 
 	snprintf(db, 7, "%s", argv[optind++]);
 	if (strcmp("passwd", db) && strcmp("group", db))
-		usage();
+		usage(argv[0]);
 
 	if (allflag) {
 		char statserver[32];
@@ -196,32 +144,20 @@
 				}
 			} /* if filter is unsuccessful just use the full set */
 		}
-	} else {
+	} else
 		nds = xp_nodeset_from_string(argv[optind++]);
-	}
 
 	if (!nds)
-		goto lerror;
+		goto error;
 
-	if (init_user() < 0)
-		goto lerror;
-	
-	if (!strcmp("passwd", db)) {
-		if (xp_nodeset_iterate(nds, read_pwent, NULL) > 0)
-			goto rerror;
-	} else {
-		if (xp_nodeset_iterate(nds, read_grent, NULL) > 0)
-			goto rerror;
-	}
-		
+	if (!strcmp("passwd", db))
+		xp_nodeset_iterate(nds, read_pwent, adminkey);
+	else
+		xp_nodeset_iterate(nds, read_grent, adminkey);
+
 	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;
 }

Reply via email to