From 4840dde7eb41cfae1b1a430cdc323b8fee46bbd6 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Tue, 1 May 2018 13:19:58 +1200 Subject: [PATCH] net: Add support for a credentials file Add support for the same -A authfile/--authentication-file authfile option that most of the other tools already do. Signed-off-by: Olly Betts --- docs-xml/manpages/net.8.xml | 1 + source3/utils/net.c | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/docs-xml/manpages/net.8.xml b/docs-xml/manpages/net.8.xml index 155c4fc7..3aa9a039 100644 --- a/docs-xml/manpages/net.8.xml +++ b/docs-xml/manpages/net.8.xml @@ -26,6 +26,7 @@ -w|--workgroup workgroup -W|--myworkgroup myworkgroup -U|--user user + -A|--authentication-file authfile -I|--ipaddress ip-address -p|--port port -n myname diff --git a/source3/utils/net.c b/source3/utils/net.c index 44daa608..a903b563 100644 --- a/source3/utils/net.c +++ b/source3/utils/net.c @@ -48,6 +48,7 @@ #include "../libcli/security/security.h" #include "passdb.h" #include "messages.h" +#include "system/filesys.h" #ifdef WITH_FAKE_KASERVER #include "utils/net_afs.h" @@ -902,6 +903,63 @@ static struct functable net_func[] = { }; +static void get_credentials_file(struct net_context *c, + const char *file) +{ + FILE *auth; + fstring buf; + uint16_t len = 0; + char *ptr, *val, *param; + + c->opt_user_name = NULL; + if ((auth=fopen(file, "r")) == NULL) + { + /* fail if we can't open the credentials file */ + d_printf("ERROR: Unable to open credentials file!\n"); + exit(-1); + } + + while (!feof(auth)) + { + /* get a line from the file */ + if (!fgets(buf, sizeof(buf), auth)) + continue; + len = strlen(buf); + + if ((len) && (buf[len-1]=='\n')) + { + buf[len-1] = '\0'; + len--; + } + if (len == 0) + continue; + + /* break up the line into parameter & value. + * will need to eat a little whitespace possibly */ + param = buf; + if (!(ptr = strchr_m (buf, '='))) + continue; + + val = ptr+1; + *ptr = '\0'; + + /* eat leading white space */ + while ((*val!='\0') && ((*val==' ') || (*val=='\t'))) + val++; + + if (strwicmp("password", param) == 0) { + c->opt_password = SMB_STRDUP(val); + } else if (strwicmp("username", param) == 0) { + c->opt_user_specified = true; + c->opt_user_name = SMB_STRDUP(val); + } else if (strwicmp("domain", param) == 0) { + c->opt_target_workgroup = SMB_STRDUP(val); + } + memset(buf, 0, sizeof(buf)); + } + fclose(auth); +} + /**************************************************************************** main program ****************************************************************************/ @@ -923,6 +981,7 @@ static struct functable net_func[] = { {"help", 'h', POPT_ARG_NONE, 0, 'h'}, {"workgroup", 'w', POPT_ARG_STRING, &c->opt_target_workgroup}, {"user", 'U', POPT_ARG_STRING, &c->opt_user_name, 'U'}, + {"authentication-file", 'A', POPT_ARG_STRING, &c->opt_user_name, 'A', "Get the credentials from a file", "FILE"}, {"ipaddress", 'I', POPT_ARG_STRING, 0,'I'}, {"port", 'p', POPT_ARG_INT, &c->opt_port}, {"myname", 'n', POPT_ARG_STRING, &c->opt_requester_name}, @@ -1023,6 +1082,9 @@ static struct functable net_func[] = { c->opt_password = p+1; } break; + case 'A': + get_credentials_file(c, c->opt_user_name); + break; default: d_fprintf(stderr, _("\nInvalid option %s: %s\n"), poptBadOption(pc, 0), poptStrerror(opt)); -- 2.17.0