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