Small patch for pdbedit -i

Tom Alsberg alsbergt at cs.huji.ac.il
Sun May 16 08:12:47 GMT 2004


A sometimes useful option for pdbedit's import functionality is to
only import a specific account.

Attached is a patch to pdbedit.c that, when in addition to the '-i' or
'-e' flag, a username is given, will only export that account.  Didn't
do it for groups, though.

  Cheers,
  -- Tom

-- 
  Tom Alsberg - hacker (being the best description fitting this space)
  Web page:	http://www.cs.huji.ac.il/~alsbergt/
DISCLAIMER:  The above message does not even necessarily represent what
my fingers have typed on the keyboard, save anything further.
-------------- next part --------------
--- source.old/utils/pdbedit.c
+++ source/utils/pdbedit.c
@@ -57,9 +57,12 @@
  Add all currently available users to another db
  ********************************************************/
 
-static int export_database (struct pdb_context *in, struct pdb_context *out) {
+static int export_database (struct pdb_context *in, struct pdb_context
+			    *out, const char *username) {
 	SAM_ACCOUNT *user = NULL;
 
+	DEBUG(3, ("called with username=\"%s\"\n", username));
+
 	if (NT_STATUS_IS_ERR(in->pdb_setsampwent(in, 0))) {
 		fprintf(stderr, "Can't sampwent!\n");
 		return 1;
@@ -71,10 +74,17 @@
 	}
 
 	while (NT_STATUS_IS_OK(in->pdb_getsampwent(in, user))) {
-		out->pdb_add_sam_account(out, user);
-		if (!NT_STATUS_IS_OK(pdb_reset_sam(user))){
-			fprintf(stderr, "Can't reset SAM_ACCOUNT!\n");
-			return 1;
+		DEBUG(4, ("Processing account %s\n",
+			  user->private.username));
+		if (!username || 
+		    (strcmp(username, user->private.username)
+		     == 0)) {
+			out->pdb_add_sam_account(out, user);
+			if (!NT_STATUS_IS_OK(pdb_reset_sam(user))) {
+				fprintf(stderr,
+					"Can't reset SAM_ACCOUNT!\n");
+				return 1;
+			}
 		}
 	}
 
@@ -759,7 +769,7 @@
 
 	/* import and export operations */
 	if (((checkparms & BIT_IMPORT) || (checkparms & BIT_EXPORT))
-			&& !(checkparms & ~(BIT_IMPORT +BIT_EXPORT))) {
+	    && !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
 		if (backend_in) {
 			if (!NT_STATUS_IS_OK(make_pdb_context_string(&bin, backend_in))) {
 				fprintf(stderr, "Can't initialize passdb backend.\n");
@@ -777,9 +787,15 @@
 			bout = bdef;
 		}
 		if (transfer_groups) {
-			return export_groups(bin, bout);
+			if (!(checkparms & BIT_USER))
+				return export_groups(bin, bout);
 		} else {
-			return export_database(bin, bout);
+			if (checkparms & BIT_USER)
+				return export_database(bin, bout,
+						       user_name);
+			else
+				return export_database(bin, bout,
+						       NULL);
 		}
 	}
 


More information about the samba-technical mailing list