diff options
Diffstat (limited to 'source3/lib')
-rw-r--r-- | source3/lib/charcnv.c | 13 | ||||
-rw-r--r-- | source3/lib/popt_common.c | 138 | ||||
-rw-r--r-- | source3/lib/readline.c | 23 | ||||
-rw-r--r-- | source3/lib/util.c | 122 |
4 files changed, 192 insertions, 104 deletions
diff --git a/source3/lib/charcnv.c b/source3/lib/charcnv.c index ed778ad5e2..2515e6cef3 100644 --- a/source3/lib/charcnv.c +++ b/source3/lib/charcnv.c @@ -1692,10 +1692,15 @@ size_t push_string_fn(const char *function, unsigned int line, The resulting string in "dest" is always null terminated. **/ -size_t pull_string_fn(const char *function, unsigned int line, - const void *base_ptr, uint16 smb_flags2, char *dest, - const void *src, size_t dest_len, size_t src_len, - int flags) +size_t pull_string_fn(const char *function, + unsigned int line, + const void *base_ptr, + uint16 smb_flags2, + char *dest, + const void *src, + size_t dest_len, + size_t src_len, + int flags) { #ifdef DEVELOPER clobber_region(function, line, dest, dest_len); diff --git a/source3/lib/popt_common.c b/source3/lib/popt_common.c index dbb66b0ba5..99c26106af 100644 --- a/source3/lib/popt_common.c +++ b/source3/lib/popt_common.c @@ -36,8 +36,6 @@ extern bool AllowDebugChange; extern bool override_logfile; -struct user_auth_info cmdline_auth_info; - static void set_logfile(poptContext con, const char * arg) { @@ -100,7 +98,7 @@ static void popt_common_callback(poptContext con, case 's': if (arg) { - pstrcpy(dyn_CONFIGFILE, arg); + strlcpy(dyn_CONFIGFILE, arg,sizeof(dyn_CONFIGFILE)); } break; @@ -213,13 +211,13 @@ static void popt_dynconfig_callback(poptContext con, case DYN_LMHOSTSFILE: if (arg) { - pstrcpy(dyn_LMHOSTSFILE, arg); + strlcpy(dyn_LMHOSTSFILE, arg,sizeof(dyn_LMHOSTSFILE)); } break; case DYN_LIBDIR: if (arg) { - pstrcpy(dyn_LIBDIR, arg); + strlcpy(dyn_LIBDIR, arg,sizeof(dyn_LIBDIR)); } break; @@ -231,25 +229,25 @@ static void popt_dynconfig_callback(poptContext con, case DYN_LOCKDIR: if (arg) { - pstrcpy(dyn_LOCKDIR, arg); + strlcpy(dyn_LOCKDIR, arg,sizeof(dyn_LOCKDIR)); } break; case DYN_PIDDIR: if (arg) { - pstrcpy(dyn_PIDDIR, arg); + strlcpy(dyn_PIDDIR, arg,sizeof(dyn_PIDDIR)); } break; case DYN_SMB_PASSWD_FILE: if (arg) { - pstrcpy(dyn_SMB_PASSWD_FILE, arg); + strlcpy(dyn_SMB_PASSWD_FILE, arg,sizeof(dyn_SMB_PASSWD_FILE)); } break; case DYN_PRIVATE_DIR: if (arg) { - pstrcpy(dyn_PRIVATE_DIR, arg); + strlcpy(dyn_PRIVATE_DIR, arg, sizeof(dyn_PRIVATE_DIR)); } break; @@ -289,7 +287,7 @@ const struct poptOption popt_common_dynconfig[] = { * exit on failure * ****************************************************************************/ -static void get_password_file(struct user_auth_info *a) +static void get_password_file(void) { int fd = -1; char *p; @@ -342,12 +340,14 @@ static void get_password_file(struct user_auth_info *a) } } SAFE_FREE(spec); - pstrcpy(a->password, pass); - if (close_it) + + set_cmdline_auth_info_password(pass); + if (close_it) { close(fd); + } } -static void get_credentials_file(const char *file, struct user_auth_info *info) +static void get_credentials_file(const char *file) { XFILE *auth; fstring buf; @@ -389,15 +389,13 @@ static void get_credentials_file(const char *file, struct user_auth_info *info) while ((*val!='\0') && ((*val==' ') || (*val=='\t'))) val++; - if (strwicmp("password", param) == 0) - { - pstrcpy(info->password, val); - info->got_pass = True; - } - else if (strwicmp("username", param) == 0) - pstrcpy(info->username, val); - else if (strwicmp("domain", param) == 0) + if (strwicmp("password", param) == 0) { + set_cmdline_auth_info_password(val); + } else if (strwicmp("username", param) == 0) { + set_cmdline_auth_info_username(val); + } else if (strwicmp("domain", param) == 0) { set_global_myworkgroup(val); + } memset(buf, 0, sizeof(buf)); } x_fclose(auth); @@ -413,7 +411,7 @@ static void get_credentials_file(const char *file, struct user_auth_info *info) */ -static void popt_common_credentials_callback(poptContext con, +static void popt_common_credentials_callback(poptContext con, enum poptCallbackReason reason, const struct poptOption *opt, const char *arg, const void *data) @@ -421,32 +419,35 @@ static void popt_common_credentials_callback(poptContext con, char *p; if (reason == POPT_CALLBACK_REASON_PRE) { - cmdline_auth_info.use_kerberos = False; - cmdline_auth_info.got_pass = False; - cmdline_auth_info.signing_state = Undefined; - pstrcpy(cmdline_auth_info.username, "GUEST"); + set_cmdline_auth_info_username("GUEST"); - if (getenv("LOGNAME"))pstrcpy(cmdline_auth_info.username,getenv("LOGNAME")); + if (getenv("LOGNAME")) { + set_cmdline_auth_info_username(getenv("LOGNAME")); + } if (getenv("USER")) { - pstrcpy(cmdline_auth_info.username,getenv("USER")); + char *puser = SMB_STRDUP(getenv("USER")); + if (!puser) { + exit(ENOMEM); + } + set_cmdline_auth_info_username(puser); - if ((p = strchr_m(cmdline_auth_info.username,'%'))) { + if ((p = strchr_m(puser,'%'))) { + size_t len; *p = 0; - pstrcpy(cmdline_auth_info.password,p+1); - cmdline_auth_info.got_pass = True; - memset(strchr_m(getenv("USER"),'%')+1,'X',strlen(cmdline_auth_info.password)); + len = strlen(p+1); + set_cmdline_auth_info_password(p+1); + memset(strchr_m(getenv("USER"),'%')+1,'X',len); } + SAFE_FREE(puser); } if (getenv("PASSWD")) { - pstrcpy(cmdline_auth_info.password,getenv("PASSWD")); - cmdline_auth_info.got_pass = True; + set_cmdline_auth_info_password(getenv("PASSWD")); } if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) { - get_password_file(&cmdline_auth_info); - cmdline_auth_info.got_pass = True; + get_password_file(); } return; @@ -456,19 +457,24 @@ static void popt_common_credentials_callback(poptContext con, case 'U': { char *lp; + char *puser = SMB_STRDUP(arg); - pstrcpy(cmdline_auth_info.username,arg); - if ((lp=strchr_m(cmdline_auth_info.username,'%'))) { + if ((lp=strchr_m(puser,'%'))) { + size_t len; *lp = 0; - pstrcpy(cmdline_auth_info.password,lp+1); - cmdline_auth_info.got_pass = True; - memset(strchr_m(arg,'%')+1,'X',strlen(cmdline_auth_info.password)); + set_cmdline_auth_info_username(puser); + set_cmdline_auth_info_password(lp+1); + len = strlen(lp+1); + memset(strchr_m(arg,'%')+1,'X',len); + } else { + set_cmdline_auth_info_username(puser); } + SAFE_FREE(puser); } break; case 'A': - get_credentials_file(arg, &cmdline_auth_info); + get_credentials_file(arg); break; case 'k': @@ -476,64 +482,58 @@ static void popt_common_credentials_callback(poptContext con, d_printf("No kerberos support compiled in\n"); exit(1); #else - cmdline_auth_info.use_kerberos = True; - cmdline_auth_info.got_pass = True; + set_cmdline_auth_info_use_krb5_ticket(); #endif break; case 'S': - { - cmdline_auth_info.signing_state = -1; - if (strequal(arg, "off") || strequal(arg, "no") || strequal(arg, "false")) - cmdline_auth_info.signing_state = False; - else if (strequal(arg, "on") || strequal(arg, "yes") || strequal(arg, "true") || - strequal(arg, "auto") ) - cmdline_auth_info.signing_state = True; - else if (strequal(arg, "force") || strequal(arg, "required") || strequal(arg, "forced")) - cmdline_auth_info.signing_state = Required; - else { - fprintf(stderr, "Unknown signing option %s\n", arg ); - exit(1); - } + if (!set_cmdline_auth_info_signing_state(arg)) { + fprintf(stderr, "Unknown signing option %s\n", arg ); + exit(1); } break; case 'P': { char *opt_password = NULL; + char *pwd = NULL; + /* it is very useful to be able to make ads queries as the machine account for testing purposes and for domain leave */ - + if (!secrets_init()) { d_printf("ERROR: Unable to open secrets database\n"); exit(1); } - + opt_password = secrets_fetch_machine_password(lp_workgroup(), NULL, NULL); - + if (!opt_password) { d_printf("ERROR: Unable to fetch machine password\n"); exit(1); } - pstr_sprintf(cmdline_auth_info.username, "%s$", - global_myname()); - pstrcpy(cmdline_auth_info.password,opt_password); + if (asprintf(&pwd, "%s$", global_myname()) < 0) { + exit(ENOMEM); + } + set_cmdline_auth_info_username(pwd); + set_cmdline_auth_info_password(opt_password); + SAFE_FREE(pwd); SAFE_FREE(opt_password); /* machine accounts only work with kerberos */ - cmdline_auth_info.use_kerberos = True; - cmdline_auth_info.got_pass = True; + set_cmdline_auth_info_use_krb5_ticket(); } break; + case 'N': + set_cmdline_auth_info_password(""); + break; } } - - struct poptOption popt_common_credentials[] = { { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback }, { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" }, - { "no-pass", 'N', POPT_ARG_NONE, &cmdline_auth_info.got_pass, 0, "Don't ask for a password" }, - { "kerberos", 'k', POPT_ARG_NONE, &cmdline_auth_info.use_kerberos, 'k', "Use kerberos (active directory) authentication" }, + { "no-pass", 'N', POPT_ARG_NONE, NULL, 'N', "Don't ask for a password" }, + { "kerberos", 'k', POPT_ARG_NONE, NULL, 'k', "Use kerberos (active directory) authentication" }, { "authentication-file", 'A', POPT_ARG_STRING, NULL, 'A', "Get the credentials from a file", "FILE" }, { "signing", 'S', POPT_ARG_STRING, NULL, 'S', "Set the client signing state", "on|off|required" }, {"machine-pass", 'P', POPT_ARG_NONE, NULL, 'P', "Use stored machine account password" }, diff --git a/source3/lib/readline.c b/source3/lib/readline.c index 7c127817be..254f55c86a 100644 --- a/source3/lib/readline.c +++ b/source3/lib/readline.c @@ -53,7 +53,7 @@ static char *smb_readline_replacement(const char *prompt, void (*callback)(void) char **(completion_fn)(const char *text, int start, int end)) { fd_set fds; - static char *line; + char *line = NULL; struct timeval timeout; int fd = x_fileno(x_stdin); char *ret; @@ -64,11 +64,9 @@ static char *smb_readline_replacement(const char *prompt, void (*callback)(void) x_fflush(x_stdout); } - if (line == NULL) { - line = (char *)SMB_MALLOC(BUFSIZ); - if (!line) { - return NULL; - } + line = (char *)SMB_MALLOC(BUFSIZ); + if (!line) { + return NULL; } while (1) { @@ -80,10 +78,14 @@ static char *smb_readline_replacement(const char *prompt, void (*callback)(void) if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) { ret = x_fgets(line, BUFSIZ, x_stdin); + if (ret == 0) { + SAFE_FREE(line); + } return ret; } - if (callback) + if (callback) { callback(); + } } } @@ -91,7 +93,7 @@ static char *smb_readline_replacement(const char *prompt, void (*callback)(void) Display the prompt and wait for input. Call callback() regularly. ****************************************************************************/ -char *smb_readline(const char *prompt, void (*callback)(void), +char *smb_readline(const char *prompt, void (*callback)(void), char **(completion_fn)(const char *text, int start, int end)) { char *ret; @@ -99,7 +101,7 @@ char *smb_readline(const char *prompt, void (*callback)(void), interactive = isatty(x_fileno(x_stdin)) || getenv("CLI_FORCE_INTERACTIVE"); if (!interactive) { - return smb_readline_replacement(NULL, callback, completion_fn); + return smb_readline_replacement(NULL, callback, completion_fn); } #if HAVE_LIBREADLINE @@ -167,7 +169,7 @@ int cmd_history(void) int i; hlist = history_list(); - + for (i = 0; hlist && hlist[i]; i++) { DEBUG(0, ("%d: %s\n", i, hlist[i]->line)); } @@ -177,4 +179,3 @@ int cmd_history(void) return 0; } - diff --git a/source3/lib/util.c b/source3/lib/util.c index f0ea6c8e33..3d653d9b80 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -57,9 +57,6 @@ extern unsigned int global_clobber_region_line; enum protocol_types Protocol = PROTOCOL_COREPLUS; -/* a default finfo structure to ensure all fields are sensible */ -file_info def_finfo; - /* this is used by the chaining code */ int chain_size = 0; @@ -190,7 +187,7 @@ void gfree_names(void) void gfree_all( void ) { - gfree_names(); + gfree_names(); gfree_loadparm(); gfree_case_tables(); gfree_debugsyms(); @@ -283,6 +280,106 @@ bool init_names(void) } /**************************************************************************n + Code to cope with username/password auth options from the commandline. + Used mainly in client tools. +****************************************************************************/ + +static struct user_auth_info cmdline_auth_info = { + NULL, /* username */ + NULL, /* password */ + false, /* got_pass */ + false, /* use_kerberos */ + Undefined /* signing state */ +}; + +const char *get_cmdline_auth_info_username(void) +{ + if (!cmdline_auth_info.username) { + return ""; + } + return cmdline_auth_info.username; +} + +void set_cmdline_auth_info_username(const char *username) +{ + SAFE_FREE(cmdline_auth_info.username); + cmdline_auth_info.username = SMB_STRDUP(username); + if (!cmdline_auth_info.username) { + exit(ENOMEM); + } +} + +const char *get_cmdline_auth_info_password(void) +{ + if (!cmdline_auth_info.password) { + return ""; + } + return cmdline_auth_info.password; +} + +void set_cmdline_auth_info_password(const char *password) +{ + SAFE_FREE(cmdline_auth_info.password); + cmdline_auth_info.password = SMB_STRDUP(password); + if (!cmdline_auth_info.password) { + exit(ENOMEM); + } + cmdline_auth_info.got_pass = true; +} + +bool set_cmdline_auth_info_signing_state(const char *arg) +{ + cmdline_auth_info.signing_state = -1; + if (strequal(arg, "off") || strequal(arg, "no") || + strequal(arg, "false")) { + cmdline_auth_info.signing_state = false; + } else if (strequal(arg, "on") || strequal(arg, "yes") || + strequal(arg, "true") || strequal(arg, "auto")) { + cmdline_auth_info.signing_state = true; + } else if (strequal(arg, "force") || strequal(arg, "required") || + strequal(arg, "forced")) { + cmdline_auth_info.signing_state = Required; + } else { + return false; + } + return true; +} + +int get_cmdline_auth_info_signing_state(void) +{ + return cmdline_auth_info.signing_state; +} + +bool get_cmdline_auth_info_use_kerberos(void) +{ + return cmdline_auth_info.use_kerberos; +} + +/* This should only be used by lib/popt_common.c JRA */ +void set_cmdline_auth_info_use_krb5_ticket(void) +{ + cmdline_auth_info.use_kerberos = true; + cmdline_auth_info.got_pass = true; +} + +bool get_cmdline_auth_info_got_pass(void) +{ + return cmdline_auth_info.got_pass; +} + +bool get_cmdline_auth_info_copy(struct user_auth_info *info) +{ + *info = cmdline_auth_info; + /* Now re-alloc the strings. */ + info->username = SMB_STRDUP(get_cmdline_auth_info_username()); + info->password = SMB_STRDUP(get_cmdline_auth_info_password()); + if (!info->username || !info->password) { + return false; + } + return true; +} + +/**************************************************************************n Find a suitable temporary directory. The result should be copied immediately as it may be overwritten by a subsequent call. ****************************************************************************/ @@ -693,21 +790,6 @@ char *clean_name(TALLOC_CTX *ctx, const char *s) } /******************************************************************* - Horrible temporary hack until pstring is dead. -********************************************************************/ - -char *pstring_clean_name(pstring s) -{ - char *str = clean_name(NULL,s); - if (!str) { - return NULL; - } - pstrcpy(s, str); - TALLOC_FREE(str); - return s; -} - -/******************************************************************* Close the low 3 fd's and open dev/null in their place. ********************************************************************/ @@ -718,7 +800,7 @@ void close_low_fds(bool stderr_too) int i; close(0); - close(1); + close(1); if (stderr_too) close(2); |