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