This looks like a big change but really isn't.
[samba.git] / source3 / utils / smbpasswd.c
1 /*
2  * Unix SMB/Netbios implementation. Version 1.9. smbpasswd module. Copyright
3  * (C) Jeremy Allison 1995-1998
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  * 
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 675
17  * Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "includes.h"
21
22 extern pstring global_myname;
23
24 /*********************************************************
25  Print command usage on stderr and die.
26 **********************************************************/
27
28 static void usage(char *name, BOOL is_root)
29 {
30         if(is_root)
31                 fprintf(stderr, "Usage is : %s [-D DEBUGLEVEL] [-a] [-d] [-m] [-n] [username] [password]\n\
32 %s: [-R <name resolve order>] [-D DEBUGLEVEL] [-r machine] [username] [password]\n%s: [-h]\n", name, name, name);
33         else
34                 fprintf(stderr, "Usage is : %s [-h] [-D DEBUGLEVEL] [-r machine] [password]\n", name);
35         exit(1);
36 }
37
38 /*********************************************************
39  Start here.
40 **********************************************************/
41
42 int main(int argc, char **argv)
43 {
44   extern char *optarg;
45   extern int optind;
46   extern int DEBUGLEVEL;
47   char *prog_name;
48   int             real_uid;
49   struct passwd  *pwd;
50   fstring         old_passwd;
51   fstring         new_passwd;
52   uchar           new_p16[16];
53   uchar           new_nt_p16[16];
54   char           *p;
55   struct smb_passwd *smb_pwent;
56   FILE           *fp;
57   int             ch;
58   int             err;
59   BOOL is_root = False;
60   pstring  user_name;
61   char *remote_machine = NULL;
62   BOOL           add_user = False;
63   BOOL           got_new_pass = False;
64   BOOL           machine_account = False;
65   BOOL           disable_user = False;
66   BOOL           set_no_password = False;
67   pstring servicesf = CONFIGFILE;
68   void           *vp;
69
70   new_passwd[0] = '\0';
71   user_name[0] = '\0';
72
73   memset(old_passwd, '\0', sizeof(old_passwd));
74   memset(new_passwd, '\0', sizeof(new_passwd));
75
76   prog_name = argv[0];
77
78   TimeInit();
79
80   setup_logging(prog_name,True);
81   
82   charset_initialise();
83
84   if (!lp_load(servicesf,True,False,False)) {
85     fprintf(stderr, "%s: Can't load %s - run testparm to debug it\n", prog_name, servicesf);
86   }
87     
88   codepage_initialise(lp_client_code_page());
89
90   /* Get the real uid */
91   real_uid = getuid();
92   
93   /* Check the effective uid */
94   if ((geteuid() == 0) && (real_uid != 0)) {
95     fprintf(stderr, "%s: Must *NOT* be setuid root.\n", prog_name);
96     exit(1);
97   }
98
99   is_root = (real_uid == 0);
100
101   while ((ch = getopt(argc, argv, "adhmnr:R:D:")) != EOF) {
102     switch(ch) {
103     case 'a':
104       if(is_root)
105         add_user = True;
106       else
107         usage(prog_name, is_root);
108       break;
109     case 'd':
110       if(is_root) {
111         disable_user = True;
112         got_new_pass = True;
113         strcpy(new_passwd, "XXXXXX");
114       } else
115         usage(prog_name, is_root);
116       break;
117     case 'D':
118       DEBUGLEVEL = atoi(optarg);
119       break;
120     case 'n':
121       if(is_root) {
122         set_no_password = True;
123         got_new_pass = True;
124         strcpy(new_passwd, "NO PASSWORD");
125       } else
126         usage(prog_name, is_root);
127     case 'r':
128       remote_machine = optarg;
129       break;
130     case 'R':
131       if(is_root) {
132         lp_set_name_resolve_order(optarg);
133         break;
134       } else
135         usage(prog_name, is_root);
136     case 'm':
137       if(is_root) {
138         machine_account = True;
139       } else
140         usage(prog_name, is_root);
141       break;
142     case 'h':
143     default:
144       usage(prog_name, is_root);
145     }
146   }
147
148   argc -= optind;
149   argv += optind;
150
151   /*
152    * Ensure add_user and remote machine are
153    * not both set.
154    */
155   if(add_user && (remote_machine != NULL))
156     usage(prog_name, True);
157
158   if( is_root ) {
159
160     /*
161      * Deal with root - can add a user, but only locally.
162      */
163
164     switch(argc) {
165       case 0:
166         break;
167       case 1:
168         /* If we are root we can change another's password. */
169         pstrcpy(user_name, argv[0]);
170         break;
171       case 2:
172         pstrcpy(user_name, argv[0]);
173         fstrcpy(new_passwd, argv[1]);
174         got_new_pass = True;
175         break;
176       default:
177         usage(prog_name, True);
178     }
179
180     if(*user_name) {
181
182       if(machine_account) {
183         int username_len = strlen(user_name);
184         if(username_len >= sizeof(pstring) - 1) {
185           fprintf(stderr, "%s: machine account name too long.\n", user_name);
186           exit(1);
187         }
188
189         if(user_name[username_len-1] != '$') {
190           user_name[username_len] = '$';
191           user_name[username_len+1] = '\0';
192         }
193       }
194
195     /*
196      * Setup the pwd struct to point to known
197      * values for a machine account (it doesn't
198      * exist in /etc/passwd).
199      */
200       if((pwd = getpwnam(user_name)) == NULL) {
201         fprintf(stderr, "%s: User \"%s\" was not found in system password file.\n", 
202                     prog_name, user_name);
203         exit(1);
204       }
205     } else {
206       if((pwd = getpwuid(real_uid)) != NULL)
207         pstrcpy( user_name, pwd->pw_name);
208     }
209
210   } else {
211
212     if(add_user) {
213       fprintf(stderr, "%s: Only root can set anothers password.\n", prog_name);
214       usage(prog_name, False);
215     }
216
217     if(argc > 1)
218       usage(prog_name, False);
219
220     if(argc == 1) {
221       fstrcpy(new_passwd, argv[0]);
222       got_new_pass = True;
223     }
224
225     if((pwd = getpwuid(real_uid)) != NULL)
226       pstrcpy( user_name, pwd->pw_name);
227
228     /*
229      * A non-root user is always setting a password
230      * via a remote machine (even if that machine is
231      * localhost).
232      */
233
234     if(remote_machine == NULL)
235       remote_machine = "127.0.0.1";
236   }    
237     
238   if (*user_name == '\0') {
239     fprintf(stderr, "%s: Unable to get a user name for password change.\n", prog_name);
240     exit(1);
241   }
242
243   /*
244    * If we are adding a machine account then pretend
245    * we already have the new password, we will be using
246    * the machinename as the password.
247    */
248
249   if(add_user && machine_account) {
250     got_new_pass = True;
251     strncpy(new_passwd, user_name, sizeof(fstring));
252     new_passwd[sizeof(fstring)-1] = '\0';
253     strlower(new_passwd);
254     if(new_passwd[strlen(new_passwd)-1] == '$')
255       new_passwd[strlen(new_passwd)-1] = '\0';
256   }
257
258   /* 
259    * If we are root we don't ask for the old password (unless it's on a
260    * remote machine.
261    */
262
263   if (remote_machine != NULL) {
264     p = getpass("Old SMB password:");
265     fstrcpy(old_passwd, p);
266   }
267
268   if (!got_new_pass) {
269     new_passwd[0] = '\0';
270
271     p = getpass("New SMB password:");
272
273     strncpy(new_passwd, p, sizeof(fstring));
274     new_passwd[sizeof(fstring)-1] = '\0';
275
276     p = getpass("Retype new SMB password:");
277
278     if (strncmp(p, new_passwd, sizeof(fstring)-1))
279     {
280       fprintf(stderr, "%s: Mismatch - password unchanged.\n", prog_name);
281       exit(1);
282     }
283   }
284   
285   if (new_passwd[0] == '\0') {
286     printf("Password not set\n");
287     exit(0);
288   }
289  
290   /* 
291    * Now do things differently depending on if we're changing the
292    * password on a remote machine. Remember - a normal user is
293    * always using this code, looping back to the local smbd.
294    */
295
296   if(remote_machine != NULL) {
297     struct cli_state cli;
298     struct in_addr ip;
299
300     if(get_myname(global_myname,NULL) == False) {
301       fprintf(stderr, "%s: unable to get my hostname.\n", prog_name );
302       exit(1);
303     }
304
305     if(!resolve_name( remote_machine, &ip)) {
306       fprintf(stderr, "%s: unable to find an IP address for machine %s.\n",
307               prog_name, remote_machine );
308       exit(1);
309     }
310  
311     memset(&cli, '\0', sizeof(struct cli_state));
312  
313     if (!cli_initialise(&cli) || !cli_connect(&cli, remote_machine, &ip)) {
314       fprintf(stderr, "%s: unable to connect to SMB server on machine %s. Error was : %s.\n",
315               prog_name, remote_machine, cli_errstr(&cli) );
316       exit(1);
317     }
318   
319     if (!cli_session_request(&cli, remote_machine, 0x20, global_myname)) {
320       fprintf(stderr, "%s: machine %s rejected the session setup. Error was : %s.\n",
321               prog_name, remote_machine, cli_errstr(&cli) );
322       cli_shutdown(&cli);
323       exit(1);
324     }
325   
326     cli.protocol = PROTOCOL_NT1;
327
328     if (!cli_negprot(&cli)) {
329       fprintf(stderr, "%s: machine %s rejected the negotiate protocol. Error was : %s.\n",        
330               prog_name, remote_machine, cli_errstr(&cli) );
331       cli_shutdown(&cli);
332       exit(1);
333     }
334   
335     if (!cli_session_setup(&cli, user_name, old_passwd, strlen(old_passwd),
336                            "", 0, "")) {
337       fprintf(stderr, "%s: machine %s rejected the session setup. Error was : %s.\n",        
338               prog_name, remote_machine, cli_errstr(&cli) );
339       cli_shutdown(&cli);
340       exit(1);
341     }               
342
343     if (!cli_send_tconX(&cli, "IPC$", "IPC", "", 1)) {
344       fprintf(stderr, "%s: machine %s rejected the tconX on the IPC$ share. Error was : %s.\n",
345               prog_name, remote_machine, cli_errstr(&cli) );
346       cli_shutdown(&cli);
347       exit(1);
348     }
349
350     if(!cli_oem_change_password(&cli, user_name, new_passwd, old_passwd)) {
351       fprintf(stderr, "%s: machine %s rejected the password change: Error was : %s.\n",
352               prog_name, remote_machine, cli_errstr(&cli) );
353       cli_shutdown(&cli);
354       exit(1);
355     }
356
357     cli_shutdown(&cli);
358     exit(0);
359   }
360
361   /*
362    * Check for a machine account.
363    */
364
365   if(machine_account && !pwd) {
366     fprintf(stderr, "%s: User %s does not exist in system password file \
367 (usually /etc/passwd). Cannot add machine account without a valid system user.\n",
368            prog_name, user_name);
369     exit(1);
370   }
371
372   /* Calculate the MD4 hash (NT compatible) of the new password. */
373   
374   memset(new_nt_p16, '\0', 16);
375   E_md4hash((uchar *) new_passwd, new_nt_p16);
376   
377   /* Mangle the password into Lanman format */
378   new_passwd[14] = '\0';
379   strupper(new_passwd);
380   
381   /*
382    * Calculate the SMB (lanman) hash functions of the new password.
383    */
384   
385   memset(new_p16, '\0', 16);
386   E_P16((uchar *) new_passwd, new_p16);
387   
388   /*
389    * Open the smbpaswd file.
390    */
391   vp = startsmbpwent(True);
392   if (!vp && errno == ENOENT) {
393           fp = fopen(lp_smb_passwd_file(), "w");
394           if (fp) {
395                   fprintf(fp, "# Samba SMB password file\n");
396                   fclose(fp);
397                   vp = startsmbpwent(True);
398           }
399   }
400   if (!fp) {
401           err = errno;
402           fprintf(stderr, "%s: Failed to open password file %s.\n",
403                   prog_name, lp_smb_passwd_file());
404           errno = err;
405           perror(prog_name);
406           exit(err);
407   }
408   
409   /* Get the smb passwd entry for this user */
410   smb_pwent = getsmbpwnam(user_name);
411   if (smb_pwent == NULL) {
412     if(add_user == False) {
413       fprintf(stderr, "%s: Failed to find entry for user %s.\n",
414               prog_name, pwd->pw_name);
415       endsmbpwent(vp);
416       exit(1);
417     }
418
419     /* Create a new smb passwd entry and set it to the given password. */
420     {
421       struct smb_passwd new_smb_pwent;
422
423       new_smb_pwent.smb_userid = pwd->pw_uid;
424       new_smb_pwent.smb_name = pwd->pw_name; 
425       new_smb_pwent.smb_passwd = NULL;
426       new_smb_pwent.smb_nt_passwd = NULL;
427       new_smb_pwent.acct_ctrl = (machine_account ? ACB_WSTRUST : ACB_NORMAL);
428
429       if(disable_user) {
430         new_smb_pwent.acct_ctrl |= ACB_DISABLED;
431       } else if (set_no_password) {
432         new_smb_pwent.acct_ctrl |= ACB_PWNOTREQ;
433       } else {
434         new_smb_pwent.smb_passwd = new_p16;
435         new_smb_pwent.smb_nt_passwd = new_nt_p16;
436       }
437
438       if(add_smbpwd_entry(&new_smb_pwent) == False) {
439         fprintf(stderr, "%s: Failed to add entry for user %s.\n", 
440                 prog_name, pwd->pw_name);
441         endsmbpwent(vp);
442         exit(1);
443       }
444       
445       endsmbpwent(vp);
446       printf("%s: Added user %s.\n", prog_name, user_name);
447       exit(0);
448     }
449   } else {
450           /* the entry already existed */
451           add_user = False;
452   }
453
454   /*
455    * We are root - just write the new password
456    * and the valid last change time.
457    */
458
459   if(disable_user) {
460     /*
461      * This currently won't work as it means changing
462      * the length of the record. JRA.
463      */
464     smb_pwent->acct_ctrl |= ACB_DISABLED;
465     smb_pwent->smb_passwd = NULL;
466     smb_pwent->smb_nt_passwd = NULL;
467   } else if (set_no_password) {
468     /*
469      * This currently won't work as it means changing
470      * the length of the record. JRA.
471      */
472     smb_pwent->acct_ctrl |= ACB_PWNOTREQ;
473     smb_pwent->smb_passwd = NULL;
474     smb_pwent->smb_nt_passwd = NULL; 
475   } else {
476     smb_pwent->smb_passwd = new_p16;
477     smb_pwent->smb_nt_passwd = new_nt_p16;
478   }
479
480   if(mod_smbpwd_entry(smb_pwent) == False) {
481     fprintf(stderr, "%s: Failed to modify entry for user %s.\n",
482             prog_name, pwd->pw_name);
483     endsmbpwent(vp);
484     exit(1);
485   }
486
487   endsmbpwent(vp);
488   if(disable_user)
489     printf("User %s disabled.\n", user_name);
490   else if (set_no_password)
491     printf("User %s - set to no password.\n", user_name);
492   else
493     printf("Password changed for user %s.\n", user_name);
494   return 0;
495 }