lib/replace: make sure krb5_cc_default[_name]() is no longer used directly
[metze/samba-autobuild/.git] / source3 / utils / pdbedit.c
index a353bae7c4eb31b108a323b07fed2e6e57dd63d4..eb6b98239028b89c45551ef1dce99c9ff577dba6 100644 (file)
 */
 
 #include "includes.h"
-#include "popt_common.h"
+#include "lib/cmdline/cmdline.h"
 #include "../librpc/gen_ndr/samr.h"
 #include "../libcli/security/security.h"
 #include "passdb.h"
+#include "cmdline_contexts.h"
+#include "passwd_proto.h"
+#include "lib/util/smb_strtox.h"
+#include "lib/param/param.h"
 
 #define BIT_BACKEND    0x00000004
 #define BIT_VERBOSE    0x00000008
@@ -225,7 +229,7 @@ static int reinit_account_policies (void)
  Add all currently available account policy from tdb to one backend
  ********************************************************/
 
-static int export_account_policies (struct pdb_methods *in, struct pdb_methods *out) 
+static int export_account_policies (struct pdb_methods *in, struct pdb_methods *out)
 {
        int i;
 
@@ -267,14 +271,15 @@ static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdst
        if (verbosity) {
                char temp[44];
                const uint8_t *hours;
+               struct dom_sid_buf buf;
 
                printf ("Unix username:        %s\n", pdb_get_username(sam_pwent));
                printf ("NT username:          %s\n", pdb_get_nt_username(sam_pwent));
                printf ("Account Flags:        %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
                printf ("User SID:             %s\n",
-                       sid_string_tos(pdb_get_user_sid(sam_pwent)));
+                       dom_sid_str_buf(pdb_get_user_sid(sam_pwent), &buf));
                printf ("Primary Group SID:    %s\n",
-                       sid_string_tos(pdb_get_group_sid(sam_pwent)));
+                       dom_sid_str_buf(pdb_get_group_sid(sam_pwent), &buf));
                printf ("Full Name:            %s\n", pdb_get_fullname(sam_pwent));
                printf ("Home Directory:       %s\n", pdb_get_homedir(sam_pwent));
                printf ("HomeDir Drive:        %s\n", pdb_get_dir_drive(sam_pwent));
@@ -323,7 +328,7 @@ static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdst
                        printf ("LM hash             : %s\n", temp);
                        pdb_sethexpwd(temp, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
                        printf ("NT hash             : %s\n", temp);
-               }       
+               }
 
        } else if (smbpwdstyle) {
                char lm_passwd[33];
@@ -592,13 +597,18 @@ static int set_user_info(const char *username, const char *fullname,
        }
 
        if (kickoff_time) {
-               char *endptr;
                time_t value = get_time_t_max();
 
                if (strcmp(kickoff_time, "never") != 0) {
-                       uint32_t num = strtoul(kickoff_time, &endptr, 10);
-
-                       if ((endptr == kickoff_time) || (endptr[0] != '\0')) {
+                       int error = 0;
+                       uint32_t num;
+
+                       num = smb_strtoul(kickoff_time,
+                                         NULL,
+                                         10,
+                                         &error,
+                                         SMB_STR_FULL_STR_CONV);
+                       if (error != 0) {
                                fprintf(stderr, "Failed to parse kickoff time\n");
                                return -1;
                        }
@@ -614,18 +624,18 @@ static int set_user_info(const char *username, const char *fullname,
                        fprintf(stderr, "Invalid hash\n");
                        return -1;
                }
-                                        
+
                pdb_gethexpwd(str_hex_pwd, new_nt_p16);
-               
+
                if (!pdb_set_nt_passwd (sam_pwent, new_nt_p16 , PDB_CHANGED)) {
                        fprintf(stderr, "Failed to set password from nt-hash\n");
                        return -1;
-               }       
+               }
 
                if (!pdb_set_pass_last_set_time (sam_pwent, time(NULL), PDB_CHANGED)){
                        fprintf(stderr, "Failed to set last password set time\n");
                        return -1;
-               }       
+               }
                if (!pdb_update_history(sam_pwent, new_nt_p16)){
                        fprintf(stderr, "Failed to update password history\n");
                        return -1;
@@ -633,7 +643,7 @@ static int set_user_info(const char *username, const char *fullname,
        }
 
        if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
-               
+
                print_user_info(username, True, (str_hex_pwd != NULL ));
        } else {
                fprintf (stderr, "Unable to modify entry!\n");
@@ -750,7 +760,7 @@ static int new_user(const char *username, const char *fullname,
        NTSTATUS status;
        struct dom_sid u_sid;
        int flags;
-       int ret;
+       int ret = -1;
 
        tosctx = talloc_tos();
        if (!tosctx) {
@@ -766,10 +776,14 @@ static int new_user(const char *username, const char *fullname,
        }
 
        pwd1 = get_pass( "new password:", stdin_get);
+       if (pwd1 == NULL) {
+               fprintf(stderr, "Failed to read passwords.\n");
+               goto done;
+       }
        pwd2 = get_pass( "retype new password:", stdin_get);
-       if (!pwd1 || !pwd2) {
+       if (pwd2 == NULL) {
                fprintf(stderr, "Failed to read passwords.\n");
-               return -1;
+               goto done;
        }
        ret = strcmp(pwd1, pwd2);
        if (ret != 0) {
@@ -1054,8 +1068,10 @@ int main(int argc, const char **argv)
        static char *kickoff_time = NULL;
        static char *str_hex_pwd = NULL;
        TALLOC_CTX *frame = talloc_stackframe();
+       struct loadparm_context *lp_ctx = NULL;
        NTSTATUS status;
        poptContext pc;
+       bool ok;
        struct poptOption long_options[] = {
                POPT_AUTOHELP
                {"list",        'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
@@ -1092,6 +1108,7 @@ int main(int argc, const char **argv)
                {"kickoff-time", 'K', POPT_ARG_STRING, &kickoff_time, 0, "set the kickoff time", NULL},
                {"set-nt-hash", 0, POPT_ARG_STRING, &str_hex_pwd, 0, "set password from nt-hash", NULL},
                POPT_COMMON_SAMBA
+               POPT_COMMON_VERSION
                POPT_TABLEEND
        };
 
@@ -1099,32 +1116,53 @@ int main(int argc, const char **argv)
 
        smb_init_locale();
 
-       setup_logging("pdbedit", DEBUG_STDOUT);
+       ok = samba_cmdline_init(frame,
+                               SAMBA_CMDLINE_CONFIG_CLIENT,
+                               false /* require_smbconf */);
+       if (!ok) {
+               DBG_ERR("Failed to init cmdline parser!\n");
+               TALLOC_FREE(frame);
+               exit(1);
+       }
+       lp_ctx = samba_cmdline_get_lp_ctx();
 
-       pc = poptGetContext(NULL, argc, argv, long_options,
-                           POPT_CONTEXT_KEEP_FIRST);
+       pc = samba_popt_get_context(getprogname(),
+                                   argc,
+                                   argv,
+                                   long_options,
+                                   POPT_CONTEXT_KEEP_FIRST);
+       if (pc == NULL) {
+               DBG_ERR("Failed to setup popt context!\n");
+               TALLOC_FREE(frame);
+               exit(1);
+       }
 
        while((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case 'C':
                        account_policy_value_set = True;
                        break;
+               case POPT_ERROR_BADOPT:
+                       fprintf(stderr, "\nInvalid option %s: %s\n\n",
+                               poptBadOption(pc, 0), poptStrerror(opt));
+                       poptPrintUsage(pc, stderr, 0);
+                       exit(1);
                }
        }
 
        poptGetArg(pc); /* Drop argv[0], the program name */
 
-       if (user_name == NULL)
-               user_name = poptGetArg(pc);
-
-       if (!lp_load_global(get_dyn_CONFIGFILE())) {
-               fprintf(stderr, "Can't load %s - run testparm to debug it\n", get_dyn_CONFIGFILE());
-               exit(1);
+       if (user_name == NULL) {
+               if (poptPeekArg(pc)) {
+                       user_name = talloc_strdup(frame, poptGetArg(pc));
+                       if (user_name == NULL) {
+                               fprintf(stderr, "out of memory\n");
+                               TALLOC_FREE(frame);
+                               exit(1);
+                       }
+               }
        }
 
-       if (!init_names())
-               exit(1);
-
        setparms =      (backend ? BIT_BACKEND : 0) +
                        (verbose ? BIT_VERBOSE : 0) +
                        (spstyle ? BIT_SPSTYLE : 0) +
@@ -1153,13 +1191,13 @@ int main(int argc, const char **argv)
                        (kickoff_time ? BIT_KICKOFFTIME : 0) +
                        (str_hex_pwd ? BIT_PWSETNTHASH : 0 ) +
                        (acct_desc ? BIT_DESCRIPTION : 0);
-                       
+
 
        if (setparms & BIT_BACKEND) {
                /* HACK: set the global passdb backend by overwriting globals.
                 * This way we can use regular pdb functions for default
                 * operations that do not involve passdb migrations */
-               lp_set_cmdline("passdb backend", backend);
+               lpcfg_set_cmdline(lp_ctx, "passdb backend", backend);
        } else {
                backend = lp_passdb_backend();
        }
@@ -1173,6 +1211,7 @@ int main(int argc, const char **argv)
        checkparms = setparms & ~MASK_ALWAYS_GOOD;
 
        if (checkparms & BIT_FIX_INIT) {
+               poptFreeContext(pc);
                return fix_users_list();
        }
 
@@ -1229,6 +1268,8 @@ int main(int argc, const char **argv)
             (checkparms & BIT_EXPORT)) &&
            !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
 
+               poptFreeContext(pc);
+
                if (backend_in) {
                        status = make_pdb_method_name(&bin, backend_in);
                } else {
@@ -1286,9 +1327,11 @@ int main(int argc, const char **argv)
        /* list users operations */
        if (checkparms & BIT_LIST) {
                if (!(checkparms & ~BIT_LIST)) {
+                       poptFreeContext(pc);
                        return print_users_list(verbose, spstyle);
                }
                if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
+                       poptFreeContext(pc);
                        return print_user_info(user_name, verbose, spstyle);
                }
        }
@@ -1313,12 +1356,14 @@ int main(int argc, const char **argv)
                /* check use of -u option */
                if (!(checkparms & BIT_USER)) {
                        fprintf (stderr, "Username not specified! (use -u option)\n");
+                       poptFreeContext(pc);
                        return -1;
                }
 
                /* account creation operations */
                if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
-                       if (checkparms & BIT_MACHINE) {
+                       poptFreeContext(pc);
+                       if (checkparms & BIT_MACHINE) {
                                return new_machine(user_name, machine_sid);
                        } else {
                                return new_user(user_name, full_name,
@@ -1330,7 +1375,8 @@ int main(int argc, const char **argv)
 
                /* account deletion operations */
                if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
-                       if (checkparms & BIT_MACHINE) {
+                       poptFreeContext(pc);
+                       if (checkparms & BIT_MACHINE) {
                                return delete_machine_entry(user_name);
                        } else {
                                return delete_user_entry(user_name);
@@ -1339,6 +1385,7 @@ int main(int argc, const char **argv)
 
                /* account modification operations */
                if (!(checkparms & ~(BIT_MODIFY + BIT_USER + BIT_MACHINE))) {
+                       poptFreeContext(pc);
                        if (checkparms & BIT_MACHINE) {
                                return set_machine_info(user_name,
                                                        account_control,
@@ -1360,6 +1407,8 @@ int main(int argc, const char **argv)
        }
        poptPrintHelp(pc, stderr, 0);
 
+       gfree_all();
+       poptFreeContext(pc);
        TALLOC_FREE(frame);
        return 1;
 }