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