Skip to contentSkip to conversation

cifscreds: cleanup and overhaul of cifscreds utility

PATCH In review Jeff Layton 2 participants 22 messages Jan 6, 2012 – Jan 24, 2012 6 messages not in the archive

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover cifscreds: cleanup and overhaul of cifscreds utility

This patchset is a cleanup and overhaul of the cifscreds utility that lives in the cifs-utils tree today. Igor Druzhinin did a wonderful job on this when he did the original code a couple of years ago, but I did a rather poor job at the time of communicating what we actually need for this tool to do. Mea culpa...

This patch is a third pass at morphing it into a tool that's more like what we need. I believe with this, I'll be able to roll some kernel patches that can use the stashed key for establishing sessions.

I've made a few changes since the last set:

- convert the tool to use the new "secret" key_type that I'm proposing

- a manpage has been added

- the key is now prefixed with "cifs:" instead of "cifscreds:"

The idea here is that we want to be able to allow users to stash their NTLM credentials in the kernel, so that it's possible to establish a session on the fly when that user walks into a multiuser mount.

This set should be considered in conjunction with the kernel patchset that I proposed earlier today:

[PATCH 0/3] cifs: allow multiuser mounts with authtypes besides krb5

Jeff Layton (15):
util: move getusername to util.c
cifscreds: add unused attribute to argv parm in cifscreds_clearall
cifscreds: eliminate domain parm from most functions
cifscreds: remove user parameter from create_description
cifscreds: make username part of value instead of description
cifscreds: make usage use "return" and have callers return
cifscreds: move option parsing into main()
cifscreds: make username parameter optional
cifscreds: add --domain flag
cifscreds: loosen allowed characters in domain names
cifscreds: use the session keyring
cifscreds: make cifscreds use the "unreadable" keyring
cifscreds: make key_search use keyctl_search
cifscreds: change prefix on keys to "cifs:" instead of "cifscreds:"
cifscreds: add a manpage

 Makefile.am   |    1 +
 cifscreds.1   |  188 ++++++++++++++++++++++++++++++++++
 cifscreds.c   |  311 +++++++++++++++++++++++++--------------------------------
 cifscreds.pod |   81 +++++++++++++++
 mount.cifs.c  |   11 --
 util.c        |   13 +++
 util.h        |    1 +
 7 files changed, 418 insertions(+), 188 deletions(-)
 create mode 100644 cifscreds.1
 create mode 100644 cifscreds.pod
Signature · 1 line
1.7.7.4

A message that is not in the archive (sent off-list or not yet received): 1325873687-11229-1-git-send-email-jlayton-eUNUBHrolfbYtjvyW6yDsg@public.gmane.org

#2 lore raw

PATCH v3 1/15 util: move getusername to util.c

---
 mount.cifs.c |   11 -----------
 util.c       |   13 +++++++++++++
 util.h       |    1 +
 3 files changed, 14 insertions(+), 11 deletions(-)
mount.cifs.c +0 -11
index 147f7fc..ced9f4e 100644
@@ -361,17 +361,6 @@ static int set_password(struct parsed_mount_info *parsed_info, const char *src)
 	return 0;
 }
 
-/* caller frees username if necessary */
-static char *getusername(uid_t uid)
-{
-	char *username = NULL;
-	struct passwd *password = getpwuid(uid);
-
-	if (password)
-		username = password->pw_name;
-	return username;
-}
-
 /*
  * Parse a username string into parsed_mount_info fields. The format is:
  *
util.c +13 -0
index 41c6784..80d5a80 100644
@@ -24,6 +24,7 @@
 
 #include <sys/types.h>
 #include <string.h>
+#include <pwd.h>
 
 /* glibc doesn't have strlcpy, strlcat. Ensure we do. JRA. We
  * don't link to libreplace so need them here. */
@@ -69,3 +70,15 @@ size_t strlcat(char *d, const char *s, size_t bufsize)
 }
 #endif
 
+/* caller frees username if necessary */
+char *
+getusername(uid_t uid)
+{
+	char *username = NULL;
+	struct passwd *password = getpwuid(uid);
+
+	if (password)
+		username = password->pw_name;
+	return username;
+}
+
util.h +1 -0
index 7a44faf..d3ca015 100644
@@ -28,5 +28,6 @@
 size_t strlcpy(char *d, const char *s, size_t bufsize);
 size_t strlcat(char *d, const char *s, size_t bufsize);
 
+char *getusername(uid_t uid);
 #endif /* _LIBUTIL_H */
 
Signature · 1 line
1.7.7.4
#3 lore raw

PATCH v3 2/15 cifscreds: add unused attribute to argv parm in cifscreds_clearall

In reply to a message not in the archive

...to eliminate this warning:

cifscreds.c: In function ‘cifscreds_clearall’:
cifscreds.c:422:47: warning: unused parameter ‘argv’

---
 cifscreds.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
cifscreds.c +1 -1
index f21a47f..d771056 100644
@@ -419,7 +419,7 @@ static int cifscreds_clear(int argc, char *argv[])
 }
 
 /* clearall command handler */
-static int cifscreds_clearall(int argc, char *argv[])
+static int cifscreds_clearall(int argc, char *argv[] __attribute__ ((unused)))
 {
 	key_serial_t key;
 	int count = 0, errors = 0;
Signature · 1 line
1.7.7.4
#4 lore raw

PATCH v3 3/15 cifscreds: eliminate domain parm from most functions

In reply to a message not in the archive

Eventually we'll add this back in a different way. The domain and address should be exclusive of one another. IOW, we want the kernel to be able to find credentials for a specific address or for the domain of which the server is a member.

---
 cifscreds.c |   85 ++++++++++++----------------------------------------------
 1 files changed, 18 insertions(+), 67 deletions(-)
cifscreds.c +18 -67
index d771056..a9181ef 100644
@@ -62,10 +62,10 @@ static int cifscreds_update(int argc, char *argv[]);
 const char *thisprogram;
 
 struct command commands[] = {
-	{ cifscreds_add,	"add",		"<host> <user> [domain]" },
-	{ cifscreds_clear,	"clear",	"<host> <user> [domain]" },
+	{ cifscreds_add,	"add",		"<host> <user>" },
+	{ cifscreds_clear,	"clear",	"<host> <user>" },
 	{ cifscreds_clearall,	"clearall",	"" },
-	{ cifscreds_update,	"update",	"<host> <user> [domain]" },
+	{ cifscreds_update,	"update",	"<host> <user>" },
 	{ NULL, "", NULL }
 };
 
@@ -85,41 +85,28 @@ static void usage(void)
 
 /* create key's description string from given credentials */
 static char *
-create_description(const char *addr, const char *user,
-		   const char *domain, char *desc)
+create_description(const char *addr, const char *user, char *desc)
 {
 	char *str_end;
 	int str_len;
 
 	sprintf(desc, "%s:%s:%s:", THIS_PROGRAM_NAME, addr, user);
 
-	if (domain != NULL) {
-		str_end = desc + strnlen(desc, INET6_ADDRSTRLEN + \
-					+ MAX_USERNAME_SIZE + \
-					+ sizeof(THIS_PROGRAM_NAME) + 3);
-		str_len = strnlen(domain, MAX_DOMAIN_SIZE);
-		while (str_len--) {
-			*str_end = tolower(*domain++);
-			str_end++;
-		}
-		*str_end = '\0';
-	}
-
 	return desc;
 }
 
 /* search a specific key in keyring */
 static key_serial_t
-key_search(const char *addr, const char *user, const char *domain)
+key_search(const char *addr, const char *user)
 {
-	char desc[INET6_ADDRSTRLEN + MAX_USERNAME_SIZE + MAX_DOMAIN_SIZE + \
+	char desc[INET6_ADDRSTRLEN + MAX_USERNAME_SIZE + \
 		+ sizeof(THIS_PROGRAM_NAME) + 3];
 	key_serial_t key, *pk;
 	void *keylist;
 	char *buffer;
 	int count, dpos, n, ret;
 
-	create_description(addr, user, domain, desc);
+	create_description(addr, user, desc);
 
 	/* read the key payload data */
 	count = keyctl_read_alloc(DEST_KEYRING, &keylist);
@@ -219,13 +206,11 @@ key_search_all_out:
 
 /* add or update a specific key to keyring */
 static key_serial_t
-key_add(const char *addr, const char *user,
-	const char *domain, const char *pass)
+key_add(const char *addr, const char *user, const char *pass)
 {
-	char desc[INET6_ADDRSTRLEN + MAX_USERNAME_SIZE + MAX_DOMAIN_SIZE + \
-		+ sizeof(THIS_PROGRAM_NAME) + 3];
+	char desc[INET6_ADDRSTRLEN + MAX_USERNAME_SIZE + sizeof(THIS_PROGRAM_NAME) + 3];
 
-	create_description(addr, user, domain, desc);
+	create_description(addr, user, desc);
 
 	return add_key("user", desc, pass, strnlen(pass, MOUNT_PASSWD_SIZE) + 1,
 		DEST_KEYRING);
@@ -239,7 +224,7 @@ static int cifscreds_add(int argc, char *argv[])
 	char *pass;
 	int ret;
 
-	if (argc != 4 && argc != 5)
+	if (argc != 4)
 		usage();
 
 	ret = resolve_host(argv[2], addrstr);
@@ -259,15 +244,6 @@ static int cifscreds_add(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
-	if (argc == 5) {
-		if (strspn(argv[4], DOMAIN_ALLOWED_CHARS) !=
-			strnlen(argv[4], MAX_DOMAIN_SIZE)
-		) {
-			fprintf(stderr, "error: Incorrect domain name\n");
-			return EXIT_FAILURE;
-		}
-	}
-
 	/* search for same credentials stashed for current host */
 	currentaddress = addrstr;
 	nextaddress = strchr(currentaddress, ',');
@@ -275,9 +251,7 @@ static int cifscreds_add(int argc, char *argv[])
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		if (key_search(currentaddress, argv[3],
-			argc == 5 ? argv[4] : NULL) > 0
-		) {
+		if (key_search(currentaddress, argv[3]) > 0) {
 			printf("You already have stashed credentials "
 				"for %s (%s)\n", currentaddress, argv[2]);
 			printf("If you want to update them use:\n");
@@ -307,8 +281,7 @@ static int cifscreds_add(int argc, char *argv[])
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		key_serial_t key = key_add(currentaddress, argv[3],
-					   argc == 5 ? argv[4] : NULL, pass);
+		key_serial_t key = key_add(currentaddress, argv[3], pass);
 		if (key <= 0) {
 			fprintf(stderr, "error: Add credential key for %s\n",
 				currentaddress);
@@ -346,7 +319,7 @@ static int cifscreds_clear(int argc, char *argv[])
 	char *currentaddress, *nextaddress;
 	int ret, count = 0, errors = 0;
 
-	if (argc != 4 && argc != 5)
+	if (argc != 4)
 		usage();
 
 	ret = resolve_host(argv[2], addrstr);
@@ -366,15 +339,6 @@ static int cifscreds_clear(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
-	if (argc == 5) {
-		if (strspn(argv[4], DOMAIN_ALLOWED_CHARS) !=
-			strnlen(argv[4], MAX_DOMAIN_SIZE)
-		) {
-			fprintf(stderr, "error: Incorrect domain name\n");
-			return EXIT_FAILURE;
-		}
-	}
-
 	/*
 	 * search for same credentials stashed for current host
 	 * and unlink them from session keyring
@@ -385,8 +349,7 @@ static int cifscreds_clear(int argc, char *argv[])
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		key_serial_t key = key_search(currentaddress, argv[3],
-						argc == 5 ? argv[4] : NULL);
+		key_serial_t key = key_search(currentaddress, argv[3]);
 		if (key > 0) {
 			if (keyctl(KEYCTL_UNLINK, key, DEST_KEYRING) < 0) {
 				fprintf(stderr, "error: Removing key from "
@@ -464,7 +427,7 @@ static int cifscreds_update(int argc, char *argv[])
 	char *addrs[16];
 	int ret, id, count = 0;
 
-	if (argc != 4 && argc != 5)
+	if (argc != 4)
 		usage();
 
 	ret = resolve_host(argv[2], addrstr);
@@ -484,15 +447,6 @@ static int cifscreds_update(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
-	if (argc == 5) {
-		if (strspn(argv[4], DOMAIN_ALLOWED_CHARS) !=
-			strnlen(argv[4], MAX_DOMAIN_SIZE)
-		) {
-			fprintf(stderr, "error: Incorrect domain name\n");
-			return EXIT_FAILURE;
-		}
-	}
-
 	/* search for necessary credentials stashed in session keyring */
 	currentaddress = addrstr;
 	nextaddress = strchr(currentaddress, ',');
@@ -500,9 +454,7 @@ static int cifscreds_update(int argc, char *argv[])
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		if (key_search(currentaddress, argv[3],
-			argc == 5 ? argv[4] : NULL) > 0
-		) {
+		if (key_search(currentaddress, argv[3]) > 0) {
 			addrs[count] = currentaddress;
 			count++;
 		}
@@ -528,8 +480,7 @@ static int cifscreds_update(int argc, char *argv[])
 	pass = getpass("Password: ");
 
 	for (id = 0; id < count; id++) {
-		key_serial_t key = key_add(addrs[id], argv[3],
-					argc == 5 ? argv[4] : NULL, pass);
+		key_serial_t key = key_add(addrs[id], argv[3], pass);
 		if (key <= 0)
 			fprintf(stderr, "error: Update credential key "
 				"for %s\n", addrs[id]);
Signature · 1 line
1.7.7.4
#5 lore raw

PATCH v3 4/15 cifscreds: remove user parameter from create_description

In reply to a message not in the archive

The username should be part of the key payload and not part of the description. Also, prefix the address with an "a:" in the description. Eventually we'll also need a "domain" key variant.

---
 cifscreds.c |   16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)
cifscreds.c +8 -8
index a9181ef..a6cec98 100644
@@ -85,19 +85,19 @@ static void usage(void)
 
 /* create key's description string from given credentials */
 static char *
-create_description(const char *addr, const char *user, char *desc)
+create_description(const char *addr, char *desc)
 {
 	char *str_end;
 	int str_len;
 
-	sprintf(desc, "%s:%s:%s:", THIS_PROGRAM_NAME, addr, user);
+	sprintf(desc, "%s:a:%s", THIS_PROGRAM_NAME, addr);
 
 	return desc;
 }
 
 /* search a specific key in keyring */
 static key_serial_t
-key_search(const char *addr, const char *user)
+key_search(const char *addr)
 {
 	char desc[INET6_ADDRSTRLEN + MAX_USERNAME_SIZE + \
 		+ sizeof(THIS_PROGRAM_NAME) + 3];
@@ -106,7 +106,7 @@ key_search(const char *addr, const char *user)
 	char *buffer;
 	int count, dpos, n, ret;
 
-	create_description(addr, user, desc);
+	create_description(addr, desc);
 
 	/* read the key payload data */
 	count = keyctl_read_alloc(DEST_KEYRING, &keylist);
@@ -210,7 +210,7 @@ key_add(const char *addr, const char *user, const char *pass)
 {
 	char desc[INET6_ADDRSTRLEN + MAX_USERNAME_SIZE + sizeof(THIS_PROGRAM_NAME) + 3];
 
-	create_description(addr, user, desc);
+	create_description(addr, desc);
 
 	return add_key("user", desc, pass, strnlen(pass, MOUNT_PASSWD_SIZE) + 1,
 		DEST_KEYRING);
@@ -251,7 +251,7 @@ static int cifscreds_add(int argc, char *argv[])
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		if (key_search(currentaddress, argv[3]) > 0) {
+		if (key_search(currentaddress) > 0) {
 			printf("You already have stashed credentials "
 				"for %s (%s)\n", currentaddress, argv[2]);
 			printf("If you want to update them use:\n");
@@ -349,7 +349,7 @@ static int cifscreds_clear(int argc, char *argv[])
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		key_serial_t key = key_search(currentaddress, argv[3]);
+		key_serial_t key = key_search(currentaddress);
 		if (key > 0) {
 			if (keyctl(KEYCTL_UNLINK, key, DEST_KEYRING) < 0) {
 				fprintf(stderr, "error: Removing key from "
@@ -454,7 +454,7 @@ static int cifscreds_update(int argc, char *argv[])
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		if (key_search(currentaddress, argv[3]) > 0) {
+		if (key_search(currentaddress) > 0) {
 			addrs[count] = currentaddress;
 			count++;
 		}
Signature · 1 line
1.7.7.4
#6 lore raw

PATCH v3 5/15 cifscreds: make username part of value instead of description

In reply to a message not in the archive

Change the payload to be "username:password". Since usernames can't contain ':', this is suitable delimiter. Also, create_description is just a sprintf now, so eliminate it.

---
 cifscreds.c |   30 +++++++++++-------------------
 1 files changed, 11 insertions(+), 19 deletions(-)
cifscreds.c +11 -19
index a6cec98..b6c1fb6 100644
@@ -83,30 +83,17 @@ static void usage(void)
 	exit(EXIT_FAILURE);
 }
 
-/* create key's description string from given credentials */
-static char *
-create_description(const char *addr, char *desc)
-{
-	char *str_end;
-	int str_len;
-
-	sprintf(desc, "%s:a:%s", THIS_PROGRAM_NAME, addr);
-
-	return desc;
-}
-
 /* search a specific key in keyring */
 static key_serial_t
 key_search(const char *addr)
 {
-	char desc[INET6_ADDRSTRLEN + MAX_USERNAME_SIZE + \
-		+ sizeof(THIS_PROGRAM_NAME) + 3];
+	char desc[INET6_ADDRSTRLEN + sizeof(THIS_PROGRAM_NAME) + 4];
 	key_serial_t key, *pk;
 	void *keylist;
 	char *buffer;
 	int count, dpos, n, ret;
 
-	create_description(addr, desc);
+	sprintf(desc, "%s:a:%s", THIS_PROGRAM_NAME, addr);
 
 	/* read the key payload data */
 	count = keyctl_read_alloc(DEST_KEYRING, &keylist);
@@ -208,12 +195,17 @@ key_search_all_out:
 static key_serial_t
 key_add(const char *addr, const char *user, const char *pass)
 {
-	char desc[INET6_ADDRSTRLEN + MAX_USERNAME_SIZE + sizeof(THIS_PROGRAM_NAME) + 3];
+	int len;
+	char desc[INET6_ADDRSTRLEN + sizeof(THIS_PROGRAM_NAME) + 4];
+	char val[MOUNT_PASSWD_SIZE +  MAX_USERNAME_SIZE + 2];
+
+	/* set key description */
+	sprintf(desc, "%s:a:%s", THIS_PROGRAM_NAME, addr);
 
-	create_description(addr, desc);
+	/* set payload contents */
+	len = sprintf(val, "%s:%s", user, pass);
 
-	return add_key("user", desc, pass, strnlen(pass, MOUNT_PASSWD_SIZE) + 1,
-		DEST_KEYRING);
+	return add_key("user", desc, val, len + 1, DEST_KEYRING);
 }
 
 /* add command handler */
Signature · 1 line
1.7.7.4
#7 lore raw

PATCH v3 6/15 cifscreds: make usage use "return" and have callers return

In reply to a message not in the archive

...banish the use of exit(), which may be helpful in the future in the event that we eventually move some of this code into a library.

---
 cifscreds.c |   21 +++++++++++----------
 1 files changed, 11 insertions(+), 10 deletions(-)
cifscreds.c +11 -10
index b6c1fb6..3ed15f2 100644
@@ -70,7 +70,8 @@ struct command commands[] = {
 };
 
 /* display usage information */
-static void usage(void)
+static int
+usage(void)
 {
 	struct command *cmd;
 
@@ -80,7 +81,7 @@ static void usage(void)
 			cmd->name, cmd->format);
 	fprintf(stderr, "\n");
 
-	exit(EXIT_FAILURE);
+	return EXIT_FAILURE;
 }
 
 /* search a specific key in keyring */
@@ -217,7 +218,7 @@ static int cifscreds_add(int argc, char *argv[])
 	int ret;
 
 	if (argc != 4)
-		usage();
+		return usage();
 
 	ret = resolve_host(argv[2], addrstr);
 	switch (ret) {
@@ -312,7 +313,7 @@ static int cifscreds_clear(int argc, char *argv[])
 	int ret, count = 0, errors = 0;
 
 	if (argc != 4)
-		usage();
+		return usage();
 
 	ret = resolve_host(argv[2], addrstr);
 	switch (ret) {
@@ -380,7 +381,7 @@ static int cifscreds_clearall(int argc, char *argv[] __attribute__ ((unused)))
 	int count = 0, errors = 0;
 
 	if (argc != 2)
-		usage();
+		return usage();
 
 	/*
 	 * search for all program's credentials stashed in session keyring
@@ -420,7 +421,7 @@ static int cifscreds_update(int argc, char *argv[])
 	int ret, id, count = 0;
 
 	if (argc != 4)
-		usage();
+		return usage();
 
 	ret = resolve_host(argv[2], addrstr);
 	switch (ret) {
@@ -491,7 +492,7 @@ int main(int argc, char **argv)
 		thisprogram = THIS_PROGRAM_NAME;
 
 	if (argc == 1)
-		usage();
+		return usage();
 
 	/* find the best fit command */
 	best = NULL;
@@ -510,7 +511,7 @@ int main(int argc, char **argv)
 		/* partial match */
 		if (best) {
 			fprintf(stderr, "Ambiguous command\n");
-			exit(EXIT_FAILURE);
+			return EXIT_FAILURE;
 		}
 
 		best = cmd;
@@ -518,8 +519,8 @@ int main(int argc, char **argv)
 
 	if (!best) {
 		fprintf(stderr, "Unknown command\n");
-		exit(EXIT_FAILURE);
+		return EXIT_FAILURE;
 	}
 
-	exit(best->action(argc, argv));
+	return best->action(argc, argv);
 }
Signature · 1 line
1.7.7.4
#8 lore raw

PATCH v3 7/15 cifscreds: move option parsing into main()

In reply to a message not in the archive

Having to parse options in every command routine is cumbersome and restrictive. Declare a struct to hold arguments, and then have the functions take that struct as an argument.

---
 cifscreds.c |   76 ++++++++++++++++++++++++++++++++++------------------------
 1 files changed, 44 insertions(+), 32 deletions(-)
cifscreds.c +44 -32
index 3ed15f2..79ab426 100644
@@ -48,16 +48,21 @@
 /* destination keyring */
 #define DEST_KEYRING KEY_SPEC_USER_KEYRING
 
+struct cmdarg {
+	char	*host;
+	char	*user;
+};
+
 struct command {
-	int (*action)(int argc, char *argv[]);
+	int (*action)(struct cmdarg *arg);
 	const char	name[MAX_COMMAND_SIZE];
 	const char	*format;
 };
 
-static int cifscreds_add(int argc, char *argv[]);
-static int cifscreds_clear(int argc, char *argv[]);
-static int cifscreds_clearall(int argc, char *argv[]);
-static int cifscreds_update(int argc, char *argv[]);
+static int cifscreds_add(struct cmdarg *arg);
+static int cifscreds_clear(struct cmdarg *arg);
+static int cifscreds_clearall(struct cmdarg *arg);
+static int cifscreds_update(struct cmdarg *arg);
 
 const char *thisprogram;
 
@@ -210,21 +215,21 @@ key_add(const char *addr, const char *user, const char *pass)
 }
 
 /* add command handler */
-static int cifscreds_add(int argc, char *argv[])
+static int cifscreds_add(struct cmdarg *arg)
 {
 	char addrstr[MAX_ADDR_LIST_LEN];
 	char *currentaddress, *nextaddress;
 	char *pass;
 	int ret;
 
-	if (argc != 4)
+	if (arg->host == NULL || arg->user == NULL)
 		return usage();
 
-	ret = resolve_host(argv[2], addrstr);
+	ret = resolve_host(arg->host, addrstr);
 	switch (ret) {
 	case EX_USAGE:
 		fprintf(stderr, "error: Could not resolve address "
-			"for %s\n", argv[2]);
+			"for %s\n", arg->host);
 		return EXIT_FAILURE;
 
 	case EX_SYSERR:
@@ -232,7 +237,7 @@ static int cifscreds_add(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
-	if (strpbrk(argv[3], USER_DISALLOWED_CHARS)) {
+	if (strpbrk(arg->user, USER_DISALLOWED_CHARS)) {
 		fprintf(stderr, "error: Incorrect username\n");
 		return EXIT_FAILURE;
 	}
@@ -246,7 +251,7 @@ static int cifscreds_add(int argc, char *argv[])
 	while (currentaddress) {
 		if (key_search(currentaddress) > 0) {
 			printf("You already have stashed credentials "
-				"for %s (%s)\n", currentaddress, argv[2]);
+				"for %s (%s)\n", currentaddress, arg->host);
 			printf("If you want to update them use:\n");
 			printf("\t%s update\n", thisprogram);
 
@@ -274,7 +279,7 @@ static int cifscreds_add(int argc, char *argv[])
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		key_serial_t key = key_add(currentaddress, argv[3], pass);
+		key_serial_t key = key_add(currentaddress, arg->user, pass);
 		if (key <= 0) {
 			fprintf(stderr, "error: Add credential key for %s\n",
 				currentaddress);
@@ -289,7 +294,7 @@ static int cifscreds_add(int argc, char *argv[])
 				if (keyctl(KEYCTL_UNLINK, key, DEST_KEYRING) < 0) {
 					fprintf(stderr, "error: Deleting key from "
 						"keyring for %s (%s)\n",
-						currentaddress, argv[2]);
+						currentaddress, arg->host);
 				}
 			}
 		}
@@ -306,20 +311,20 @@ static int cifscreds_add(int argc, char *argv[])
 }
 
 /* clear command handler */
-static int cifscreds_clear(int argc, char *argv[])
+static int cifscreds_clear(struct cmdarg *arg)
 {
 	char addrstr[MAX_ADDR_LIST_LEN];
 	char *currentaddress, *nextaddress;
 	int ret, count = 0, errors = 0;
 
-	if (argc != 4)
+	if (arg->host == NULL || arg->user == NULL)
 		return usage();
 
-	ret = resolve_host(argv[2], addrstr);
+	ret = resolve_host(arg->host, addrstr);
 	switch (ret) {
 	case EX_USAGE:
 		fprintf(stderr, "error: Could not resolve address "
-			"for %s\n", argv[2]);
+			"for %s\n", arg->host);
 		return EXIT_FAILURE;
 
 	case EX_SYSERR:
@@ -327,7 +332,7 @@ static int cifscreds_clear(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
-	if (strpbrk(argv[3], USER_DISALLOWED_CHARS)) {
+	if (strpbrk(arg->user, USER_DISALLOWED_CHARS)) {
 		fprintf(stderr, "error: Incorrect username\n");
 		return EXIT_FAILURE;
 	}
@@ -347,7 +352,7 @@ static int cifscreds_clear(int argc, char *argv[])
 			if (keyctl(KEYCTL_UNLINK, key, DEST_KEYRING) < 0) {
 				fprintf(stderr, "error: Removing key from "
 					"keyring for %s (%s)\n",
-					currentaddress, argv[2]);
+					currentaddress, arg->host);
 				errors++;
 			} else {
 				count++;
@@ -364,7 +369,7 @@ static int cifscreds_clear(int argc, char *argv[])
 
 	if (!count && !errors) {
 		printf("You have no same stashed credentials "
-			" for %s\n", argv[2]);
+			" for %s\n", arg->host);
 		printf("If you want to add them use:\n");
 		printf("\t%s add\n", thisprogram);
 
@@ -375,14 +380,11 @@ static int cifscreds_clear(int argc, char *argv[])
 }
 
 /* clearall command handler */
-static int cifscreds_clearall(int argc, char *argv[] __attribute__ ((unused)))
+static int cifscreds_clearall(struct cmdarg *arg __attribute__ ((unused)))
 {
 	key_serial_t key;
 	int count = 0, errors = 0;
 
-	if (argc != 2)
-		return usage();
-
 	/*
 	 * search for all program's credentials stashed in session keyring
 	 * and then unlink them
@@ -413,21 +415,21 @@ static int cifscreds_clearall(int argc, char *argv[] __attribute__ ((unused)))
 }
 
 /* update command handler */
-static int cifscreds_update(int argc, char *argv[])
+static int cifscreds_update(struct cmdarg *arg)
 {
 	char addrstr[MAX_ADDR_LIST_LEN];
 	char *currentaddress, *nextaddress, *pass;
 	char *addrs[16];
 	int ret, id, count = 0;
 
-	if (argc != 4)
+	if (arg->host == NULL || arg->user == NULL)
 		return usage();
 
-	ret = resolve_host(argv[2], addrstr);
+	ret = resolve_host(arg->host, addrstr);
 	switch (ret) {
 	case EX_USAGE:
 		fprintf(stderr, "error: Could not resolve address "
-			"for %s\n", argv[2]);
+			"for %s\n", arg->host);
 		return EXIT_FAILURE;
 
 	case EX_SYSERR:
@@ -435,7 +437,7 @@ static int cifscreds_update(int argc, char *argv[])
 		return EXIT_FAILURE;
 	}
 
-	if (strpbrk(argv[3], USER_DISALLOWED_CHARS)) {
+	if (strpbrk(arg->user, USER_DISALLOWED_CHARS)) {
 		fprintf(stderr, "error: Incorrect username\n");
 		return EXIT_FAILURE;
 	}
@@ -462,7 +464,7 @@ static int cifscreds_update(int argc, char *argv[])
 
 	if (!count) {
 		printf("You have no same stashed credentials "
-			"for %s\n", argv[2]);
+			"for %s\n", arg->host);
 		printf("If you want to add them use:\n");
 		printf("\t%s add\n", thisprogram);
 
@@ -473,7 +475,7 @@ static int cifscreds_update(int argc, char *argv[])
 	pass = getpass("Password: ");
 
 	for (id = 0; id < count; id++) {
-		key_serial_t key = key_add(addrs[id], argv[3], pass);
+		key_serial_t key = key_add(addrs[id], arg->user, pass);
 		if (key <= 0)
 			fprintf(stderr, "error: Update credential key "
 				"for %s\n", addrs[id]);
@@ -485,8 +487,11 @@ static int cifscreds_update(int argc, char *argv[])
 int main(int argc, char **argv)
 {
 	struct command *cmd, *best;
+	struct cmdarg arg;
 	int n;
 
+	memset(&arg, 0, sizeof(arg));
+
 	thisprogram = (char *)basename(argv[0]);
 	if (thisprogram == NULL)
 		thisprogram = THIS_PROGRAM_NAME;
@@ -522,5 +527,12 @@ int main(int argc, char **argv)
 		return EXIT_FAILURE;
 	}
 
-	return best->action(argc, argv);
+	/* first argument should be host */
+	if (argc >= 3)
+		arg.host = argv[2];
+
+	if (argc >= 4)
+		arg.user = argv[3];
+
+	return best->action(&arg);
 }
Signature · 1 line
1.7.7.4
#9 lore raw

PATCH v3 8/15 cifscreds: make username parameter optional

In reply to a message not in the archive

...and use getopt_long to get it. If someone doesn't specify the username, use getusername() to get it.

---
 cifscreds.c |   39 ++++++++++++++++++++++++++++-----------
 1 files changed, 28 insertions(+), 11 deletions(-)
cifscreds.c +28 -11
index 79ab426..f45497a 100644
@@ -27,8 +27,10 @@
 #include <string.h>
 #include <ctype.h>
 #include <keyutils.h>
+#include <getopt.h>
 #include "mount.h"
 #include "resolve_host.h"
+#include "util.h"
 
 #define THIS_PROGRAM_NAME "cifscreds"
 
@@ -49,8 +51,8 @@
 #define DEST_KEYRING KEY_SPEC_USER_KEYRING
 
 struct cmdarg {
-	char	*host;
-	char	*user;
+	char		*host;
+	char		*user;
 };
 
 struct command {
@@ -67,13 +69,18 @@ static int cifscreds_update(struct cmdarg *arg);
 const char *thisprogram;
 
 struct command commands[] = {
-	{ cifscreds_add,	"add",		"<host> <user>" },
-	{ cifscreds_clear,	"clear",	"<host> <user>" },
+	{ cifscreds_add,	"add",		"[-u username] <host>" },
+	{ cifscreds_clear,	"clear",	"[-u username] <host>" },
 	{ cifscreds_clearall,	"clearall",	"" },
-	{ cifscreds_update,	"update",	"<host> <user>" },
+	{ cifscreds_update,	"update",	"[-u username] <host>" },
 	{ NULL, "", NULL }
 };
 
+struct option longopts[] = {
+	{"username", 1, NULL, 'u'},
+	{NULL, 0, NULL, 0}
+};
+
 /* display usage information */
 static int
 usage(void)
@@ -499,12 +506,22 @@ int main(int argc, char **argv)
 	if (argc == 1)
 		return usage();
 
+	while((n = getopt_long(argc, argv, "u:", longopts, NULL)) != -1) {
+		switch (n) {
+		case 'u':
+			arg.user = optarg;
+			break;
+		default:
+			return usage();
+		}
+	}
+
 	/* find the best fit command */
 	best = NULL;
-	n = strnlen(argv[1], MAX_COMMAND_SIZE);
+	n = strnlen(argv[optind], MAX_COMMAND_SIZE);
 
 	for (cmd = commands; cmd->action; cmd++) {
-		if (memcmp(cmd->name, argv[1], n) != 0)
+		if (memcmp(cmd->name, argv[optind], n) != 0)
 			continue;
 
 		if (cmd->name[n] == 0) {
@@ -527,12 +544,12 @@ int main(int argc, char **argv)
 		return EXIT_FAILURE;
 	}
 
-	/* first argument should be host */
+	/* second argument should be host */
 	if (argc >= 3)
-		arg.host = argv[2];
+		arg.host = argv[optind + 1];
 
-	if (argc >= 4)
-		arg.user = argv[3];
+	if (arg.user == NULL)
+		arg.user = getusername(getuid());
 
 	return best->action(&arg);
 }
Signature · 1 line
1.7.7.4

A message that is not in the archive (sent off-list or not yet received): 1325873687-11229-9-git-send-email-jlayton-eUNUBHrolfbYtjvyW6yDsg@public.gmane.org

#11 lore raw

Re: [PATCH v3 08/15] cifscreds: make username parameter optional

Quoted 101 lines · Jeff Layton

...and use getopt_long to get it. If someone doesn't specify the username, use getusername() to get it.

---
 cifscreds.c |   39 ++++++++++++++++++++++++++++-----------

 1 files changed, 28 insertions(+), 11 deletions(-)
cifscreds.c +12 -5
index 79ab426..f45497a 100644
@@ -27,8 +27,10 @@
  #include <string.h>
  #include <ctype.h>
  #include <keyutils.h>
+#include <getopt.h>
  #include "mount.h"
  #include "resolve_host.h"
+#include "util.h"
 
  #define THIS_PROGRAM_NAME "cifscreds"
 
@@ -49,8 +51,8 @@
  #define DEST_KEYRING KEY_SPEC_USER_KEYRING
 
  struct cmdarg {
-       char    *host;
-       char    *user;
+       char            *host;
+       char            *user;
  };
 
  struct command {
@@ -67,13 +69,18 @@ static int cifscreds_update(struct cmdarg *arg);
  const char *thisprogram;
 
  struct command commands[] = {
-       { cifscreds_add,        "add",          "<host> <user>" },
-       { cifscreds_clear,      "clear",        "<host> <user>" },
+       { cifscreds_add,        "add",          "[-u username] <host>" },
+       { cifscreds_clear,      "clear",        "[-u username] <host>" },
        { cifscreds_clearall,   "clearall",     "" },
-       { cifscreds_update,     "update",       "<host> <user>" },
+       { cifscreds_update,     "update",       "[-u username] <host>" },
        { NULL, "", NULL }
  };
 
+struct option longopts[] = {
+       {"username", 1, NULL, 'u'},
+       {NULL, 0, NULL, 0}
+};
+
  /* display usage information */

 static int
 usage(void)

cifscreds.c +16 -7
@@ -499,12 +506,22 @@ int main(int argc, char **argv)
        if (argc == 1)
                return usage();
 
+       while((n = getopt_long(argc, argv, "u:", longopts, NULL)) != -1) {
+               switch (n) {
+               case 'u':
+                       arg.user = optarg;
+                       break;
+               default:
+                       return usage();
+               }
+       }
+
        /* find the best fit command */
        best = NULL;
-       n = strnlen(argv[1], MAX_COMMAND_SIZE);
+       n = strnlen(argv[optind], MAX_COMMAND_SIZE);
 
        for (cmd = commands; cmd->action; cmd++) {
-               if (memcmp(cmd->name, argv[1], n) != 0)
+               if (memcmp(cmd->name, argv[optind], n) != 0)
                        continue;
 
                if (cmd->name[n] == 0) {
@@ -527,12 +544,12 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
-       /* first argument should be host */
+       /* second argument should be host */
        if (argc >= 3)
-               arg.host = argv[2];
+               arg.host = argv[optind + 1];
 
-       if (argc >= 4)
-               arg.user = argv[3];
+       if (arg.user == NULL)
+               arg.user = getusername(getuid());
 
        return best->action(&arg);
  }
--

1.7.7.4

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-cifs" in the body of a message to [email protected] More majordomo info at  http://vger.kernel.org/majordomo-info.html

Just a thought, can user specify only one hostname and would hostname be always the last argument for cifscreds_* commands?

I was thinking whtether an user might want to manipulate keys for multiple servers in one command!

A message that is not in the archive (sent off-list or not yet received): CADT32eJAfBUozLzhobOpWq62hygwi1mfB76ZzWBfy7tKdVzhGA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#13 lore raw

Re: [PATCH v3 08/15] cifscreds: make username parameter optional

Quoted 110 lines · On Wed, 11 Jan 2012 12:22:30 -0600 Shirish Pargaonkar…
Quoted 101 lines · Jeff Layton

...and use getopt_long to get it. If someone doesn't specify the username, use getusername() to get it.

---
 cifscreds.c |   39 ++++++++++++++++++++++++++++-----------

 1 files changed, 28 insertions(+), 11 deletions(-)
cifscreds.c +12 -5
index 79ab426..f45497a 100644
@@ -27,8 +27,10 @@
  #include <string.h>
  #include <ctype.h>
  #include <keyutils.h>
+#include <getopt.h>
  #include "mount.h"
  #include "resolve_host.h"
+#include "util.h"
 
  #define THIS_PROGRAM_NAME "cifscreds"
 
@@ -49,8 +51,8 @@
  #define DEST_KEYRING KEY_SPEC_USER_KEYRING
 
  struct cmdarg {
-       char    *host;
-       char    *user;
+       char            *host;
+       char            *user;
  };
 
  struct command {
@@ -67,13 +69,18 @@ static int cifscreds_update(struct cmdarg *arg);
  const char *thisprogram;
 
  struct command commands[] = {
-       { cifscreds_add,        "add",          "<host> <user>" },
-       { cifscreds_clear,      "clear",        "<host> <user>" },
+       { cifscreds_add,        "add",          "[-u username] <host>" },
+       { cifscreds_clear,      "clear",        "[-u username] <host>" },
        { cifscreds_clearall,   "clearall",     "" },
-       { cifscreds_update,     "update",       "<host> <user>" },
+       { cifscreds_update,     "update",       "[-u username] <host>" },
        { NULL, "", NULL }
  };
 
+struct option longopts[] = {
+       {"username", 1, NULL, 'u'},
+       {NULL, 0, NULL, 0}
+};
+
  /* display usage information */

 static int
 usage(void)

cifscreds.c +16 -7
@@ -499,12 +506,22 @@ int main(int argc, char **argv)
        if (argc == 1)
                return usage();
 
+       while((n = getopt_long(argc, argv, "u:", longopts, NULL)) != -1) {
+               switch (n) {
+               case 'u':
+                       arg.user = optarg;
+                       break;
+               default:
+                       return usage();
+               }
+       }
+
        /* find the best fit command */
        best = NULL;
-       n = strnlen(argv[1], MAX_COMMAND_SIZE);
+       n = strnlen(argv[optind], MAX_COMMAND_SIZE);
 
        for (cmd = commands; cmd->action; cmd++) {
-               if (memcmp(cmd->name, argv[1], n) != 0)
+               if (memcmp(cmd->name, argv[optind], n) != 0)
                        continue;
 
                if (cmd->name[n] == 0) {
@@ -527,12 +544,12 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
-       /* first argument should be host */
+       /* second argument should be host */
        if (argc >= 3)
-               arg.host = argv[2];
+               arg.host = argv[optind + 1];
 
-       if (argc >= 4)
-               arg.user = argv[3];
+       if (arg.user == NULL)
+               arg.user = getusername(getuid());
 
        return best->action(&arg);
  }
--

1.7.7.4

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-cifs" in the body of a message to [email protected] More majordomo info at  http://vger.kernel.org/majordomo-info.html

Just a thought, can user specify only one hostname and would hostname be always the last argument for cifscreds_* commands?

Quoted 2 lines

I was thinking whtether an user might want to manipulate keys for multiple servers in one command!

Not a bad idea at all.

I don't see anything that would preclude that. I suppose we could take multiple host or domain arguments, and set keys for all of them. I'd probably not do that in this patchset since I'm already making a lot of changes here, but it wouldn't be too hard to do.

Signature · 1 line
Jeff Layton <[email protected]>

A message that is not in the archive (sent off-list or not yet received): 20120111134825.6a3dee8e-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org

#15 lore raw

Re: [PATCH v3 08/15] cifscreds: make username parameter optional

Quoted 113 lines · Jeff Layton
Quoted 110 lines · On Wed, 11 Jan 2012 12:22:30 -0600 Shirish Pargaonkar…
Quoted 101 lines · Jeff Layton

...and use getopt_long to get it. If someone doesn't specify the username, use getusername() to get it.

---
 cifscreds.c |   39 ++++++++++++++++++++++++++++-----------

 1 files changed, 28 insertions(+), 11 deletions(-)
cifscreds.c +12 -5
index 79ab426..f45497a 100644
@@ -27,8 +27,10 @@
  #include <string.h>
  #include <ctype.h>
  #include <keyutils.h>
+#include <getopt.h>
  #include "mount.h"
  #include "resolve_host.h"
+#include "util.h"
 
  #define THIS_PROGRAM_NAME "cifscreds"
 
@@ -49,8 +51,8 @@
  #define DEST_KEYRING KEY_SPEC_USER_KEYRING
 
  struct cmdarg {
-       char    *host;
-       char    *user;
+       char            *host;
+       char            *user;
  };
 
  struct command {
@@ -67,13 +69,18 @@ static int cifscreds_update(struct cmdarg *arg);
  const char *thisprogram;
 
  struct command commands[] = {
-       { cifscreds_add,        "add",          "<host> <user>" },
-       { cifscreds_clear,      "clear",        "<host> <user>" },
+       { cifscreds_add,        "add",          "[-u username] <host>" },
+       { cifscreds_clear,      "clear",        "[-u username] <host>" },
        { cifscreds_clearall,   "clearall",     "" },
-       { cifscreds_update,     "update",       "<host> <user>" },
+       { cifscreds_update,     "update",       "[-u username] <host>" },
        { NULL, "", NULL }
  };
 
+struct option longopts[] = {
+       {"username", 1, NULL, 'u'},
+       {NULL, 0, NULL, 0}
+};
+
  /* display usage information */

 static int
 usage(void)

cifscreds.c +16 -7
@@ -499,12 +506,22 @@ int main(int argc, char **argv)
        if (argc == 1)
                return usage();
 
+       while((n = getopt_long(argc, argv, "u:", longopts, NULL)) != -1) {
+               switch (n) {
+               case 'u':
+                       arg.user = optarg;
+                       break;
+               default:
+                       return usage();
+               }
+       }
+
        /* find the best fit command */
        best = NULL;
-       n = strnlen(argv[1], MAX_COMMAND_SIZE);
+       n = strnlen(argv[optind], MAX_COMMAND_SIZE);
 
        for (cmd = commands; cmd->action; cmd++) {
-               if (memcmp(cmd->name, argv[1], n) != 0)
+               if (memcmp(cmd->name, argv[optind], n) != 0)
                        continue;
 
                if (cmd->name[n] == 0) {
@@ -527,12 +544,12 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
-       /* first argument should be host */
+       /* second argument should be host */
        if (argc >= 3)
-               arg.host = argv[2];
+               arg.host = argv[optind + 1];
 
-       if (argc >= 4)
-               arg.user = argv[3];
+       if (arg.user == NULL)
+               arg.user = getusername(getuid());
 
        return best->action(&arg);
  }
--

1.7.7.4

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-cifs" in the body of a message to [email protected] More majordomo info at  http://vger.kernel.org/majordomo-info.html

Just a thought, can user specify only one hostname and would hostname be always the last argument for cifscreds_* commands?

Quoted 7 lines
Quoted 2 lines

I was thinking whtether an user might want to manipulate keys for multiple servers in one command!

Not a bad idea at all.

I don't see anything that would preclude that. I suppose we could take multiple host or domain arguments, and set keys for all of them. I'd

yes, something like a comma separated list

Quoted 5 lines

probably not do that in this patchset since I'm already making a lot of changes here, but it wouldn't be too hard to do.

--
Jeff Layton <[email protected]>

fair enough

#16 lore raw

PATCH v3 9/15 cifscreds: add --domain flag

In reply to a message not in the archive

...to indicate that the first argument is not a hostname but an NT domain name. If it's set, then treat the argument as a string literal.

---
 cifscreds.c |   64 ++++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 44 insertions(+), 20 deletions(-)
cifscreds.c +44 -20
index f45497a..279517a 100644
@@ -53,6 +53,7 @@
 struct cmdarg {
 	char		*host;
 	char		*user;
+	char		keytype;
 };
 
 struct command {
@@ -69,15 +70,16 @@ static int cifscreds_update(struct cmdarg *arg);
 const char *thisprogram;
 
 struct command commands[] = {
-	{ cifscreds_add,	"add",		"[-u username] <host>" },
-	{ cifscreds_clear,	"clear",	"[-u username] <host>" },
+	{ cifscreds_add,	"add",		"[-u username] [-d] <host|domain>" },
+	{ cifscreds_clear,	"clear",	"[-u username] [-d] <host|domain>" },
 	{ cifscreds_clearall,	"clearall",	"" },
-	{ cifscreds_update,	"update",	"[-u username] <host>" },
+	{ cifscreds_update,	"update",	"[-u username] [-d] <host|domain>" },
 	{ NULL, "", NULL }
 };
 
 struct option longopts[] = {
 	{"username", 1, NULL, 'u'},
+	{"domain", 0, NULL, 'd' },
 	{NULL, 0, NULL, 0}
 };
 
@@ -98,7 +100,7 @@ usage(void)
 
 /* search a specific key in keyring */
 static key_serial_t
-key_search(const char *addr)
+key_search(const char *addr, char keytype)
 {
 	char desc[INET6_ADDRSTRLEN + sizeof(THIS_PROGRAM_NAME) + 4];
 	key_serial_t key, *pk;
@@ -106,7 +108,7 @@ key_search(const char *addr)
 	char *buffer;
 	int count, dpos, n, ret;
 
-	sprintf(desc, "%s:a:%s", THIS_PROGRAM_NAME, addr);
+	sprintf(desc, "%s:%c:%s", THIS_PROGRAM_NAME, keytype, addr);
 
 	/* read the key payload data */
 	count = keyctl_read_alloc(DEST_KEYRING, &keylist);
@@ -206,14 +208,14 @@ key_search_all_out:
 
 /* add or update a specific key to keyring */
 static key_serial_t
-key_add(const char *addr, const char *user, const char *pass)
+key_add(const char *addr, const char *user, const char *pass, char keytype)
 {
 	int len;
 	char desc[INET6_ADDRSTRLEN + sizeof(THIS_PROGRAM_NAME) + 4];
 	char val[MOUNT_PASSWD_SIZE +  MAX_USERNAME_SIZE + 2];
 
 	/* set key description */
-	sprintf(desc, "%s:a:%s", THIS_PROGRAM_NAME, addr);
+	sprintf(desc, "%s:%c:%s", THIS_PROGRAM_NAME, keytype, addr);
 
 	/* set payload contents */
 	len = sprintf(val, "%s:%s", user, pass);
@@ -227,12 +229,16 @@ static int cifscreds_add(struct cmdarg *arg)
 	char addrstr[MAX_ADDR_LIST_LEN];
 	char *currentaddress, *nextaddress;
 	char *pass;
-	int ret;
+	int ret = 0;
 
 	if (arg->host == NULL || arg->user == NULL)
 		return usage();
 
-	ret = resolve_host(arg->host, addrstr);
+	if (arg->keytype == 'd')
+		strlcpy(addrstr, arg->host, MAX_ADDR_LIST_LEN);
+	else
+		ret = resolve_host(arg->host, addrstr);
+
 	switch (ret) {
 	case EX_USAGE:
 		fprintf(stderr, "error: Could not resolve address "
@@ -256,7 +262,7 @@ static int cifscreds_add(struct cmdarg *arg)
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		if (key_search(currentaddress) > 0) {
+		if (key_search(currentaddress, arg->keytype) > 0) {
 			printf("You already have stashed credentials "
 				"for %s (%s)\n", currentaddress, arg->host);
 			printf("If you want to update them use:\n");
@@ -286,7 +292,7 @@ static int cifscreds_add(struct cmdarg *arg)
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		key_serial_t key = key_add(currentaddress, arg->user, pass);
+		key_serial_t key = key_add(currentaddress, arg->user, pass, arg->keytype);
 		if (key <= 0) {
 			fprintf(stderr, "error: Add credential key for %s\n",
 				currentaddress);
@@ -322,12 +328,16 @@ static int cifscreds_clear(struct cmdarg *arg)
 {
 	char addrstr[MAX_ADDR_LIST_LEN];
 	char *currentaddress, *nextaddress;
-	int ret, count = 0, errors = 0;
+	int ret = 0, count = 0, errors = 0;
 
 	if (arg->host == NULL || arg->user == NULL)
 		return usage();
 
-	ret = resolve_host(arg->host, addrstr);
+	if (arg->keytype == 'd')
+		strlcpy(addrstr, arg->host, MAX_ADDR_LIST_LEN);
+	else
+		ret = resolve_host(arg->host, addrstr);
+
 	switch (ret) {
 	case EX_USAGE:
 		fprintf(stderr, "error: Could not resolve address "
@@ -354,7 +364,7 @@ static int cifscreds_clear(struct cmdarg *arg)
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		key_serial_t key = key_search(currentaddress);
+		key_serial_t key = key_search(currentaddress, arg->keytype);
 		if (key > 0) {
 			if (keyctl(KEYCTL_UNLINK, key, DEST_KEYRING) < 0) {
 				fprintf(stderr, "error: Removing key from "
@@ -427,12 +437,16 @@ static int cifscreds_update(struct cmdarg *arg)
 	char addrstr[MAX_ADDR_LIST_LEN];
 	char *currentaddress, *nextaddress, *pass;
 	char *addrs[16];
-	int ret, id, count = 0;
+	int ret = 0, id, count = 0;
 
 	if (arg->host == NULL || arg->user == NULL)
 		return usage();
 
-	ret = resolve_host(arg->host, addrstr);
+	if (arg->keytype == 'd')
+		strlcpy(addrstr, arg->host, MAX_ADDR_LIST_LEN);
+	else
+		ret = resolve_host(arg->host, addrstr);
+
 	switch (ret) {
 	case EX_USAGE:
 		fprintf(stderr, "error: Could not resolve address "
@@ -456,7 +470,7 @@ static int cifscreds_update(struct cmdarg *arg)
 		*nextaddress++ = '\0';
 
 	while (currentaddress) {
-		if (key_search(currentaddress) > 0) {
+		if (key_search(currentaddress, arg->keytype) > 0) {
 			addrs[count] = currentaddress;
 			count++;
 		}
@@ -482,7 +496,7 @@ static int cifscreds_update(struct cmdarg *arg)
 	pass = getpass("Password: ");
 
 	for (id = 0; id < count; id++) {
-		key_serial_t key = key_add(addrs[id], arg->user, pass);
+		key_serial_t key = key_add(addrs[id], arg->user, pass, arg->keytype);
 		if (key <= 0)
 			fprintf(stderr, "error: Update credential key "
 				"for %s\n", addrs[id]);
@@ -498,6 +512,7 @@ int main(int argc, char **argv)
 	int n;
 
 	memset(&arg, 0, sizeof(arg));
+	arg.keytype = 'a';
 
 	thisprogram = (char *)basename(argv[0]);
 	if (thisprogram == NULL)
@@ -506,8 +521,11 @@ int main(int argc, char **argv)
 	if (argc == 1)
 		return usage();
 
-	while((n = getopt_long(argc, argv, "u:", longopts, NULL)) != -1) {
+	while((n = getopt_long(argc, argv, "du:", longopts, NULL)) != -1) {
 		switch (n) {
+		case 'd':
+			arg.keytype = (char) n;
+			break;
 		case 'u':
 			arg.user = optarg;
 			break;
@@ -544,10 +562,16 @@ int main(int argc, char **argv)
 		return EXIT_FAILURE;
 	}
 
-	/* second argument should be host */
+	/* second argument should be host or domain */
 	if (argc >= 3)
 		arg.host = argv[optind + 1];
 
+	if (arg.host && arg.keytype == 'd' &&
+	    strspn(arg.host, DOMAIN_ALLOWED_CHARS) != strnlen(arg.host, MAX_DOMAIN_SIZE)) {
+		fprintf(stderr, "error: Domain name contains invalid characters\n");
+		return EXIT_FAILURE;
+	}
+
 	if (arg.user == NULL)
 		arg.user = getusername(getuid());
 
Signature · 1 line
1.7.7.4

A message that is not in the archive (sent off-list or not yet received): 1325873687-11229-10-git-send-email-jlayton-eUNUBHrolfbYtjvyW6yDsg@public.gmane.org

#18 lore raw

Re: [PATCH v3 09/15] cifscreds: add --domain flag

Quoted 220 lines · Jeff Layton

...to indicate that the first argument is not a hostname but an NT domain name. If it's set, then treat the argument as a string literal.

---
 cifscreds.c |   64 ++++++++++++++++++++++++++++++++++++++++------------------

 1 files changed, 44 insertions(+), 20 deletions(-)
cifscreds.c +5 -3
index f45497a..279517a 100644
@@ -53,6 +53,7 @@
  struct cmdarg {
        char            *host;
        char            *user;
+       char            keytype;
  };
 
  struct command {
@@ -69,15 +70,16 @@ static int cifscreds_update(struct cmdarg *arg);
  const char *thisprogram;
 
  struct command commands[] = {
-       { cifscreds_add,        "add",          "[-u username] <host>" },
-       { cifscreds_clear,      "clear",        "[-u username] <host>" },
+       { cifscreds_add,        "add",          "[-u username] [-d] <host|domain>" },
+       { cifscreds_clear,      "clear",        "[-u username] [-d] <host|domain>" },
        { cifscreds_clearall,   "clearall",     "" },
-       { cifscreds_update,     "update",       "[-u username] <host>" },
+       { cifscreds_update,     "update",       "[-u username] [-d] <host|domain>" },
        { NULL, "", NULL }
  };
 
  struct option longopts[] = {
        {"username", 1, NULL, 'u'},
+       {"domain", 0, NULL, 'd' },
        {NULL, 0, NULL, 0}
  };
 
@@ -98,7 +100,7 @@ usage(void)
 
  /* search a specific key in keyring */

 static key_serial_t

cifscreds.c +1 -1
-key_search(const char *addr)
+key_search(const char *addr, char keytype)

 {
       char desc[INET6_ADDRSTRLEN + sizeof(THIS_PROGRAM_NAME) + 4]; key_serial_t key, *pk;

cifscreds.c +1 -1
@@ -106,7 +108,7 @@ key_search(const char *addr)
        char *buffer;
        int count, dpos, n, ret;
 
-       sprintf(desc, "%s:a:%s", THIS_PROGRAM_NAME, addr);
+       sprintf(desc, "%s:%c:%s", THIS_PROGRAM_NAME, keytype, addr);
 
        /* read the key payload data */
        count = keyctl_read_alloc(DEST_KEYRING, &keylist);
@@ -206,14 +208,14 @@ key_search_all_out:
 
  /* add or update a specific key to keyring */

 static key_serial_t

cifscreds.c +1 -1
-key_add(const char *addr, const char *user, const char *pass)
+key_add(const char *addr, const char *user, const char *pass, char keytype)

 {
       int len;
       char desc[INET6_ADDRSTRLEN + sizeof(THIS_PROGRAM_NAME) + 4]; char val[MOUNT_PASSWD_SIZE +  MAX_USERNAME_SIZE + 2];

       /* set key description */
-       sprintf(desc, "%s:a:%s", THIS_PROGRAM_NAME, addr);
+       sprintf(desc, "%s:%c:%s", THIS_PROGRAM_NAME, keytype, addr);

       /* set payload contents */
       len = sprintf(val, "%s:%s", user, pass);

cifscreds.c +6 -2
@@ -227,12 +229,16 @@ static int cifscreds_add(struct cmdarg *arg)
        char addrstr[MAX_ADDR_LIST_LEN];
        char *currentaddress, *nextaddress;
        char *pass;
-       int ret;
+       int ret = 0;
 
        if (arg->host == NULL || arg->user == NULL)
                return usage();
 
-       ret = resolve_host(arg->host, addrstr);
+       if (arg->keytype == 'd')
+               strlcpy(addrstr, arg->host, MAX_ADDR_LIST_LEN);
+       else
+               ret = resolve_host(arg->host, addrstr);
+
        switch (ret) {

       case EX_USAGE:
               fprintf(stderr, "error: Could not resolve address "

cifscreds.c +1 -1
@@ -256,7 +262,7 @@ static int cifscreds_add(struct cmdarg *arg)
                *nextaddress++ = '\0';
 
        while (currentaddress) {
-               if (key_search(currentaddress) > 0) {
+               if (key_search(currentaddress, arg->keytype) > 0) {

                       printf("You already have stashed credentials " "for %s (%s)\n", currentaddress, arg->host); printf("If you want to update them use:\n");

cifscreds.c +7 -3
@@ -286,7 +292,7 @@ static int cifscreds_add(struct cmdarg *arg)
                *nextaddress++ = '\0';
 
        while (currentaddress) {
-               key_serial_t key = key_add(currentaddress, arg->user, pass);
+               key_serial_t key = key_add(currentaddress, arg->user, pass, arg->keytype);
                if (key <= 0) {
                        fprintf(stderr, "error: Add credential key for %s\n",
                                currentaddress);
@@ -322,12 +328,16 @@ static int cifscreds_clear(struct cmdarg *arg)
  {
        char addrstr[MAX_ADDR_LIST_LEN];
        char *currentaddress, *nextaddress;
-       int ret, count = 0, errors = 0;
+       int ret = 0, count = 0, errors = 0;
 
        if (arg->host == NULL || arg->user == NULL)
                return usage();
 
-       ret = resolve_host(arg->host, addrstr);
+       if (arg->keytype == 'd')
+               strlcpy(addrstr, arg->host, MAX_ADDR_LIST_LEN);
+       else
+               ret = resolve_host(arg->host, addrstr);
+
        switch (ret) {

       case EX_USAGE:
               fprintf(stderr, "error: Could not resolve address "

cifscreds.c +1 -1
@@ -354,7 +364,7 @@ static int cifscreds_clear(struct cmdarg *arg)
                *nextaddress++ = '\0';
 
        while (currentaddress) {
-               key_serial_t key = key_search(currentaddress);
+               key_serial_t key = key_search(currentaddress, arg->keytype);
                if (key > 0) {
                        if (keyctl(KEYCTL_UNLINK, key, DEST_KEYRING) < 0) {

                               fprintf(stderr, "error: Removing key from "

cifscreds.c +6 -2
@@ -427,12 +437,16 @@ static int cifscreds_update(struct cmdarg *arg)
        char addrstr[MAX_ADDR_LIST_LEN];
        char *currentaddress, *nextaddress, *pass;
        char *addrs[16];
-       int ret, id, count = 0;
+       int ret = 0, id, count = 0;
 
        if (arg->host == NULL || arg->user == NULL)
                return usage();
 
-       ret = resolve_host(arg->host, addrstr);
+       if (arg->keytype == 'd')
+               strlcpy(addrstr, arg->host, MAX_ADDR_LIST_LEN);
+       else
+               ret = resolve_host(arg->host, addrstr);
+
        switch (ret) {

       case EX_USAGE:
               fprintf(stderr, "error: Could not resolve address "

cifscreds.c +2 -2
@@ -456,7 +470,7 @@ static int cifscreds_update(struct cmdarg *arg)
                *nextaddress++ = '\0';
 
        while (currentaddress) {
-               if (key_search(currentaddress) > 0) {
+               if (key_search(currentaddress, arg->keytype) > 0) {
                        addrs[count] = currentaddress;
                        count++;
                }
@@ -482,7 +496,7 @@ static int cifscreds_update(struct cmdarg *arg)
        pass = getpass("Password: ");
 
        for (id = 0; id < count; id++) {
-               key_serial_t key = key_add(addrs[id], arg->user, pass);
+               key_serial_t key = key_add(addrs[id], arg->user, pass, arg->keytype);
                if (key <= 0)

                       fprintf(stderr, "error: Update credential key " "for %s\n", addrs[id]);

cifscreds.c +5 -1
@@ -498,6 +512,7 @@ int main(int argc, char **argv)
        int n;
 
        memset(&arg, 0, sizeof(arg));
+       arg.keytype = 'a';
 
        thisprogram = (char *)basename(argv[0]);
        if (thisprogram == NULL)
@@ -506,8 +521,11 @@ int main(int argc, char **argv)
        if (argc == 1)
                return usage();
 
-       while((n = getopt_long(argc, argv, "u:", longopts, NULL)) != -1) {
+       while((n = getopt_long(argc, argv, "du:", longopts, NULL)) != -1) {
                switch (n) {
+               case 'd':
+                       arg.keytype = (char) n;
+                       break;

               case 'u':
                       arg.user = optarg;
                       break;

cifscreds.c +7 -2
@@ -544,10 +562,16 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
-       /* second argument should be host */
+       /* second argument should be host or domain */
        if (argc >= 3)
                arg.host = argv[optind + 1];
 
+       if (arg.host && arg.keytype == 'd' &&
+           strspn(arg.host, DOMAIN_ALLOWED_CHARS) != strnlen(arg.host, MAX_DOMAIN_SIZE)) {
+               fprintf(stderr, "error: Domain name contains invalid characters\n");
+               return EXIT_FAILURE;
+       }
+
        if (arg.user == NULL)
                arg.user = getusername(getuid());
 
--

1.7.7.4

Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-cifs" in the body of a message to [email protected] More majordomo info at  http://vger.kernel.org/majordomo-info.html

if we have -d and -u, why not -h or -s for servername/hostname?

A message that is not in the archive (sent off-list or not yet received): CADT32eLBd=xjh3RKv+9-zzbobeBzShheSP6ugR7RmOBYRZ4oTQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#20 lore raw

Re: [PATCH v3 09/15] cifscreds: add --domain flag

Quoted 221 lines · On Wed, 11 Jan 2012 12:31:29 -0600 Shirish Pargaonkar…
Quoted 220 lines · Jeff Layton

...to indicate that the first argument is not a hostname but an NT domain name. If it's set, then treat the argument as a string literal.

---
 cifscreds.c |   64 ++++++++++++++++++++++++++++++++++++++++------------------

 1 files changed, 44 insertions(+), 20 deletions(-)
cifscreds.c +5 -3
index f45497a..279517a 100644
@@ -53,6 +53,7 @@
  struct cmdarg {
        char            *host;
        char            *user;
+       char            keytype;
  };
 
  struct command {
@@ -69,15 +70,16 @@ static int cifscreds_update(struct cmdarg *arg);
  const char *thisprogram;
 
  struct command commands[] = {
-       { cifscreds_add,        "add",          "[-u username] <host>" },
-       { cifscreds_clear,      "clear",        "[-u username] <host>" },
+       { cifscreds_add,        "add",          "[-u username] [-d] <host|domain>" },
+       { cifscreds_clear,      "clear",        "[-u username] [-d] <host|domain>" },
        { cifscreds_clearall,   "clearall",     "" },
-       { cifscreds_update,     "update",       "[-u username] <host>" },
+       { cifscreds_update,     "update",       "[-u username] [-d] <host|domain>" },
        { NULL, "", NULL }
  };
 
  struct option longopts[] = {
        {"username", 1, NULL, 'u'},
+       {"domain", 0, NULL, 'd' },
        {NULL, 0, NULL, 0}
  };
 
@@ -98,7 +100,7 @@ usage(void)
 
  /* search a specific key in keyring */

 static key_serial_t

cifscreds.c +1 -1
-key_search(const char *addr)
+key_search(const char *addr, char keytype)

 {
       char desc[INET6_ADDRSTRLEN + sizeof(THIS_PROGRAM_NAME) + 4]; key_serial_t key, *pk;

cifscreds.c +1 -1
@@ -106,7 +108,7 @@ key_search(const char *addr)
        char *buffer;
        int count, dpos, n, ret;
 
-       sprintf(desc, "%s:a:%s", THIS_PROGRAM_NAME, addr);
+       sprintf(desc, "%s:%c:%s", THIS_PROGRAM_NAME, keytype, addr);
 
        /* read the key payload data */
        count = keyctl_read_alloc(DEST_KEYRING, &keylist);
@@ -206,14 +208,14 @@ key_search_all_out:
 
  /* add or update a specific key to keyring */

 static key_serial_t

cifscreds.c +1 -1
-key_add(const char *addr, const char *user, const char *pass)
+key_add(const char *addr, const char *user, const char *pass, char keytype)

 {
       int len;
       char desc[INET6_ADDRSTRLEN + sizeof(THIS_PROGRAM_NAME) + 4]; char val[MOUNT_PASSWD_SIZE +  MAX_USERNAME_SIZE + 2];

       /* set key description */
-       sprintf(desc, "%s:a:%s", THIS_PROGRAM_NAME, addr);
+       sprintf(desc, "%s:%c:%s", THIS_PROGRAM_NAME, keytype, addr);

       /* set payload contents */
       len = sprintf(val, "%s:%s", user, pass);

cifscreds.c +6 -2
@@ -227,12 +229,16 @@ static int cifscreds_add(struct cmdarg *arg)
        char addrstr[MAX_ADDR_LIST_LEN];
        char *currentaddress, *nextaddress;
        char *pass;
-       int ret;
+       int ret = 0;
 
        if (arg->host == NULL || arg->user == NULL)
                return usage();
 
-       ret = resolve_host(arg->host, addrstr);
+       if (arg->keytype == 'd')
+               strlcpy(addrstr, arg->host, MAX_ADDR_LIST_LEN);
+       else
+               ret = resolve_host(arg->host, addrstr);
+
        switch (ret) {

       case EX_USAGE:
               fprintf(stderr, "error: Could not resolve address "

cifscreds.c +1 -1
@@ -256,7 +262,7 @@ static int cifscreds_add(struct cmdarg *arg)
                *nextaddress++ = '\0';
 
        while (currentaddress) {
-               if (key_search(currentaddress) > 0) {
+               if (key_search(currentaddress, arg->keytype) > 0) {

                       printf("You already have stashed credentials " "for %s (%s)\n", currentaddress, arg->host); printf("If you want to update them use:\n");

cifscreds.c +7 -3
@@ -286,7 +292,7 @@ static int cifscreds_add(struct cmdarg *arg)
                *nextaddress++ = '\0';
 
        while (currentaddress) {
-               key_serial_t key = key_add(currentaddress, arg->user, pass);
+               key_serial_t key = key_add(currentaddress, arg->user, pass, arg->keytype);
                if (key <= 0) {
                        fprintf(stderr, "error: Add credential key for %s\n",
                                currentaddress);
@@ -322,12 +328,16 @@ static int cifscreds_clear(struct cmdarg *arg)
  {
        char addrstr[MAX_ADDR_LIST_LEN];
        char *currentaddress, *nextaddress;
-       int ret, count = 0, errors = 0;
+       int ret = 0, count = 0, errors = 0;
 
        if (arg->host == NULL || arg->user == NULL)
                return usage();
 
-       ret = resolve_host(arg->host, addrstr);
+       if (arg->keytype == 'd')
+               strlcpy(addrstr, arg->host, MAX_ADDR_LIST_LEN);
+       else
+               ret = resolve_host(arg->host, addrstr);
+
        switch (ret) {

       case EX_USAGE:
               fprintf(stderr, "error: Could not resolve address "

cifscreds.c +1 -1
@@ -354,7 +364,7 @@ static int cifscreds_clear(struct cmdarg *arg)
                *nextaddress++ = '\0';
 
        while (currentaddress) {
-               key_serial_t key = key_search(currentaddress);
+               key_serial_t key = key_search(currentaddress, arg->keytype);
                if (key > 0) {
                        if (keyctl(KEYCTL_UNLINK, key, DEST_KEYRING) < 0) {

                               fprintf(stderr, "error: Removing key from "

cifscreds.c +6 -2
@@ -427,12 +437,16 @@ static int cifscreds_update(struct cmdarg *arg)
        char addrstr[MAX_ADDR_LIST_LEN];
        char *currentaddress, *nextaddress, *pass;
        char *addrs[16];
-       int ret, id, count = 0;
+       int ret = 0, id, count = 0;
 
        if (arg->host == NULL || arg->user == NULL)
                return usage();
 
-       ret = resolve_host(arg->host, addrstr);
+       if (arg->keytype == 'd')
+               strlcpy(addrstr, arg->host, MAX_ADDR_LIST_LEN);
+       else
+               ret = resolve_host(arg->host, addrstr);
+
        switch (ret) {

       case EX_USAGE:
               fprintf(stderr, "error: Could not resolve address "

cifscreds.c +2 -2
@@ -456,7 +470,7 @@ static int cifscreds_update(struct cmdarg *arg)
                *nextaddress++ = '\0';
 
        while (currentaddress) {
-               if (key_search(currentaddress) > 0) {
+               if (key_search(currentaddress, arg->keytype) > 0) {
                        addrs[count] = currentaddress;
                        count++;
                }
@@ -482,7 +496,7 @@ static int cifscreds_update(struct cmdarg *arg)
        pass = getpass("Password: ");
 
        for (id = 0; id < count; id++) {
-               key_serial_t key = key_add(addrs[id], arg->user, pass);
+               key_serial_t key = key_add(addrs[id], arg->user, pass, arg->keytype);
                if (key <= 0)

                       fprintf(stderr, "error: Update credential key " "for %s\n", addrs[id]);

cifscreds.c +5 -1
@@ -498,6 +512,7 @@ int main(int argc, char **argv)
        int n;
 
        memset(&arg, 0, sizeof(arg));
+       arg.keytype = 'a';
 
        thisprogram = (char *)basename(argv[0]);
        if (thisprogram == NULL)
@@ -506,8 +521,11 @@ int main(int argc, char **argv)
        if (argc == 1)
                return usage();
 
-       while((n = getopt_long(argc, argv, "u:", longopts, NULL)) != -1) {
+       while((n = getopt_long(argc, argv, "du:", longopts, NULL)) != -1) {
                switch (n) {
+               case 'd':
+                       arg.keytype = (char) n;
+                       break;

               case 'u':
                       arg.user = optarg;
                       break;

cifscreds.c +7 -2
@@ -544,10 +562,16 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
-       /* second argument should be host */
+       /* second argument should be host or domain */
        if (argc >= 3)
                arg.host = argv[optind + 1];
 
+       if (arg.host && arg.keytype == 'd' &&
+           strspn(arg.host, DOMAIN_ALLOWED_CHARS) != strnlen(arg.host, MAX_DOMAIN_SIZE)) {
+               fprintf(stderr, "error: Domain name contains invalid characters\n");
+               return EXIT_FAILURE;
+       }
+
        if (arg.user == NULL)
                arg.user = getusername(getuid());
 
--

1.7.7.4

Quoted 6 lines
Quoted 4 lines

--
To unsubscribe from this list: send the line "unsubscribe linux-cifs" in the body of a message to [email protected] More majordomo info at  http://vger.kernel.org/majordomo-info.html

if we have -d and -u, why not -h or -s for servername/hostname?

Because setting keys by server is the default.

Signature · 1 line
Jeff Layton <[email protected]>
#21 lore raw

PATCH v3 10/15 cifscreds: loosen allowed characters in domain names

In reply to a message not in the archive

As Donald points out, NetBIOS domains are allowed more characters than the code currently allows. Change the test to one that checks for disallowed characters instead.

Also, I can't find anything that says that '@' is not allowed in a username. Might as well allow that too. Worst case, the server will reject the username.

---
 cifscreds.c |   13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)
cifscreds.c +8 -5
index 279517a..cbd431e 100644
@@ -42,10 +42,13 @@
 #define MOUNT_PASSWD_SIZE 128
 #define MAX_DOMAIN_SIZE 64
 
-/* allowed and disallowed characters for user and domain name */
-#define USER_DISALLOWED_CHARS "\\/\"[]:|<>+=;,?*@"
-#define DOMAIN_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz" \
-			     "ABCDEFGHIJKLMNOPQRSTUVWXYZ-."
+/*
+ * disallowed characters for user and domain names. See:
+ * http://technet.microsoft.com/en-us/library/bb726984.aspx
+ * http://support.microsoft.com/kb/909264
+ */
+#define USER_DISALLOWED_CHARS "\\/\"[]:|<>+=;,?*"
+#define DOMAIN_DISALLOWED_CHARS "\\/:*?\"<>|"
 
 /* destination keyring */
 #define DEST_KEYRING KEY_SPEC_USER_KEYRING
@@ -567,7 +570,7 @@ int main(int argc, char **argv)
 		arg.host = argv[optind + 1];
 
 	if (arg.host && arg.keytype == 'd' &&
-	    strspn(arg.host, DOMAIN_ALLOWED_CHARS) != strnlen(arg.host, MAX_DOMAIN_SIZE)) {
+	    strpbrk(arg.host, DOMAIN_DISALLOWED_CHARS)) {
 		fprintf(stderr, "error: Domain name contains invalid characters\n");
 		return EXIT_FAILURE;
 	}
Signature · 1 line
1.7.7.4
#22 lore raw

PATCH v3 11/15 cifscreds: use the session keyring

In reply to a message not in the archive

This seems like a reasonable change, but I'm willing to listen to arguments to the contrary...

cifscreds currently hangs the keys off of the uid keyring. It seems more appropriate though that we require that each session have its own set. This might be particularly important in a containerized situation. If a user authenticates in one container, then we probably don't want to allow a user in another to "borrow" those creds.

---
 cifscreds.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
cifscreds.c +1 -1
index cbd431e..6079b38 100644
@@ -51,7 +51,7 @@
 #define DOMAIN_DISALLOWED_CHARS "\\/:*?\"<>|"
 
 /* destination keyring */
-#define DEST_KEYRING KEY_SPEC_USER_KEYRING
+#define DEST_KEYRING KEY_SPEC_SESSION_KEYRING
 
 struct cmdarg {
 	char		*host;
Signature · 1 line
1.7.7.4
#23 lore raw

PATCH v3 12/15 cifscreds: make cifscreds use the "unreadable" keyring

In reply to a message not in the archive

...and have it loosen the permissions to allow searching. There seems to be no clear way to make user keys unreadable, but still allow for them to be searched, so we'll need a new key_type that doesn't allow you to read the payload from userspace. That will be proposed in a separate kernel patch.

---
 cifscreds.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
cifscreds.c +5 -5
index 6079b38..5c5b578 100644
@@ -52,6 +52,9 @@
 
 /* destination keyring */
 #define DEST_KEYRING KEY_SPEC_SESSION_KEYRING
+#define CIFS_KEY_TYPE  "secret"
+#define CIFS_KEY_PERMS (KEY_POS_VIEW|KEY_POS_WRITE|KEY_POS_SEARCH| \
+			KEY_USR_VIEW|KEY_USR_WRITE|KEY_USR_SEARCH)
 
 struct cmdarg {
 	char		*host;
@@ -223,7 +226,7 @@ key_add(const char *addr, const char *user, const char *pass, char keytype)
 	/* set payload contents */
 	len = sprintf(val, "%s:%s", user, pass);
 
-	return add_key("user", desc, val, len + 1, DEST_KEYRING);
+	return add_key(CIFS_KEY_TYPE, desc, val, len + 1, DEST_KEYRING);
 }
 
 /* add command handler */
@@ -300,10 +303,7 @@ static int cifscreds_add(struct cmdarg *arg)
 			fprintf(stderr, "error: Add credential key for %s\n",
 				currentaddress);
 		} else {
-			if (keyctl(KEYCTL_SETPERM, key, KEY_POS_VIEW | \
-				KEY_POS_WRITE | KEY_USR_VIEW | \
-				KEY_USR_WRITE) < 0
-			) {
+			if (keyctl(KEYCTL_SETPERM, key, CIFS_KEY_PERMS) < 0) {
 				fprintf(stderr, "error: Setting permissons "
 					"on key, attempt to delete...\n");
 
Signature · 1 line
1.7.7.4
#24 lore raw

PATCH v3 13/15 cifscreds: make key_search use keyctl_search

In reply to a message not in the archive

...which is much more efficient than walking the keyring by hand.

---
 cifscreds.c |   46 +---------------------------------------------
 1 files changed, 1 insertions(+), 45 deletions(-)
cifscreds.c +1 -45
index 5c5b578..64da070 100644
@@ -109,54 +109,10 @@ static key_serial_t
 key_search(const char *addr, char keytype)
 {
 	char desc[INET6_ADDRSTRLEN + sizeof(THIS_PROGRAM_NAME) + 4];
-	key_serial_t key, *pk;
-	void *keylist;
-	char *buffer;
-	int count, dpos, n, ret;
 
 	sprintf(desc, "%s:%c:%s", THIS_PROGRAM_NAME, keytype, addr);
 
-	/* read the key payload data */
-	count = keyctl_read_alloc(DEST_KEYRING, &keylist);
-	if (count < 0)
-		return 0;
-
-	count /= sizeof(key_serial_t);
-
-	if (count == 0) {
-		ret = 0;
-		goto key_search_out;
-	}
-
-	/* list the keys in the keyring */
-	pk = keylist;
-	do {
-		key = *pk++;
-
-		ret = keyctl_describe_alloc(key, &buffer);
-		if (ret < 0)
-			continue;
-
-		n = sscanf(buffer, "%*[^;];%*d;%*d;%*x;%n", &dpos);
-		if (n) {
-			free(buffer);
-			continue;
-		}
-
-		if (!strcmp(buffer + dpos, desc)) {
-			ret = key;
-			free(buffer);
-			goto key_search_out;
-		}
-		free(buffer);
-
-	} while (--count);
-
-	ret = 0;
-
-key_search_out:
-	free(keylist);
-	return ret;
+	return keyctl_search(DEST_KEYRING, CIFS_KEY_TYPE, desc, 0);
 }
 
 /* search all program's keys in keyring */
Signature · 1 line
1.7.7.4
#25 lore raw

PATCH v3 14/15 cifscreds: change prefix on keys to "cifs:" instead of "cifscreds:"

In reply to a message not in the archive

The "creds" portion seems redundant.

---
 cifscreds.c |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)
cifscreds.c +7 -6
index 64da070..8f3b829 100644
@@ -33,6 +33,7 @@
 #include "util.h"
 
 #define THIS_PROGRAM_NAME "cifscreds"
+#define KEY_PREFIX	  "cifs"
 
 /* max length of appropriate command */
 #define MAX_COMMAND_SIZE 32
@@ -108,9 +109,9 @@ usage(void)
 static key_serial_t
 key_search(const char *addr, char keytype)
 {
-	char desc[INET6_ADDRSTRLEN + sizeof(THIS_PROGRAM_NAME) + 4];
+	char desc[INET6_ADDRSTRLEN + sizeof(KEY_PREFIX) + 4];
 
-	sprintf(desc, "%s:%c:%s", THIS_PROGRAM_NAME, keytype, addr);
+	sprintf(desc, "%s:%c:%s", KEY_PREFIX, keytype, addr);
 
 	return keyctl_search(DEST_KEYRING, CIFS_KEY_TYPE, desc, 0);
 }
@@ -150,7 +151,7 @@ static key_serial_t key_search_all(void)
 			continue;
 		}
 
-		if (strstr(buffer + dpos, THIS_PROGRAM_NAME ":") ==
+		if (strstr(buffer + dpos, KEY_PREFIX ":") ==
 			buffer + dpos
 		) {
 			ret = key;
@@ -173,11 +174,11 @@ static key_serial_t
 key_add(const char *addr, const char *user, const char *pass, char keytype)
 {
 	int len;
-	char desc[INET6_ADDRSTRLEN + sizeof(THIS_PROGRAM_NAME) + 4];
+	char desc[INET6_ADDRSTRLEN + sizeof(KEY_PREFIX) + 4];
 	char val[MOUNT_PASSWD_SIZE +  MAX_USERNAME_SIZE + 2];
 
 	/* set key description */
-	sprintf(desc, "%s:%c:%s", THIS_PROGRAM_NAME, keytype, addr);
+	sprintf(desc, "%s:%c:%s", KEY_PREFIX, keytype, addr);
 
 	/* set payload contents */
 	len = sprintf(val, "%s:%s", user, pass);
@@ -379,7 +380,7 @@ static int cifscreds_clearall(struct cmdarg *arg __attribute__ ((unused)))
 	} while (key > 0);
 
 	if (!count && !errors) {
-		printf("You have no stashed " THIS_PROGRAM_NAME
+		printf("You have no stashed " KEY_PREFIX
 			" credentials\n");
 		printf("If you want to add them use:\n");
 		printf("\t%s add\n", thisprogram);
Signature · 1 line
1.7.7.4
#26 lore raw

PATCH v3 15/15 cifscreds: add a manpage

In reply to a message not in the archive

...I'm also including the POD sources here for easier editing, but we may remove them in the future if it becomes burdensome to keep up 2 copies.

---
 Makefile.am   |    1 +
 cifscreds.1   |  188 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 cifscreds.pod |   81 ++++++++++++++++++++++++
 3 files changed, 270 insertions(+), 0 deletions(-)
 create mode 100644 cifscreds.1
 create mode 100644 cifscreds.pod
Makefile.am +1 -0
index cb20442..e74ce9b 100644
@@ -35,6 +35,7 @@ if CONFIG_CIFSCREDS
 bin_PROGRAMS += cifscreds
 cifscreds_SOURCES = cifscreds.c resolve_host.c util.c
 cifscreds_LDADD = -lkeyutils
+man_MANS += cifscreds.1
 endif
 
 if CONFIG_CIFSIDMAP
cifscreds.1 new file +188 -0

188 lines not on this page · raw message

cifscreds.pod new file +81 -0

81 lines not on this page · raw message

Signature · 1 line
1.7.7.4
#27 lore raw
In reply to a message not in the archive
Quoted 45 lines · On Fri, 6 Jan 2012 13:14:32 -0500 Jeff Layton…

This patchset is a cleanup and overhaul of the cifscreds utility that lives in the cifs-utils tree today. Igor Druzhinin did a wonderful job on this when he did the original code a couple of years ago, but I did a rather poor job at the time of communicating what we actually need for this tool to do. Mea culpa...

This patch is a third pass at morphing it into a tool that's more like what we need. I believe with this, I'll be able to roll some kernel patches that can use the stashed key for establishing sessions.

I've made a few changes since the last set:

- convert the tool to use the new "secret" key_type that I'm proposing

- a manpage has been added

- the key is now prefixed with "cifs:" instead of "cifscreds:"

The idea here is that we want to be able to allow users to stash their NTLM credentials in the kernel, so that it's possible to establish a session on the fly when that user walks into a multiuser mount.

This set should be considered in conjunction with the kernel patchset that I proposed earlier today:

[PATCH 0/3] cifs: allow multiuser mounts with authtypes besides krb5

Jeff Layton (15):
util: move getusername to util.c
cifscreds: add unused attribute to argv parm in cifscreds_clearall
cifscreds: eliminate domain parm from most functions
cifscreds: remove user parameter from create_description
cifscreds: make username part of value instead of description
cifscreds: make usage use "return" and have callers return
cifscreds: move option parsing into main()
cifscreds: make username parameter optional
cifscreds: add --domain flag
cifscreds: loosen allowed characters in domain names
cifscreds: use the session keyring
cifscreds: make cifscreds use the "unreadable" keyring
cifscreds: make key_search use keyctl_search
cifscreds: change prefix on keys to "cifs:" instead of "cifscreds:"
cifscreds: add a manpage

Quoted 11 lines
 Makefile.am   |    1 +
 cifscreds.1   |  188 ++++++++++++++++++++++++++++++++++
 cifscreds.c   |  311 +++++++++++++++++++++++++--------------------------------
 cifscreds.pod |   81 +++++++++++++++
 mount.cifs.c  |   11 --
 util.c        |   13 +++
 util.h        |    1 +
 7 files changed, 418 insertions(+), 188 deletions(-)
 create mode 100644 cifscreds.1
 create mode 100644 cifscreds.pod

I've gone ahead and merged this patchset with some small changes to account for the new "login" key type, and some small changes to the new manpage.

Signature · 1 line
Jeff Layton <[email protected]>