~vcs-imports/samba/main

« back to all changes in this revision

Viewing changes to source/utils/net.c

  • Committer: jerry
  • Date: 2006-07-14 21:48:39 UTC
  • Revision ID: vcs-imports@canonical.com-20060714214839-586d8c489a8fcead
gutting trunk to move to svn:externals

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
2
 
   Samba Unix/Linux SMB client library 
3
 
   Distributed SMB/CIFS Server Management Utility 
4
 
   Copyright (C) 2001 Steve French  (sfrench@us.ibm.com)
5
 
   Copyright (C) 2001 Jim McDonough (jmcd@us.ibm.com)
6
 
   Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
7
 
   Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
8
 
 
9
 
   Originally written by Steve and Jim. Largely rewritten by tridge in
10
 
   November 2001.
11
 
 
12
 
   Reworked again by abartlet in December 2001
13
 
 
14
 
   This program is free software; you can redistribute it and/or modify
15
 
   it under the terms of the GNU General Public License as published by
16
 
   the Free Software Foundation; either version 2 of the License, or
17
 
   (at your option) any later version.
18
 
   
19
 
   This program is distributed in the hope that it will be useful,
20
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 
   GNU General Public License for more details.
23
 
   
24
 
   You should have received a copy of the GNU General Public License
25
 
   along with this program; if not, write to the Free Software
26
 
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
27
 
 
28
 
/*****************************************************/
29
 
/*                                                   */
30
 
/*   Distributed SMB/CIFS Server Management Utility  */
31
 
/*                                                   */
32
 
/*   The intent was to make the syntax similar       */
33
 
/*   to the NET utility (first developed in DOS      */
34
 
/*   with additional interesting & useful functions  */
35
 
/*   added in later SMB server network operating     */
36
 
/*   systems).                                       */
37
 
/*                                                   */
38
 
/*****************************************************/
39
 
 
40
 
#include "includes.h"
41
 
#include "utils/net.h"
42
 
 
43
 
/***********************************************************************/
44
 
/* Beginning of internationalization section.  Translatable constants  */
45
 
/* should be kept in this area and referenced in the rest of the code. */
46
 
/*                                                                     */
47
 
/* No functions, outside of Samba or LSB (Linux Standards Base) should */
48
 
/* be used (if possible).                                              */
49
 
/***********************************************************************/
50
 
 
51
 
#define YES_STRING              "Yes"
52
 
#define NO_STRING               "No"
53
 
 
54
 
/************************************************************************************/
55
 
/*                       end of internationalization section                        */
56
 
/************************************************************************************/
57
 
 
58
 
/* Yes, these buggers are globals.... */
59
 
const char *opt_requester_name = NULL;
60
 
const char *opt_host = NULL; 
61
 
const char *opt_password = NULL;
62
 
const char *opt_user_name = NULL;
63
 
BOOL opt_user_specified = False;
64
 
const char *opt_workgroup = NULL;
65
 
int opt_long_list_entries = 0;
66
 
int opt_reboot = 0;
67
 
int opt_force = 0;
68
 
int opt_stdin = 0;
69
 
int opt_port = 0;
70
 
int opt_verbose = 0;
71
 
int opt_maxusers = -1;
72
 
const char *opt_comment = "";
73
 
const char *opt_container = NULL;
74
 
int opt_flags = -1;
75
 
int opt_timeout = 0;
76
 
const char *opt_target_workgroup = NULL;
77
 
int opt_machine_pass = 0;
78
 
BOOL opt_localgroup = False;
79
 
BOOL opt_domaingroup = False;
80
 
static BOOL do_talloc_report=False;
81
 
const char *opt_newntname = "";
82
 
int opt_rid = 0;
83
 
int opt_acls = 0;
84
 
int opt_attrs = 0;
85
 
int opt_timestamps = 0;
86
 
const char *opt_exclude = NULL;
87
 
const char *opt_destination = NULL;
88
 
 
89
 
BOOL opt_have_ip = False;
90
 
struct in_addr opt_dest_ip;
91
 
 
92
 
extern struct in_addr loopback_ip;
93
 
extern BOOL AllowDebugChange;
94
 
 
95
 
uint32 get_sec_channel_type(const char *param) 
96
 
{
97
 
        if (!(param && *param)) {
98
 
                return get_default_sec_channel();
99
 
        } else {
100
 
                if (strequal(param, "PDC")) {
101
 
                        return SEC_CHAN_BDC;
102
 
                } else if (strequal(param, "BDC")) {
103
 
                        return SEC_CHAN_BDC;
104
 
                } else if (strequal(param, "MEMBER")) {
105
 
                        return SEC_CHAN_WKSTA;
106
 
#if 0                   
107
 
                } else if (strequal(param, "DOMAIN")) {
108
 
                        return SEC_CHAN_DOMAIN;
109
 
#endif
110
 
                } else {
111
 
                        return get_default_sec_channel();
112
 
                }
113
 
        }
114
 
}
115
 
 
116
 
/*
117
 
  run a function from a function table. If not found then
118
 
  call the specified usage function 
119
 
*/
120
 
int net_run_function(int argc, const char **argv, struct functable *table, 
121
 
                     int (*usage_fn)(int argc, const char **argv))
122
 
{
123
 
        int i;
124
 
        
125
 
        if (argc < 1) {
126
 
                d_printf("\nUsage: \n");
127
 
                return usage_fn(argc, argv);
128
 
        }
129
 
        for (i=0; table[i].funcname; i++) {
130
 
                if (StrCaseCmp(argv[0], table[i].funcname) == 0)
131
 
                        return table[i].fn(argc-1, argv+1);
132
 
        }
133
 
        d_fprintf(stderr, "No command: %s\n", argv[0]);
134
 
        return usage_fn(argc, argv);
135
 
}
136
 
 
137
 
/*
138
 
 * run a function from a function table.
139
 
 */
140
 
int net_run_function2(int argc, const char **argv, const char *whoami,
141
 
                      struct functable2 *table)
142
 
{
143
 
        int i;
144
 
 
145
 
        if (argc != 0) {
146
 
                for (i=0; table[i].funcname; i++) {
147
 
                        if (StrCaseCmp(argv[0], table[i].funcname) == 0)
148
 
                                return table[i].fn(argc-1, argv+1);
149
 
                }
150
 
        }
151
 
 
152
 
        for (i=0; table[i].funcname != NULL; i++) {
153
 
                d_printf("%s %-15s %s\n", whoami, table[i].funcname,
154
 
                         table[i].helptext);
155
 
        }
156
 
 
157
 
        return -1;
158
 
}
159
 
 
160
 
/****************************************************************************
161
 
connect to \\server\service 
162
 
****************************************************************************/
163
 
NTSTATUS connect_to_service(struct cli_state **c, struct in_addr *server_ip,
164
 
                                        const char *server_name, 
165
 
                                        const char *service_name, 
166
 
                                        const char *service_type)
167
 
{
168
 
        NTSTATUS nt_status;
169
 
 
170
 
        if (!opt_password && !opt_machine_pass) {
171
 
                char *pass = getpass("Password:");
172
 
                if (pass) {
173
 
                        opt_password = SMB_STRDUP(pass);
174
 
                }
175
 
        }
176
 
        
177
 
        nt_status = cli_full_connection(c, NULL, server_name, 
178
 
                                        server_ip, opt_port,
179
 
                                        service_name, service_type,  
180
 
                                        opt_user_name, opt_workgroup,
181
 
                                        opt_password, 0, Undefined, NULL);
182
 
        
183
 
        if (NT_STATUS_IS_OK(nt_status)) {
184
 
                return nt_status;
185
 
        } else {
186
 
                d_fprintf(stderr, "Could not connect to server %s\n", server_name);
187
 
 
188
 
                /* Display a nicer message depending on the result */
189
 
 
190
 
                if (NT_STATUS_V(nt_status) == 
191
 
                    NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
192
 
                        d_fprintf(stderr, "The username or password was not correct.\n");
193
 
 
194
 
                if (NT_STATUS_V(nt_status) == 
195
 
                    NT_STATUS_V(NT_STATUS_ACCOUNT_LOCKED_OUT))
196
 
                        d_fprintf(stderr, "The account was locked out.\n");
197
 
 
198
 
                if (NT_STATUS_V(nt_status) == 
199
 
                    NT_STATUS_V(NT_STATUS_ACCOUNT_DISABLED))
200
 
                        d_fprintf(stderr, "The account was disabled.\n");
201
 
 
202
 
                return nt_status;
203
 
        }
204
 
}
205
 
 
206
 
 
207
 
/****************************************************************************
208
 
connect to \\server\ipc$  
209
 
****************************************************************************/
210
 
NTSTATUS connect_to_ipc(struct cli_state **c, struct in_addr *server_ip,
211
 
                                        const char *server_name)
212
 
{
213
 
        return connect_to_service(c, server_ip, server_name, "IPC$", "IPC");
214
 
}
215
 
 
216
 
/****************************************************************************
217
 
connect to \\server\ipc$ anonymously
218
 
****************************************************************************/
219
 
NTSTATUS connect_to_ipc_anonymous(struct cli_state **c,
220
 
                        struct in_addr *server_ip, const char *server_name)
221
 
{
222
 
        NTSTATUS nt_status;
223
 
 
224
 
        nt_status = cli_full_connection(c, opt_requester_name, server_name, 
225
 
                                        server_ip, opt_port,
226
 
                                        "IPC$", "IPC",  
227
 
                                        "", "",
228
 
                                        "", 0, Undefined, NULL);
229
 
        
230
 
        if (NT_STATUS_IS_OK(nt_status)) {
231
 
                return nt_status;
232
 
        } else {
233
 
                DEBUG(1,("Cannot connect to server (anonymously).  Error was %s\n", nt_errstr(nt_status)));
234
 
                return nt_status;
235
 
        }
236
 
}
237
 
 
238
 
/****************************************************************************
239
 
connect to \\server\ipc$ using KRB5
240
 
****************************************************************************/
241
 
NTSTATUS connect_to_ipc_krb5(struct cli_state **c,
242
 
                        struct in_addr *server_ip, const char *server_name)
243
 
{
244
 
        NTSTATUS nt_status;
245
 
 
246
 
        nt_status = cli_full_connection(c, NULL, server_name, 
247
 
                                        server_ip, opt_port,
248
 
                                        "IPC$", "IPC",  
249
 
                                        opt_user_name, opt_workgroup,
250
 
                                        opt_password, CLI_FULL_CONNECTION_USE_KERBEROS, 
251
 
                                        Undefined, NULL);
252
 
        
253
 
        if (NT_STATUS_IS_OK(nt_status)) {
254
 
                return nt_status;
255
 
        } else {
256
 
                DEBUG(1,("Cannot connect to server using kerberos.  Error was %s\n", nt_errstr(nt_status)));
257
 
                return nt_status;
258
 
        }
259
 
}
260
 
 
261
 
/**
262
 
 * Connect a server and open a given pipe
263
 
 *
264
 
 * @param cli_dst               A cli_state 
265
 
 * @param pipe                  The pipe to open
266
 
 * @param got_pipe              boolean that stores if we got a pipe
267
 
 *
268
 
 * @return Normal NTSTATUS return.
269
 
 **/
270
 
NTSTATUS connect_dst_pipe(struct cli_state **cli_dst, struct rpc_pipe_client **pp_pipe_hnd, int pipe_num)
271
 
{
272
 
        NTSTATUS nt_status;
273
 
        char *server_name = SMB_STRDUP("127.0.0.1");
274
 
        struct cli_state *cli_tmp = NULL;
275
 
        struct rpc_pipe_client *pipe_hnd = NULL;
276
 
 
277
 
        if (server_name == NULL) {
278
 
                return NT_STATUS_NO_MEMORY;
279
 
        }
280
 
 
281
 
        if (opt_destination) {
282
 
                SAFE_FREE(server_name);
283
 
                if ((server_name = SMB_STRDUP(opt_destination)) == NULL) {
284
 
                        return NT_STATUS_NO_MEMORY;
285
 
                }
286
 
        }
287
 
 
288
 
        /* make a connection to a named pipe */
289
 
        nt_status = connect_to_ipc(&cli_tmp, NULL, server_name);
290
 
        if (!NT_STATUS_IS_OK(nt_status)) {
291
 
                SAFE_FREE(server_name);
292
 
                return nt_status;
293
 
        }
294
 
 
295
 
        pipe_hnd = cli_rpc_pipe_open_noauth(cli_tmp, pipe_num, &nt_status);
296
 
        if (!pipe_hnd) {
297
 
                DEBUG(0, ("couldn't not initialize pipe\n"));
298
 
                cli_shutdown(cli_tmp);
299
 
                SAFE_FREE(server_name);
300
 
                return nt_status;
301
 
        }
302
 
 
303
 
        *cli_dst = cli_tmp;
304
 
        *pp_pipe_hnd = pipe_hnd;
305
 
        SAFE_FREE(server_name);
306
 
 
307
 
        return nt_status;
308
 
}
309
 
 
310
 
/****************************************************************************
311
 
 Use the local machine's password for this session.
312
 
****************************************************************************/
313
 
 
314
 
int net_use_machine_password(void) 
315
 
{
316
 
        char *user_name = NULL;
317
 
 
318
 
        if (!secrets_init()) {
319
 
                d_fprintf(stderr, "ERROR: Unable to open secrets database\n");
320
 
                exit(1);
321
 
        }
322
 
 
323
 
        user_name = NULL;
324
 
        opt_password = secrets_fetch_machine_password(opt_target_workgroup, NULL, NULL);
325
 
        if (asprintf(&user_name, "%s$@%s", global_myname(), lp_realm()) == -1) {
326
 
                return -1;
327
 
        }
328
 
        opt_user_name = user_name;
329
 
        return 0;
330
 
}
331
 
 
332
 
BOOL net_find_server(const char *domain, unsigned flags, struct in_addr *server_ip, char **server_name)
333
 
{
334
 
        const char *d = domain ? domain : opt_target_workgroup;
335
 
 
336
 
        if (opt_host) {
337
 
                *server_name = SMB_STRDUP(opt_host);
338
 
        }               
339
 
 
340
 
        if (opt_have_ip) {
341
 
                *server_ip = opt_dest_ip;
342
 
                if (!*server_name) {
343
 
                        *server_name = SMB_STRDUP(inet_ntoa(opt_dest_ip));
344
 
                }
345
 
        } else if (*server_name) {
346
 
                /* resolve the IP address */
347
 
                if (!resolve_name(*server_name, server_ip, 0x20))  {
348
 
                        DEBUG(1,("Unable to resolve server name\n"));
349
 
                        return False;
350
 
                }
351
 
        } else if (flags & NET_FLAGS_PDC) {
352
 
                struct in_addr pdc_ip;
353
 
 
354
 
                if (get_pdc_ip(d, &pdc_ip)) {
355
 
                        fstring dc_name;
356
 
                        
357
 
                        if (is_zero_ip(pdc_ip))
358
 
                                return False;
359
 
                        
360
 
                        if ( !name_status_find(d, 0x1b, 0x20, pdc_ip, dc_name) )
361
 
                                return False;
362
 
                                
363
 
                        *server_name = SMB_STRDUP(dc_name);
364
 
                        *server_ip = pdc_ip;
365
 
                }
366
 
        } else if (flags & NET_FLAGS_DMB) {
367
 
                struct in_addr msbrow_ip;
368
 
                /*  if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
369
 
                if (!resolve_name(d, &msbrow_ip, 0x1B))  {
370
 
                        DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
371
 
                        return False;
372
 
                } else {
373
 
                        *server_ip = msbrow_ip;
374
 
                }
375
 
                *server_name = SMB_STRDUP(inet_ntoa(opt_dest_ip));
376
 
        } else if (flags & NET_FLAGS_MASTER) {
377
 
                struct in_addr brow_ips;
378
 
                if (!resolve_name(d, &brow_ips, 0x1D))  {
379
 
                                /* go looking for workgroups */
380
 
                        DEBUG(1,("Unable to resolve master browser via name lookup\n"));
381
 
                        return False;
382
 
                } else {
383
 
                        *server_ip = brow_ips;
384
 
                }
385
 
                *server_name = SMB_STRDUP(inet_ntoa(opt_dest_ip));
386
 
        } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
387
 
                *server_ip = loopback_ip;
388
 
                *server_name = SMB_STRDUP("127.0.0.1");
389
 
        }
390
 
 
391
 
        if (!server_name || !*server_name) {
392
 
                DEBUG(1,("no server to connect to\n"));
393
 
                return False;
394
 
        }
395
 
 
396
 
        return True;
397
 
}
398
 
 
399
 
 
400
 
BOOL net_find_pdc(struct in_addr *server_ip, fstring server_name, const char *domain_name)
401
 
{
402
 
        if (get_pdc_ip(domain_name, server_ip)) {
403
 
                if (is_zero_ip(*server_ip))
404
 
                        return False;
405
 
                
406
 
                if (!name_status_find(domain_name, 0x1b, 0x20, *server_ip, server_name))
407
 
                        return False;
408
 
                        
409
 
                return True;    
410
 
        } 
411
 
        else
412
 
                return False;
413
 
}
414
 
 
415
 
struct cli_state *net_make_ipc_connection( unsigned flags )
416
 
{
417
 
        return net_make_ipc_connection_ex( NULL, NULL, NULL, flags );
418
 
}
419
 
 
420
 
struct cli_state *net_make_ipc_connection_ex( const char *domain, const char *server,
421
 
                                              struct in_addr *ip, unsigned flags)
422
 
{
423
 
        char *server_name = NULL;
424
 
        struct in_addr server_ip;
425
 
        struct cli_state *cli = NULL;
426
 
        NTSTATUS nt_status;
427
 
 
428
 
        if ( !server || !ip ) {
429
 
                if (!net_find_server(domain, flags, &server_ip, &server_name)) {
430
 
                        d_fprintf(stderr, "Unable to find a suitable server\n");
431
 
                        return NULL;
432
 
                }
433
 
        } else {
434
 
                server_name = SMB_STRDUP( server );
435
 
                server_ip = *ip;
436
 
        }
437
 
 
438
 
        if (flags & NET_FLAGS_ANONYMOUS) {
439
 
                nt_status = connect_to_ipc_anonymous(&cli, &server_ip, server_name);
440
 
        } else {
441
 
                nt_status = connect_to_ipc(&cli, &server_ip, server_name);
442
 
        }
443
 
 
444
 
        /* store the server in the affinity cache if it was a PDC */
445
 
 
446
 
        if ( (flags & NET_FLAGS_PDC) && NT_STATUS_IS_OK(nt_status) )
447
 
                saf_store( cli->server_domain, cli->desthost );
448
 
 
449
 
        SAFE_FREE(server_name);
450
 
        if (NT_STATUS_IS_OK(nt_status)) {
451
 
                return cli;
452
 
        } else {
453
 
                d_fprintf(stderr, "Connection failed: %s\n",
454
 
                          nt_errstr(nt_status));
455
 
                return NULL;
456
 
        }
457
 
}
458
 
 
459
 
static int net_user(int argc, const char **argv)
460
 
{
461
 
        if (net_ads_check() == 0)
462
 
                return net_ads_user(argc, argv);
463
 
 
464
 
        /* if server is not specified, default to PDC? */
465
 
        if (net_rpc_check(NET_FLAGS_PDC))
466
 
                return net_rpc_user(argc, argv);
467
 
 
468
 
        return net_rap_user(argc, argv);
469
 
}
470
 
 
471
 
static int net_group(int argc, const char **argv)
472
 
{
473
 
        if (net_ads_check() == 0)
474
 
                return net_ads_group(argc, argv);
475
 
 
476
 
        if (argc == 0 && net_rpc_check(NET_FLAGS_PDC))
477
 
                return net_rpc_group(argc, argv);
478
 
 
479
 
        return net_rap_group(argc, argv);
480
 
}
481
 
 
482
 
static int net_join(int argc, const char **argv)
483
 
{
484
 
        if (net_ads_check() == 0) {
485
 
                if (net_ads_join(argc, argv) == 0)
486
 
                        return 0;
487
 
                else
488
 
                        d_fprintf(stderr, "ADS join did not work, falling back to RPC...\n");
489
 
        }
490
 
        return net_rpc_join(argc, argv);
491
 
}
492
 
 
493
 
static int net_changetrustpw(int argc, const char **argv)
494
 
{
495
 
        if (net_ads_check() == 0)
496
 
                return net_ads_changetrustpw(argc, argv);
497
 
 
498
 
        return net_rpc_changetrustpw(argc, argv);
499
 
}
500
 
 
501
 
static void set_line_buffering(FILE *f)
502
 
{
503
 
        setvbuf(f, NULL, _IOLBF, 0);
504
 
}
505
 
 
506
 
static int net_changesecretpw(int argc, const char **argv)
507
 
{
508
 
        char *trust_pw;
509
 
        uint32 sec_channel_type = SEC_CHAN_WKSTA;
510
 
 
511
 
        if(opt_force) {
512
 
                if (opt_stdin) {
513
 
                        set_line_buffering(stdin);
514
 
                        set_line_buffering(stdout);
515
 
                        set_line_buffering(stderr);
516
 
                }
517
 
 
518
 
                trust_pw = get_pass("Enter machine password: ", opt_stdin);
519
 
 
520
 
                if (!secrets_store_machine_password(trust_pw, lp_workgroup(), sec_channel_type)) {
521
 
                            d_fprintf(stderr, "Unable to write the machine account password in the secrets database");
522
 
                            return 1;
523
 
                }
524
 
                else {
525
 
                    d_printf("Modified trust account password in secrets database\n");
526
 
                }
527
 
        }
528
 
        else {
529
 
                d_printf("Machine account password change requires the -f flag.\n");
530
 
                d_printf("Do NOT use this function unless you know what it does!\n");
531
 
                d_printf("This function will change the ADS Domain member machine account password in the secrets.tdb file!\n");
532
 
        }
533
 
 
534
 
        return 0;
535
 
}
536
 
 
537
 
static int net_share(int argc, const char **argv)
538
 
{
539
 
        if (net_rpc_check(0))
540
 
                return net_rpc_share(argc, argv);
541
 
        return net_rap_share(argc, argv);
542
 
}
543
 
 
544
 
static int net_file(int argc, const char **argv)
545
 
{
546
 
        if (net_rpc_check(0))
547
 
                return net_rpc_file(argc, argv);
548
 
        return net_rap_file(argc, argv);
549
 
}
550
 
 
551
 
/*
552
 
 Retrieve our local SID or the SID for the specified name
553
 
 */
554
 
static int net_getlocalsid(int argc, const char **argv)
555
 
{
556
 
        DOM_SID sid;
557
 
        const char *name;
558
 
        fstring sid_str;
559
 
 
560
 
        if (argc >= 1) {
561
 
                name = argv[0];
562
 
        }
563
 
        else {
564
 
                name = global_myname();
565
 
        }
566
 
 
567
 
        if(!initialize_password_db(False)) {
568
 
                DEBUG(0, ("WARNING: Could not open passdb - local sid may not reflect passdb\n"
569
 
                          "backend knowlege (such as the sid stored in LDAP)\n"));
570
 
        }
571
 
 
572
 
        /* first check to see if we can even access secrets, so we don't
573
 
           panic when we can't. */
574
 
 
575
 
        if (!secrets_init()) {
576
 
                d_fprintf(stderr, "Unable to open secrets.tdb.  Can't fetch domain SID for name: %s\n", name);
577
 
                return 1;
578
 
        }
579
 
 
580
 
        /* Generate one, if it doesn't exist */
581
 
        get_global_sam_sid();
582
 
 
583
 
        if (!secrets_fetch_domain_sid(name, &sid)) {
584
 
                DEBUG(0, ("Can't fetch domain SID for name: %s\n", name));
585
 
                return 1;
586
 
        }
587
 
        sid_to_string(sid_str, &sid);
588
 
        d_printf("SID for domain %s is: %s\n", name, sid_str);
589
 
        return 0;
590
 
}
591
 
 
592
 
static int net_setlocalsid(int argc, const char **argv)
593
 
{
594
 
        DOM_SID sid;
595
 
 
596
 
        if ( (argc != 1)
597
 
             || (strncmp(argv[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0)
598
 
             || (!string_to_sid(&sid, argv[0]))
599
 
             || (sid.num_auths != 4)) {
600
 
                d_printf("usage: net setlocalsid S-1-5-21-x-y-z\n");
601
 
                return 1;
602
 
        }
603
 
 
604
 
        if (!secrets_store_domain_sid(global_myname(), &sid)) {
605
 
                DEBUG(0,("Can't store domain SID as a pdc/bdc.\n"));
606
 
                return 1;
607
 
        }
608
 
 
609
 
        return 0;
610
 
}
611
 
 
612
 
static int net_setdomainsid(int argc, const char **argv)
613
 
{
614
 
        DOM_SID sid;
615
 
 
616
 
        if ( (argc != 1)
617
 
             || (strncmp(argv[0], "S-1-5-21-", strlen("S-1-5-21-")) != 0)
618
 
             || (!string_to_sid(&sid, argv[0]))
619
 
             || (sid.num_auths != 4)) {
620
 
                d_printf("usage: net setdomainsid S-1-5-21-x-y-z\n");
621
 
                return 1;
622
 
        }
623
 
 
624
 
        if (!secrets_store_domain_sid(lp_workgroup(), &sid)) {
625
 
                DEBUG(0,("Can't store domain SID.\n"));
626
 
                return 1;
627
 
        }
628
 
 
629
 
        return 0;
630
 
}
631
 
 
632
 
static int net_getdomainsid(int argc, const char **argv)
633
 
{
634
 
        DOM_SID domain_sid;
635
 
        fstring sid_str;
636
 
 
637
 
        if(!initialize_password_db(False)) {
638
 
                DEBUG(0, ("WARNING: Could not open passdb - domain sid may not reflect passdb\n"
639
 
                          "backend knowlege (such as the sid stored in LDAP)\n"));
640
 
        }
641
 
 
642
 
        /* Generate one, if it doesn't exist */
643
 
        get_global_sam_sid();
644
 
 
645
 
        if (!secrets_fetch_domain_sid(global_myname(), &domain_sid)) {
646
 
                d_fprintf(stderr, "Could not fetch local SID\n");
647
 
                return 1;
648
 
        }
649
 
        sid_to_string(sid_str, &domain_sid);
650
 
        d_printf("SID for domain %s is: %s\n", global_myname(), sid_str);
651
 
 
652
 
        if (!secrets_fetch_domain_sid(opt_workgroup, &domain_sid)) {
653
 
                d_fprintf(stderr, "Could not fetch domain SID\n");
654
 
                return 1;
655
 
        }
656
 
 
657
 
        sid_to_string(sid_str, &domain_sid);
658
 
        d_printf("SID for domain %s is: %s\n", opt_workgroup, sid_str);
659
 
 
660
 
        return 0;
661
 
}
662
 
 
663
 
#ifdef WITH_FAKE_KASERVER
664
 
 
665
 
int net_help_afs(int argc, const char **argv)
666
 
{
667
 
        d_printf("  net afs key filename\n"
668
 
                 "\tImports a OpenAFS KeyFile into our secrets.tdb\n\n");
669
 
        d_printf("  net afs impersonate <user> <cell>\n"
670
 
                 "\tCreates a token for user@cell\n\n");
671
 
        return -1;
672
 
}
673
 
 
674
 
static int net_afs_key(int argc, const char **argv)
675
 
{
676
 
        int fd;
677
 
        struct afs_keyfile keyfile;
678
 
 
679
 
        if (argc != 2) {
680
 
                d_printf("usage: 'net afs key <keyfile> cell'\n");
681
 
                return -1;
682
 
        }
683
 
 
684
 
        if (!secrets_init()) {
685
 
                d_fprintf(stderr, "Could not open secrets.tdb\n");
686
 
                return -1;
687
 
        }
688
 
 
689
 
        if ((fd = open(argv[0], O_RDONLY, 0)) < 0) {
690
 
                d_fprintf(stderr, "Could not open %s\n", argv[0]);
691
 
                return -1;
692
 
        }
693
 
 
694
 
        if (read(fd, &keyfile, sizeof(keyfile)) != sizeof(keyfile)) {
695
 
                d_fprintf(stderr, "Could not read keyfile\n");
696
 
                return -1;
697
 
        }
698
 
 
699
 
        if (!secrets_store_afs_keyfile(argv[1], &keyfile)) {
700
 
                d_fprintf(stderr, "Could not write keyfile to secrets.tdb\n");
701
 
                return -1;
702
 
        }
703
 
 
704
 
        return 0;
705
 
}
706
 
 
707
 
static int net_afs_impersonate(int argc, const char **argv)
708
 
{
709
 
        char *token;
710
 
 
711
 
        if (argc != 2) {
712
 
                fprintf(stderr, "Usage: net afs impersonate <user> <cell>\n");
713
 
                exit(1);
714
 
        }
715
 
 
716
 
        token = afs_createtoken_str(argv[0], argv[1]);
717
 
 
718
 
        if (token == NULL) {
719
 
                fprintf(stderr, "Could not create token\n");
720
 
                exit(1);
721
 
        }
722
 
 
723
 
        if (!afs_settoken_str(token)) {
724
 
                fprintf(stderr, "Could not set token into kernel\n");
725
 
                exit(1);
726
 
        }
727
 
 
728
 
        printf("Success: %s@%s\n", argv[0], argv[1]);
729
 
        return 0;
730
 
}
731
 
 
732
 
static int net_afs(int argc, const char **argv)
733
 
{
734
 
        struct functable func[] = {
735
 
                {"key", net_afs_key},
736
 
                {"impersonate", net_afs_impersonate},
737
 
                {"help", net_help_afs},
738
 
                {NULL, NULL}
739
 
        };
740
 
        return net_run_function(argc, argv, func, net_help_afs);
741
 
}
742
 
 
743
 
#endif /* WITH_FAKE_KASERVER */
744
 
 
745
 
static BOOL search_maxrid(struct pdb_search *search, const char *type,
746
 
                          uint32 *max_rid)
747
 
{
748
 
        struct samr_displayentry *entries;
749
 
        uint32 i, num_entries;
750
 
 
751
 
        if (search == NULL) {
752
 
                d_fprintf(stderr, "get_maxrid: Could not search %s\n", type);
753
 
                return False;
754
 
        }
755
 
 
756
 
        num_entries = pdb_search_entries(search, 0, 0xffffffff, &entries);
757
 
        for (i=0; i<num_entries; i++)
758
 
                *max_rid = MAX(*max_rid, entries[i].rid);
759
 
        pdb_search_destroy(search);
760
 
        return True;
761
 
}
762
 
 
763
 
static uint32 get_maxrid(void)
764
 
{
765
 
        uint32 max_rid = 0;
766
 
 
767
 
        if (!search_maxrid(pdb_search_users(0), "users", &max_rid))
768
 
                return 0;
769
 
 
770
 
        if (!search_maxrid(pdb_search_groups(), "groups", &max_rid))
771
 
                return 0;
772
 
 
773
 
        if (!search_maxrid(pdb_search_aliases(get_global_sam_sid()),
774
 
                           "aliases", &max_rid))
775
 
                return 0;
776
 
        
777
 
        return max_rid;
778
 
}
779
 
 
780
 
static int net_maxrid(int argc, const char **argv)
781
 
{
782
 
        uint32 rid;
783
 
 
784
 
        if (argc != 0) {
785
 
                DEBUG(0, ("usage: net maxrid\n"));
786
 
                return 1;
787
 
        }
788
 
 
789
 
        if ((rid = get_maxrid()) == 0) {
790
 
                DEBUG(0, ("can't get current maximum rid\n"));
791
 
                return 1;
792
 
        }
793
 
 
794
 
        d_printf("Currently used maximum rid: %d\n", rid);
795
 
 
796
 
        return 0;
797
 
}
798
 
 
799
 
/* main function table */
800
 
static struct functable net_func[] = {
801
 
        {"RPC", net_rpc},
802
 
        {"RAP", net_rap},
803
 
        {"ADS", net_ads},
804
 
 
805
 
        /* eventually these should auto-choose the transport ... */
806
 
        {"FILE", net_file},
807
 
        {"SHARE", net_share},
808
 
        {"SESSION", net_rap_session},
809
 
        {"SERVER", net_rap_server},
810
 
        {"DOMAIN", net_rap_domain},
811
 
        {"PRINTQ", net_rap_printq},
812
 
        {"USER", net_user},
813
 
        {"GROUP", net_group},
814
 
        {"GROUPMAP", net_groupmap},
815
 
        {"SAM", net_sam},
816
 
        {"VALIDATE", net_rap_validate},
817
 
        {"GROUPMEMBER", net_rap_groupmember},
818
 
        {"ADMIN", net_rap_admin},
819
 
        {"SERVICE", net_rap_service},   
820
 
        {"PASSWORD", net_rap_password},
821
 
        {"CHANGETRUSTPW", net_changetrustpw},
822
 
        {"CHANGESECRETPW", net_changesecretpw},
823
 
        {"TIME", net_time},
824
 
        {"LOOKUP", net_lookup},
825
 
        {"JOIN", net_join},
826
 
        {"CACHE", net_cache},
827
 
        {"GETLOCALSID", net_getlocalsid},
828
 
        {"SETLOCALSID", net_setlocalsid},
829
 
        {"SETDOMAINSID", net_setdomainsid},
830
 
        {"GETDOMAINSID", net_getdomainsid},
831
 
        {"MAXRID", net_maxrid},
832
 
        {"IDMAP", net_idmap},
833
 
        {"STATUS", net_status},
834
 
        {"USERSHARE", net_usershare},
835
 
        {"USERSIDLIST", net_usersidlist},
836
 
#ifdef WITH_FAKE_KASERVER
837
 
        {"AFS", net_afs},
838
 
#endif
839
 
 
840
 
        {"HELP", net_help},
841
 
        {NULL, NULL}
842
 
};
843
 
 
844
 
 
845
 
/****************************************************************************
846
 
  main program
847
 
****************************************************************************/
848
 
 int main(int argc, const char **argv)
849
 
{
850
 
        int opt,i;
851
 
        char *p;
852
 
        int rc = 0;
853
 
        int argc_new = 0;
854
 
        const char ** argv_new;
855
 
        poptContext pc;
856
 
 
857
 
        struct poptOption long_options[] = {
858
 
                {"help",        'h', POPT_ARG_NONE,   0, 'h'},
859
 
                {"workgroup",   'w', POPT_ARG_STRING, &opt_target_workgroup},
860
 
                {"user",        'U', POPT_ARG_STRING, &opt_user_name, 'U'},
861
 
                {"ipaddress",   'I', POPT_ARG_STRING, 0,'I'},
862
 
                {"port",        'p', POPT_ARG_INT,    &opt_port},
863
 
                {"myname",      'n', POPT_ARG_STRING, &opt_requester_name},
864
 
                {"server",      'S', POPT_ARG_STRING, &opt_host},
865
 
                {"container",   'c', POPT_ARG_STRING, &opt_container},
866
 
                {"comment",     'C', POPT_ARG_STRING, &opt_comment},
867
 
                {"maxusers",    'M', POPT_ARG_INT,    &opt_maxusers},
868
 
                {"flags",       'F', POPT_ARG_INT,    &opt_flags},
869
 
                {"long",        'l', POPT_ARG_NONE,   &opt_long_list_entries},
870
 
                {"reboot",      'r', POPT_ARG_NONE,   &opt_reboot},
871
 
                {"force",       'f', POPT_ARG_NONE,   &opt_force},
872
 
                {"stdin",       'i', POPT_ARG_NONE,   &opt_stdin},
873
 
                {"timeout",     't', POPT_ARG_INT,    &opt_timeout},
874
 
                {"machine-pass",'P', POPT_ARG_NONE,   &opt_machine_pass},
875
 
                {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup},
876
 
                {"verbose",     'v', POPT_ARG_NONE,   &opt_verbose},
877
 
                /* Options for 'net groupmap set' */
878
 
                {"local",       'L', POPT_ARG_NONE,   &opt_localgroup},
879
 
                {"domain",      'D', POPT_ARG_NONE,   &opt_domaingroup},
880
 
                {"ntname",      'N', POPT_ARG_STRING, &opt_newntname},
881
 
                {"rid",         'R', POPT_ARG_INT,    &opt_rid},
882
 
                /* Options for 'net rpc share migrate' */
883
 
                {"acls",        0, POPT_ARG_NONE,     &opt_acls},
884
 
                {"attrs",       0, POPT_ARG_NONE,     &opt_attrs},
885
 
                {"timestamps",  0, POPT_ARG_NONE,     &opt_timestamps},
886
 
                {"exclude",     'e', POPT_ARG_STRING, &opt_exclude},
887
 
                {"destination", 0, POPT_ARG_STRING,   &opt_destination},
888
 
                {"tallocreport", 0, POPT_ARG_NONE, &do_talloc_report},
889
 
 
890
 
                POPT_COMMON_SAMBA
891
 
                { 0, 0, 0, 0}
892
 
        };
893
 
 
894
 
        zero_ip(&opt_dest_ip);
895
 
 
896
 
        load_case_tables();
897
 
 
898
 
        /* set default debug level to 0 regardless of what smb.conf sets */
899
 
        DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
900
 
        dbf = x_stderr;
901
 
        
902
 
        pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
903
 
                            POPT_CONTEXT_KEEP_FIRST);
904
 
        
905
 
        while((opt = poptGetNextOpt(pc)) != -1) {
906
 
                switch (opt) {
907
 
                case 'h':
908
 
                        net_help(argc, argv);
909
 
                        exit(0);
910
 
                        break;
911
 
                case 'I':
912
 
                        opt_dest_ip = *interpret_addr2(poptGetOptArg(pc));
913
 
                        if (is_zero_ip(opt_dest_ip))
914
 
                                d_fprintf(stderr, "\nInvalid ip address specified\n");
915
 
                        else
916
 
                                opt_have_ip = True;
917
 
                        break;
918
 
                case 'U':
919
 
                        opt_user_specified = True;
920
 
                        opt_user_name = SMB_STRDUP(opt_user_name);
921
 
                        p = strchr(opt_user_name,'%');
922
 
                        if (p) {
923
 
                                *p = 0;
924
 
                                opt_password = p+1;
925
 
                        }
926
 
                        break;
927
 
                default:
928
 
                        d_fprintf(stderr, "\nInvalid option %s: %s\n", 
929
 
                                 poptBadOption(pc, 0), poptStrerror(opt));
930
 
                        net_help(argc, argv);
931
 
                        exit(1);
932
 
                }
933
 
        }
934
 
        
935
 
        /*
936
 
         * Don't load debug level from smb.conf. It should be
937
 
         * set by cmdline arg or remain default (0)
938
 
         */
939
 
        AllowDebugChange = False;
940
 
        lp_load(dyn_CONFIGFILE,True,False,False,True);
941
 
        
942
 
        argv_new = (const char **)poptGetArgs(pc);
943
 
 
944
 
        argc_new = argc;
945
 
        for (i=0; i<argc; i++) {
946
 
                if (argv_new[i] == NULL) {
947
 
                        argc_new = i;
948
 
                        break;
949
 
                }
950
 
        }
951
 
 
952
 
        if (do_talloc_report) {
953
 
                talloc_enable_leak_report();
954
 
        }
955
 
 
956
 
        if (opt_requester_name) {
957
 
                set_global_myname(opt_requester_name);
958
 
        }
959
 
 
960
 
        if (!opt_user_name && getenv("LOGNAME")) {
961
 
                opt_user_name = getenv("LOGNAME");
962
 
        }
963
 
 
964
 
        if (!opt_workgroup) {
965
 
                opt_workgroup = smb_xstrdup(lp_workgroup());
966
 
        }
967
 
        
968
 
        if (!opt_target_workgroup) {
969
 
                opt_target_workgroup = smb_xstrdup(lp_workgroup());
970
 
        }
971
 
        
972
 
        if (!init_names())
973
 
                exit(1);
974
 
 
975
 
        load_interfaces();
976
 
        
977
 
        /* this makes sure that when we do things like call scripts, 
978
 
           that it won't assert becouse we are not root */
979
 
        sec_init();
980
 
 
981
 
        if (opt_machine_pass) {
982
 
                /* it is very useful to be able to make ads queries as the
983
 
                   machine account for testing purposes and for domain leave */
984
 
 
985
 
                net_use_machine_password();
986
 
        }
987
 
 
988
 
        if (!opt_password) {
989
 
                opt_password = getenv("PASSWD");
990
 
        }
991
 
         
992
 
        rc = net_run_function(argc_new-1, argv_new+1, net_func, net_help);
993
 
        
994
 
        DEBUG(2,("return code = %d\n", rc));
995
 
        return rc;
996
 
}