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