[PATCH] Cleanup 'net idmap' and 'net groupmap'

John H Terpstra jht at samba.org
Sat Jun 21 08:22:51 GMT 2003


On Sat, 21 Jun 2003, Andrew Bartlett wrote:

> This removes the silly StrCaseCmp() stuff from 'net idmap' and 'net
> groupmap'.  The correct way to implement this stuff is via a function
> table, as exampled in all the other parts of 'net'.
>
> This also moves the idmap code into a new file.  Volker, is this your
> code?  You might want to put your name on it.

Patch works Ok. No objection from me.

- John T.
-- 
John H Terpstra
Email: jht at samba.org
-------------- next part --------------
? utils/net_idmap.c
Index: Makefile.in
===================================================================
RCS file: /home/cvs/samba/source/Makefile.in,v
retrieving revision 1.468.2.123
diff -u -r1.468.2.123 Makefile.in
--- Makefile.in	21 Jun 2003 04:04:59 -0000	1.468.2.123
+++ Makefile.in	21 Jun 2003 07:29:21 -0000
@@ -476,7 +476,7 @@
 NET_OBJ1 = utils/net.o utils/net_ads.o utils/net_ads_cldap.o utils/net_help.o \
 	   utils/net_rap.o utils/net_rpc.o utils/net_rpc_samsync.o \
 	   utils/net_rpc_join.o utils/net_time.o utils/net_lookup.o \
-	   utils/net_cache.o utils/net_groupmap.o
+	   utils/net_cache.o utils/net_groupmap.o utils/net_idmap.o
 
 NET_OBJ = $(NET_OBJ1) $(PARAM_OBJ) $(SECRETS_OBJ) $(LIBSMB_OBJ) \
 	  $(RPC_PARSE_OBJ) $(PASSDB_OBJ) $(GROUPDB_OBJ) \
Index: utils/net.c
===================================================================
RCS file: /home/cvs/samba/source/utils/net.c,v
retrieving revision 1.43.2.25
diff -u -r1.43.2.25 net.c
--- utils/net.c	18 Jun 2003 15:24:10 -0000	1.43.2.25
+++ utils/net.c	21 Jun 2003 07:29:21 -0000
@@ -348,150 +348,6 @@
 	return net_rap_file(argc, argv);
 }
 
-/***********************************************************
- migrated functionality from smbgroupedit
- **********************************************************/
-static int net_groupmap(int argc, const char **argv)
-{
-	if ( 0 == argc )
-		return net_help_groupmap( argc, argv );
-
-	if ( !StrCaseCmp( argv[0], "add" ) )
-		return net_groupmap_add(argc-1, argv+1);
-	else if ( !StrCaseCmp( argv[0], "modify" ) )
-		return net_groupmap_modify(argc-1, argv+1);
-	else if ( !StrCaseCmp( argv[0], "delete" ) )
-		return net_groupmap_delete(argc-1, argv+1);
-	else if ( !StrCaseCmp( argv[0], "list" ) )
-		return net_groupmap_list(argc-1, argv+1);
-	
-	return net_help_groupmap( argc, argv );
-}
-
-/***********************************************************
- Helper function for net_idmap_dump. Dump one entry.
- **********************************************************/
-static int net_idmap_dump_one_entry(TDB_CONTEXT *tdb,
-				    TDB_DATA key,
-				    TDB_DATA data,
-				    void *unused)
-{
-	if (strcmp(key.dptr, "USER HWM") == 0) {
-		printf("USER HWM %d\n", IVAL(data.dptr,0));
-		return 0;
-	}
-
-	if (strcmp(key.dptr, "GROUP HWM") == 0) {
-		printf("GROUP HWM %d\n", IVAL(data.dptr,0));
-		return 0;
-	}
-
-	if (strncmp(key.dptr, "S-", 2) != 0)
-		return 0;
-
-	printf("%s %s\n", data.dptr, key.dptr);
-	return 0;
-}
-
-/***********************************************************
- Dump the current idmap
- **********************************************************/
-static int net_idmap_dump(int argc, const char **argv)
-{
-	TDB_CONTEXT *idmap_tdb;
-
-	if ( argc != 1 )
-		return net_help_idmap( argc, argv );
-
-	idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
-
-	if (idmap_tdb == NULL) {
-		d_printf("Could not open idmap: %s\n", argv[0]);
-		return -1;
-	}
-
-	tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);
-
-	tdb_close(idmap_tdb);
-
-	return 0;
-}
-
-/***********************************************************
- Write entries from stdin to current local idmap
- **********************************************************/
-static int net_idmap_restore(int argc, const char **argv)
-{
-	if (!idmap_init()) {
-		d_printf("Could not init idmap\n");
-		return -1;
-	}
-
-	while (!feof(stdin)) {
-		fstring line, sid_string;
-		int len;
-		unid_t id;
-		int type = ID_EMPTY;
-		DOM_SID sid;
-
-		if (fgets(line, sizeof(line)-1, stdin) == NULL)
-			break;
-
-		len = strlen(line);
-
-		if ( (len > 0) && (line[len-1] == '\n') )
-			line[len-1] = '\0';
-
-		if (sscanf(line, "GID %d %s", &id.gid, sid_string) == 2) {
-			type = ID_GROUPID;
-		}
-
-		if (sscanf(line, "UID %d %s", &id.uid, sid_string) == 2) {
-			type = ID_USERID;
-		}
-
-		if (type == ID_EMPTY) {
-			d_printf("ignoring invalid line [%s]\n", line);
-			continue;
-		}
-
-		if (!string_to_sid(&sid, sid_string)) {
-			d_printf("ignoring invalid sid [%s]\n", sid_string);
-			continue;
-		}
-
-		if (!NT_STATUS_IS_OK(idmap_set_mapping(&sid, id, type))) {
-			d_printf("Could not set mapping of %s %d to sid %s\n",
-				 (type == ID_GROUPID) ? "GID" : "UID",
-				 (type == ID_GROUPID) ? id.gid : id.uid,
-				 sid_string_static(&sid));
-			continue;
-		}
-				 
-	}
-
-	idmap_close();
-	return 0;
-}
-
-/***********************************************************
- Look at the current idmap
- **********************************************************/
-static int net_idmap(int argc, const char **argv)
-{
-	if ( 0 == argc )
-		return net_help_idmap( argc, argv );
-
-	if ( !StrCaseCmp( argv[0], "dump" ) )
-		return net_idmap_dump(argc-1, argv+1);
-
-	if ( !StrCaseCmp( argv[0], "restore" ) )
-		return net_idmap_restore(argc-1, argv+1);
-
-	return net_help_idmap( argc, argv );
-}
-
-
 /*
  Retrieve our local SID or the SID for the specified name
  */
Index: utils/net_groupmap.c
===================================================================
RCS file: /home/cvs/samba/source/utils/net_groupmap.c,v
retrieving revision 1.1.2.12
diff -u -r1.1.2.12 net_groupmap.c
--- utils/net_groupmap.c	18 Jun 2003 15:24:10 -0000	1.1.2.12
+++ utils/net_groupmap.c	21 Jun 2003 07:29:21 -0000
@@ -106,7 +106,7 @@
 /*********************************************************
  List the groups.
 **********************************************************/
-int net_groupmap_list(int argc, const char **argv)
+static int net_groupmap_list(int argc, const char **argv)
 {
 	int entries;
 	BOOL long_list = False;
@@ -177,7 +177,7 @@
  Add a new group mapping entry
 **********************************************************/
 
-int net_groupmap_add(int argc, const char **argv)
+static int net_groupmap_add(int argc, const char **argv)
 {
 	DOM_SID sid;
 	fstring ntgroup = "";
@@ -283,7 +283,7 @@
 	return 0;
 }
 
-int net_groupmap_modify(int argc, const char **argv)
+static int net_groupmap_modify(int argc, const char **argv)
 {
 	DOM_SID sid;
 	GROUP_MAP map;
@@ -412,7 +412,7 @@
 	return 0;
 }
 
-int net_groupmap_delete(int argc, const char **argv)
+static int net_groupmap_delete(int argc, const char **argv)
 {
 	DOM_SID sid;
 	fstring ntgroup = "";
@@ -464,5 +464,47 @@
 	d_printf("Sucessfully removed %s from the mapping db\n", ntgroup);
 
 	return 0;
+}
+
+int net_help_groupmap(int argc, const char **argv)
+{
+	d_printf("net groupmap add"\
+		"\n  Create a new group mapping\n");
+	d_printf("net groupmap modify"\
+		"\n  Update a group mapping\n");
+	d_printf("net groupmap delete"\
+		"\n  Remove a group mapping\n");
+	d_printf("net groupmap list"\
+		"\n  List current group map\n");
+	
+	return -1;
+}
+
+
+/***********************************************************
+ migrated functionality from smbgroupedit
+ **********************************************************/
+int net_groupmap(int argc, const char **argv)
+{
+	/* we shouldn't have silly checks like this */
+	if (getuid() != 0) {
+		d_printf("You must be root to edit group mappings.\nExiting...\n");
+		return -1;
+	}
+	
+	struct functable func[] = {
+		{"add", net_groupmap_add},
+		{"modify", net_groupmap_modify},
+		{"delete", net_groupmap_delete},
+		{"list", net_groupmap_list},
+		{"help", net_help_groupmap},
+		{NULL, NULL}
+	};
+
+	return net_run_function(argc, argv, func, net_help_groupmap);
+	if ( 0 == argc )
+		return net_help_groupmap( argc, argv );
+
+	return net_help_groupmap( argc, argv );
 }
 
Index: utils/net_help.c
===================================================================
RCS file: /home/cvs/samba/source/utils/net_help.c,v
retrieving revision 1.2.2.9
diff -u -r1.2.2.9 net_help.c
--- utils/net_help.c	14 Jun 2003 17:51:09 -0000	1.2.2.9
+++ utils/net_help.c	21 Jun 2003 07:29:21 -0000
@@ -99,36 +99,6 @@
 	return -1;
 }
 
-int net_help_groupmap(int argc, const char **argv)
-{
-	if (getuid() != 0) {
-		d_printf("You must be root to edit group mappings.\nExiting...\n");
-		return -1;
-	}
-	
-	d_printf("net groupmap add"\
-		"\n  Create a new group mapping\n");
-	d_printf("net groupmap modify"\
-		"\n  Update a group mapping\n");
-	d_printf("net groupmap delete"\
-		"\n  Remove a group mapping\n");
-	d_printf("net groupmap list"\
-		"\n  List current group map\n");
-	
-	return -1;
-}
-
-int net_help_idmap(int argc, const char **argv)
-{
-	d_printf("net idmap dump filename"\
-		 "\n  Dump current id mapping\n");
-
-	d_printf("net idmap restore"\
-		 "\n  Restore entries from stdin to current local idmap\n");
-
-	return -1;
-}
-
 int net_help_join(int argc, const char **argv)
 {
 	d_printf("\nnet [<method>] join [misc. options]\n"
--- /dev/null	2002-08-31 09:31:37.000000000 +1000
+++ utils/net_idmap.c	2003-06-21 16:48:28.000000000 +1000
@@ -0,0 +1,156 @@
+/* 
+   Samba Unix/Linux SMB client library 
+   Distributed SMB/CIFS Server Management Utility 
+   Copyright (C) 2003 Andrew Bartlett (abartlet at samba.org)
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+
+#include "includes.h"
+#include "../utils/net.h"
+
+
+/***********************************************************
+ Helper function for net_idmap_dump. Dump one entry.
+ **********************************************************/
+static int net_idmap_dump_one_entry(TDB_CONTEXT *tdb,
+				    TDB_DATA key,
+				    TDB_DATA data,
+				    void *unused)
+{
+	if (strcmp(key.dptr, "USER HWM") == 0) {
+		printf("USER HWM %d\n", IVAL(data.dptr,0));
+		return 0;
+	}
+
+	if (strcmp(key.dptr, "GROUP HWM") == 0) {
+		printf("GROUP HWM %d\n", IVAL(data.dptr,0));
+		return 0;
+	}
+
+	if (strncmp(key.dptr, "S-", 2) != 0)
+		return 0;
+
+	printf("%s %s\n", data.dptr, key.dptr);
+	return 0;
+}
+
+/***********************************************************
+ Dump the current idmap
+ **********************************************************/
+static int net_idmap_dump(int argc, const char **argv)
+{
+	TDB_CONTEXT *idmap_tdb;
+
+	if ( argc != 1 )
+		return net_help_idmap( argc, argv );
+
+	idmap_tdb = tdb_open_log(argv[0], 0, TDB_DEFAULT, O_RDONLY, 0);
+
+	if (idmap_tdb == NULL) {
+		d_printf("Could not open idmap: %s\n", argv[0]);
+		return -1;
+	}
+
+	tdb_traverse(idmap_tdb, net_idmap_dump_one_entry, NULL);
+
+	tdb_close(idmap_tdb);
+
+	return 0;
+}
+
+/***********************************************************
+ Write entries from stdin to current local idmap
+ **********************************************************/
+static int net_idmap_restore(int argc, const char **argv)
+{
+	if (!idmap_init()) {
+		d_printf("Could not init idmap\n");
+		return -1;
+	}
+
+	while (!feof(stdin)) {
+		fstring line, sid_string;
+		int len;
+		unid_t id;
+		int type = ID_EMPTY;
+		DOM_SID sid;
+
+		if (fgets(line, sizeof(line)-1, stdin) == NULL)
+			break;
+
+		len = strlen(line);
+
+		if ( (len > 0) && (line[len-1] == '\n') )
+			line[len-1] = '\0';
+
+		if (sscanf(line, "GID %d %s", &id.gid, sid_string) == 2) {
+			type = ID_GROUPID;
+		}
+
+		if (sscanf(line, "UID %d %s", &id.uid, sid_string) == 2) {
+			type = ID_USERID;
+		}
+
+		if (type == ID_EMPTY) {
+			d_printf("ignoring invalid line [%s]\n", line);
+			continue;
+		}
+
+		if (!string_to_sid(&sid, sid_string)) {
+			d_printf("ignoring invalid sid [%s]\n", sid_string);
+			continue;
+		}
+
+		if (!NT_STATUS_IS_OK(idmap_set_mapping(&sid, id, type))) {
+			d_printf("Could not set mapping of %s %d to sid %s\n",
+				 (type == ID_GROUPID) ? "GID" : "UID",
+				 (type == ID_GROUPID) ? id.gid : id.uid,
+				 sid_string_static(&sid));
+			continue;
+		}
+				 
+	}
+
+	idmap_close();
+	return 0;
+}
+
+int net_help_idmap(int argc, const char **argv)
+{
+	d_printf("net idmap dump filename"\
+		 "\n  Dump current id mapping\n");
+
+	d_printf("net idmap restore"\
+		 "\n  Restore entries from stdin to current local idmap\n");
+
+	return -1;
+}
+
+/***********************************************************
+ Look at the current idmap
+ **********************************************************/
+int net_idmap(int argc, const char **argv)
+{
+	struct functable func[] = {
+		{"dump", net_idmap_dump},
+		{"restore", net_idmap_restore},
+		{"help", net_help_idmap},
+		{NULL, NULL}
+	};
+
+	return net_run_function(argc, argv, func, net_help_idmap);
+}
+
+


More information about the samba-technical mailing list