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