Merge branch 'master' of ssh://git.samba.org/data/git/samba
[ira/wip.git] / source3 / lib / popt_common.c
index 7f7d23fa00b9daff311984ecd93c46f7714e1ee1..cad14ec493092ebd18b4fd1a4c48795681e0f9f9 100644 (file)
@@ -39,7 +39,7 @@ extern bool override_logfile;
 static void set_logfile(poptContext con, const char * arg)
 {
 
-       char *logfile = NULL;
+       char *lfile = NULL;
        const char *pname;
 
        /* Find out basename of current program */
@@ -50,11 +50,11 @@ static void set_logfile(poptContext con, const char * arg)
        else
                pname++;
 
-       if (asprintf(&logfile, "%s/log.%s", arg, pname) < 0) {
+       if (asprintf(&lfile, "%s/log.%s", arg, pname) < 0) {
                return;
        }
-       lp_set_logfile(logfile);
-       SAFE_FREE(logfile);
+       lp_set_logfile(lfile);
+       SAFE_FREE(lfile);
 }
 
 static bool PrintSambaVersionString;
@@ -166,11 +166,17 @@ struct poptOption popt_common_configfile[] = {
 };
 
 struct poptOption popt_common_version[] = {
-       { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
+       { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_POST, (void *)popt_common_callback },
        { "version", 'V', POPT_ARG_NONE, NULL, 'V', "Print version" },
        POPT_TABLEEND
 };
 
+struct poptOption popt_common_debuglevel[] = {
+       { NULL, 0, POPT_ARG_CALLBACK, (void *)popt_common_callback },
+       { "debuglevel", 'd', POPT_ARG_STRING, NULL, 'd', "Set debug level", "DEBUGLEVEL" },
+       POPT_TABLEEND
+};
+
 
 /* Handle command line options:
  *             --sbindir
@@ -178,6 +184,7 @@ struct poptOption popt_common_version[] = {
  *             --swatdir
  *             --lmhostsfile
  *             --libdir
+ *             --modulesdir
  *             --shlibext
  *             --lockdir
  *             --piddir
@@ -191,6 +198,7 @@ enum dyn_item{
        DYN_SWATDIR,
        DYN_LMHOSTSFILE,
        DYN_LIBDIR,
+       DYN_MODULESDIR,
        DYN_SHLIBEXT,
        DYN_LOCKDIR,
        DYN_PIDDIR,
@@ -236,6 +244,12 @@ static void popt_dynconfig_callback(poptContext con,
                }
                break;
 
+       case DYN_MODULESDIR:
+               if (arg) {
+                       set_dyn_MODULESDIR(arg);
+               }
+               break;
+
        case DYN_SHLIBEXT:
                if (arg) {
                        set_dyn_SHLIBEXT(arg);
@@ -283,6 +297,8 @@ const struct poptOption popt_common_dynconfig[] = {
            "Path to lmhosts file", "LMHOSTSFILE" },
        { "libdir", '\0' , POPT_ARG_STRING, NULL, DYN_LIBDIR,
            "Path to shared library directory", "LIBDIR" },
+       { "modulesdir", '\0' , POPT_ARG_STRING, NULL, DYN_MODULESDIR,
+           "Path to shared modules directory", "MODULESDIR" },
        { "shlibext", '\0' , POPT_ARG_STRING, NULL, DYN_SHLIBEXT,
            "Shared library extension", "SHLIBEXT" },
        { "lockdir", '\0' , POPT_ARG_STRING, NULL, DYN_LOCKDIR,
@@ -302,7 +318,7 @@ const struct poptOption popt_common_dynconfig[] = {
  * exit on failure
  * ****************************************************************************/
 
-static void get_password_file(void)
+static void get_password_file(struct user_auth_info *auth_info)
 {
        int fd = -1;
        char *p;
@@ -327,6 +343,11 @@ static void get_password_file(void)
                close_it = True;
        }
 
+       if (fd < 0) {
+               fprintf(stderr, "fd = %d, < 0\n", fd);
+               exit(1);
+       }
+
        for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
                p && p - pass < sizeof(pass);) {
                switch (read(fd, p, 1)) {
@@ -356,13 +377,14 @@ static void get_password_file(void)
        }
        SAFE_FREE(spec);
 
-       set_cmdline_auth_info_password(pass);
+       set_cmdline_auth_info_password(auth_info, pass);
        if (close_it) {
                close(fd);
        }
 }
 
-static void get_credentials_file(const char *file)
+static void get_credentials_file(struct user_auth_info *auth_info,
+                                const char *file)
 {
        XFILE *auth;
        fstring buf;
@@ -405,9 +427,9 @@ static void get_credentials_file(const char *file)
                        val++;
 
                if (strwicmp("password", param) == 0) {
-                       set_cmdline_auth_info_password(val);
+                       set_cmdline_auth_info_password(auth_info, val);
                } else if (strwicmp("username", param) == 0) {
-                       set_cmdline_auth_info_username(val);
+                       set_cmdline_auth_info_username(auth_info, val);
                } else if (strwicmp("domain", param) == 0) {
                        set_global_myworkgroup(val);
                }
@@ -432,13 +454,16 @@ static void popt_common_credentials_callback(poptContext con,
                                        const struct poptOption *opt,
                                        const char *arg, const void *data)
 {
+       struct user_auth_info *auth_info = talloc_get_type_abort(
+               *((const char **)data), struct user_auth_info);
        char *p;
 
        if (reason == POPT_CALLBACK_REASON_PRE) {
-               set_cmdline_auth_info_username("GUEST");
+               set_cmdline_auth_info_username(auth_info, "GUEST");
 
                if (getenv("LOGNAME")) {
-                       set_cmdline_auth_info_username(getenv("LOGNAME"));
+                       set_cmdline_auth_info_username(auth_info,
+                                                      getenv("LOGNAME"));
                }
 
                if (getenv("USER")) {
@@ -446,24 +471,25 @@ static void popt_common_credentials_callback(poptContext con,
                        if (!puser) {
                                exit(ENOMEM);
                        }
-                       set_cmdline_auth_info_username(puser);
+                       set_cmdline_auth_info_username(auth_info, puser);
 
                        if ((p = strchr_m(puser,'%'))) {
                                size_t len;
                                *p = 0;
                                len = strlen(p+1);
-                               set_cmdline_auth_info_password(p+1);
+                               set_cmdline_auth_info_password(auth_info, p+1);
                                memset(strchr_m(getenv("USER"),'%')+1,'X',len);
                        }
                        SAFE_FREE(puser);
                }
 
                if (getenv("PASSWD")) {
-                       set_cmdline_auth_info_password(getenv("PASSWD"));
+                       set_cmdline_auth_info_password(auth_info,
+                                                      getenv("PASSWD"));
                }
 
                if (getenv("PASSWD_FD") || getenv("PASSWD_FILE")) {
-                       get_password_file();
+                       get_password_file(auth_info);
                }
 
                return;
@@ -478,19 +504,22 @@ static void popt_common_credentials_callback(poptContext con,
                        if ((lp=strchr_m(puser,'%'))) {
                                size_t len;
                                *lp = 0;
-                               set_cmdline_auth_info_username(puser);
-                               set_cmdline_auth_info_password(lp+1);
+                               set_cmdline_auth_info_username(auth_info,
+                                                              puser);
+                               set_cmdline_auth_info_password(auth_info,
+                                                              lp+1);
                                len = strlen(lp+1);
                                memset(strchr_m(arg,'%')+1,'X',len);
                        } else {
-                               set_cmdline_auth_info_username(puser);
+                               set_cmdline_auth_info_username(auth_info,
+                                                              puser);
                        }
                        SAFE_FREE(puser);
                }
                break;
 
        case 'A':
-               get_credentials_file(arg);
+               get_credentials_file(auth_info, arg);
                break;
 
        case 'k':
@@ -498,59 +527,40 @@ static void popt_common_credentials_callback(poptContext con,
                d_printf("No kerberos support compiled in\n");
                exit(1);
 #else
-               set_cmdline_auth_info_use_krb5_ticket();
+               set_cmdline_auth_info_use_krb5_ticket(auth_info);
 #endif
                break;
 
        case 'S':
-               if (!set_cmdline_auth_info_signing_state(arg)) {
+               if (!set_cmdline_auth_info_signing_state(auth_info, 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);
-                       }
-                       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 */
-                       set_cmdline_auth_info_use_krb5_ticket();
-               }
+               set_cmdline_auth_info_use_machine_account(auth_info);
                break;
        case 'N':
-               set_cmdline_auth_info_password("");
+               set_cmdline_auth_info_password(auth_info, "");
                break;
        case 'e':
-               set_cmdline_auth_info_smb_encrypt();
+               set_cmdline_auth_info_smb_encrypt(auth_info);
                break;
 
        }
 }
 
+static struct user_auth_info *global_auth_info;
+
+void popt_common_set_auth_info(struct user_auth_info *auth_info)
+{
+       global_auth_info = auth_info;
+}
+
 struct poptOption popt_common_credentials[] = {
-       { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE, (void *)popt_common_credentials_callback },
+       { NULL, 0, POPT_ARG_CALLBACK|POPT_CBFLAG_PRE,
+         (void *)popt_common_credentials_callback, 0,
+         (const char *)&global_auth_info },
        { "user", 'U', POPT_ARG_STRING, NULL, 'U', "Set the network username", "USERNAME" },
        { "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" },