2 Unix SMB/CIFS implementation.
3 passdb editing frontend
5 Copyright (C) Simo Sorce 2000
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 extern pstring global_myname;
27 extern BOOL AllowDebugChange;
29 /*********************************************************
30 Add all currently available users to another db
31 ********************************************************/
33 int export_database (struct pdb_context *in, char *db){
34 struct pdb_context *context;
35 SAM_ACCOUNT *user = NULL;
37 if(!NT_STATUS_IS_OK(make_pdb_context_name(&context, db))){
38 fprintf(stderr, "Can't initialize %s.\n", db);
42 if(!in->pdb_setsampwent(in, 0)){
43 fprintf(stderr, "Can't sampwent!\n");
47 if(!NT_STATUS_IS_OK(pdb_init_sam(&user))){
48 fprintf(stderr, "Can't initialize new SAM_ACCOUNT!\n");
52 while(in->pdb_getsampwent(in,user)){
53 context->pdb_add_sam_account(context,user);
54 if(!NT_STATUS_IS_OK(pdb_reset_sam(user))){
55 fprintf(stderr, "Can't reset SAM_ACCOUNT!\n");
60 in->pdb_endsampwent(in);
65 /*********************************************************
66 Print info from sam structure
67 **********************************************************/
69 static int print_sam_info (SAM_ACCOUNT *sam_pwent, BOOL verbosity, BOOL smbpwdstyle)
75 /* TODO: chaeck if entry is a user or a workstation */
76 if (!sam_pwent) return -1;
79 printf ("Unix/NT username: %s/%s\n", pdb_get_username(sam_pwent),pdb_get_nt_username(sam_pwent));
80 if (IS_SAM_UNIX_USER(sam_pwent)) {
81 uid = pdb_get_uid(sam_pwent);
82 gid = pdb_get_gid(sam_pwent);
83 printf ("user ID/Group: %d/%d\n", uid, gid);
85 printf ("user RID/GRID: %u/%u\n", (unsigned int)pdb_get_user_rid(sam_pwent),
86 (unsigned int)pdb_get_group_rid(sam_pwent));
87 printf ("Full Name: %s\n", pdb_get_fullname(sam_pwent));
88 printf ("Home Directory: %s\n", pdb_get_homedir(sam_pwent));
89 printf ("HomeDir Drive: %s\n", pdb_get_dirdrive(sam_pwent));
90 printf ("Logon Script: %s\n", pdb_get_logon_script(sam_pwent));
91 printf ("Profile Path: %s\n", pdb_get_profile_path(sam_pwent));
92 printf ("Domain: %s\n", pdb_get_domain(sam_pwent));
93 printf ("Account desc: %s\n", pdb_get_acct_desc(sam_pwent));
94 printf ("Workstations: %s\n", pdb_get_workstations(sam_pwent));
95 printf ("Munged dial: %s\n", pdb_get_munged_dial(sam_pwent));
97 tmp = pdb_get_logon_time(sam_pwent);
98 printf ("Logon time: %s\n", tmp ? http_timestring(tmp) : "0");
100 tmp = pdb_get_logoff_time(sam_pwent);
101 printf ("Logoff time: %s\n", tmp ? http_timestring(tmp) : "0");
103 tmp = pdb_get_kickoff_time(sam_pwent);
104 printf ("Kickoff time: %s\n", tmp ? http_timestring(tmp) : "0");
106 tmp = pdb_get_pass_last_set_time(sam_pwent);
107 printf ("Password last set: %s\n", tmp ? http_timestring(tmp) : "0");
109 tmp = pdb_get_pass_can_change_time(sam_pwent);
110 printf ("Password can change: %s\n", tmp ? http_timestring(tmp) : "0");
112 tmp = pdb_get_pass_must_change_time(sam_pwent);
113 printf ("Password must change: %s\n", tmp ? http_timestring(tmp) : "0");
115 } else if (smbpwdstyle) {
116 if (IS_SAM_UNIX_USER(sam_pwent)) {
120 uid = pdb_get_uid(sam_pwent);
121 pdb_sethexpwd(lm_passwd,
122 pdb_get_lanman_passwd(sam_pwent),
123 pdb_get_acct_ctrl(sam_pwent));
124 pdb_sethexpwd(nt_passwd,
125 pdb_get_nt_passwd(sam_pwent),
126 pdb_get_acct_ctrl(sam_pwent));
128 printf("%s:%d:%s:%s:%s:LCT-%08X:\n",
129 pdb_get_username(sam_pwent),
133 pdb_encode_acct_ctrl(pdb_get_acct_ctrl(sam_pwent),NEW_PW_FORMAT_SPACE_PADDED_LEN),
134 (uint32)pdb_get_pass_last_set_time(sam_pwent));
136 fprintf(stderr, "Can't output in smbpasswd format, no uid on this record.\n");
139 if (IS_SAM_UNIX_USER(sam_pwent)) {
140 printf ("%s:%d:%s\n", pdb_get_username(sam_pwent), pdb_get_uid(sam_pwent),
141 pdb_get_fullname(sam_pwent));
143 printf ("%s:(null):%s\n", pdb_get_username(sam_pwent), pdb_get_fullname(sam_pwent));
150 /*********************************************************
151 Get an Print User Info
152 **********************************************************/
154 static int print_user_info (struct pdb_context *in, char *username, BOOL verbosity, BOOL smbpwdstyle)
156 SAM_ACCOUNT *sam_pwent=NULL;
159 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
163 ret = in->pdb_getsampwnam (in, sam_pwent, username);
166 fprintf (stderr, "Username not found!\n");
167 pdb_free_sam(&sam_pwent);
171 ret=print_sam_info (sam_pwent, verbosity, smbpwdstyle);
172 pdb_free_sam(&sam_pwent);
177 /*********************************************************
179 **********************************************************/
180 static int print_users_list (struct pdb_context *in, BOOL verbosity, BOOL smbpwdstyle)
182 SAM_ACCOUNT *sam_pwent=NULL;
185 errno = 0; /* testing --simo */
186 check = in->pdb_setsampwent(in, False);
187 if (check && errno == ENOENT) {
188 fprintf (stderr,"Password database not found!\n");
193 if (!(NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent)))) return 1;
195 while (check && (ret = in->pdb_getsampwent (in, sam_pwent))) {
197 printf ("---------------\n");
198 print_sam_info (sam_pwent, verbosity, smbpwdstyle);
199 pdb_free_sam(&sam_pwent);
200 check = NT_STATUS_IS_OK(pdb_init_sam(&sam_pwent));
202 if (check) pdb_free_sam(&sam_pwent);
204 in->pdb_endsampwent(in);
208 /*********************************************************
210 **********************************************************/
212 static int set_user_info (struct pdb_context *in, char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
214 SAM_ACCOUNT *sam_pwent=NULL;
217 pdb_init_sam(&sam_pwent);
219 ret = in->pdb_getsampwnam (in, sam_pwent, username);
221 fprintf (stderr, "Username not found!\n");
222 pdb_free_sam(&sam_pwent);
227 pdb_set_fullname(sam_pwent, fullname);
229 pdb_set_homedir(sam_pwent, homedir, True);
231 pdb_set_dir_drive(sam_pwent,drive, True);
233 pdb_set_logon_script(sam_pwent, script, True);
235 pdb_set_profile_path (sam_pwent, profile, True);
237 if (in->pdb_update_sam_account (in, sam_pwent))
238 print_user_info (in, username, True, False);
240 fprintf (stderr, "Unable to modify entry!\n");
241 pdb_free_sam(&sam_pwent);
244 pdb_free_sam(&sam_pwent);
248 /*********************************************************
250 **********************************************************/
251 static int new_user (struct pdb_context *in, char *username, char *fullname, char *homedir, char *drive, char *script, char *profile)
253 SAM_ACCOUNT *sam_pwent=NULL;
254 struct passwd *pwd = NULL;
255 char *password1, *password2;
257 ZERO_STRUCT(sam_pwent);
259 if ((pwd = getpwnam_alloc(username))) {
260 pdb_init_sam_pw (&sam_pwent, pwd);
263 fprintf (stderr, "WARNING: user %s does not exist in system passwd\n", username);
264 pdb_init_sam(&sam_pwent);
265 if (!pdb_set_username(sam_pwent, username)) {
270 password1 = getpass("new password:");
271 password2 = getpass("retype new password:");
272 if (strcmp (password1, password2)) {
273 fprintf (stderr, "Passwords does not match!\n");
274 pdb_free_sam (&sam_pwent);
278 pdb_set_plaintext_passwd(sam_pwent, password1);
281 pdb_set_fullname(sam_pwent, fullname);
283 pdb_set_homedir (sam_pwent, homedir, True);
285 pdb_set_dir_drive (sam_pwent, drive, True);
287 pdb_set_logon_script(sam_pwent, script, True);
289 pdb_set_profile_path (sam_pwent, profile, True);
291 pdb_set_acct_ctrl (sam_pwent, ACB_NORMAL);
293 if (in->pdb_add_sam_account (in, sam_pwent)) {
294 print_user_info (in, username, True, False);
296 fprintf (stderr, "Unable to add user! (does it alredy exist?)\n");
297 pdb_free_sam (&sam_pwent);
300 pdb_free_sam (&sam_pwent);
304 /*********************************************************
306 **********************************************************/
308 static int new_machine (struct pdb_context *in, char *machinename)
310 SAM_ACCOUNT *sam_pwent=NULL;
312 char *password = NULL;
314 if (!NT_STATUS_IS_OK(pdb_init_sam (&sam_pwent))) {
318 if (machinename[strlen (machinename) -1] == '$')
319 machinename[strlen (machinename) -1] = '\0';
321 safe_strcpy (name, machinename, 16);
322 safe_strcat (name, "$", 16);
324 string_set (&password, machinename);
325 strlower_m(password);
327 pdb_set_plaintext_passwd (sam_pwent, password);
329 pdb_set_username (sam_pwent, name);
331 pdb_set_acct_ctrl (sam_pwent, ACB_WSTRUST);
333 pdb_set_group_rid(sam_pwent, DOMAIN_GROUP_RID_COMPUTERS);
335 if (in->pdb_add_sam_account (in, sam_pwent)) {
336 print_user_info (in, name, True, False);
338 fprintf (stderr, "Unable to add machine! (does it already exist?)\n");
339 pdb_free_sam (&sam_pwent);
342 pdb_free_sam (&sam_pwent);
346 /*********************************************************
348 **********************************************************/
350 static int delete_user_entry (struct pdb_context *in, char *username)
352 SAM_ACCOUNT *samaccount = NULL;
354 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
358 if (!in->pdb_getsampwnam(in, samaccount, username)) {
359 fprintf (stderr, "user %s does not exist in the passdb\n", username);
363 return in->pdb_delete_sam_account (in, samaccount);
366 /*********************************************************
368 **********************************************************/
370 static int delete_machine_entry (struct pdb_context *in, char *machinename)
373 SAM_ACCOUNT *samaccount = NULL;
375 safe_strcpy (name, machinename, 16);
376 if (name[strlen(name)] != '$')
377 safe_strcat (name, "$", 16);
379 if (!NT_STATUS_IS_OK(pdb_init_sam (&samaccount))) {
383 if (!in->pdb_getsampwnam(in, samaccount, name)) {
384 fprintf (stderr, "user %s does not exist in the passdb\n", name);
388 return in->pdb_delete_sam_account (in, samaccount);
391 /*********************************************************
393 **********************************************************/
395 int main (int argc, char **argv)
397 static BOOL list_users = False;
398 static BOOL verbose = False;
399 static BOOL spstyle = False;
400 static BOOL setparms = False;
401 static BOOL machine = False;
402 static BOOL add_user = False;
403 static BOOL delete_user = False;
404 static BOOL import = False;
406 static char *full_name = NULL;
407 static char *user_name = NULL;
408 static char *home_dir = NULL;
409 static char *home_drive = NULL;
410 static char *backend_in = NULL;
411 static char *backend_out = NULL;
412 static char *logon_script = NULL;
413 static char *profile_path = NULL;
415 struct pdb_context *in;
417 struct poptOption long_options[] = {
419 {"list", 'l',POPT_ARG_VAL, &list_users, 1, "list all users", NULL},
420 {"verbose", 'v',POPT_ARG_VAL, &verbose, 1, "be verbose", NULL },
421 {"smbpasswd-style", 'w',POPT_ARG_VAL, &spstyle, 1, "give output in smbpasswd style", NULL},
422 {"user", 'u',POPT_ARG_STRING,&user_name, 0, "use username", "USER" },
423 {"fullname", 'f',POPT_ARG_STRING,&full_name, 0, "set full name", NULL},
424 {"homedir", 'h',POPT_ARG_STRING,&home_dir, 0, "set home directory", NULL},
425 {"drive", 'd',POPT_ARG_STRING,&home_drive, 0, "set home drive", NULL},
426 {"script", 's',POPT_ARG_STRING,&logon_script, 0, "set logon script", NULL},
427 {"profile", 'p',POPT_ARG_STRING,&profile_path, 0, "set profile path", NULL},
428 {"create", 'a',POPT_ARG_VAL,&add_user, 1, "create user", NULL},
429 {"machine", 'm',POPT_ARG_VAL,&machine, 1,"account is a machine account",NULL},
430 {"delete", 'x',POPT_ARG_VAL,&delete_user,1,"delete user",NULL},
431 {"import", 'i',POPT_ARG_STRING,&backend_in,0,"use different passdb backend",NULL},
432 {"export", 'e',POPT_ARG_STRING,&backend_out,0,"export user accounts to backend", NULL},
433 {"debuglevel",'D',POPT_ARG_INT,&DEBUGLEVEL,0,"set debuglevel",NULL},
438 setup_logging("pdbedit", True);
439 AllowDebugChange = False;
441 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
442 fprintf(stderr, "Can't load %s - run testparm to debug it\n",
447 backend_in = lp_passdb_backend();
449 pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
450 POPT_CONTEXT_KEEP_FIRST);
452 while((opt = poptGetNextOpt(pc)) != -1);
454 setparms = (full_name || home_dir || home_drive || logon_script || profile_path);
456 if (((add_user?1:0) + (delete_user?1:0) + (list_users?1:0) + (import?1:0) + (setparms?1:0)) + (backend_out?1:0) > 1) {
457 fprintf (stderr, "Incompatible options on command line!\n");
462 if(!NT_STATUS_IS_OK(make_pdb_context_name(&in, backend_in))){
463 fprintf(stderr, "Can't initialize %s.\n", backend_in);
469 fprintf (stderr, "Username not specified! (use -u option)\n");
473 return new_machine (in, user_name);
475 return new_user (in, user_name, full_name, home_dir,
476 home_drive, logon_script,
482 fprintf (stderr, "Username not specified! (use -u option)\n");
486 return delete_machine_entry (in, user_name);
488 return delete_user_entry (in, user_name);
493 return set_user_info (in, user_name, full_name,
499 return print_user_info (in, user_name, verbose,
504 return print_users_list (in, verbose, spstyle);
507 return export_database(in, backend_out);
509 poptPrintHelp(pc, stderr, 0);