summaryrefslogtreecommitdiffstats
path: root/source3/rpcclient/rpcclient.c
blob: f0f4a6de5c958eeae05e94ee371b40a3482203ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
/* 
   Unix SMB/Netbios implementation.
   Version 1.9.
   SMB client
   Copyright (C) Andrew Tridgell 1994-1998
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifdef SYSLOG
#undef SYSLOG
#endif

#include "includes.h"

#ifndef REGISTER
#define REGISTER 0
#endif

extern pstring debugf;
extern pstring scope;
extern pstring global_myname;

extern pstring user_socket_options;


extern int DEBUGLEVEL;


#define CNV_LANG(s) dos2unix_format(s,False)
#define CNV_INPUT(s) unix2dos_format(s,True)

static int process_tok(fstring tok);
static void cmd_help(struct client_info *info, int argc, char *argv[]);
static void cmd_quit(struct client_info *info, int argc, char *argv[]);
static void cmd_set (struct client_info *info, int argc, char *argv[]);

static struct user_credentials usr;

static struct cli_state smbcli;
struct cli_state *smb_cli = &smbcli;

static struct client_info cli_info;

static char  **cmd_argv = NULL;
static uint32 cmd_argc = 0;

FILE *out_hnd;

#define COMPL_NONE 0
#define COMPL_REGKEY 1
#define COMPL_SAMUSR 3
#define COMPL_SAMGRP 4
#define COMPL_SAMALS 5
#define COMPL_SVCLST 6
#define COMPL_PRTLST 7

/****************************************************************************
 This defines the commands supported by this client
 ****************************************************************************/
struct
{
	char *name;
	void (*fn)(struct client_info*, int, char*[]);
	char *description;
	char compl_args[2];
}
commands[] = 
{
	/*
	 * eventlog
	 */

	{
		"eventlog",
		cmd_eventlog,
		"list the events",
		{COMPL_NONE, COMPL_NONE}
	},

	/*
	 * service control
	 */

	{
		"svcenum",
		cmd_svc_enum,
		"[-i] Lists Services Manager",
		{COMPL_NONE, COMPL_NONE}
	},

	{
		"svcinfo",
		cmd_svc_info,
		"<service> Service Information",
		{COMPL_SVCLST, COMPL_NONE}
	},

	{
		"svcstart",
		cmd_svc_start,
		"<service> [arg 0] [arg 1] ... Start Service",
		{COMPL_SVCLST, COMPL_NONE}
	},

	{
		"svcset",
		cmd_svc_set,
		"<service> Test Set Service",
		{COMPL_SVCLST, COMPL_NONE}
	},

	{
		"svcstop",
		cmd_svc_stop,
		"<service> Stop Service",
		{COMPL_SVCLST, COMPL_NONE}
	},

	/*
	 * scheduler
	 */

	{
		"at",
		cmd_at,
		"Scheduler control (at /? for syntax)",
		{COMPL_NONE, COMPL_NONE}
	},

	/*
	 * registry
	 */

	{
		"regenum",
		cmd_reg_enum,
		"<keyname> Registry Enumeration (keys, values)",
		{COMPL_REGKEY, COMPL_NONE}
	},
	{
		"regdeletekey",
		cmd_reg_delete_key,
		"<keyname> Registry Key Delete",
		{COMPL_REGKEY, COMPL_NONE}
	},
	{
		"regcreatekey",
		cmd_reg_create_key,
		"<keyname> [keyclass] Registry Key Create",
		{COMPL_REGKEY, COMPL_NONE}
	},
	{
		"shutdown",
		cmd_reg_shutdown,
		"[-m message] [-t timeout] [-r or --reboot] [-f or --force-close] Remote Shutdown",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"regqueryval",
		cmd_reg_query_info,
		"<valname> Registry Value Query",
		{COMPL_REGKEY, COMPL_NONE}
	},
	{
		"regquerykey",
		cmd_reg_query_key,
		"<keyname> Registry Key Query",
		{COMPL_REGKEY, COMPL_NONE}
	},
	{
		"regdeleteval",
		cmd_reg_delete_val,
		"<valname> Registry Value Delete",
		{COMPL_REGKEY, COMPL_REGKEY}
	},
	{
		"regcreateval",
		cmd_reg_create_val,
		"<valname> <valtype> <value> Registry Key Create",
		{COMPL_REGKEY, COMPL_NONE}
	},
	{
		"reggetsec",
		cmd_reg_get_key_sec,
		"<keyname> Registry Key Security",
		{COMPL_REGKEY, COMPL_NONE}
	},
	{
		"regtestsec",
		cmd_reg_test_key_sec,
		"<keyname> Test Registry Key Security",
		{COMPL_REGKEY, COMPL_NONE}
	},

	/*
	 * printer testing
	 */

	{
		"spoolenum",
		cmd_spoolss_enum_printers,
		"Enumerate Printers",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"spooljobs",
		cmd_spoolss_enum_jobs,
		"<printer name> Enumerate Printer Jobs",
		{COMPL_PRTLST, COMPL_NONE}
	},
	{
		"spoolopen",
		cmd_spoolss_open_printer_ex,
		"<printer name> Spool Printer Open Test",
		{COMPL_PRTLST, COMPL_NONE}
	},
	/*
	 * server
	 */
	{
		"time",
		cmd_time,
		"Display remote time",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"brsinfo",
		cmd_brs_query_info,
		"Browser Query Info",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"wksinfo",
		cmd_wks_query_info,
		"Workstation Query Info",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"srvinfo",
		cmd_srv_query_info,
		"Server Query Info",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"srvsessions",
		cmd_srv_enum_sess,
		"List sessions on a server",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"srvshares",
		cmd_srv_enum_shares,
		"List shares on a server",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"srvtransports",
		cmd_srv_enum_tprt,
		"List transports on a server",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"srvconnections",
		cmd_srv_enum_conn,
		"List connections on a server",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"srvfiles",
		cmd_srv_enum_files,
		"List files on a server",
		{COMPL_NONE, COMPL_NONE}
	},

	/*
	 * lsa
	 */

	{
		"lsaquery",
		cmd_lsa_query_info,
		"Query Info Policy (domain member or server)",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"lsaenumdomains",
		cmd_lsa_enum_trust_dom,
		"Enumerate Trusted Domains",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"lookupsids",
		cmd_lsa_lookup_sids,
		"Resolve names from SIDs",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"lookupnames",
		cmd_lsa_lookup_names,
		"Resolve SIDs from names",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"querysecret",
		cmd_lsa_query_secret,
		"LSA Query Secret (developer use)",
		{COMPL_NONE, COMPL_NONE}
	},

	/*
	 * netlogon
	 */

	{
		"ntlogin",
		cmd_netlogon_login_test,
		"[username] [password] NT Domain login test",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"domtrust",
		cmd_netlogon_domain_test,
		"<domain> NT Inter-Domain test",
		{COMPL_NONE, COMPL_NONE}
	},

	/*
	 * sam
	 */

	{
		"lookupdomain",
		cmd_sam_lookup_domain,
		"Obtain SID for a local domain",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"enumusers",
		cmd_sam_enum_users,
		"SAM User Database Query (experimental!)",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"addgroupmem",
		cmd_sam_add_groupmem,
		"<group rid> [user] [user] ... SAM Add Domain Group Member",
		{COMPL_SAMGRP, COMPL_SAMUSR}
	},

	{
		"addaliasmem",
		cmd_sam_add_aliasmem,
		"<alias rid> [member sid1] [member sid2] ... SAM Add Domain Alias Member",
		{COMPL_SAMALS, COMPL_NONE}
	},
	{
		"delgroupmem",
		cmd_sam_del_groupmem,
		"<group rid> [user] [user] ... SAM Delete Domain Group Member",
		{COMPL_SAMGRP, COMPL_SAMUSR}
	},
	{
		"delaliasmem",
		cmd_sam_del_aliasmem,
		"<alias rid> [member sid1] [member sid2] ... SAM Delete Domain Alias Member",
		{COMPL_SAMALS, COMPL_NONE}
	},
	{
		"creategroup",
		cmd_sam_create_dom_group,
		"SAM Create Domain Group",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"createalias",
		cmd_sam_create_dom_alias,
		"SAM Create Domain Alias",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"createuser",
		cmd_sam_create_dom_user,
		"<username> SAM Create Domain User",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"delgroup",
		cmd_sam_delete_dom_group,
		"SAM Delete Domain Group",
		{COMPL_SAMGRP, COMPL_NONE}
	},
	{
		"delalias",
		cmd_sam_delete_dom_alias,
		"SAM Delete Domain Alias",
		{COMPL_SAMALS, COMPL_NONE}
	},
	{
		"ntpass",
		cmd_sam_ntchange_pwd,
		"NT SAM Password Change",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"samuserset2",
		cmd_sam_set_userinfo2,
		"<username> [-s acb_bits] SAM User Set Info 2 (experimental!)",
		{COMPL_SAMUSR, COMPL_NONE}
	},
	{
		"samuserset",
		cmd_sam_set_userinfo,
		"<username> [-p password] SAM User Set Info (experimental!)",
		{COMPL_SAMUSR, COMPL_NONE}
	},
	{
		"samuser",
		cmd_sam_query_user,
		"<username> SAM User Query (experimental!)",
		{COMPL_SAMUSR, COMPL_NONE}
	},
	{
		"samgroup",
		cmd_sam_query_group,
		"<groupname> SAM Group Query (experimental!)",
		{COMPL_SAMGRP, COMPL_NONE}
	},
	{
		"samalias",
		cmd_sam_query_alias,
		"<aliasname> SAM Alias Query",
		{COMPL_SAMALS, COMPL_NONE}
	},
	{
		"samaliasmem",
		cmd_sam_query_aliasmem,
		"<aliasname> SAM Alias Members",
		{COMPL_SAMALS, COMPL_NONE}
	},
	{
		"samgroupmem",
		cmd_sam_query_groupmem,
		"SAM Group Members",
		{COMPL_SAMGRP, COMPL_NONE}
	},
	{
		"samtest",
		cmd_sam_test      ,
		"SAM User Encrypted RPC test (experimental!)",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"enumaliases",
		cmd_sam_enum_aliases,
		"SAM Aliases Database Query (experimental!)",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"enumdomains",
		cmd_sam_enum_domains,
		"SAM Domains Database Query (experimental!)",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"enumgroups",
		cmd_sam_enum_groups,
		"SAM Group Database Query (experimental!)",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"dominfo",
		cmd_sam_query_dominfo,
		"SAM Query Domain Info",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"dispinfo",
		cmd_sam_query_dispinfo,
		"SAM Query Display Info",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"samsync",
		cmd_sam_sync,
		"SAM Synchronization Test (experimental)",
		{COMPL_NONE, COMPL_NONE}
	},

	{
		"rpcclient",
		cmd_set,
		"run rpcclient inside rpcclient (change options etc.)",
		{COMPL_NONE, COMPL_NONE}
	},
	/*
	 * bye bye
	 */

	{
		"quit",
		cmd_quit,
		"logoff the server",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"q",
		cmd_quit,
		"logoff the server",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"exit",
		cmd_quit,
		"logoff the server",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"bye",
		cmd_quit,
		"logoff the server",
		{COMPL_NONE, COMPL_NONE}
	},

	/*
	 * eek!
	 */

	{
		"help",
		cmd_help,
		"[command] give help on a command",
		{COMPL_NONE, COMPL_NONE}
	},
	{
		"?",
		cmd_help,
		"[command] give help on a command",
		{COMPL_NONE, COMPL_NONE}
	},

	/*
	 * shell
	 */

	{
		"!",
		NULL,
		"run a shell command on the local system",
		{COMPL_NONE, COMPL_NONE}
	},

	/*
	 * oop!
	 */

	{
		"",
		NULL,
		NULL,
		{COMPL_NONE, COMPL_NONE}
	}
};


/****************************************************************************
do a (presumably graceful) quit...
****************************************************************************/
static void cmd_quit(struct client_info *info, int argc, char *argv[])
{
#ifdef MEM_MAN
	{
		extern FILE* dbf;
		smb_mem_write_status(dbf);
		smb_mem_write_errors(dbf);
		smb_mem_write_verbose(dbf);
		dbgflush();
	}
#endif
	exit(0);
}

/****************************************************************************
help
****************************************************************************/
static void cmd_help(struct client_info *info, int argc, char *argv[])
{
  int i=0,j;

  if (argc > 1)
    {
      if ((i = process_tok(argv[1])) >= 0)
	fprintf(out_hnd, "HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);		    
    }
  else
    while (commands[i].description)
      {
	for (j=0; commands[i].description && (j<5); j++) {
	  fprintf(out_hnd, "%-15s",commands[i].name);
	  i++;
	}
	fprintf(out_hnd, "\n");
      }
}

/*******************************************************************
  lookup a command string in the list of commands, including 
  abbreviations
  ******************************************************************/
static int process_tok(char *tok)
{
  int i = 0, matches = 0;
  int cmd=0;
  int tok_len = strlen(tok);
  
  while (commands[i].fn != NULL)
    {
      if (strequal(commands[i].name,tok))
	{
	  matches = 1;
	  cmd = i;
	  break;
	}
      else if (strnequal(commands[i].name, tok, tok_len))
	{
	  matches++;
	  cmd = i;
	}
      i++;
    }
  
  if (matches == 0)
    return(-1);
  else if (matches == 1)
    return(cmd);
  else
    return(-2);
}

/****************************************************************************
  turn command line into command argument array
****************************************************************************/
static BOOL get_cmd_args(char *line)
{
	char *ptr = line;
	pstring tok;
	cmd_argc = 0;
	cmd_argv = NULL;

	/* get the first part of the command */
	if (!next_token(&ptr,tok,NULL, sizeof(tok)))
	{
		return False;
	}

	do
	{
		add_chars_to_array(&cmd_argc, &cmd_argv, tok);

	} while (next_token(NULL, tok, NULL, sizeof(tok)));

	return True;
}

/* command options mask */
static uint32 cmd_set_options = 0xffffffff;

/****************************************************************************
  process commands from the client
****************************************************************************/
static BOOL do_command(struct client_info *info, char *line)
{
	int i;

	if (!get_cmd_args(line)) return False;

	if (cmd_argc == 0)
	{
		return False;
	}

	cmd_set_options = 0x0;

	if ((i = process_tok(cmd_argv[0])) >= 0)
	{
		int argc = (int)cmd_argc;
		char **argv = cmd_argv;
		optind = 0;

		commands[i].fn(info, argc, argv);
	}
	else if (i == -2)
	{
		fprintf(out_hnd, "%s: command abbreviation ambiguous\n",
		                 CNV_LANG(cmd_argv[0]));
	}
	else
	{
		fprintf(out_hnd, "%s: command not found\n",
		                 CNV_LANG(cmd_argv[0]));
	}

	free_char_array(cmd_argc, cmd_argv);

	return True;
}

#ifndef HAVE_LIBREADLINE
/****************************************************************************
wait for keyboard activity, swallowing network packets
****************************************************************************/
static void wait_keyboard(struct cli_state *cli)
{
	fd_set fds;
	struct timeval timeout;
  
	while (1) {
		FD_ZERO(&fds);
		FD_SET(cli->fd,&fds);
		FD_SET(fileno(stdin),&fds);

		timeout.tv_sec = 20;
		timeout.tv_usec = 0;
		sys_select(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
      
		if (FD_ISSET(fileno(stdin),&fds))
			return;

		/* We deliberately use receive_smb instead of
		   client_receive_smb as we want to receive
		   session keepalives and then drop them here.
		*/
		if (FD_ISSET(cli->fd,&fds))
			receive_smb(cli->fd,cli->inbuf,0);
	}  
}
#endif

/****************************************************************************
  process commands from the client
****************************************************************************/
static BOOL process( struct client_info *info, char *cmd_str)
{
	pstring line;
	char *cmd = cmd_str;

	if (cmd != NULL)
	{
		while (cmd[0] != '\0')
		{
			char *p;

			if ((p = strchr(cmd, ';')) == 0)
			{
				strncpy(line, cmd, 999);
				line[1000] = '\0';
				cmd += strlen(cmd);
			}
			else
			{
				if (p - cmd > 999) p = cmd + 999;
				strncpy(line, cmd, p - cmd);
				line[p - cmd] = '\0';
				cmd = p + 1;
			}

			/* input language code to internal one */
			CNV_INPUT (line);

			if (!do_command(info, line)) continue;
		}
	}
	else while (!feof(stdin))
	{
#ifdef HAVE_LIBREADLINE
	        pstring promptline;
#endif

#ifndef HAVE_LIBREADLINE

		/* display a prompt */
		fprintf(out_hnd, "%s$ ", CNV_LANG(cli_info.dest_host));
		fflush(out_hnd);

#ifdef CLIX
		line[0] = wait_keyboard(smb_cli);
		/* this might not be such a good idea... */
		if ( line[0] == EOF)
		{
			break;
		}
#else
		wait_keyboard(smb_cli);
#endif

		/* and get a response */
#ifdef CLIX
		fgets( &line[1],999, stdin);
#else
		if (!fgets(line,1000,stdin))
		{
			break;
		}
#endif

#else /* HAVE_LIBREADLINE */

		slprintf(promptline, sizeof(promptline) - 1, "%s$ ",
			 CNV_LANG(cli_info.dest_host));

		if (!readline(promptline))
		    break;

		/* Copy read line to samba buffer */

		pstrcpy(line, rl_line_buffer);

		/* Add to history */

		if (strlen(line) > 0) 
		    add_history(line);
#endif
		/* input language code to internal one */
		CNV_INPUT (line);

		/* special case - first char is ! */
		if (*line == '!')
		{
			system(line + 1);
			continue;
		}

		fprintf(out_hnd, "%s\n", line);

		if (!do_command(info, line)) continue;
	}

	return(True);
}

/****************************************************************************
usage on the program
****************************************************************************/
static void usage(char *pname)
{
  fprintf(out_hnd, "Usage: %s [password] [-S server] [-U user] -[W domain] [-l log] ",
	   pname);

  fprintf(out_hnd, "\nVersion %s\n",VERSION);
  fprintf(out_hnd, "\t-d debuglevel         set the debuglevel\n");
  fprintf(out_hnd, "\t-S server             connect to \\\\server\\IPC$ \n");
  fprintf(out_hnd, "\t-l log basename.      Basename for log/debug files\n");
  fprintf(out_hnd, "\t-n netbios name.      Use this name as my netbios name\n");
  fprintf(out_hnd, "\t-N                    don't ask for a password\n");
  fprintf(out_hnd, "\t-m max protocol       set the max protocol level\n");
  fprintf(out_hnd, "\t-I dest IP            use this IP to connect to\n");
  fprintf(out_hnd, "\t-E                    write messages to stderr instead of stdout\n");
  fprintf(out_hnd, "\t-U username           set the network username\n");
  fprintf(out_hnd, "\t-U username%%pass      set the network username and password\n");
  fprintf(out_hnd, "\t-W domain             set the domain name\n");
  fprintf(out_hnd, "\t-c 'command string'   execute semicolon separated commands\n");
  fprintf(out_hnd, "\t-t terminal code      terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
  fprintf(out_hnd, "\n");
}

#ifdef HAVE_LIBREADLINE

/****************************************************************************
GNU readline completion functions
****************************************************************************/

/* Complete a remote registry enum */

static uint32 reg_list_len = 0;
static char **reg_name = NULL;

static void reg_init(int val, const char *full_keyname, int num)
{
	switch (val)
	{
		case 0:
		{
			free_char_array(reg_list_len, reg_name);
			reg_list_len = 0;
			reg_name = NULL;
			break;
		}
		default:
		{
			break;
		}
	}
}

static void reg_key_list(const char *full_name,
				const char *name, time_t key_mod_time)
{
	fstring key_name;
	slprintf(key_name, sizeof(key_name)-1, "%s\\", name);
	add_chars_to_array(&reg_list_len, &reg_name, key_name);
}

static void reg_val_list(const char *full_name,
				const char* name,
				uint32 type,
				const BUFFER2 *value)
{
	add_chars_to_array(&reg_list_len, &reg_name, name);
}

static char *complete_regenum(char *text, int state)
{
	pstring full_keyname;
	static uint32 i = 0;
    
	if (state == 0)
	{
		fstring srv_name;
		fstrcpy(srv_name, "\\\\");
		fstrcat(srv_name, cli_info.dest_host);
		strupper(srv_name);

		if (cmd_argc >= 2 && cmd_argv != NULL && cmd_argv[1] != NULL)
		{
			char *sep;
			split_server_keyname(srv_name, full_keyname,
			                     cmd_argv[1]);

			sep = strrchr(full_keyname, '\\');
			if (sep != NULL)
			{
				*sep = 0;
			}
		}

		/* Iterate all keys / values */
		if (!msrpc_reg_enum_key(srv_name, full_keyname,
		                   reg_init, reg_key_list, reg_val_list))
		{
			return NULL;
		}

		i = 0;
    	}

	for (; i < reg_list_len; i++)
	{
		if (text == NULL || text[0] == 0 ||
		    strnequal(text, reg_name[i], strlen(text)))
		{
			char *name = strdup(reg_name[i]);
			i++;
			return name;
		}
	}
	
	return NULL;
}


static char *complete_samenum_usr(char *text, int state)
{
	static uint32 i = 0;
	static uint32 num_usrs = 0;
	static struct acct_info *sam = NULL;
    
	if (state == 0)
	{
		fstring srv_name;
		fstring domain;
		fstring sid;
		DOM_SID sid1;
		sid_copy(&sid1, &cli_info.dom.level5_sid);
		sid_to_string(sid, &sid1);
		fstrcpy(domain, cli_info.dom.level5_dom);

		if (sid1.num_auths == 0)
		{
			return NULL;
		}

		fstrcpy(srv_name, "\\\\");
		fstrcat(srv_name, cli_info.dest_host);
		strupper(srv_name);

		free(sam);
		sam = NULL;
		num_usrs = 0;

		/* Iterate all users */
		if (msrpc_sam_enum_users(srv_name, domain, &sid1, 
		                   &sam, &num_usrs,
		                   NULL, NULL, NULL, NULL) == 0)
		{
			return NULL;
		}

		i = 0;
    	}

	for (; i < num_usrs; i++)
	{
		char *usr_name = sam[i].acct_name;
		if (text == NULL || text[0] == 0 ||
		    strnequal(text, usr_name, strlen(text)))
		{
			char *name = strdup(usr_name);
			i++;
			return name;
		}
	}
	
	return NULL;
}

static char *complete_samenum_als(char *text, int state)
{
	static uint32 i = 0;
	static uint32 num_als = 0;
	static struct acct_info *sam = NULL;
    
	if (state == 0)
	{
		fstring srv_name;
		fstring domain;
		fstring sid;
		DOM_SID sid1;
		sid_copy(&sid1, &cli_info.dom.level5_sid);
		sid_to_string(sid, &sid1);
		fstrcpy(domain, cli_info.dom.level5_dom);

		if (sid1.num_auths == 0)
		{
			return NULL;
		}

		fstrcpy(srv_name, "\\\\");
		fstrcat(srv_name, cli_info.dest_host);
		strupper(srv_name);

		free(sam);
		sam = NULL;
		num_als = 0;

		/* Iterate all aliases */
		if (msrpc_sam_enum_aliases(srv_name, domain, &sid1, 
		                   &sam, &num_als,
		                   NULL, NULL, NULL) == 0)
		{
			return NULL;
		}

		i = 0;
    	}

	for (; i < num_als; i++)
	{
		char *als_name = sam[i].acct_name;
		if (text == NULL || text[0] == 0 ||
		    strnequal(text, als_name, strlen(text)))
		{
			char *name = strdup(als_name);
			i++;
			return name;
		}
	}
	
	return NULL;
}

static char *complete_samenum_grp(char *text, int state)
{
	static uint32 i = 0;
	static uint32 num_grps = 0;
	static struct acct_info *sam = NULL;
    
	if (state == 0)
	{
		fstring srv_name;
		fstring domain;
		fstring sid;
		DOM_SID sid1;
		sid_copy(&sid1, &cli_info.dom.level5_sid);
		sid_to_string(sid, &sid1);
		fstrcpy(domain, cli_info.dom.level5_dom);

		if (sid1.num_auths == 0)
		{
			return NULL;
		}

		fstrcpy(srv_name, "\\\\");
		fstrcat(srv_name, cli_info.dest_host);
		strupper(srv_name);

		free(sam);
		sam = NULL;
		num_grps = 0;

		/* Iterate all groups */
		if (msrpc_sam_enum_groups(srv_name,
		                   domain, &sid1, 
		                   &sam, &num_grps,
		                   NULL, NULL, NULL) == 0)
		{
			return NULL;
		}

		i = 0;
    	}

	for (; i < num_grps; i++)
	{
		char *grp_name = sam[i].acct_name;
		if (text == NULL || text[0] == 0 ||
		    strnequal(text, grp_name, strlen(text)))
		{
			char *name = strdup(grp_name);
			i++;
			return name;
		}
	}
	
	return NULL;
}

static char *complete_svcenum(char *text, int state)
{
	static uint32 i = 0;
	static uint32 num_svcs = 0;
	static ENUM_SRVC_STATUS *svc = NULL;
	fstring srv_name;

	fstrcpy(srv_name, "\\\\");
	fstrcat(srv_name, cli_info.dest_host);
	strupper(srv_name);

    
	if (state == 0)
	{
		free(svc);
		svc = NULL;
		num_svcs = 0;

		/* Iterate all users */
		if (msrpc_svc_enum(srv_name, &svc, &num_svcs,
		                   NULL, NULL) == 0)
		{
			return NULL;
		}

		i = 0;
    	}

	for (; i < num_svcs; i++)
	{
		fstring svc_name;
		unistr_to_ascii(svc_name, svc[i].uni_srvc_name.buffer,
			sizeof(svc_name)-1);

		if (text == NULL || text[0] == 0 ||
		    strnequal(text, svc_name, strlen(text)))
		{
			char *name = strdup(svc_name);
			i++;
			return name;
		}
	}
	
	return NULL;
}

static char *complete_printersenum(char *text, int state)
{
	static uint32 i = 0;
	static uint32 num = 0;
	static PRINTER_INFO_1 **ctr = NULL;
    
	if (state == 0)
	{
		fstring srv_name;
		fstrcpy(srv_name, "\\\\");
		fstrcat(srv_name, smb_cli->desthost);
		strupper(srv_name);

		free_print1_array(num, ctr);
		ctr = NULL;
		num = 0;

		/* Iterate all users */
		if (!msrpc_spoolss_enum_printers(smb_cli, srv_name,
		                   1, &num, (void***)&ctr,
		                   NULL))
		{
			return NULL;
		}

		i = 0;
    	}

	for (; i < num; i++)
	{
		fstring name;
		unistr_to_ascii(name, ctr[i]->name.buffer,
			sizeof(name)-1);

		if (text == NULL || text[0] == 0 ||
		    strnequal(text, name, strlen(text)))
		{
			char *copy = strdup(name);
			i++;
			return copy;
		}
	}
	
	return NULL;
}

/* Complete an rpcclient command */

static char *complete_cmd(char *text, int state)
{
    static int cmd_index;
    char *name;

    /* Initialise */

    if (state == 0) {
	cmd_index = 0;
    }

    /* Return the next name which partially matches the list of commands */
    
    while (strlen(name = commands[cmd_index++].name) > 0) {
	if (strncmp(name, text, strlen(text)) == 0) {
	    return strdup(name);
	}
    }
    
    return NULL;
}

/* Main completion function */

static char **completion_fn(char *text, int start, int end)
{
	pstring cmd_partial;
	int cmd_index;
	int num_words;

    int i;
    char lastch = ' ';

	(void)get_cmd_args(rl_line_buffer);

	safe_strcpy(cmd_partial, rl_line_buffer,
	            MAX(sizeof(cmd_partial),end)-1);

    /* Complete rpcclient command */

    if (start == 0)
	{
	return completion_matches(text, complete_cmd);
    }

    /* Count # of words in command */
    
    num_words = 0;
    for (i = 0; i <= end; i++) {
	if ((rl_line_buffer[i] != ' ') && (lastch == ' '))
	{
		num_words++;
	}
	lastch = rl_line_buffer[i];
    }
    
    if (rl_line_buffer[end] == ' ')
	num_words++;

    /* Work out which command we are completing for */

    for (cmd_index = 0; strcmp(commands[cmd_index].name, "") != 0; 
	 cmd_index++) {
	
	/* Check each command in array */
	
	if (strncmp(rl_line_buffer, commands[cmd_index].name,
		    strlen(commands[cmd_index].name)) == 0) {
	    
	    /* Call appropriate completion function */

      if (num_words == 2 || num_words == 3)
      {
        switch (commands[cmd_index].compl_args[num_words - 2])
        {

        case COMPL_SAMGRP:
          return completion_matches(text, complete_samenum_grp);

        case COMPL_SAMALS:
          return completion_matches(text, complete_samenum_als);

        case COMPL_SAMUSR:
          return completion_matches(text, complete_samenum_usr);

        case COMPL_SVCLST:
          return completion_matches(text, complete_svcenum);

        case COMPL_PRTLST:
          return completion_matches(text, complete_printersenum);

        case COMPL_REGKEY:
          return completion_matches(text, complete_regenum);

        default:
            /* An invalid completion type */
            break;
        }
      }
	}
    }

    /* Eeek! */

    return NULL;
}

/* To avoid filename completion being activated when no valid
   completions are found, we assign this stub completion function
   to the rl_completion_entry_function variable. */

static char *complete_cmd_null(char *text, int state)
{
	return NULL;
}

#endif /* HAVE_LIBREADLINE */

static void set_user_password(struct user_credentials *u,
				BOOL got_pass, char *password)
{
	/* set the password cache info */
	if (got_pass)
	{
		if (password == NULL)
		{
			pwd_set_nullpwd(&(u->pwd));
		}
		else
		{
			/* generate 16 byte hashes */
			pwd_make_lm_nt_16(&(u->pwd), password);
		}
	}
	else 
	{
		pwd_read(&(u->pwd), "Enter Password:", True);
	}
}

#define CMD_STR 0x1
#define CMD_DBF 0x2
#define CMD_SVC 0x4
#define CMD_TERM 0x8
#define CMD_PASS 0x10
#define CMD_USER 0x20
#define CMD_NOPW 0x40
#define CMD_DBLV 0x80
#define CMD_HELP 0x100
#define CMD_SOCK 0x200
#define CMD_IFACE 0x400
#define CMD_DOM 0x800
#define CMD_IP 0x1000
#define CMD_HOST 0x2000
#define CMD_NAME 0x4000
#define CMD_DBG 0x8000
#define CMD_SCOPE 0x10000
#define CMD_INTER 0x20000

static void cmd_set(struct client_info *info, int argc, char *argv[])
{
	BOOL interactive = True;
	char *cmd_str = NULL;
	char opt;
	extern FILE *dbf;
	extern char *optarg;
	static pstring servicesf = CONFIGFILE;
	pstring term_code;
	pstring password; /* local copy only, if one is entered */

#ifdef KANJI
	pstrcpy(term_code, KANJI);
#else /* KANJI */
	*term_code = 0;
#endif /* KANJI */

	if (argc > 1 && *argv[1] != '-')
	{
		if (argc > 1 && (*argv[1] != '-'))
		{
			cmd_set_options |= CMD_PASS;
			pstrcpy(password,argv[1]);  
			memset(argv[1],'X',strlen(argv[1]));
			argc--;
			argv++;
		}
	}

	while ((opt = getopt(argc, argv, "s:B:O:M:S:i:N:n:d:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF)
	{
		switch (opt)
		{
			case 'm':
			{
				/* FIXME ... max_protocol seems to be funny here */

				int max_protocol = 0;
				max_protocol = interpret_protocol(optarg,max_protocol);
				fprintf(stderr, "max protocol not currently supported\n");
				break;
			}

			case 'O':
			{
				cmd_set_options |= CMD_SOCK;
				pstrcpy(user_socket_options,optarg);
				break;	
			}

			case 'S':
			{
				cmd_set_options |= CMD_HOST;
				pstrcpy(cli_info.dest_host,optarg);
				strupper(cli_info.dest_host);
				break;
			}

			case 'B':
			{
				cmd_set_options |= CMD_IFACE;
				iface_set_default(NULL,optarg,NULL);
				break;
			}

			case 'i':
			{
				cmd_set_options |= CMD_SCOPE;
				pstrcpy(scope, optarg);
				break;
			}

			case 'U':
			{
				char *lp;
				cmd_set_options |= CMD_USER;
				pstrcpy(usr.user_name,optarg);
				if ((lp=strchr(usr.user_name,'%')))
				{
					*lp = 0;
					pstrcpy(password,lp+1);
					cmd_set_options |= CMD_PASS;
					memset(strchr(optarg,'%')+1,'X',strlen(password));
				}
				if (usr.user_name[0] == 0 && password[0] == 0)
				{
					cmd_set_options |= CMD_NOPW;
				}
				break;
			}

			case 'W':
			{
				cmd_set_options |= CMD_DOM;
				pstrcpy(usr.domain,optarg);
				break;
			}

			case 'E':
			{
				cmd_set_options |= CMD_DBG;
				dbf = stderr;
				break;
			}

			case 'I':
			{
				cmd_set_options |= CMD_IP;
				cli_info.dest_ip = *interpret_addr2(optarg);
				if (zero_ip(cli_info.dest_ip))
				{
					exit(1);
				}
				break;
			}

			case 'n':
			{
				cmd_set_options |= CMD_NAME;
				fstrcpy(global_myname, optarg);
				break;
			}

			case 'N':
			{
				cmd_set_options |= CMD_NOPW | CMD_PASS;
				break;
			}

			case 'd':
			{
				cmd_set_options |= CMD_DBLV;
				if (*optarg == 'A')
					DEBUGLEVEL = 10000;
				else
					DEBUGLEVEL = atoi(optarg);
				break;
			}

			case 'l':
			{
				cmd_set_options |= CMD_INTER;
				slprintf(debugf, sizeof(debugf)-1,
				         "%s.client", optarg);
				interactive = False;
				break;
			}

			case 'c':
			{
				cmd_set_options |= CMD_STR | CMD_PASS;
				cmd_str = optarg;
				break;
			}

			case 'h':
			{
				cmd_set_options |= CMD_HELP;
				usage(argv[0]);
				break;
			}

			case 's':
			{
				cmd_set_options |= CMD_SVC;
				pstrcpy(servicesf, optarg);
				break;
			}

			case 't':
			{
				cmd_set_options |= CMD_TERM;
				pstrcpy(term_code, optarg);
				break;
			}

			default:
			{
				cmd_set_options |= CMD_HELP;
				usage(argv[0]);
				break;
			}
		}
	}

	DEBUG(10,("cmd_set: options: %x\n", cmd_set_options));

	if (IS_BITS_SET_ALL(cmd_set_options, CMD_HELP))
	{
		return;
	}

	if (IS_BITS_SET_ALL(cmd_set_options, CMD_INTER))
	{
		setup_logging(debugf, interactive);
		reopen_logs();
	}

	if (IS_BITS_SET_ALL(cmd_set_options, CMD_NOPW))
	{
		set_user_password(&usr, True, NULL);
	}
	else if (IS_BITS_SET_ALL(cmd_set_options, CMD_PASS))
	{
		set_user_password(&usr, True, password);
	}

	/* paranoia: destroy the local copy of the password */
	bzero(password, sizeof(password)); 

	strupper(global_myname);
	fstrcpy(cli_info.myhostname, global_myname);

	if (IS_BITS_SET_ALL(cmd_set_options, CMD_SVC))
	{
		if (!lp_load(servicesf,True, False, False))
		{
			fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
		}

	}

	if (IS_BITS_SET_ALL(cmd_set_options, CMD_INTER))
	{
		load_interfaces();
	}

	fstrcpy(cli_info.mach_acct, cli_info.myhostname);
	strupper(cli_info.mach_acct);
	fstrcat(cli_info.mach_acct, "$");

	if (cmd_str != NULL)
	{
		process(&cli_info, cmd_str);
	}
}

static void read_user_env(struct user_credentials *u)
{
	pstring password;

	password[0] = 0;

	if (getenv("USER"))
	{
		char *p;
		pstrcpy(u->user_name,getenv("USER"));

		/* modification to support userid%passwd syntax in the USER var
		25.Aug.97, jdblair@uab.edu */

		if ((p=strchr(u->user_name,'%')))
		{
			*p = 0;
			pstrcpy(password,p+1);
			memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
		}
		strupper(u->user_name);
	}

	/* modification to support PASSWD environmental var
	   25.Aug.97, jdblair@uab.edu */
	if (getenv("PASSWD"))
	{
		pstrcpy(password,getenv("PASSWD"));
	}

	if (*u->user_name == 0 && getenv("LOGNAME"))
	{
		pstrcpy(u->user_name,getenv("LOGNAME"));
		strupper(u->user_name);
	}

	set_user_password(u, True, password);

	/* paranoia: destroy the local copy of the password */
	bzero(password, sizeof(password)); 
}

void readline_init(void)
{
#ifdef HAVE_LIBREADLINE

	/* Initialise GNU Readline */
	
	rl_readline_name = "rpcclient";
	rl_attempted_completion_function = completion_fn;
	rl_completion_entry_function = (Function *)complete_cmd_null;
	
	/* Initialise history list */
	
	using_history();

#else
	int x;
	x = 0; /* stop compiler warnings */
#endif /* HAVE_LIBREADLINE */
}

/****************************************************************************
  main program
****************************************************************************/
 int main(int argc,char *argv[])
{
	extern struct user_credentials *usr_creds;
	mode_t myumask = 0755;

	DEBUGLEVEL = 2;

	usr.ntlmssp_flags = 0x0;

	usr_creds = &usr;
	out_hnd = stdout;
	fstrcpy(debugf, argv[0]);

	init_policy_hnd(64);

	pstrcpy(usr.domain, "");
	pstrcpy(usr.user_name, "");

	pstrcpy(cli_info.myhostname, "");
	pstrcpy(cli_info.dest_host, "");
	cli_info.dest_ip.s_addr = 0;

	ZERO_STRUCT(cli_info.dom.level3_sid);
	ZERO_STRUCT(cli_info.dom.level5_sid);
	fstrcpy(cli_info.dom.level3_dom, "");
	fstrcpy(cli_info.dom.level5_dom, "");

	readline_init();
	TimeInit();
	charset_initialise();

	myumask = umask(0);
	umask(myumask);

	if (!get_myname(global_myname, NULL))
	{
		fprintf(stderr, "Failed to get my hostname.\n");
	}

	if (argc < 2)
	{
		usage(argv[0]);
		exit(1);
	}

	read_user_env(&usr);

	cmd_set_options &= ~CMD_HELP;
	cmd_set_options &= ~CMD_NOPW;

	cmd_set(&cli_info, argc, argv);

	if (IS_BITS_SET_ALL(cmd_set_options, CMD_HELP))
	{
		exit(0);
	}

	codepage_initialise(lp_client_code_page());

	DEBUG(3,("%s client started (version %s)\n",timestring(),VERSION));

	process(&cli_info, NULL);

	return(0);
}