another four next_token() removals (using getopt instead)
[ira/wip.git] / source3 / rpcclient / cmd_netlogon.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    NT Domain Authentication SMB / MSRPC client
5    Copyright (C) Andrew Tridgell 1994-1997
6    Copyright (C) Luke Kenneth Casson Leighton 1996-1997
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23
24
25 #ifdef SYSLOG
26 #undef SYSLOG
27 #endif
28
29 #include "includes.h"
30 #include "nterr.h"
31
32 extern int DEBUGLEVEL;
33
34 #define DEBUG_TESTING
35
36 extern struct cli_state *smb_cli;
37
38 extern FILE* out_hnd;
39 extern pstring global_myname;
40
41
42 /****************************************************************************
43 experimental nt login.
44 ****************************************************************************/
45 void cmd_netlogon_login_test(struct client_info *info, int argc, char *argv[])
46 {
47         uint16 nt_pipe_fnum;
48 #if 0
49         extern BOOL global_machine_password_needs_changing;
50 #endif
51
52         fstring nt_user_name;
53         fstring password;
54         BOOL res = True;
55         char *nt_password;
56         unsigned char trust_passwd[16];
57
58 #if 0
59         /* machine account passwords */
60         pstring new_mach_pwd;
61
62         /* initialisation */
63         new_mach_pwd[0] = 0;
64 #endif
65
66         argc--;
67         argv++;
68
69         if (argc < 1)
70         {
71                 fstrcpy(nt_user_name, smb_cli->user_name);
72                 if (nt_user_name[0] == 0)
73                 {
74                         report(out_hnd,"ntlogin: must specify username with anonymous connection\n");
75                         return;
76                 }
77         }
78         else
79         {
80                 fstrcpy(nt_user_name, argv[0]);
81         }
82
83         argc--;
84         argv++;
85
86         if (argc < 2)
87         {
88                 nt_password = argv[0];
89         }
90         else
91         {
92                 nt_password = getpass("Enter NT Login password:");
93         }
94
95         DEBUG(5,("do_nt_login_test: username %s\n", nt_user_name));
96
97         res = res ? trust_get_passwd(trust_passwd, smb_cli->domain, info->myhostname) : False;
98
99 #if 0
100         /* check whether the user wants to change their machine password */
101         res = res ? trust_account_check(info->dest_ip, info->dest_host,
102                                         info->myhostname, smb_cli->domain,
103                                         info->mach_acct, new_mach_pwd) : False;
104 #endif
105         /* open NETLOGON session.  negotiate credentials */
106         res = res ? cli_nt_session_open(smb_cli, PIPE_NETLOGON, &nt_pipe_fnum) : False;
107
108         res = res ? cli_nt_setup_creds(smb_cli, nt_pipe_fnum,
109                                        smb_cli->mach_acct, global_myname,
110                                        trust_passwd, SEC_CHAN_WKSTA) == 0x0 : False;
111
112 #if 0
113         /* change the machine password? */
114         if (global_machine_password_needs_changing)
115         {
116                 unsigned char new_trust_passwd[16];
117                 generate_random_buffer(new_trust_passwd, 16, True);
118                 res = res ? cli_nt_srv_pwset(smb_cli, nt_pipe_fnum, new_trust_passwd, SEC_CHAN_WKSTA) : False;
119
120                 if (res)
121                 {
122                         global_machine_password_needs_changing = !set_trust_account_password(new_trust_passwd);
123                 }
124
125                 memset(new_trust_passwd, 0, 16);
126         }
127 #endif
128
129         memset(trust_passwd, 0, 16);
130
131         /* do an NT login */
132         res = res ? cli_nt_login_interactive(smb_cli, nt_pipe_fnum,
133                          smb_cli->domain, nt_user_name,
134                          getuid(), nt_password,
135                          &info->dom.ctr, &info->dom.user_info3) : False;
136
137         /*** clear out the password ***/
138         memset(password, 0, sizeof(password));
139
140         /* ok!  you're logged in!  do anything you like, then... */
141
142         /* do an NT logout */
143         res = res ? cli_nt_logoff(smb_cli, nt_pipe_fnum, &info->dom.ctr) : False;
144
145         /* close the session */
146         cli_nt_session_close(smb_cli, nt_pipe_fnum);
147
148         report(out_hnd,"cmd_nt_login: login (%s) test succeeded: %s\n",
149                 nt_user_name, BOOLSTR(res));
150 }
151
152 /****************************************************************************
153 experimental nt login.
154 ****************************************************************************/
155 void cmd_netlogon_domain_test(struct client_info *info, int argc, char *argv[])
156 {
157         uint16 nt_pipe_fnum;
158
159         char *nt_trust_dom;
160         BOOL res = True;
161         unsigned char trust_passwd[16];
162         fstring inter_dom_acct;
163
164         if (argc < 2)
165         {
166                 report(out_hnd,"domtest: must specify domain name\n");
167                 return;
168         }
169
170         nt_trust_dom = argv[1];
171
172         DEBUG(5,("do_nt_login_test: domain %s\n", nt_trust_dom));
173
174         fstrcpy(inter_dom_acct, nt_trust_dom);
175         fstrcat(inter_dom_acct, "$");
176
177         res = res ? trust_get_passwd(trust_passwd, smb_cli->domain, nt_trust_dom) : False;
178
179         /* open NETLOGON session.  negotiate credentials */
180         res = res ? cli_nt_session_open(smb_cli, PIPE_NETLOGON, &nt_pipe_fnum) : False;
181
182         res = res ? cli_nt_setup_creds(smb_cli, nt_pipe_fnum, inter_dom_acct,
183                                        global_myname, trust_passwd, 
184                                        SEC_CHAN_DOMAIN) == 0x0 : False;
185
186         memset(trust_passwd, 0, 16);
187
188         /* close the session */
189         cli_nt_session_close(smb_cli, nt_pipe_fnum);
190
191         report(out_hnd,"cmd_nt_login: credentials (%s) test succeeded: %s\n",
192                 nt_trust_dom, BOOLSTR(res));
193 }
194
195 /****************************************************************************
196 experimental SAM synchronisation.
197 ****************************************************************************/
198 void cmd_sam_sync(struct client_info *info, int argc, char *argv[])
199 {
200         SAM_DELTA_HDR hdr_deltas[MAX_SAM_DELTAS];
201         SAM_DELTA_CTR deltas[MAX_SAM_DELTAS];
202         uint32 num;
203         uchar trust_passwd[16];
204         extern pstring global_myname;
205
206         if (!trust_get_passwd(trust_passwd, smb_cli->domain, global_myname))
207         {
208                 report(out_hnd, "cmd_sam_sync: no trust account password\n");
209                 return;
210         }
211
212         if (do_sam_sync(smb_cli, trust_passwd,
213             smb_cli->mach_acct, global_myname,
214             hdr_deltas, deltas, &num))
215         {
216                 display_sam_sync(out_hnd, ACTION_HEADER   , hdr_deltas, deltas, num);
217                 display_sam_sync(out_hnd, ACTION_ENUMERATE, hdr_deltas, deltas, num);
218                 display_sam_sync(out_hnd, ACTION_FOOTER   , hdr_deltas, deltas, num);
219         }
220 }