LCOV - code coverage report
Current view: top level - source3/utils - pdbedit.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 209 677 30.9 %
Date: 2024-06-13 04:01:37 Functions: 6 16 37.5 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             :    passdb editing frontend
       4             : 
       5             :    Copyright (C) Simo Sorce      2000-2009
       6             :    Copyright (C) Andrew Bartlett 2001
       7             :    Copyright (C) Jelmer Vernooij 2002
       8             : 
       9             :    This program is free software; you can redistribute it and/or modify
      10             :    it under the terms of the GNU General Public License as published by
      11             :    the Free Software Foundation; either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    This program is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : #include "includes.h"
      24             : #include "lib/cmdline/cmdline.h"
      25             : #include "../librpc/gen_ndr/samr.h"
      26             : #include "../libcli/security/security.h"
      27             : #include "passdb.h"
      28             : #include "cmdline_contexts.h"
      29             : #include "passwd_proto.h"
      30             : #include "lib/util/smb_strtox.h"
      31             : 
      32             : #define BIT_BACKEND     0x00000004
      33             : #define BIT_VERBOSE     0x00000008
      34             : #define BIT_SPSTYLE     0x00000010
      35             : #define BIT_CAN_CHANGE  0x00000020
      36             : #define BIT_MUST_CHANGE 0x00000040
      37             : #define BIT_USERSIDS    0x00000080
      38             : #define BIT_FULLNAME    0x00000100
      39             : #define BIT_HOMEDIR     0x00000200
      40             : #define BIT_HDIRDRIVE   0x00000400
      41             : #define BIT_LOGSCRIPT   0x00000800
      42             : #define BIT_PROFILE     0x00001000
      43             : #define BIT_MACHINE     0x00002000
      44             : #define BIT_USERDOMAIN  0x00004000
      45             : #define BIT_USER        0x00008000
      46             : #define BIT_LIST        0x00010000
      47             : #define BIT_MODIFY      0x00020000
      48             : #define BIT_CREATE      0x00040000
      49             : #define BIT_DELETE      0x00080000
      50             : #define BIT_ACCPOLICY   0x00100000
      51             : #define BIT_ACCPOLVAL   0x00200000
      52             : #define BIT_ACCTCTRL    0x00400000
      53             : #define BIT_RESERV_7    0x00800000
      54             : #define BIT_IMPORT      0x01000000
      55             : #define BIT_EXPORT      0x02000000
      56             : #define BIT_FIX_INIT    0x04000000
      57             : #define BIT_BADPWRESET  0x08000000
      58             : #define BIT_LOGONHOURS  0x10000000
      59             : #define BIT_KICKOFFTIME 0x20000000
      60             : #define BIT_DESCRIPTION 0x40000000
      61             : #define BIT_PWSETNTHASH 0x80000000
      62             : 
      63             : #define MASK_ALWAYS_GOOD        0x0000001F
      64             : #define MASK_USER_GOOD          0xE0405FE0
      65             : 
      66           0 : static int get_sid_from_cli_string(struct dom_sid *sid, const char *str_sid)
      67             : {
      68             :         uint32_t rid;
      69             : 
      70           0 :         if (!string_to_sid(sid, str_sid)) {
      71             :                 /* not a complete sid, may be a RID,
      72             :                  * try building a SID */
      73             : 
      74           0 :                 if (sscanf(str_sid, "%u", &rid) != 1) {
      75           0 :                         fprintf(stderr, "Error passed string is not "
      76             :                                         "a complete SID or RID!\n");
      77           0 :                         return -1;
      78             :                 }
      79           0 :                 sid_compose(sid, get_global_sam_sid(), rid);
      80             :         }
      81             : 
      82           0 :         return 0;
      83             : }
      84             : 
      85             : /*********************************************************
      86             :  Add all currently available users to another db
      87             :  ********************************************************/
      88             : 
      89           0 : static int export_database (struct pdb_methods *in,
      90             :                             struct pdb_methods *out,
      91             :                             const char *username)
      92             : {
      93             :         NTSTATUS status;
      94             :         struct pdb_search *u_search;
      95             :         struct samr_displayentry userentry;
      96             : 
      97           0 :         DEBUG(3, ("export_database: username=\"%s\"\n", username ? username : "(NULL)"));
      98             : 
      99           0 :         u_search = pdb_search_init(talloc_tos(), PDB_USER_SEARCH);
     100           0 :         if (u_search == NULL) {
     101           0 :                 DEBUG(0, ("pdb_search_init failed\n"));
     102           0 :                 return 1;
     103             :         }
     104             : 
     105           0 :         if (!in->search_users(in, u_search, 0)) {
     106           0 :                 DEBUG(0, ("Could not start searching users\n"));
     107           0 :                 TALLOC_FREE(u_search);
     108           0 :                 return 1;
     109             :         }
     110             : 
     111           0 :         while (u_search->next_entry(u_search, &userentry)) {
     112             :                 struct samu *user;
     113             :                 struct samu *account;
     114             :                 struct dom_sid user_sid;
     115             : 
     116           0 :                 DEBUG(4, ("Processing account %s\n", userentry.account_name));
     117             : 
     118           0 :                 if ((username != NULL)
     119           0 :                     && (strcmp(username, userentry.account_name) != 0)) {
     120             :                         /*
     121             :                          * ignore unwanted users
     122             :                          */
     123           0 :                         continue;
     124             :                 }
     125             : 
     126           0 :                 user = samu_new(talloc_tos());
     127           0 :                 if (user == NULL) {
     128           0 :                         DEBUG(0, ("talloc failed\n"));
     129           0 :                         break;
     130             :                 }
     131             : 
     132           0 :                 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
     133             : 
     134           0 :                 status = in->getsampwsid(in, user, &user_sid);
     135             : 
     136           0 :                 if (!NT_STATUS_IS_OK(status)) {
     137           0 :                         DEBUG(2, ("getsampwsid failed: %s\n",
     138             :                                   nt_errstr(status)));
     139           0 :                         TALLOC_FREE(user);
     140           0 :                         continue;
     141             :                 }
     142             : 
     143           0 :                 account = samu_new(NULL);
     144           0 :                 if (account == NULL) {
     145           0 :                         fprintf(stderr, "export_database: Memory allocation "
     146             :                                 "failure!\n");
     147           0 :                         TALLOC_FREE( user );
     148           0 :                         TALLOC_FREE(u_search);
     149           0 :                         return 1;
     150             :                 }
     151             : 
     152           0 :                 printf("Importing account for %s...", user->username);
     153           0 :                 status = out->getsampwnam(out, account, user->username);
     154             : 
     155           0 :                 if (NT_STATUS_IS_OK(status)) {
     156           0 :                         status = out->update_sam_account( out, user );
     157             :                 } else {
     158           0 :                         status = out->add_sam_account(out, user);
     159             :                 }
     160             : 
     161           0 :                 if ( NT_STATUS_IS_OK(status) ) {
     162           0 :                         printf( "ok\n");
     163             :                 } else {
     164           0 :                         printf( "failed\n");
     165             :                 }
     166             : 
     167           0 :                 TALLOC_FREE( account );
     168           0 :                 TALLOC_FREE( user );
     169             :         }
     170             : 
     171           0 :         TALLOC_FREE(u_search);
     172             : 
     173           0 :         return 0;
     174             : }
     175             : 
     176             : /*********************************************************
     177             :  Add all currently available group mappings to another db
     178             :  ********************************************************/
     179             : 
     180           0 : static int export_groups (struct pdb_methods *in, struct pdb_methods *out)
     181             : {
     182           0 :         GROUP_MAP **maps = NULL;
     183           0 :         size_t i, entries = 0;
     184             :         NTSTATUS status;
     185             : 
     186           0 :         status = in->enum_group_mapping(in, get_global_sam_sid(),
     187             :                         SID_NAME_DOM_GRP, &maps, &entries, False);
     188             : 
     189           0 :         if ( NT_STATUS_IS_ERR(status) ) {
     190           0 :                 fprintf(stderr, "Unable to enumerate group map entries.\n");
     191           0 :                 return 1;
     192             :         }
     193             : 
     194           0 :         for (i=0; i<entries; i++) {
     195           0 :                 out->add_group_mapping_entry(out, maps[i]);
     196             :         }
     197             : 
     198           0 :         TALLOC_FREE(maps);
     199             : 
     200           0 :         return 0;
     201             : }
     202             : 
     203             : /*********************************************************
     204             :  Reset account policies to their default values and remove marker
     205             :  ********************************************************/
     206             : 
     207           0 : static int reinit_account_policies (void)
     208             : {
     209             :         int i;
     210             : 
     211           0 :         for (i=1; decode_account_policy_name(i) != NULL; i++) {
     212             :                 uint32_t policy_value;
     213           0 :                 if (!account_policy_get_default(i, &policy_value)) {
     214           0 :                         fprintf(stderr, "Can't get default account policy\n");
     215           0 :                         return -1;
     216             :                 }
     217           0 :                 if (!account_policy_set(i, policy_value)) {
     218           0 :                         fprintf(stderr, "Can't set account policy in tdb\n");
     219           0 :                         return -1;
     220             :                 }
     221             :         }
     222             : 
     223           0 :         return 0;
     224             : }
     225             : 
     226             : 
     227             : /*********************************************************
     228             :  Add all currently available account policy from tdb to one backend
     229             :  ********************************************************/
     230             : 
     231           0 : static int export_account_policies (struct pdb_methods *in, struct pdb_methods *out) 
     232             : {
     233             :         int i;
     234             : 
     235           0 :         for ( i=1; decode_account_policy_name(i) != NULL; i++ ) {
     236             :                 uint32_t policy_value;
     237             :                 NTSTATUS status;
     238             : 
     239           0 :                 status = in->get_account_policy(in, i, &policy_value);
     240             : 
     241           0 :                 if ( NT_STATUS_IS_ERR(status) ) {
     242           0 :                         fprintf(stderr, "Unable to get account policy from %s\n", in->name);
     243           0 :                         return -1;
     244             :                 }
     245             : 
     246           0 :                 status = out->set_account_policy(out, i, policy_value);
     247             : 
     248           0 :                 if ( NT_STATUS_IS_ERR(status) ) {
     249           0 :                         fprintf(stderr, "Unable to migrate account policy to %s\n", out->name);
     250           0 :                         return -1;
     251             :                 }
     252             :         }
     253             : 
     254           0 :         return 0;
     255             : }
     256             : 
     257             : 
     258             : /*********************************************************
     259             :  Print info from sam structure
     260             : **********************************************************/
     261             : 
     262           3 : static int print_sam_info (struct samu *sam_pwent, bool verbosity, bool smbpwdstyle)
     263             : {
     264             :         uid_t uid;
     265             :         time_t tmp;
     266             : 
     267             :         /* TODO: check if entry is a user or a workstation */
     268           3 :         if (!sam_pwent) return -1;
     269             : 
     270           3 :         if (verbosity) {
     271             :                 char temp[44];
     272             :                 const uint8_t *hours;
     273             :                 struct dom_sid_buf buf;
     274             : 
     275           3 :                 printf ("Unix username:        %s\n", pdb_get_username(sam_pwent));
     276           3 :                 printf ("NT username:          %s\n", pdb_get_nt_username(sam_pwent));
     277           3 :                 printf ("Account Flags:        %s\n", pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent), NEW_PW_FORMAT_SPACE_PADDED_LEN));
     278           3 :                 printf ("User SID:             %s\n",
     279             :                         dom_sid_str_buf(pdb_get_user_sid(sam_pwent), &buf));
     280           3 :                 printf ("Primary Group SID:    %s\n",
     281             :                         dom_sid_str_buf(pdb_get_group_sid(sam_pwent), &buf));
     282           3 :                 printf ("Full Name:            %s\n", pdb_get_fullname(sam_pwent));
     283           3 :                 printf ("Home Directory:       %s\n", pdb_get_homedir(sam_pwent));
     284           3 :                 printf ("HomeDir Drive:        %s\n", pdb_get_dir_drive(sam_pwent));
     285           3 :                 printf ("Logon Script:         %s\n", pdb_get_logon_script(sam_pwent));
     286           3 :                 printf ("Profile Path:         %s\n", pdb_get_profile_path(sam_pwent));
     287           3 :                 printf ("Domain:               %s\n", pdb_get_domain(sam_pwent));
     288           3 :                 printf ("Account desc:         %s\n", pdb_get_acct_desc(sam_pwent));
     289           3 :                 printf ("Workstations:         %s\n", pdb_get_workstations(sam_pwent));
     290           3 :                 printf ("Munged dial:          %s\n", pdb_get_munged_dial(sam_pwent));
     291             : 
     292           3 :                 tmp = pdb_get_logon_time(sam_pwent);
     293           5 :                 printf ("Logon time:           %s\n",
     294           2 :                                 tmp ? http_timestring(talloc_tos(), tmp) : "0");
     295             : 
     296           3 :                 tmp = pdb_get_logoff_time(sam_pwent);
     297           3 :                 printf ("Logoff time:          %s\n",
     298           0 :                                 tmp ? http_timestring(talloc_tos(), tmp) : "0");
     299             : 
     300           3 :                 tmp = pdb_get_kickoff_time(sam_pwent);
     301           6 :                 printf ("Kickoff time:         %s\n",
     302           3 :                                 tmp ? http_timestring(talloc_tos(), tmp) : "0");
     303             : 
     304           3 :                 tmp = pdb_get_pass_last_set_time(sam_pwent);
     305           6 :                 printf ("Password last set:    %s\n",
     306           3 :                                 tmp ? http_timestring(talloc_tos(), tmp) : "0");
     307             : 
     308           3 :                 tmp = pdb_get_pass_can_change_time(sam_pwent);
     309           6 :                 printf ("Password can change:  %s\n",
     310           3 :                                 tmp ? http_timestring(talloc_tos(), tmp) : "0");
     311             : 
     312           3 :                 tmp = pdb_get_pass_must_change_time(sam_pwent);
     313           6 :                 printf ("Password must change: %s\n",
     314           3 :                                 tmp ? http_timestring(talloc_tos(), tmp) : "0");
     315             : 
     316           3 :                 tmp = pdb_get_bad_password_time(sam_pwent);
     317           3 :                 printf ("Last bad password   : %s\n",
     318           0 :                                 tmp ? http_timestring(talloc_tos(), tmp) : "0");
     319           3 :                 printf ("Bad password count  : %d\n",
     320           3 :                         pdb_get_bad_password_count(sam_pwent));
     321             : 
     322           3 :                 hours = pdb_get_hours(sam_pwent);
     323           3 :                 pdb_sethexhours(temp, hours);
     324           3 :                 printf ("Logon hours         : %s\n", temp);
     325           3 :                 if (smbpwdstyle){
     326           1 :                         pdb_sethexpwd(temp, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
     327           1 :                         printf ("LM hash             : %s\n", temp);
     328           1 :                         pdb_sethexpwd(temp, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
     329           1 :                         printf ("NT hash             : %s\n", temp);
     330             :                 }       
     331             : 
     332           0 :         } else if (smbpwdstyle) {
     333             :                 char lm_passwd[33];
     334             :                 char nt_passwd[33];
     335             : 
     336           0 :                 uid = nametouid(pdb_get_username(sam_pwent));
     337           0 :                 pdb_sethexpwd(lm_passwd, pdb_get_lanman_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
     338           0 :                 pdb_sethexpwd(nt_passwd, pdb_get_nt_passwd(sam_pwent), pdb_get_acct_ctrl(sam_pwent));
     339             : 
     340           0 :                 printf("%s:%lu:%s:%s:%s:LCT-%08X:\n",
     341             :                        pdb_get_username(sam_pwent),
     342             :                        (unsigned long)uid,
     343             :                        lm_passwd,
     344             :                        nt_passwd,
     345             :                        pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
     346           0 :                        (uint32_t)convert_time_t_to_uint32_t(pdb_get_pass_last_set_time(sam_pwent)));
     347             :         } else {
     348           0 :                 uid = nametouid(pdb_get_username(sam_pwent));
     349           0 :                 printf ("%s:%lu:%s\n", pdb_get_username(sam_pwent), (unsigned long)uid,
     350             :                         pdb_get_fullname(sam_pwent));
     351             :         }
     352             : 
     353           3 :         return 0;
     354             : }
     355             : 
     356             : /*********************************************************
     357             :  Get an Print User Info
     358             : **********************************************************/
     359             : 
     360           3 : static int print_user_info(const char *username,
     361             :                            bool verbosity, bool smbpwdstyle)
     362             : {
     363           3 :         struct samu *sam_pwent = NULL;
     364             :         bool bret;
     365             :         int ret;
     366             : 
     367           3 :         sam_pwent = samu_new(NULL);
     368           3 :         if (!sam_pwent) {
     369           0 :                 return -1;
     370             :         }
     371             : 
     372           3 :         bret = pdb_getsampwnam(sam_pwent, username);
     373           3 :         if (!bret) {
     374           0 :                 fprintf (stderr, "Username not found!\n");
     375           0 :                 TALLOC_FREE(sam_pwent);
     376           0 :                 return -1;
     377             :         }
     378             : 
     379           3 :         ret = print_sam_info(sam_pwent, verbosity, smbpwdstyle);
     380             : 
     381           3 :         TALLOC_FREE(sam_pwent);
     382           3 :         return ret;
     383             : }
     384             : 
     385             : /*********************************************************
     386             :  List Users
     387             : **********************************************************/
     388           0 : static int print_users_list(bool verbosity, bool smbpwdstyle)
     389             : {
     390             :         struct pdb_search *u_search;
     391             :         struct samr_displayentry userentry;
     392             :         struct samu *sam_pwent;
     393             :         TALLOC_CTX *tosctx;
     394             :         struct dom_sid user_sid;
     395             :         bool bret;
     396             :         int ret;
     397             : 
     398           0 :         tosctx = talloc_tos();
     399           0 :         if (!tosctx) {
     400           0 :                 DEBUG(0, ("talloc failed\n"));
     401           0 :                 return 1;
     402             :         }
     403             : 
     404           0 :         u_search = pdb_search_users(tosctx, 0);
     405           0 :         if (!u_search) {
     406           0 :                 DEBUG(0, ("User Search failed!\n"));
     407           0 :                 ret = 1;
     408           0 :                 goto done;
     409             :         }
     410             : 
     411           0 :         while (u_search->next_entry(u_search, &userentry)) {
     412             : 
     413           0 :                 sam_pwent = samu_new(tosctx);
     414           0 :                 if (sam_pwent == NULL) {
     415           0 :                         DEBUG(0, ("talloc failed\n"));
     416           0 :                         ret = 1;
     417           0 :                         goto done;
     418             :                 }
     419             : 
     420           0 :                 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
     421             : 
     422           0 :                 bret = pdb_getsampwsid(sam_pwent, &user_sid);
     423           0 :                 if (!bret) {
     424           0 :                         DEBUG(2, ("getsampwsid failed\n"));
     425           0 :                         TALLOC_FREE(sam_pwent);
     426           0 :                         continue;
     427             :                 }
     428             : 
     429           0 :                 if (verbosity) {
     430           0 :                         printf ("---------------\n");
     431             :                 }
     432           0 :                 print_sam_info(sam_pwent, verbosity, smbpwdstyle);
     433           0 :                 TALLOC_FREE(sam_pwent);
     434             :         }
     435             : 
     436           0 :         ret = 0;
     437             : 
     438           0 : done:
     439           0 :         TALLOC_FREE(tosctx);
     440           0 :         return ret;
     441             : }
     442             : 
     443             : /*********************************************************
     444             :  Fix a list of Users for uninitialised passwords
     445             : **********************************************************/
     446           0 : static int fix_users_list(void)
     447             : {
     448             :         struct pdb_search *u_search;
     449             :         struct samr_displayentry userentry;
     450             :         struct samu *sam_pwent;
     451             :         TALLOC_CTX *tosctx;
     452             :         struct dom_sid user_sid;
     453             :         NTSTATUS status;
     454             :         bool bret;
     455             :         int ret;
     456             : 
     457           0 :         tosctx = talloc_tos();
     458           0 :         if (!tosctx) {
     459           0 :                 fprintf(stderr, "Out of memory!\n");
     460           0 :                 return 1;
     461             :         }
     462             : 
     463           0 :         u_search = pdb_search_users(tosctx, 0);
     464           0 :         if (!u_search) {
     465           0 :                 fprintf(stderr, "User Search failed!\n");
     466           0 :                 ret = 1;
     467           0 :                 goto done;
     468             :         }
     469             : 
     470           0 :         while (u_search->next_entry(u_search, &userentry)) {
     471             : 
     472           0 :                 sam_pwent = samu_new(tosctx);
     473           0 :                 if (sam_pwent == NULL) {
     474           0 :                         fprintf(stderr, "Out of memory!\n");
     475           0 :                         ret = 1;
     476           0 :                         goto done;
     477             :                 }
     478             : 
     479           0 :                 sid_compose(&user_sid, get_global_sam_sid(), userentry.rid);
     480             : 
     481           0 :                 bret = pdb_getsampwsid(sam_pwent, &user_sid);
     482           0 :                 if (!bret) {
     483           0 :                         DEBUG(2, ("getsampwsid failed\n"));
     484           0 :                         TALLOC_FREE(sam_pwent);
     485           0 :                         continue;
     486             :                 }
     487             : 
     488           0 :                 status = pdb_update_sam_account(sam_pwent);
     489           0 :                 if (!NT_STATUS_IS_OK(status)) {
     490           0 :                         printf("Update of user %s failed!\n",
     491             :                                pdb_get_username(sam_pwent));
     492             :                 }
     493           0 :                 TALLOC_FREE(sam_pwent);
     494             :         }
     495             : 
     496           0 :         ret = 0;
     497             : 
     498           0 : done:
     499           0 :         TALLOC_FREE(tosctx);
     500           0 :         return ret;
     501             : }
     502             : 
     503             : /*********************************************************
     504             :  Set User Info
     505             : **********************************************************/
     506             : 
     507           2 : static int set_user_info(const char *username, const char *fullname,
     508             :                          const char *homedir, const char *acct_desc,
     509             :                          const char *drive, const char *script,
     510             :                          const char *profile, const char *account_control,
     511             :                          const char *user_sid, const char *user_domain,
     512             :                          const bool badpw, const bool hours,
     513             :                          const char *kickoff_time, const char *str_hex_pwd)
     514             : {
     515           2 :         bool updated_autolock = False, updated_badpw = False;
     516             :         struct samu *sam_pwent;
     517             :         uint8_t hours_array[MAX_HOURS_LEN];
     518             :         uint32_t hours_len;
     519             :         uint32_t acb_flags;
     520             :         uint32_t not_settable;
     521             :         uint32_t new_flags;
     522             :         struct dom_sid u_sid;
     523             :         bool ret;
     524             : 
     525           2 :         sam_pwent = samu_new(NULL);
     526           2 :         if (!sam_pwent) {
     527           0 :                 return 1;
     528             :         }
     529             : 
     530           2 :         ret = pdb_getsampwnam(sam_pwent, username);
     531           2 :         if (!ret) {
     532           0 :                 fprintf (stderr, "Username not found!\n");
     533           0 :                 TALLOC_FREE(sam_pwent);
     534           0 :                 return -1;
     535             :         }
     536             : 
     537           2 :         if (hours) {
     538           0 :                 hours_len = pdb_get_hours_len(sam_pwent);
     539           0 :                 memset(hours_array, 0xff, hours_len);
     540             : 
     541           0 :                 pdb_set_hours(sam_pwent, hours_array, hours_len, PDB_CHANGED);
     542             :         }
     543             : 
     544           2 :         if (!pdb_update_autolock_flag(sam_pwent, &updated_autolock)) {
     545           0 :                 DEBUG(2,("pdb_update_autolock_flag failed.\n"));
     546             :         }
     547             : 
     548           2 :         if (!pdb_update_bad_password_count(sam_pwent, &updated_badpw)) {
     549           0 :                 DEBUG(2,("pdb_update_bad_password_count failed.\n"));
     550             :         }
     551             : 
     552           2 :         if (fullname)
     553           0 :                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
     554           2 :         if (acct_desc)
     555           0 :                 pdb_set_acct_desc(sam_pwent, acct_desc, PDB_CHANGED);
     556           2 :         if (homedir)
     557           0 :                 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
     558           2 :         if (drive)
     559           1 :                 pdb_set_dir_drive(sam_pwent,drive, PDB_CHANGED);
     560           2 :         if (script)
     561           0 :                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
     562           2 :         if (profile)
     563           0 :                 pdb_set_profile_path (sam_pwent, profile, PDB_CHANGED);
     564           2 :         if (user_domain)
     565           0 :                 pdb_set_domain(sam_pwent, user_domain, PDB_CHANGED);
     566             : 
     567           2 :         if (account_control) {
     568           0 :                 not_settable = ~(ACB_DISABLED | ACB_HOMDIRREQ |
     569             :                                  ACB_PWNOTREQ | ACB_PWNOEXP | ACB_AUTOLOCK);
     570             : 
     571           0 :                 new_flags = pdb_decode_acct_ctrl(account_control);
     572             : 
     573           0 :                 if (new_flags & not_settable) {
     574           0 :                         fprintf(stderr, "Can only set [NDHLX] flags\n");
     575           0 :                         TALLOC_FREE(sam_pwent);
     576           0 :                         return -1;
     577             :                 }
     578             : 
     579           0 :                 acb_flags = pdb_get_acct_ctrl(sam_pwent);
     580             : 
     581           0 :                 pdb_set_acct_ctrl(sam_pwent,
     582           0 :                                   (acb_flags & not_settable) | new_flags,
     583             :                                   PDB_CHANGED);
     584             :         }
     585           2 :         if (user_sid) {
     586           0 :                 if (get_sid_from_cli_string(&u_sid, user_sid)) {
     587           0 :                         fprintf(stderr, "Failed to parse SID\n");
     588           0 :                         return -1;
     589             :                 }
     590           0 :                 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
     591             :         }
     592             : 
     593           2 :         if (badpw) {
     594           0 :                 pdb_set_bad_password_count(sam_pwent, 0, PDB_CHANGED);
     595           0 :                 pdb_set_bad_password_time(sam_pwent, 0, PDB_CHANGED);
     596             :         }
     597             : 
     598           2 :         if (kickoff_time) {
     599           0 :                 time_t value = get_time_t_max();
     600             : 
     601           0 :                 if (strcmp(kickoff_time, "never") != 0) {
     602           0 :                         int error = 0;
     603             :                         uint32_t num;
     604             : 
     605           0 :                         num = smb_strtoul(kickoff_time,
     606             :                                           NULL,
     607             :                                           10,
     608             :                                           &error,
     609             :                                           SMB_STR_FULL_STR_CONV);
     610           0 :                         if (error != 0) {
     611           0 :                                 fprintf(stderr, "Failed to parse kickoff time\n");
     612           0 :                                 return -1;
     613             :                         }
     614             : 
     615           0 :                         value = convert_uint32_t_to_time_t(num);
     616             :                 }
     617             : 
     618           0 :                 pdb_set_kickoff_time(sam_pwent, value, PDB_CHANGED);
     619             :         }
     620           2 :         if (str_hex_pwd) {
     621             :                 unsigned char  new_nt_p16[NT_HASH_LEN];
     622           1 :                 if(strlen(str_hex_pwd) != (NT_HASH_LEN *2)){
     623           0 :                         fprintf(stderr, "Invalid hash\n");
     624           0 :                         return -1;
     625             :                 }
     626             :                                          
     627           1 :                 pdb_gethexpwd(str_hex_pwd, new_nt_p16);
     628             :                 
     629           1 :                 if (!pdb_set_nt_passwd (sam_pwent, new_nt_p16 , PDB_CHANGED)) {
     630           0 :                         fprintf(stderr, "Failed to set password from nt-hash\n");
     631           0 :                         return -1;
     632             :                 }       
     633             : 
     634           1 :                 if (!pdb_set_pass_last_set_time (sam_pwent, time(NULL), PDB_CHANGED)){
     635           0 :                         fprintf(stderr, "Failed to set last password set time\n");
     636           0 :                         return -1;
     637             :                 }       
     638           1 :                 if (!pdb_update_history(sam_pwent, new_nt_p16)){
     639           0 :                         fprintf(stderr, "Failed to update password history\n");
     640           0 :                         return -1;
     641             :                 }
     642             :         }
     643             : 
     644           2 :         if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
     645             :                 
     646           2 :                 print_user_info(username, True, (str_hex_pwd != NULL ));
     647             :         } else {
     648           0 :                 fprintf (stderr, "Unable to modify entry!\n");
     649           0 :                 TALLOC_FREE(sam_pwent);
     650           0 :                 return -1;
     651             :         }
     652           2 :         TALLOC_FREE(sam_pwent);
     653           2 :         return 0;
     654             : }
     655             : 
     656           0 : static int set_machine_info(const char *machinename,
     657             :                             const char *account_control,
     658             :                             const char *machine_sid)
     659             : {
     660           0 :         struct samu *sam_pwent = NULL;
     661             :         TALLOC_CTX *tosctx;
     662             :         uint32_t acb_flags;
     663             :         uint32_t not_settable;
     664             :         uint32_t new_flags;
     665             :         struct dom_sid m_sid;
     666             :         char *name;
     667             :         int len;
     668             :         bool ret;
     669             : 
     670           0 :         len = strlen(machinename);
     671           0 :         if (len == 0) {
     672           0 :                 fprintf(stderr, "No machine name given\n");
     673           0 :                 return -1;
     674             :         }
     675             : 
     676           0 :         tosctx = talloc_tos();
     677           0 :         if (!tosctx) {
     678           0 :                 fprintf(stderr, "Out of memory!\n");
     679           0 :                 return -1;
     680             :         }
     681             : 
     682           0 :         sam_pwent = samu_new(tosctx);
     683           0 :         if (!sam_pwent) {
     684           0 :                 return 1;
     685             :         }
     686             : 
     687           0 :         if (machinename[len-1] == '$') {
     688           0 :                 name = talloc_strdup(sam_pwent, machinename);
     689             :         } else {
     690           0 :                 name = talloc_asprintf(sam_pwent, "%s$", machinename);
     691             :         }
     692           0 :         if (!name) {
     693           0 :                 fprintf(stderr, "Out of memory!\n");
     694           0 :                 TALLOC_FREE(sam_pwent);
     695           0 :                 return -1;
     696             :         }
     697             : 
     698           0 :         if (!strlower_m(name)) {
     699           0 :                 fprintf(stderr, "strlower_m %s failed\n", name);
     700           0 :                 TALLOC_FREE(sam_pwent);
     701           0 :                 return -1;
     702             :         }
     703             : 
     704           0 :         ret = pdb_getsampwnam(sam_pwent, name);
     705           0 :         if (!ret) {
     706           0 :                 fprintf (stderr, "Username not found!\n");
     707           0 :                 TALLOC_FREE(sam_pwent);
     708           0 :                 return -1;
     709             :         }
     710             : 
     711           0 :         if (account_control) {
     712           0 :                 not_settable = ~(ACB_DISABLED);
     713             : 
     714           0 :                 new_flags = pdb_decode_acct_ctrl(account_control);
     715             : 
     716           0 :                 if (new_flags & not_settable) {
     717           0 :                         fprintf(stderr, "Can only set [D] flags\n");
     718           0 :                         TALLOC_FREE(sam_pwent);
     719           0 :                         return -1;
     720             :                 }
     721             : 
     722           0 :                 acb_flags = pdb_get_acct_ctrl(sam_pwent);
     723             : 
     724           0 :                 pdb_set_acct_ctrl(sam_pwent,
     725           0 :                                   (acb_flags & not_settable) | new_flags,
     726             :                                   PDB_CHANGED);
     727             :         }
     728           0 :         if (machine_sid) {
     729           0 :                 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
     730           0 :                         fprintf(stderr, "Failed to parse SID\n");
     731           0 :                         return -1;
     732             :                 }
     733           0 :                 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
     734             :         }
     735             : 
     736           0 :         if (NT_STATUS_IS_OK(pdb_update_sam_account(sam_pwent))) {
     737           0 :                 print_user_info(name, True, False);
     738             :         } else {
     739           0 :                 fprintf (stderr, "Unable to modify entry!\n");
     740           0 :                 TALLOC_FREE(sam_pwent);
     741           0 :                 return -1;
     742             :         }
     743           0 :         TALLOC_FREE(sam_pwent);
     744           0 :         return 0;
     745             : }
     746             : 
     747             : /*********************************************************
     748             :  Add New User
     749             : **********************************************************/
     750           1 : static int new_user(const char *username, const char *fullname,
     751             :                     const char *homedir, const char *drive,
     752             :                     const char *script, const char *profile,
     753             :                     char *user_sid, bool stdin_get)
     754             : {
     755           1 :         char *pwd1 = NULL, *pwd2 = NULL;
     756           1 :         char *err = NULL, *msg = NULL;
     757           1 :         struct samu *sam_pwent = NULL;
     758             :         TALLOC_CTX *tosctx;
     759             :         NTSTATUS status;
     760             :         struct dom_sid u_sid;
     761             :         int flags;
     762           1 :         int ret = -1;
     763             : 
     764           1 :         tosctx = talloc_tos();
     765           1 :         if (!tosctx) {
     766           0 :                 fprintf(stderr, "Out of memory!\n");
     767           0 :                 return -1;
     768             :         }
     769             : 
     770           1 :         if (user_sid) {
     771           0 :                 if (get_sid_from_cli_string(&u_sid, user_sid)) {
     772           0 :                         fprintf(stderr, "Failed to parse SID\n");
     773           0 :                         return -1;
     774             :                 }
     775             :         }
     776             : 
     777           1 :         pwd1 = get_pass( "new password:", stdin_get);
     778           1 :         if (pwd1 == NULL) {
     779           0 :                 fprintf(stderr, "Failed to read passwords.\n");
     780           0 :                 goto done;
     781             :         }
     782           1 :         pwd2 = get_pass( "retype new password:", stdin_get);
     783           1 :         if (pwd2 == NULL) {
     784           0 :                 fprintf(stderr, "Failed to read passwords.\n");
     785           0 :                 goto done;
     786             :         }
     787           1 :         ret = strcmp(pwd1, pwd2);
     788           1 :         if (ret != 0) {
     789           0 :                 fprintf (stderr, "Passwords do not match!\n");
     790           0 :                 goto done;
     791             :         }
     792             : 
     793           1 :         flags = LOCAL_ADD_USER | LOCAL_SET_PASSWORD;
     794             : 
     795           1 :         status = local_password_change(username, flags, pwd1, &err, &msg);
     796           1 :         if (!NT_STATUS_IS_OK(status)) {
     797           0 :                 if (err) fprintf(stderr, "%s", err);
     798           0 :                 ret = -1;
     799           0 :                 goto done;
     800             :         }
     801             : 
     802           1 :         sam_pwent = samu_new(tosctx);
     803           1 :         if (!sam_pwent) {
     804           0 :                 fprintf(stderr, "Out of memory!\n");
     805           0 :                 ret = -1;
     806           0 :                 goto done;
     807             :         }
     808             : 
     809           1 :         if (!pdb_getsampwnam(sam_pwent, username)) {
     810           0 :                 fprintf(stderr, "User %s not found!\n", username);
     811           0 :                 ret = -1;
     812           0 :                 goto done;
     813             :         }
     814             : 
     815           1 :         if (fullname)
     816           0 :                 pdb_set_fullname(sam_pwent, fullname, PDB_CHANGED);
     817           1 :         if (homedir)
     818           0 :                 pdb_set_homedir(sam_pwent, homedir, PDB_CHANGED);
     819           1 :         if (drive)
     820           0 :                 pdb_set_dir_drive(sam_pwent, drive, PDB_CHANGED);
     821           1 :         if (script)
     822           0 :                 pdb_set_logon_script(sam_pwent, script, PDB_CHANGED);
     823           1 :         if (profile)
     824           0 :                 pdb_set_profile_path(sam_pwent, profile, PDB_CHANGED);
     825           1 :         if (user_sid)
     826           0 :                 pdb_set_user_sid(sam_pwent, &u_sid, PDB_CHANGED);
     827             : 
     828           1 :         status = pdb_update_sam_account(sam_pwent);
     829           1 :         if (!NT_STATUS_IS_OK(status)) {
     830           0 :                 fprintf(stderr,
     831             :                         "Failed to modify entry for user %s.!\n",
     832             :                         username);
     833           0 :                 ret = -1;
     834           0 :                 goto done;
     835             :         }
     836             : 
     837           1 :         print_user_info(username, True, False);
     838           1 :         ret = 0;
     839             : 
     840           1 : done:
     841           1 :         if (pwd1) memset(pwd1, 0, strlen(pwd1));
     842           1 :         if (pwd2) memset(pwd2, 0, strlen(pwd2));
     843           1 :         SAFE_FREE(pwd1);
     844           1 :         SAFE_FREE(pwd2);
     845           1 :         SAFE_FREE(err);
     846           1 :         SAFE_FREE(msg);
     847           1 :         TALLOC_FREE(sam_pwent);
     848           1 :         return ret;
     849             : }
     850             : 
     851             : /*********************************************************
     852             :  Add New Machine
     853             : **********************************************************/
     854             : 
     855           0 : static int new_machine(const char *machinename, char *machine_sid)
     856             : {
     857           0 :         char *err = NULL, *msg = NULL;
     858           0 :         struct samu *sam_pwent = NULL;
     859             :         TALLOC_CTX *tosctx;
     860             :         NTSTATUS status;
     861             :         struct dom_sid m_sid;
     862             :         char *compatpwd;
     863             :         char *name;
     864             :         int flags;
     865             :         int len;
     866             :         int ret;
     867             : 
     868           0 :         len = strlen(machinename);
     869           0 :         if (len == 0) {
     870           0 :                 fprintf(stderr, "No machine name given\n");
     871           0 :                 return -1;
     872             :         }
     873             : 
     874           0 :         tosctx = talloc_tos();
     875           0 :         if (!tosctx) {
     876           0 :                 fprintf(stderr, "Out of memory!\n");
     877           0 :                 return -1;
     878             :         }
     879             : 
     880           0 :         if (machine_sid) {
     881           0 :                 if (get_sid_from_cli_string(&m_sid, machine_sid)) {
     882           0 :                         fprintf(stderr, "Failed to parse SID\n");
     883           0 :                         return -1;
     884             :                 }
     885             :         }
     886             : 
     887           0 :         compatpwd = talloc_strdup(tosctx, machinename);
     888           0 :         if (!compatpwd) {
     889           0 :                 fprintf(stderr, "Out of memory!\n");
     890           0 :                 return -1;
     891             :         }
     892             : 
     893           0 :         if (machinename[len-1] == '$') {
     894           0 :                 name = talloc_strdup(tosctx, machinename);
     895           0 :                 compatpwd[len-1] = '\0';
     896             :         } else {
     897           0 :                 name = talloc_asprintf(tosctx, "%s$", machinename);
     898             :         }
     899           0 :         if (!name) {
     900           0 :                 fprintf(stderr, "Out of memory!\n");
     901           0 :                 return -1;
     902             :         }
     903             : 
     904           0 :         if (!strlower_m(name)) {
     905           0 :                 fprintf(stderr, "strlower_m %s failed\n", name);
     906           0 :                 return -1;
     907             :         }
     908             : 
     909           0 :         flags = LOCAL_ADD_USER | LOCAL_TRUST_ACCOUNT | LOCAL_SET_PASSWORD;
     910             : 
     911           0 :         status = local_password_change(name, flags, compatpwd, &err, &msg);
     912             : 
     913           0 :         if (!NT_STATUS_IS_OK(status)) {
     914           0 :                 if (err) fprintf(stderr, "%s", err);
     915           0 :                 ret = -1;
     916             :         }
     917             : 
     918           0 :         sam_pwent = samu_new(tosctx);
     919           0 :         if (!sam_pwent) {
     920           0 :                 fprintf(stderr, "Out of memory!\n");
     921           0 :                 ret = -1;
     922           0 :                 goto done;
     923             :         }
     924             : 
     925           0 :         if (!pdb_getsampwnam(sam_pwent, name)) {
     926           0 :                 fprintf(stderr, "Machine %s not found!\n", name);
     927           0 :                 ret = -1;
     928           0 :                 goto done;
     929             :         }
     930             : 
     931           0 :         if (machine_sid)
     932           0 :                 pdb_set_user_sid(sam_pwent, &m_sid, PDB_CHANGED);
     933             : 
     934           0 :         status = pdb_update_sam_account(sam_pwent);
     935           0 :         if (!NT_STATUS_IS_OK(status)) {
     936           0 :                 fprintf(stderr,
     937             :                         "Failed to modify entry for %s.!\n", name);
     938           0 :                 ret = -1;
     939           0 :                 goto done;
     940             :         }
     941             : 
     942           0 :         print_user_info(name, True, False);
     943           0 :         ret = 0;
     944             : 
     945           0 : done:
     946           0 :         SAFE_FREE(err);
     947           0 :         SAFE_FREE(msg);
     948           0 :         TALLOC_FREE(sam_pwent);
     949           0 :         return ret;
     950             : }
     951             : 
     952             : /*********************************************************
     953             :  Delete user entry
     954             : **********************************************************/
     955             : 
     956           1 : static int delete_user_entry(const char *username)
     957             : {
     958             :         struct samu *samaccount;
     959             : 
     960           1 :         samaccount = samu_new(NULL);
     961           1 :         if (!samaccount) {
     962           0 :                 fprintf(stderr, "Out of memory!\n");
     963           0 :                 return -1;
     964             :         }
     965             : 
     966           1 :         if (!pdb_getsampwnam(samaccount, username)) {
     967           0 :                 fprintf (stderr,
     968             :                          "user %s does not exist in the passdb\n", username);
     969           0 :                 TALLOC_FREE(samaccount);
     970           0 :                 return -1;
     971             :         }
     972             : 
     973           1 :         if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
     974           0 :                 fprintf (stderr, "Unable to delete user %s\n", username);
     975           0 :                 TALLOC_FREE(samaccount);
     976           0 :                 return -1;
     977             :         }
     978             : 
     979           1 :         TALLOC_FREE(samaccount);
     980           1 :         return 0;
     981             : }
     982             : 
     983             : /*********************************************************
     984             :  Delete machine entry
     985             : **********************************************************/
     986             : 
     987           0 : static int delete_machine_entry(const char *machinename)
     988             : {
     989           0 :         struct samu *samaccount = NULL;
     990             :         const char *name;
     991             : 
     992           0 :         if (strlen(machinename) == 0) {
     993           0 :                 fprintf(stderr, "No machine name given\n");
     994           0 :                 return -1;
     995             :         }
     996             : 
     997           0 :         samaccount = samu_new(NULL);
     998           0 :         if (!samaccount) {
     999           0 :                 fprintf(stderr, "Out of memory!\n");
    1000           0 :                 return -1;
    1001             :         }
    1002             : 
    1003           0 :         if (machinename[strlen(machinename)-1] != '$') {
    1004           0 :                 name = talloc_asprintf(samaccount, "%s$", machinename);
    1005             :         } else {
    1006           0 :                 name = machinename;
    1007             :         }
    1008             : 
    1009           0 :         if (!pdb_getsampwnam(samaccount, name)) {
    1010           0 :                 fprintf (stderr,
    1011             :                          "machine %s does not exist in the passdb\n", name);
    1012           0 :                 TALLOC_FREE(samaccount);
    1013           0 :                 return -1;
    1014             :         }
    1015             : 
    1016           0 :         if (!NT_STATUS_IS_OK(pdb_delete_sam_account(samaccount))) {
    1017           0 :                 fprintf (stderr, "Unable to delete machine %s\n", name);
    1018           0 :                 TALLOC_FREE(samaccount);
    1019           0 :                 return -1;
    1020             :         }
    1021             : 
    1022           0 :         TALLOC_FREE(samaccount);
    1023           0 :         return 0;
    1024             : }
    1025             : 
    1026             : /*********************************************************
    1027             :  Start here.
    1028             : **********************************************************/
    1029             : 
    1030           4 : int main(int argc, const char **argv)
    1031             : {
    1032             :         static int list_users = False;
    1033             :         static int verbose = False;
    1034             :         static int spstyle = False;
    1035             :         static int machine = False;
    1036             :         static int add_user = False;
    1037             :         static int delete_user = False;
    1038             :         static int modify_user = False;
    1039             :         uint32_t   setparms, checkparms;
    1040             :         int opt;
    1041             :         static char *full_name = NULL;
    1042             :         static char *acct_desc = NULL;
    1043             :         static const char *user_name = NULL;
    1044             :         static char *home_dir = NULL;
    1045             :         static char *home_drive = NULL;
    1046             :         static const char *backend = NULL;
    1047             :         static char *backend_in = NULL;
    1048             :         static char *backend_out = NULL;
    1049             :         static int transfer_groups = False;
    1050             :         static int transfer_account_policies = False;
    1051             :         static int reset_account_policies = False;
    1052             :         static int  force_initialised_password = False;
    1053             :         static char *logon_script = NULL;
    1054             :         static char *profile_path = NULL;
    1055             :         static char *user_domain = NULL;
    1056             :         static char *account_control = NULL;
    1057             :         static char *account_policy = NULL;
    1058             :         static char *user_sid = NULL;
    1059             :         static char *machine_sid = NULL;
    1060             :         static long int account_policy_value = 0;
    1061           4 :         bool account_policy_value_set = False;
    1062             :         static int badpw_reset = False;
    1063             :         static int hours_reset = False;
    1064             :         static char *pwd_time_format = NULL;
    1065             :         static int pw_from_stdin = False;
    1066             :         struct pdb_methods *bin, *bout;
    1067             :         static char *kickoff_time = NULL;
    1068             :         static char *str_hex_pwd = NULL;
    1069           4 :         TALLOC_CTX *frame = talloc_stackframe();
    1070             :         NTSTATUS status;
    1071             :         poptContext pc;
    1072             :         bool ok;
    1073          12 :         struct poptOption long_options[] = {
    1074             :                 POPT_AUTOHELP
    1075             :                 {"list",      'L', POPT_ARG_NONE, &list_users, 0, "list all users", NULL},
    1076             :                 {"verbose",   'v', POPT_ARG_NONE, &verbose, 0, "be verbose", NULL },
    1077             :                 {"smbpasswd-style",   'w',POPT_ARG_NONE, &spstyle, 0, "give output in smbpasswd style", NULL},
    1078             :                 {"user",      'u', POPT_ARG_STRING, &user_name, 0, "use username", "USER" },
    1079             :                 {"account-desc",      'N', POPT_ARG_STRING, &acct_desc, 0, "set account description", NULL},
    1080             :                 {"fullname",  'f', POPT_ARG_STRING, &full_name, 0, "set full name", NULL},
    1081             :                 {"homedir",   'h', POPT_ARG_STRING, &home_dir, 0, "set home directory", NULL},
    1082             :                 {"drive",     'D', POPT_ARG_STRING, &home_drive, 0, "set home drive", NULL},
    1083             :                 {"script",    'S', POPT_ARG_STRING, &logon_script, 0, "set logon script", NULL},
    1084             :                 {"profile",   'p', POPT_ARG_STRING, &profile_path, 0, "set profile path", NULL},
    1085             :                 {"domain",    'I', POPT_ARG_STRING, &user_domain, 0, "set a users' domain", NULL},
    1086             :                 {"user SID",  'U', POPT_ARG_STRING, &user_sid, 0, "set user SID or RID", NULL},
    1087             :                 {"machine SID",       'M', POPT_ARG_STRING, &machine_sid, 0, "set machine SID or RID", NULL},
    1088             :                 {"create",    'a', POPT_ARG_NONE, &add_user, 0, "create user", NULL},
    1089             :                 {"modify",    'r', POPT_ARG_NONE, &modify_user, 0, "modify user", NULL},
    1090             :                 {"machine",   'm', POPT_ARG_NONE, &machine, 0, "account is a machine account", NULL},
    1091             :                 {"delete",    'x', POPT_ARG_NONE, &delete_user, 0, "delete user", NULL},
    1092             :                 {"backend",   'b', POPT_ARG_STRING, &backend, 0, "use different passdb backend as default backend", NULL},
    1093             :                 {"import",    'i', POPT_ARG_STRING, &backend_in, 0, "import user accounts from this backend", NULL},
    1094             :                 {"export",    'e', POPT_ARG_STRING, &backend_out, 0, "export user accounts to this backend", NULL},
    1095             :                 {"group",     'g', POPT_ARG_NONE, &transfer_groups, 0, "use -i and -e for groups", NULL},
    1096             :                 {"policies",  'y', POPT_ARG_NONE, &transfer_account_policies, 0, "use -i and -e to move account policies between backends", NULL},
    1097             :                 {"policies-reset",    0, POPT_ARG_NONE, &reset_account_policies, 0, "restore default policies", NULL},
    1098             :                 {"account-policy",    'P', POPT_ARG_STRING, &account_policy, 0,"value of an account policy (like maximum password age)",NULL},
    1099             :                 {"value",       'C', POPT_ARG_LONG, &account_policy_value, 'C',"set the account policy to this value", NULL},
    1100             :                 {"account-control",   'c', POPT_ARG_STRING, &account_control, 0, "Values of account control", NULL},
    1101             :                 {"force-initialized-passwords", 0, POPT_ARG_NONE, &force_initialised_password, 0, "Force initialization of corrupt password strings in a passdb backend", NULL},
    1102             :                 {"bad-password-count-reset", 'z', POPT_ARG_NONE, &badpw_reset, 0, "reset bad password count", NULL},
    1103             :                 {"logon-hours-reset", 'Z', POPT_ARG_NONE, &hours_reset, 0, "reset logon hours", NULL},
    1104             :                 {"time-format", 0, POPT_ARG_STRING, &pwd_time_format, 0, "The time format for time parameters", NULL },
    1105             :                 {"password-from-stdin", 't', POPT_ARG_NONE, &pw_from_stdin, 0, "get password from standard in", NULL},
    1106             :                 {"kickoff-time", 'K', POPT_ARG_STRING, &kickoff_time, 0, "set the kickoff time", NULL},
    1107             :                 {"set-nt-hash", 0, POPT_ARG_STRING, &str_hex_pwd, 0, "set password from nt-hash", NULL},
    1108           4 :                 POPT_COMMON_SAMBA
    1109           4 :                 POPT_COMMON_VERSION
    1110             :                 POPT_TABLEEND
    1111             :         };
    1112             : 
    1113           4 :         bin = bout = NULL;
    1114             : 
    1115           4 :         smb_init_locale();
    1116             : 
    1117           4 :         ok = samba_cmdline_init(frame,
    1118             :                                 SAMBA_CMDLINE_CONFIG_CLIENT,
    1119             :                                 false /* require_smbconf */);
    1120           4 :         if (!ok) {
    1121           0 :                 DBG_ERR("Failed to init cmdline parser!\n");
    1122           0 :                 TALLOC_FREE(frame);
    1123           0 :                 exit(1);
    1124             :         }
    1125             : 
    1126           4 :         pc = samba_popt_get_context(getprogname(),
    1127             :                                     argc,
    1128             :                                     argv,
    1129             :                                     long_options,
    1130             :                                     POPT_CONTEXT_KEEP_FIRST);
    1131           4 :         if (pc == NULL) {
    1132           0 :                 DBG_ERR("Failed to setup popt context!\n");
    1133           0 :                 TALLOC_FREE(frame);
    1134           0 :                 exit(1);
    1135             :         }
    1136             : 
    1137           8 :         while((opt = poptGetNextOpt(pc)) != -1) {
    1138           0 :                 switch (opt) {
    1139           0 :                 case 'C':
    1140           0 :                         account_policy_value_set = True;
    1141           0 :                         break;
    1142           0 :                 case POPT_ERROR_BADOPT:
    1143           0 :                         fprintf(stderr, "\nInvalid option %s: %s\n\n",
    1144             :                                 poptBadOption(pc, 0), poptStrerror(opt));
    1145           0 :                         poptPrintUsage(pc, stderr, 0);
    1146           0 :                         exit(1);
    1147             :                 }
    1148             :         }
    1149             : 
    1150           4 :         poptGetArg(pc); /* Drop argv[0], the program name */
    1151             : 
    1152           4 :         if (user_name == NULL) {
    1153           3 :                 if (poptPeekArg(pc)) {
    1154           3 :                         user_name = talloc_strdup(frame, poptGetArg(pc));
    1155           3 :                         if (user_name == NULL) {
    1156           0 :                                 fprintf(stderr, "out of memory\n");
    1157           0 :                                 TALLOC_FREE(frame);
    1158           0 :                                 exit(1);
    1159             :                         }
    1160             :                 }
    1161             :         }
    1162             : 
    1163          12 :         setparms =      (backend ? BIT_BACKEND : 0) +
    1164           8 :                         (verbose ? BIT_VERBOSE : 0) +
    1165           8 :                         (spstyle ? BIT_SPSTYLE : 0) +
    1166           8 :                         (full_name ? BIT_FULLNAME : 0) +
    1167           8 :                         (home_dir ? BIT_HOMEDIR : 0) +
    1168           8 :                         (home_drive ? BIT_HDIRDRIVE : 0) +
    1169           8 :                         (logon_script ? BIT_LOGSCRIPT : 0) +
    1170           8 :                         (profile_path ? BIT_PROFILE : 0) +
    1171           8 :                         (user_domain ? BIT_USERDOMAIN : 0) +
    1172           8 :                         (machine ? BIT_MACHINE : 0) +
    1173           8 :                         (user_name ? BIT_USER : 0) +
    1174           8 :                         (list_users ? BIT_LIST : 0) +
    1175           8 :                         (force_initialised_password ? BIT_FIX_INIT : 0) +
    1176           8 :                         (user_sid ? BIT_USERSIDS : 0) +
    1177           8 :                         (machine_sid ? BIT_USERSIDS : 0) +
    1178           8 :                         (modify_user ? BIT_MODIFY : 0) +
    1179           8 :                         (add_user ? BIT_CREATE : 0) +
    1180           8 :                         (delete_user ? BIT_DELETE : 0) +
    1181           8 :                         (account_control ? BIT_ACCTCTRL : 0) +
    1182           8 :                         (account_policy ? BIT_ACCPOLICY : 0) +
    1183           8 :                         (account_policy_value_set ? BIT_ACCPOLVAL : 0) +
    1184           8 :                         (backend_in ? BIT_IMPORT : 0) +
    1185           8 :                         (backend_out ? BIT_EXPORT : 0) +
    1186           8 :                         (badpw_reset ? BIT_BADPWRESET : 0) +
    1187           8 :                         (hours_reset ? BIT_LOGONHOURS : 0) +
    1188           8 :                         (kickoff_time ? BIT_KICKOFFTIME : 0) +
    1189           4 :                         (str_hex_pwd ? BIT_PWSETNTHASH : 0 ) +
    1190           4 :                         (acct_desc ? BIT_DESCRIPTION : 0);
    1191             :                         
    1192             : 
    1193           4 :         if (setparms & BIT_BACKEND) {
    1194             :                 /* HACK: set the global passdb backend by overwriting globals.
    1195             :                  * This way we can use regular pdb functions for default
    1196             :                  * operations that do not involve passdb migrations */
    1197           0 :                 lp_set_cmdline("passdb backend", backend);
    1198             :         } else {
    1199           4 :                 backend = lp_passdb_backend();
    1200             :         }
    1201             : 
    1202           4 :         if (!initialize_password_db(False, NULL)) {
    1203           0 :                 fprintf(stderr, "Can't initialize passdb backend.\n");
    1204           0 :                 exit(1);
    1205             :         }
    1206             : 
    1207             :         /* the lowest bit options are always accepted */
    1208           4 :         checkparms = setparms & ~MASK_ALWAYS_GOOD;
    1209             : 
    1210           4 :         if (checkparms & BIT_FIX_INIT) {
    1211           0 :                 poptFreeContext(pc);
    1212           0 :                 return fix_users_list();
    1213             :         }
    1214             : 
    1215             :         /* account policy operations */
    1216           4 :         if ((checkparms & BIT_ACCPOLICY) && !(checkparms & ~(BIT_ACCPOLICY + BIT_ACCPOLVAL))) {
    1217             :                 uint32_t value;
    1218           0 :                 enum pdb_policy_type field = account_policy_name_to_typenum(account_policy);
    1219           0 :                 if (field == 0) {
    1220             :                         const char **names;
    1221             :                         int count;
    1222             :                         int i;
    1223           0 :                         account_policy_names_list(talloc_tos(), &names, &count);
    1224           0 :                         fprintf(stderr, "No account policy by that name!\n");
    1225           0 :                         if (count !=0) {
    1226           0 :                                 fprintf(stderr, "Account policy names are:\n");
    1227           0 :                                 for (i = 0; i < count ; i++) {
    1228           0 :                                         d_fprintf(stderr, "%s\n", names[i]);
    1229             :                                 }
    1230             :                         }
    1231           0 :                         TALLOC_FREE(names);
    1232           0 :                         exit(1);
    1233             :                 }
    1234           0 :                 if (!pdb_get_account_policy(field, &value)) {
    1235           0 :                         fprintf(stderr, "valid account policy, but unable to fetch value!\n");
    1236           0 :                         if (!account_policy_value_set)
    1237           0 :                                 exit(1);
    1238             :                 }
    1239           0 :                 printf("account policy \"%s\" description: %s\n", account_policy, account_policy_get_desc(field));
    1240           0 :                 if (account_policy_value_set) {
    1241           0 :                         printf("account policy \"%s\" value was: %u\n", account_policy, value);
    1242           0 :                         if (!pdb_set_account_policy(field, account_policy_value)) {
    1243           0 :                                 fprintf(stderr, "valid account policy, but unable to set value!\n");
    1244           0 :                                 exit(1);
    1245             :                         }
    1246           0 :                         printf("account policy \"%s\" value is now: %lu\n", account_policy, account_policy_value);
    1247           0 :                         exit(0);
    1248             :                 } else {
    1249           0 :                         printf("account policy \"%s\" value is: %u\n", account_policy, value);
    1250           0 :                         exit(0);
    1251             :                 }
    1252             :         }
    1253             : 
    1254           4 :         if (reset_account_policies) {
    1255           0 :                 if (reinit_account_policies()) {
    1256           0 :                         exit(1);
    1257             :                 }
    1258             : 
    1259           0 :                 exit(0);
    1260             :         }
    1261             : 
    1262             :         /* import and export operations */
    1263             : 
    1264           8 :         if (((checkparms & BIT_IMPORT) ||
    1265           4 :              (checkparms & BIT_EXPORT)) &&
    1266           0 :             !(checkparms & ~(BIT_IMPORT +BIT_EXPORT +BIT_USER))) {
    1267             : 
    1268           0 :                 poptFreeContext(pc);
    1269             : 
    1270           0 :                 if (backend_in) {
    1271           0 :                         status = make_pdb_method_name(&bin, backend_in);
    1272             :                 } else {
    1273           0 :                         status = make_pdb_method_name(&bin, backend);
    1274             :                 }
    1275             : 
    1276           0 :                 if (!NT_STATUS_IS_OK(status)) {
    1277           0 :                         fprintf(stderr, "Unable to initialize %s.\n",
    1278           0 :                                 backend_in ? backend_in : backend);
    1279           0 :                         return 1;
    1280             :                 }
    1281             : 
    1282           0 :                 if (backend_out) {
    1283           0 :                         status = make_pdb_method_name(&bout, backend_out);
    1284             :                 } else {
    1285           0 :                         status = make_pdb_method_name(&bout, backend);
    1286             :                 }
    1287             : 
    1288           0 :                 if (!NT_STATUS_IS_OK(status)) {
    1289           0 :                         fprintf(stderr, "Unable to initialize %s.\n",
    1290           0 :                                 backend_out ? backend_out : backend);
    1291           0 :                         return 1;
    1292             :                 }
    1293             : 
    1294           0 :                 if (transfer_account_policies) {
    1295             : 
    1296           0 :                         if (!(checkparms & BIT_USER)) {
    1297           0 :                                 return export_account_policies(bin, bout);
    1298             :                         }
    1299             : 
    1300           0 :                 } else  if (transfer_groups) {
    1301             : 
    1302           0 :                         if (!(checkparms & BIT_USER)) {
    1303           0 :                                 return export_groups(bin, bout);
    1304             :                         }
    1305             : 
    1306             :                 } else {
    1307           0 :                         return export_database(bin, bout,
    1308           0 :                                 (checkparms & BIT_USER) ? user_name : NULL);
    1309             :                 }
    1310             :         }
    1311             : 
    1312             :         /* if BIT_USER is defined but nothing else then threat it as -l -u for compatibility */
    1313             :         /* fake up BIT_LIST if only BIT_USER is defined */
    1314           4 :         if ((checkparms & BIT_USER) && !(checkparms & ~BIT_USER)) {
    1315           0 :                 checkparms += BIT_LIST;
    1316             :         }
    1317             : 
    1318             :         /* modify flag is optional to maintain backwards compatibility */
    1319             :         /* fake up BIT_MODIFY if BIT_USER  and at least one of MASK_USER_GOOD is defined */
    1320           4 :         if (!((checkparms & ~MASK_USER_GOOD) & ~BIT_USER) && (checkparms & MASK_USER_GOOD)) {
    1321           1 :                 checkparms += BIT_MODIFY;
    1322             :         }
    1323             : 
    1324             :         /* list users operations */
    1325           4 :         if (checkparms & BIT_LIST) {
    1326           0 :                 if (!(checkparms & ~BIT_LIST)) {
    1327           0 :                         poptFreeContext(pc);
    1328           0 :                         return print_users_list(verbose, spstyle);
    1329             :                 }
    1330           0 :                 if (!(checkparms & ~(BIT_USER + BIT_LIST))) {
    1331           0 :                         poptFreeContext(pc);
    1332           0 :                         return print_user_info(user_name, verbose, spstyle);
    1333             :                 }
    1334             :         }
    1335             : 
    1336             :         /* mask out users options */
    1337           4 :         checkparms &= ~MASK_USER_GOOD;
    1338             : 
    1339             :         /* if bad password count is reset, we must be modifying */
    1340           4 :         if (checkparms & BIT_BADPWRESET) {
    1341           0 :                 checkparms |= BIT_MODIFY;
    1342           0 :                 checkparms &= ~BIT_BADPWRESET;
    1343             :         }
    1344             : 
    1345             :         /* if logon hours is reset, must modify */
    1346           4 :         if (checkparms & BIT_LOGONHOURS) {
    1347           0 :                 checkparms |= BIT_MODIFY;
    1348           0 :                 checkparms &= ~BIT_LOGONHOURS;
    1349             :         }
    1350             : 
    1351             :         /* account operation */
    1352           4 :         if ((checkparms & BIT_CREATE) || (checkparms & BIT_MODIFY) || (checkparms & BIT_DELETE)) {
    1353             :                 /* check use of -u option */
    1354           4 :                 if (!(checkparms & BIT_USER)) {
    1355           0 :                         fprintf (stderr, "Username not specified! (use -u option)\n");
    1356           0 :                         poptFreeContext(pc);
    1357           0 :                         return -1;
    1358             :                 }
    1359             : 
    1360             :                 /* account creation operations */
    1361           4 :                 if (!(checkparms & ~(BIT_CREATE + BIT_USER + BIT_MACHINE))) {
    1362           1 :                         poptFreeContext(pc);
    1363           1 :                         if (checkparms & BIT_MACHINE) {
    1364           0 :                                 return new_machine(user_name, machine_sid);
    1365             :                         } else {
    1366           1 :                                 return new_user(user_name, full_name,
    1367             :                                                 home_dir, home_drive,
    1368             :                                                 logon_script, profile_path,
    1369             :                                                 user_sid, pw_from_stdin);
    1370             :                         }
    1371             :                 }
    1372             : 
    1373             :                 /* account deletion operations */
    1374           3 :                 if (!(checkparms & ~(BIT_DELETE + BIT_USER + BIT_MACHINE))) {
    1375           1 :                         poptFreeContext(pc);
    1376           1 :                         if (checkparms & BIT_MACHINE) {
    1377           0 :                                 return delete_machine_entry(user_name);
    1378             :                         } else {
    1379           1 :                                 return delete_user_entry(user_name);
    1380             :                         }
    1381             :                 }
    1382             : 
    1383             :                 /* account modification operations */
    1384           2 :                 if (!(checkparms & ~(BIT_MODIFY + BIT_USER + BIT_MACHINE))) {
    1385           2 :                         poptFreeContext(pc);
    1386           2 :                         if (checkparms & BIT_MACHINE) {
    1387           0 :                                 return set_machine_info(user_name,
    1388             :                                                         account_control,
    1389             :                                                         machine_sid);
    1390             :                         } else {
    1391           2 :                                 return set_user_info(user_name, full_name,
    1392             :                                                      home_dir, acct_desc,
    1393             :                                                      home_drive, logon_script,
    1394             :                                                      profile_path, account_control,
    1395             :                                                      user_sid, user_domain,
    1396             :                                                      badpw_reset, hours_reset,
    1397             :                                                      kickoff_time, str_hex_pwd);
    1398             :                         }
    1399             :                 }
    1400             :         }
    1401             : 
    1402           0 :         if (setparms >= 0x20) {
    1403           0 :                 fprintf (stderr, "Incompatible or insufficient options on command line!\n");
    1404             :         }
    1405           0 :         poptPrintHelp(pc, stderr, 0);
    1406             : 
    1407           0 :         poptFreeContext(pc);
    1408           0 :         TALLOC_FREE(frame);
    1409           0 :         return 1;
    1410             : }

Generated by: LCOV version 1.13