summaryrefslogtreecommitdiff
path: root/source3/utils/net.c
blob: 8e0a2f6bd9783c44824517a1a0927fa624a8ade3 (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
/* 
   Samba Unix/Linux SMB client library 
   Version 3.0
   Distributed SMB/CIFS Server Management Utility 
   Copyright (C) 2001 Steve French  (sfrench@us.ibm.com)
   Copyright (C) 2001 Jim McDonough (jmcd@us.ibm.com)

   connect_to_ipc based on similar routine
   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.
*/
 
/*****************************************************/
/*                                                   */
/*   Distributed SMB/CIFS Server Management Utility  */
/*                                                   */
/*   The intent was to make the syntax similar       */
/*   to the NET utility (first developed in DOS      */
/*   with additional interesting & useful functions  */
/*   added in later SMB server network operating     */
/*   systems).                                       */
/*                                                   */
/*****************************************************/

#include <includes.h>

/***********************************************************************/
/* Beginning of internationalization section.  Translatable constants  */
/* should be kept in this area and referenced in the rest of the code. */
/*                                                                     */
/* No functions, outside of Samba or LSB (Linux Standards Base) should */
/* be used (if possible).                                              */
/***********************************************************************/

typedef struct _functable {
  int func;
  char funcname[12];
} functable;

/* functions available */
#define FILEF     1
#define SHAREF    2
#define SESSIONF  3
#define SERVERF   4
#define DOMAINF   5
#define PRINTQF   6 
#define USERF     7
#define GROUPF    8
#define VALIDATEF 9
#define GROUPMEMBERF 10
#define ADMINF    11
#define SERVICEF  12
#define PASSWORDF 13
#define HELPF     14

const functable net_func[] = {
  { FILEF, "FILE"},
  { SHAREF, "SHARE"},
  { SESSIONF, "SESSION"},
  { SERVERF, "SERVER"},
  { DOMAINF, "DOMAIN"},
  { PRINTQF, "PRINTQ"},
  { USERF, "USER"},
  { GROUPF, "GROUP"},
  { VALIDATEF, "VALIDATE"},
  { GROUPMEMBERF, "GROUPMEMBER"},
  { ADMINF, "ADMIN"},
  { SERVICEF, "SERVICE"},
  { PASSWORDF, "PASSWORD"},
  { HELPF, "HELP"}
};

/* subfunctions available */
#define OTHER_SF  0
#define LIST_SF   1  /* enumerate */
#define ADD_SF    2  /* also used for create and start */
#define DELETE_SF 3  /* also used for close and stop */
#define INFO_SF   4  /* get information */

const functable net_subfunc[] = {
  { LIST_SF, "LIST" },
  { LIST_SF, "ENUMERATE" },
  { ADD_SF, "ADD" },
  { ADD_SF, "CREATE" },
  { DELETE_SF, "CLOSE" },
  { DELETE_SF, "DELETE" },
  { INFO_SF, "INFO"}
};

const char share_type[][6] = {
  "Disk",
  "Print",
  "Dev",
  "IPC"
};

#define PASSWORD_PROMPT		"Password: "
#define YES_STRING              "Yes"
#define NO_STRING               "No"

#define NET_USAGE \
    "\nUsage: \n"\
    "  net domain \tto list domains \n"\
    "  net file \tto list open files on a server \n"\
    "  net group \tto list user groups  \n"\
    "  net groupmember to list users in a group \n"\
    "  net password to change the password of a user\n"\
    "  net printq \tto list the print queues on a server\n"\
    "  net server \tto list servers in a domain\n"\
    "  net session \tto list clients with open sessions to a server\n"\
    "  net share \tto list shares exported by a server\n"\
    "  net user \tto list users\n"\
    "  net validate \tto check whether a user and the corresponding password are valid\n"\
    "  net help\n"\
    "\nType \"net help <option>\" to get more information on that option\n"

#define NET_FILE_USAGE \
    "\nnet file [misc. options] [targets]"\
    "\n\tenumerates all open files on file server\n"\
    "\nnet file <username> [misc. options] [targets]"\
    "\n\tenumerates all files opened by username on file server\n"\
    "\nnet file CLOSE <id> [misc. options] [targets]"\
    "\n\tcloses specified file on target server\n"

#define FILE_INFO_DISPLAY \
    "File ID          %d\n"\
    "User name        %s\n"\
    "Locks            0x%-4.2x\n"\
    "Path             %s\n"\
    "Permissions      0x%x\n"
#define FILE_ENUM_DISPLAY \
    "\nEnumerating open files on remote server:\n\n"\
    "\n\tFileId  Opened by            Perms  Locks  Path \n"\
    "\t------  ---------            -----  -----  ---- \n"

#define NET_SHARE_USAGE \
    "\nnet share [misc. options] [targets] \n"\
    "\tenumerates all exported resources (network shares) on target server\n"\
    "\nnet share ADD <name=serverpath> [misc. options] [targets]"\
    "\n\tAdd a share from a server (makes the export active)\n"\
    "\nnet share DELETE <sharename> [misc. options] [targets]\n"\
    "\tor"\
    "\nnet share CLOSE <sharename> [misc. options] [targets]"\
    "\n\tDeletes a share from a server (makes the export inactive)\n"
    
#define SHARE_ENUM_DISPLAY \
    "\nEnumerating shared resources (exports) on remote server:\n\n"\
    "\nShare name   Type     Description\n"\
    "----------   ----     -----------\n"


#define NET_SESSION_USAGE \
    "\nnet session [misc. options] [targets]"\
    "\n\tenumerates all active SMB/CIFS sessions on target server\n"\
    "\nnet session DELETE <client_name> [misc. options] [targets] \n"\
    "\tor"\
    "\nnet session CLOSE <client_name> [misc. options] [targets]"\
    "\n\tDeletes (closes) a session from specified client to server\n"

#define SESSION_ENUM_DISPLAY \
    "Computer             User name            Client Type        Opens Idle time\n\n"\
    "------------------------------------------------------------------------------\n"


#define SESSION_DISPLAY_ONE \
    "User name       %-20.20s\n"\
    "Computer        %-20.20s\n"\
    "Guest logon     %-20.20s\n"\
    "Client Type     %-40.40s\n"\
    "Sess time       %2.2d:%2.2d:%2.2d\n"\
    "Idle time       %2.2d:%2.2d:%2.2d\n"

#define SESSION_DISPLAY_CONNS \
    "Share name     Type     # Opens\n"\
    "------------------------------------------------------------------------------\n"

#define NET_SERVER_USAGE \
    "\nUsage:\n"\
    "  net server [domain]\tlists the servers in the specified domain or workgroup.\n"\
    "    If domain is not specified, it uses the current domain or workgroup as\n"\
    "    the default.\n"

#define SERVER_ENUM_DISPLAY "\nEnumerating servers in this domain or workgroup: \n\n"  \
    "\n\tServer name          Server description\n"\
    "\t-------------        ----------------------------\n"


#define NET_DOMAIN_USAGE \
    "\nUsage:\n"\
    "  net domain [misc. options] [target]\n\tlists the domains "\
    "or workgroups visible on the current network\n"

#define DOMAIN_ENUM_DISPLAY \
    "\nEnumerating domains:\n\n"\
    "\n\tDomain name          Server name of Browse Master\n"\
    "\t-------------        ----------------------------\n"

#define NET_PRINTQ_USAGE \
    "\nUsage:\n"\
    " net printq [misc. options] [targets]\n"\
    "\tor\n"\
    " net printq list [<queue_name>] [misc. options] [targets]\n"\
    "\tlists the specified queue and jobs on the target server.\n"\
    "\tIf the queue name is not specified, all queues are listed.\n\n"\
    " net printq delete [<queue name>] [misc. options] [targets]\n"\
    "\tdeletes the specified job number on the target server, or the\n"\
    "\tprinter queue if no job number is specified\n"
#define PRINTQ_ENUM_DISPLAY \
    "Print queues at \\\\%s\n\n"\
    "Name                         Job #      Size            Status\n\n"\
    "------------------------------------------------------------------"\
    "-------------\n"
#define PRINTQ_DISPLAY_ONE "%-23.23s %5d jobs                      %-22.22s\n"
#define PRINTQ_PRINTER_ACTIVE "*Printer Active*"
#define PRINTQ_PRINTER_PAUSED "*Printer Paused*"
#define PRINTQ_PRINTER_ERROR "*Printer error*"
#define PRINTQ_PRINTER_DELPEND "*Delete Pending*"
#define PRINTQ_PRINTER_STATUNK "**UNKNOWN STATUS**"
#define PRINTQ_DISPLAY_JOB "     %-23.23s %5d %9d            %-22.22s\n"
#define PRINTQ_JOB_PRINTING "Printing"
#define PRINTQ_JOB_QUEUED "Waiting"
#define PRINTQ_JOB_PAUSED "Held in queue"
#define PRINTQ_JOB_SPOOLING "Spooling"
#define PRINTQ_QUEUE_WORD " Queue"

#define NET_USER_USAGE \
    "\nnet user [misc. options] [targets]\n\tEnumerate users\n"\
    "\nnet user DELETE <name> [misc. options] [targets]"\
    "\n\tDelete specified user\n"\
    "\nnet user INFO <name> [misc. options] [targets]"\
    "\n\tList the domain groups of the specified user\n"\
    "\nnet user ADD <name> [-F user flags] [misc. options] [targets]"\
    "\n\tAdd specified user\n"

#define USER_ENUM_DISPLAY \
    "\nEnumerating shared resources (exports) on remote server:\n\n"\
    "\nUser name             Description                                     Home Directory                          Profile Directory\n"\
    "---------             -----------                                     --------------                          -----------------\n"

#define NET_GROUP_USAGE \
    "\nnet group [misc. options] [targets]"\
    "\n\tEnumerate user groups\n"\
    "\nnet group DELETE <name> [misc. options] [targets]"\
    "\n\tDelete specified group\n"\
    "\nnet group ADD <name> [-C comment] [misc. options] [targets]"\
    "\n\tCreate specified group\n"

#define NET_GROUPMEMBER_USAGE \
    "\nnet groupmember LIST <group name> [misc. options] [targets]"\
    "\n\t Enumerate users in a group\n"\
    "\nnet groupmember DELETE <group name> <user name> [misc. options] "\
    "[targets]\n\t Delete sepcified user from specified group\n"\
    "\nnet groupmember ADD <group name> <user name> [misc. options] [targets]"\
    "\n\t Add specified user to specified group\n"

            
#define NET_ADMIN_USAGE \
    "\nnet admin <remote command to execute> [cmd arguments [environment]] [misc_options] [targets]\n"\
    "\texecutes a remote command on an os/2 target server\n"
    
#define NET_PASSWORD_USAGE \
    "\nnet password <user> <old password> <new password> [misc_options] [targets]\n"\
    "\tchanges the password for the specified user on a remote server\n"

#define NET_SERVICE_USAGE \
    "\nnet service [misc. options] [targets] \n"\
    "\tenumerates all running service daemons on target server\n"\
    "\nnet service ADD <name> [service startup arguments] [misc. options] [targets]"\
    "\n\tStart named service on remote server\n"\
    "\nnet service DELETE <name> [misc. options] [targets]\n"\
    "\n\tStop named service on remote server\n"
    

#define NET_VALIDATE_USAGE \
    "\nnet validate <username> [password]\n"\
    "\tValidate user and password to check whether they can access target server or domain\n"

#define TARGET_USAGE      "\nValid targets: choose one (none defaults to using the %s)\n"
#define GLBL_LCL_MASTER   "global browsemaster or local browse master if that is not found"
#define DOMAIN_MASTER     "local domain browse master"
#define LOCAL_HOST        "localhost"
#define MISC_OPT_USAGE    "\nValid miscellaneous options are:\n"
#define SERVER_USAGE      "\t-S or --server=<server>\t\tserver name\n"
#define IPADDRESS_USAGE   "\t-I or --ipaddress=<ipaddr>\tip address of target server\n"
#define PORT_USAGE        "\t-p or --port=<port number>\tconnection port on target server\n"
#define WORKGROUP_USAGE   "\t-w or --workgroup=<wg>\t\ttarget workgroup or domain name\n"
#define COMMENT_USAGE     "\t-C or --comment=<comment>\tdescriptive comment (for add only)\n"
#define MYWORKGROUP_USAGE "\t-W or --myworkgroup=<wg>\tclient workgroup\n"
#define DEBUG_USAGE       "\t-d or --debug=<level>\t\tdebug level (1-9)\n"
#define MYNAME_USAGE      "\t-n or --myname=<name>\t\tclient name\n"
#define USER_USAGE        "\t-U or --user=<name>\t\tuser name\n"
#define CONF_USAGE        "\t-s or --conf=<path>\t\tpathname of smb.conf file\n"
#define JOBID_USAGE       "\t-j or --jobid=<job id>\t\tjob id\n"
#define MAXUSERS_USAGE    "\t-M or --maxusers=<num>\t\tmax users allowed for share\n"
#define LONG_USAGE        "\t-l or --long\t\t\tDisplay full information\n"

#define ERRMSG_NOCONN_TARGET_SRVR	"\nUnable to connect to target server\n"
#define ERRMSG_NOCONN_BROWSE_MSTR	"\nUnable to connect to browse master\n"
#define ERRMSG_NOT_IMPLEMENTED		"\nNot implemented\n"
#define ERRMSG_FILEID_MISSING		"\nMissing fileid of file to close\n\n"
#define ERRMSG_GROUPNAME_MISSING        "\n\nGroup name not specified\n"
#define ERRMSG_USERNAME_MISSING        "\n\nUser name not specified\n"
#define ERRMSG_SHARENAME_MISSING        "\n\nShare name not specified\n"
#define ERRMSG_TARGET_WG_NOT_VALID      "\nTarget workgroup option not valid "\
					"except on net server command, ignored"
#define ERRMSG_INVALID_HELP_OPTION	"\nInvalid help option\n"
#define ERRMSG_INVALID_OPTION		"\nInvalid option %c (%d)\n"
#define ERRMSG_INVALID_IPADDRESS        "\nInvalid ip address specified\n"
#define ERRMSG_SPURIOUS_PARM            "\nInvalid paramater ignored: %s\n"
#define ERRMSG_BOTH_SERVER_IPADDRESS    "\nTarget server and IP address both "\
  "specified. Do not set both at the same time.  The target IP address was used\n"
#define ERRMSG_INVALID_DOMAIN_ACTION	"\nInvalid action for NET DOMAIN command"\
  " ignored. Only listing domains permitted.\n"

/* Column headers */
#define COMMENT_STR   "Comment "
#define USER_STR      "User name "
#define GROUP_STR     "Group name "  
#define SERVICE_STR   "Service name"
#define HOMED_STR     "Home directory "
#define LOGONS_STR    "Logon script "
/************************************************************************************/
/*                       end of internationalization section                        */
/************************************************************************************/

extern int optind, opterr, optopt;
extern struct in_addr ipzero;

struct cli_state *cli;
static pstring global_requester_name;
static pstring host; /* target server */
static pstring password;
static pstring global_user_name;
static pstring global_workgroup;
static int port = SMB_PORT;
static int long_list_entries = 0;
static BOOL got_pass = False;
static BOOL have_ip = False;
struct in_addr dest_ip;

int get_func(const char *parm)
{
  int i;

  for (i=0;i<=sizeof(net_func);i++)
    if (StrnCaseCmp(parm, net_func[i].funcname,10) == 0)
      return net_func[i].func;
  return 0;
}

int get_subfunc(const char *parm)
{
  int i;

  for (i=0;i<=sizeof(net_subfunc);i++)
    if (StrnCaseCmp(parm, net_subfunc[i].funcname,10) == 0)
      return net_subfunc[i].func;
  return 0;
}

/****************************************************************************
  
****************************************************************************/
struct cli_state *connect_to_ipc(char *server)
{
  struct cli_state *c;
  struct nmb_name called, calling;
  struct in_addr ip;
  char *server_n;
  fstring servicename;
  char *sharename;
	
	
  /* make a copy so we don't modify the global string 'service' */
  safe_strcpy(servicename, "IPC$", sizeof(servicename)-1);
  sharename = servicename;
  if (*sharename == '\\') {
    server = sharename+2;
    sharename = strchr(server,'\\');
    if (!sharename) return NULL;
    *sharename = 0;
    sharename++;
  }

  if(server == NULL)
    return NULL;  /* if ip addr specified, ascii version of ip address is used as host name */

  server_n = server; 
	
  ip = ipzero;
  make_nmb_name(&calling, global_requester_name, 0x0);
  make_nmb_name(&called , server, 0x20);

 again:
  if (have_ip)
    ip = dest_ip;
  else ip = ipzero;

  DEBUG(3,("Connecting to host=%s\\share=%s\n\n", 
	   server, "IPC$"));

  /* have to open a new connection */
  if (!(c=cli_initialise(NULL)) || (cli_set_port(c, port) == 0) ||
      !cli_connect(c, server_n, &ip)) {
    DEBUG(1,("Connection to %s failed\n", server_n));
    return NULL;
  }

  c->protocol = PROTOCOL_NT1;

  if (!cli_session_request(c, &calling, &called)) {
    char *p;
    DEBUG(1,("session request to %s failed (%s)\n", 
	     called.name, cli_errstr(c)));
    cli_shutdown(c);
    SAFE_FREE(c);
    if ((p=strchr(called.name, '.'))) {
      *p = 0;
      goto again;
    }
    if (strcmp(called.name, "*SMBSERVER")) {
      make_nmb_name(&called , "*SMBSERVER", 0x20);
      goto again;
    }
    return NULL;
  }

  DEBUG(4,(" session request ok\n"));

  if (!cli_negprot(c)) {
    DEBUG(1,("protocol negotiation failed\n"));
    cli_shutdown(c);
    SAFE_FREE(c);
    return NULL;
  }

  if (!got_pass) {
    char *pass = getpass(PASSWORD_PROMPT);
    if (pass) {
      pstrcpy(password, pass);
    }
  }

  if (!cli_session_setup(c, global_user_name, 
			 password, strlen(password),
			 password, strlen(password),
			 global_workgroup)) {
    /*  try again with a null username */
    if (!cli_session_setup(c, "", "", 0, "", 0, global_workgroup)) { 
      DEBUG(1,("session setup failed: %s\n", cli_errstr(c)));
      cli_shutdown(c);
      SAFE_FREE(c);
      return NULL;
    }
    DEBUG(3,("Anonymous login successful\n"));
  }
	
  DEBUG(4,(" session setup ok\n"));
  if (!cli_send_tconX(c, sharename, "?????",
		      password, strlen(password)+1)) {
    DEBUG(1,("tree connect failed: %s\n", cli_errstr(c)));
    cli_shutdown(c);
    SAFE_FREE(c);
    return NULL;
  }

  DEBUG(4,(" tconx ok\n"));

  return c;
}


void usage(void)
{
  d_printf(NET_USAGE);
}

void file_usage(void)
{
  d_printf(NET_FILE_USAGE); /* command syntax */
  
  d_printf(TARGET_USAGE, LOCAL_HOST); /* target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);

  d_printf(MISC_OPT_USAGE); /* misc options */
  d_printf(PORT_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(CONF_USAGE);
}

/***************************************************************************
  list info on an open file
***************************************************************************/
static void file_fn(const char * pPath, const char * pUser, uint16 perms, uint16 locks, uint32 id)
{
  d_printf("\t%-7.1d %-20.20s 0x%-4.2x %-6.1d %s\n",
	 id, pUser, perms, locks, pPath);
}

static void one_file_fn(const char *pPath, const char *pUser, uint16 perms, uint16 locks, uint32 id)
{
  d_printf(FILE_INFO_DISPLAY, id, pUser, locks, pPath, perms);
}

int net_file(int subfunct, const char * id)
{
  struct in_addr target_ip;
	
  if((have_ip == 0) && (host[0] == 0)) {
    if (!resolve_name("localhost", &target_ip, 0x20)) {
      DEBUG(1,("No remote server specified, unable to resolve connection to localhost via name lookup"));
      return -1;
    } else {
      have_ip = True;
      dest_ip = target_ip;
    }
  }
  if(host[0] == 0) strncpy(host, inet_ntoa(dest_ip),16);
  cli = connect_to_ipc(host);
  if(!cli) {
    d_printf(ERRMSG_NOCONN_TARGET_SRVR);
    return -2;
  }

  if(subfunct == DELETE_SF) { /* close open file on remote server */
    if(id == NULL) {
      d_printf(ERRMSG_FILEID_MISSING);
      return -1;
    } else 
      return cli_NetFileClose(cli, atoi(id));
  } else if(subfunct == LIST_SF) {
    d_printf(FILE_ENUM_DISPLAY); /* file list header */
    return cli_NetFileEnum(cli, NULL, NULL, file_fn);
  } else if ((subfunct == OTHER_SF) && id) {
    return cli_NetFileGetInfo(cli, atoi(id), one_file_fn);
  } else file_usage();
  return -1;
}
		       
void share_usage(void)
{
  d_printf(NET_SHARE_USAGE); /* command syntax */

  d_printf(TARGET_USAGE, LOCAL_HOST); /* target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);

  d_printf(MISC_OPT_USAGE); /* misc options */
  d_printf(PORT_USAGE);
  d_printf(COMMENT_USAGE);
  d_printf(MAXUSERS_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(CONF_USAGE);
}

void long_share_fn(const char *share_name, uint32 type, const char *comment, void *state)
{
   d_printf("%-12.12s %-8.8s %-50.50s\n", share_name, share_type[type], comment);
}

void share_fn(const char *share_name, uint32 type, const char *comment, void *state)
{
   d_printf("%-12.12s\n", share_name);
}

int net_share(int subfunct, const char * sharename, const char * comment, int maxusers)
{
  struct in_addr target_ip;
 
  if((have_ip == 0) && (host[0] == 0)) {
    if (!resolve_name("localhost", &target_ip, 0x20)) {
      DEBUG(1,("No remote server specified, unable to resolve connection to localhost via name lookup"));
      return -1;

    } else {
      have_ip = True;
      dest_ip = target_ip;
    }
  }
  if(host[0] == 0) 
    strncpy(host, inet_ntoa(dest_ip),16);
  cli = connect_to_ipc(host);
  if(!cli) {
    d_printf(ERRMSG_NOCONN_TARGET_SRVR);
    return -2;
  }
  if (subfunct == DELETE_SF) {
    if (sharename == NULL) {
      d_printf(ERRMSG_SHARENAME_MISSING);
      return -1;
    } else
      return cli_NetShareDelete(cli, sharename);
  } else if (subfunct == LIST_SF) {
      if(long_list_entries) {
       d_printf(SHARE_ENUM_DISPLAY);
       return cli_RNetShareEnum(cli, long_share_fn, NULL);
      } else {      
       return cli_RNetShareEnum(cli, share_fn, NULL);
      }
  } else if (subfunct == ADD_SF) {
    if (sharename == NULL) {
      d_printf(ERRMSG_SHARENAME_MISSING);
      return -1;
    } else {
      RAP_SHARE_INFO_2 sinfo;
      char *p;

      p = strchr(sharename, '=');
      strncpy(sinfo.share_name, sharename, PTR_DIFF(p,sharename));
      sinfo.reserved1 = '\0';
      sinfo.share_type = 0;
      sinfo.comment = comment;
      sinfo.perms = 0;
      sinfo.maximum_users = maxusers;
      sinfo.active_users = 0;
      sinfo.path = p+1;
      memset(sinfo.password, '\0', sizeof(sinfo.password));
      sinfo.reserved2 = '\0';

      return cli_NetShareAdd(cli, &sinfo);
    }
  } else
    d_printf(ERRMSG_NOT_IMPLEMENTED);
  return -1;
}
		    
		
void session_usage(void)
{
  d_printf(NET_SESSION_USAGE); /* command syntax */

  d_printf(TARGET_USAGE, LOCAL_HOST); /* Target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);

  d_printf(MISC_OPT_USAGE); /* Misc options */
  d_printf(PORT_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(CONF_USAGE);
}
    
void list_sessions_func(char *wsname, char *username, uint16 conns,
			uint16 opens, uint16 users, uint32 sess_time,
			uint32 idle_time, uint32 user_flags, char *clitype)
{
  int hrs = idle_time / 3600;
  int min = (idle_time / 60) % 60;
  int sec = idle_time % 60;

  d_printf("\\\\%-18.18s %-20.20s %-18.18s %5d %2.2d:%2.2d:%2.2d\n",
	 wsname, username, clitype, opens, hrs, min, sec);
}

void display_session_func(const char *wsname, const char *username, uint16 conns,
			  uint16 opens, uint16 users, uint32 sess_time,
			  uint32 idle_time, uint32 user_flags, const char *clitype)
{
  int ihrs = idle_time / 3600;
  int imin = (idle_time / 60) % 60;
  int isec = idle_time % 60;
  int shrs = sess_time / 3600;
  int smin = (sess_time / 60) % 60;
  int ssec = sess_time % 60;
  d_printf(SESSION_DISPLAY_ONE, username, wsname, 
	 (user_flags&0x0)?YES_STRING:NO_STRING, clitype,
	 shrs, smin, ssec, ihrs, imin, isec);
}

void display_conns_func(uint16 conn_id, uint16 conn_type, uint16 opens, uint16 users, uint32 conn_time, const char *username, const char *netname)
{
  d_printf("%-14.14s %-8.8s %5d\n", netname, share_type[conn_type], opens);
}

int net_session(int subfunct, const char * sessname)
{
  struct in_addr target_ip;
  int res;
 
  if((have_ip == 0) && (host[0] == 0)) {
    if (!resolve_name("localhost", &target_ip, 0x20)) {
      DEBUG(1,("No remote server specified, unable to resolve connection to localhost via name lookup"));
      return -1;

    } else {
      have_ip = True;
      dest_ip = target_ip;
    }
  }
  if(host[0] == 0)  
    strncpy(host, inet_ntoa(dest_ip),16);
  cli = connect_to_ipc(host);
  if(!cli) {
    d_printf(ERRMSG_NOCONN_TARGET_SRVR);
    return -2;
  }
  switch(subfunct){
    case LIST_SF:
      if (sessname) {
	res = cli_NetSessionGetInfo(cli, sessname, display_session_func);
	if (res >= 0) {
	  d_printf(SESSION_DISPLAY_CONNS);
	  return cli_NetConnectionEnum(cli, sessname, display_conns_func);
	} else 
	  return res;
      } else {
	d_printf(SESSION_ENUM_DISPLAY);
	return cli_NetSessionEnum(cli, list_sessions_func);
      }
    case DELETE_SF:
      return cli_NetSessionDel(cli, sessname);
    default:
      d_printf(ERRMSG_NOT_IMPLEMENTED);
      session_usage();
  }
  return -1;
}
	
/****************************************************************************
list a server name
****************************************************************************/
static void display_server_func(const char *name, uint32 m, const char *comment, void * reserved)
{
  d_printf("\t%-16.16s     %s\n", name, comment);
}


void server_usage(void)
{
  d_printf(NET_SERVER_USAGE); /* command syntax */

  d_printf(TARGET_USAGE, DOMAIN_MASTER); /* Target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);
  d_printf(WORKGROUP_USAGE);

  d_printf(MISC_OPT_USAGE); /* Misc options */
  d_printf(PORT_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(CONF_USAGE);
}
		    
int net_server(char * temp_workgroup, int subfunct)
{
  /* try to find master browser for our domain - if we fail try to find pdc */
  char our_workgroup[16];
  struct in_addr brow_ips;

  if((have_ip == 0) && (host[0] == 0)) {
    /* find target server based on workgroup or domain */
    if((temp_workgroup == 0) || (temp_workgroup[0] == 0)) 
      temp_workgroup = lp_workgroup();  /* by default enum our local workgroup or domain */
	
    safe_strcpy(our_workgroup, temp_workgroup,15);
        
    if (!resolve_name(our_workgroup, &brow_ips, 0x1D))  {
      /* go looking for workgroups */
      DEBUG(1,("Unable to resolve master browser via name lookup\n"));
      return -2;
    } else {
      have_ip = True;
      dest_ip = brow_ips;
    }
  }
  if(host[0] == 0) strncpy(host, inet_ntoa(dest_ip),16);
  cli = connect_to_ipc(host);
  if(!cli) {
    d_printf(ERRMSG_NOCONN_BROWSE_MSTR);
    return -2;
  }
  d_printf(SERVER_ENUM_DISPLAY); /* header for list of servers */
  return cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_ALL, display_server_func,NULL); 
	
  /* BB add mechanism to find PDC for our domain and send enum to it in this error case */ 
	   
  /* BB add server service (smb server daemon) start and stop */
}
		      
void domain_usage(void)
{
  d_printf(NET_DOMAIN_USAGE); /* command syntax */

  d_printf(TARGET_USAGE, GLBL_LCL_MASTER); /* target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);
    
  d_printf(MISC_OPT_USAGE); /* misc options */
  d_printf(PORT_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(CONF_USAGE);
}

		  
int net_domain(void)
{
  char *our_workgroup;
  struct in_addr msbrow_ip;
	

  our_workgroup = lp_workgroup();
  if((have_ip == 0) && (host[0] == 0)) {
    if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) {
      DEBUG(1,("Unable to resolve global master browser via name lookup"));
      if (!resolve_name(our_workgroup, &msbrow_ip, 0x1D))  {
        DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
        return -2;
        } else {
          have_ip = True;
          dest_ip = msbrow_ip;
        }
    } else {
      have_ip = True;
      dest_ip = msbrow_ip;
    }
  }
  if(host[0] == 0)
    strncpy(host, inet_ntoa(dest_ip),16);
  cli = connect_to_ipc(host);
  if(!cli) {
    d_printf(ERRMSG_NOCONN_BROWSE_MSTR);
    return -2;
  }
  d_printf(DOMAIN_ENUM_DISPLAY); /* header for list of domains */
  return cli_NetServerEnum(cli, cli->server_domain, SV_TYPE_DOMAIN_ENUM, display_server_func,NULL);	
	   
}
		      
void printq_usage(void)
{
  d_printf(NET_PRINTQ_USAGE);

  d_printf(TARGET_USAGE, LOCAL_HOST);
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);

  d_printf(MISC_OPT_USAGE);
  d_printf(PORT_USAGE);
  d_printf(JOBID_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(CONF_USAGE);
}	

void enum_queue(const char *queuename, uint16 pri, uint16 start, uint16 until, const char *sep, const char *pproc, const char *dest, const char *qparms, const char *qcomment, uint16 status, uint16 jobcount) {
  pstring queuecol;
  pstring statcol;

  pstrcpy(queuecol, queuename);
  pstrcat(queuecol, PRINTQ_QUEUE_WORD);

  switch (status) {
    case 0:
      pstrcpy(statcol, PRINTQ_PRINTER_ACTIVE);
      break;
    case 1:
      pstrcpy(statcol, PRINTQ_PRINTER_PAUSED);
      break;
    case 2:
      pstrcpy(statcol, PRINTQ_PRINTER_ERROR);
      break;
    case 3:
      pstrcpy(statcol, PRINTQ_PRINTER_DELPEND);
      break;
    default:
      pstrcpy(statcol, PRINTQ_PRINTER_STATUNK);
  }
  d_printf(PRINTQ_DISPLAY_ONE, queuecol, jobcount, statcol);
}

void enum_jobs(uint16 jobid, const char *ownername, const char *notifyname, const char *datatype, const char *jparms, uint16 pos, uint16 status, const char *jstatus, uint submitted, uint jobsize, const char *comment) {
  pstring statcol;

  switch (status) {
    case 0:
      pstrcpy(statcol, PRINTQ_JOB_QUEUED);
      break;
    case 1:
      pstrcpy(statcol, PRINTQ_JOB_PAUSED);
      break;
    case 2:
      pstrcpy(statcol, PRINTQ_JOB_SPOOLING);
      break;
    case 3:
      pstrcpy(statcol, PRINTQ_JOB_PRINTING);
      break;
    default:
      pstrcpy(statcol, PRINTQ_PRINTER_STATUNK);
  }
  d_printf(PRINTQ_DISPLAY_JOB, ownername, jobid, jobsize, statcol);
}

int net_printq(int subfunct, const char *printq, int jobid)
{
  struct in_addr target_ip;
 
  if((have_ip == 0) && (host[0] == 0)) {
    if (!resolve_name("localhost", &target_ip, 0x20)) {
      DEBUG(1,("No remote server specified, unable to resolve connection to localhost via name lookup"));
      return -1;

    } else {
      have_ip = True;
      dest_ip = target_ip;
    }
  }
  if(host[0] == 0) 
    strncpy(host, inet_ntoa(dest_ip),16);
  cli = connect_to_ipc(host);
  if(!cli) {
    d_printf(ERRMSG_NOCONN_TARGET_SRVR);
    return -2;
  }
  switch(subfunct) {
    case LIST_SF:
	d_printf(PRINTQ_ENUM_DISPLAY, host);
      if (printq)
	return cli_NetPrintQGetInfo(cli, printq, enum_queue, enum_jobs);
      else
	return cli_NetPrintQEnum(cli, enum_queue, enum_jobs);
    case DELETE_SF:
      return cli_printjob_del(cli, jobid);
    default:
      d_printf(ERRMSG_NOT_IMPLEMENTED);
      return -1;
  }
}


	
void user_usage(void)
{
  d_printf(NET_USER_USAGE); /* command syntax */

  d_printf(TARGET_USAGE, LOCAL_HOST); /* target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);
  d_printf(WORKGROUP_USAGE);
    
  d_printf(MISC_OPT_USAGE); /* misc options */
  d_printf(PORT_USAGE);
  d_printf(COMMENT_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(CONF_USAGE);
  d_printf(LONG_USAGE);
} 
	
void user_fn(const char *user_name, const char *comment, const char * home_dir, const char * logon_script, void *state)
{
   d_printf("%-21.21s\n", user_name);
}

void long_user_fn(const char *user_name, const char *comment, const char * home_dir, const char * logon_script, void *state)
{
   d_printf("%-21.21s %-47.47s %-35.35s %35.35s\n", user_name, comment, home_dir, logon_script);
}
      		  
void group_member_fn(const char *user_name, void *state)
{
   d_printf("%-21.21s\n", user_name);
}

int net_user(int subfunct, const char * username, const char * comment, int flags)
{
  struct in_addr target_ip;
 
  if((have_ip == 0) && (host[0] == 0)) {
    if (!resolve_name("localhost", &target_ip, 0x20)) {
      DEBUG(1,("No remote server specified, unable to resolve connection to localhost via name lookup"));
      return -1;

    } else {
      have_ip = True;
      dest_ip = target_ip;
    }
  }
  if(host[0] == 0)  
    strncpy(host, inet_ntoa(dest_ip),16);
  cli = connect_to_ipc(host);
  if(!cli) {
    d_printf(ERRMSG_NOCONN_TARGET_SRVR);
    return -2;
  }
  if (subfunct == DELETE_SF) {
    if (username == NULL) {
      d_printf(ERRMSG_USERNAME_MISSING);
      return -1;
    } else
      return cli_NetUserDelete(cli, username);
  } else if (subfunct == LIST_SF) {
      if(long_list_entries) {
        d_printf(USER_ENUM_DISPLAY);
	    return cli_RNetUserEnum(cli, long_user_fn, NULL);
      }
      else
	    return cli_RNetUserEnum(cli, user_fn, NULL); 
  } else if (subfunct == ADD_SF) {
    if (username == NULL) {
      d_printf(ERRMSG_USERNAME_MISSING);
      return -1;
    } else {
      RAP_USER_INFO_1 userinfo;
      
      safe_strcpy(userinfo.user_name, username, sizeof(userinfo.user_name));
      if(flags == -1) flags = 0x21; 

      userinfo.userflags = flags;
      userinfo.reserved1 = '\0';
      userinfo.comment = comment;
      userinfo.priv = 1; 
      userinfo.home_dir = NULL;
      userinfo.logon_script = NULL;

      return cli_NetUserAdd(cli, &userinfo);
    }
  } else if (subfunct == INFO_SF) {
    if (username == NULL) {
      d_printf(ERRMSG_USERNAME_MISSING);
      return -1;
    } else {
      /*  RAP_USER_INFO_1 userinfo;
          cli_NetUserInfo (cli, &userinfo);     */  /* BB need to add call to get user info level 3 or 4 */
        return  cli_NetUserGetGroups(cli, username, group_member_fn, NULL ); 
    }
  }
  else
    d_printf(ERRMSG_NOT_IMPLEMENTED);
  return -1;

}


void group_usage(void)
{
  d_printf(NET_GROUP_USAGE); /* command syntax */

  d_printf(TARGET_USAGE, LOCAL_HOST); /* target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);

  d_printf(MISC_OPT_USAGE); /* misc options */
  d_printf(PORT_USAGE);
  d_printf(COMMENT_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(WORKGROUP_USAGE);
  d_printf(CONF_USAGE);
  d_printf(LONG_USAGE);
}

void long_group_fn(const char *group_name, const char *comment, void *state)
{
   d_printf("%-21.21s %-50.50s\n", group_name, comment);
}

void group_fn(const char *group_name, const char *comment, void *state)
{
   d_printf("%-21.21s\n", group_name);
}

int net_group(int subfunct, const char * groupname, const char * comment)
{
  struct in_addr target_ip;
 
  if((have_ip == 0) && (host[0] == 0)) {
    if (!resolve_name("localhost", &target_ip, 0x20)) {
      DEBUG(1,("No remote server specified, unable to resolve connection to localhost via name lookup"));
      return -1;

    } else {
      have_ip = True;
      dest_ip = target_ip;
    }
  }
  if(host[0] == 0)  
    strncpy(host, inet_ntoa(dest_ip),16);
  cli = connect_to_ipc(host);
  if(!cli) {
    d_printf(ERRMSG_NOCONN_TARGET_SRVR);
    return -2;
  }
  if (subfunct == DELETE_SF) {
    if (groupname == NULL) {
      d_printf(ERRMSG_GROUPNAME_MISSING);
      return -1;
    } else
      return cli_NetGroupDelete(cli, groupname);
  } else if (subfunct == LIST_SF) {
    if(long_list_entries) {
	  d_printf("%-21.21s %-50.50s\n", GROUP_STR, COMMENT_STR); 
      d_printf("-----------------------------\n");
	  return cli_RNetGroupEnum(cli, long_group_fn, NULL);
    }
    else
      return cli_RNetGroupEnum(cli, group_fn, NULL); 
  } else if (subfunct == ADD_SF) {
    if (groupname == NULL) {
      d_printf(ERRMSG_GROUPNAME_MISSING);
      return -1;
    } else {
      RAP_GROUP_INFO_1 grinfo;

  /* BB check for length 21 or smaller explicitly ? BB */
      safe_strcpy(grinfo.group_name, groupname, sizeof(grinfo.group_name));
      grinfo.reserved1 = '\0';
      grinfo.comment = comment;

      return cli_NetGroupAdd(cli, &grinfo);
    }
  } else
    d_printf(ERRMSG_NOT_IMPLEMENTED);
  return -1;
}

void groupmember_usage(void)
{
  d_printf(NET_GROUPMEMBER_USAGE); /* command syntax */

  d_printf(TARGET_USAGE, LOCAL_HOST); /* target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);

  d_printf(MISC_OPT_USAGE); /* misc options */
  d_printf(PORT_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(WORKGROUP_USAGE);
  d_printf(CONF_USAGE);
}


int net_groupmember(int subfunct, const char * groupname, const char * username)
{
    struct in_addr target_ip;

    if((have_ip == 0) && (host[0] == 0)) {
        if (!resolve_name("localhost", &target_ip, 0x20)) {
            DEBUG(1,("No remote server specified, unable to resolve connection to localhost via name lookup"));
            return -1;
        } else {
            have_ip = True;
            dest_ip = target_ip;
        }
    }
    if(host[0] == 0)  
        strncpy(host, inet_ntoa(dest_ip),16);
    cli = connect_to_ipc(host);
    if(!cli) {
        d_printf(ERRMSG_NOCONN_TARGET_SRVR);
        return -2;
    }
    if (groupname == NULL) {
      d_printf(ERRMSG_GROUPNAME_MISSING);
      return -1;
    }

    if (subfunct == LIST_SF)
      return cli_NetGroupGetUsers(cli, groupname, group_member_fn, NULL ); 

    if (username == NULL) {
      d_printf(ERRMSG_USERNAME_MISSING);
      return -1;
    }

    if (subfunct == DELETE_SF)
      return cli_NetGroupDelUser(cli, groupname, username);
    else if (subfunct == ADD_SF)
      return cli_NetGroupAddUser(cli, groupname, username);
    else
      d_printf(ERRMSG_NOT_IMPLEMENTED);
    return -1;

}

void validate_usage(void)
{
  d_printf(NET_VALIDATE_USAGE); /* command syntax */
  
  d_printf(TARGET_USAGE, GLBL_LCL_MASTER); /* target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);
  d_printf(WORKGROUP_USAGE);
    
  d_printf(MISC_OPT_USAGE); /* misc options */
  d_printf(PORT_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(CONF_USAGE);
}

int net_validate(char * username)
{
  d_printf(ERRMSG_NOT_IMPLEMENTED);
  return 0;
}

void service_usage(void)
{
  d_printf(NET_SERVICE_USAGE); /* command syntax */
  
  d_printf(TARGET_USAGE, GLBL_LCL_MASTER); /* target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);
    
  d_printf(MISC_OPT_USAGE); /* misc options */
  d_printf(PORT_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(CONF_USAGE);
}


int net_service(int subfunct, const char * servicename, const char * srvc_args)
{
struct in_addr target_ip;

if((have_ip == 0) && (host[0] == 0)) {
  if (!resolve_name("localhost", &target_ip, 0x20)) {
    DEBUG(1,("No remote server specified, unable to resolve connection to localhost via name lookup"));
    return -1;
  } else {
    have_ip = True;
    dest_ip = target_ip;
  }
}
if(host[0] == 0)  
  strncpy(host, inet_ntoa(dest_ip),16);
cli = connect_to_ipc(host);
if(!cli) {
  d_printf(ERRMSG_NOCONN_TARGET_SRVR);
  return -2;
}

if (subfunct == LIST_SF) {
  if(long_list_entries) {
    d_printf("%-15.15s %-50.50s\n", SERVICE_STR, COMMENT_STR); 
    d_printf("-----------------------------\n");
    return cli_RNetServiceEnum(cli, long_group_fn, NULL);
  }
  else
    return cli_RNetServiceEnum(cli, group_fn, NULL); 
} else
  d_printf(ERRMSG_NOT_IMPLEMENTED);
return -1;

}

int net_password(const char * username, const char * old_password, const char * new_password)
{
    struct in_addr target_ip;

    if((have_ip == 0) && (host[0] == 0)) {
      if (!resolve_name("localhost", &target_ip, 0x20)) {
        DEBUG(1,("No remote server specified, unable to resolve connection to localhost via name lookup"));
        return -1;

      } else {
        have_ip = True;
        dest_ip = target_ip;
      }
    }
    if(host[0] == 0)  
      strncpy(host, inet_ntoa(dest_ip),16);
    cli = connect_to_ipc(host);
    if(!cli) {
      d_printf(ERRMSG_NOCONN_TARGET_SRVR);
      return -2;
    }

    /* BB Add check for password lengths? */
    return cli_oem_change_password(cli, username, new_password, old_password);
}

void password_usage(void)
{
  d_printf(NET_PASSWORD_USAGE); /* command syntax */
  
  d_printf(TARGET_USAGE, GLBL_LCL_MASTER); /* target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);
  d_printf(WORKGROUP_USAGE);
    
  d_printf(MISC_OPT_USAGE); /* misc options */
  d_printf(PORT_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(CONF_USAGE);
}


void admin_usage(void)
{
  d_printf(NET_ADMIN_USAGE); /* command syntax */
  
  d_printf(TARGET_USAGE, GLBL_LCL_MASTER); /* target options */
  d_printf(SERVER_USAGE);
  d_printf(IPADDRESS_USAGE);
  d_printf(WORKGROUP_USAGE);
    
  d_printf(MISC_OPT_USAGE); /* misc options */
  d_printf(PORT_USAGE);
  d_printf(MYWORKGROUP_USAGE);
  d_printf(DEBUG_USAGE);
  d_printf(MYNAME_USAGE);
  d_printf(USER_USAGE);
  d_printf(CONF_USAGE);
}


int net_admin(const char * command, const char * cmd_args, const char * environment)
{
  d_printf(ERRMSG_NOT_IMPLEMENTED);
  return 0;

}

/****************************************************************************
  main program
****************************************************************************/
int main(int argc,char *argv[])
{
  int opt,i;
  char *p;
  int rc = 0;
  int func = 0;
  int subfunc = LIST_SF;
  int argc_new = 0;
  const char ** argv_new;
  poptContext pc;
  static char *servicesf = CONFIGFILE;
  static char *target_workgroup = NULL;
  static char *comment = "";
  static char *user_name = NULL;
  static char *my_workgroup = NULL;
  static char *requester_name = NULL;
  static char *dest_host = NULL;
  static int maxusers = -1;
  static int flagsarg = -1;
  static int jobid = 0;
  static int debuglevel;
  
  static struct poptOption long_options[] = {
    {"help",        'h', POPT_ARG_NONE,   0,     'h'},
    {"workgroup",   'w', POPT_ARG_STRING, &target_workgroup},
    {"myworkgroup", 'W', POPT_ARG_STRING, &my_workgroup},
    {"user",        'U', POPT_ARG_STRING, &user_name, 'U'},
    {"ipaddress",   'I', POPT_ARG_STRING, 0,     'I'},
    {"port",        'p', POPT_ARG_INT,    &port},
    {"myname",      'n', POPT_ARG_STRING, &requester_name},
    {"conf",        's', POPT_ARG_STRING, &servicesf},
    {"debug",       'd', POPT_ARG_INT,    &debuglevel, 'd'},
    {"server",      'S', POPT_ARG_STRING, &dest_host},
    {"comment",     'C', POPT_ARG_STRING, &comment},
    {"maxusers",    'M', POPT_ARG_INT,    &maxusers},
    {"flags",       'F', POPT_ARG_INT,    &flagsarg},
    {"jobid",       'j', POPT_ARG_INT,    &jobid},
    {"long",        'l', POPT_ARG_NONE,   &long_list_entries},
    { 0, 0, 0, 0}
  };

  got_pass = 0;
  dest_ip = ipzero;
  host[0] = 0;

  dbf = x_stdout;

  pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
		      POPT_CONTEXT_KEEP_FIRST);

  while((opt = poptGetNextOpt(pc)) != -1) {
    switch (opt) {
      case 'h':
        usage();
	exit(0);
	break;
      case 'd':
	DEBUGLEVEL=debuglevel;
	break;
      case 'I':
	dest_ip = *interpret_addr2(poptGetOptArg(pc));
	if(zero_ip(dest_ip))
	  d_printf(ERRMSG_INVALID_IPADDRESS);
	else
	  have_ip = True;
	break;
      case 'U':
	p = strchr(user_name,'%');
	pstrcpy(global_user_name, user_name);
	if (p) {
	  *p = 0;
	  pstrcpy(password,p+1);
	  got_pass = 1;
	}
	break;
      default:
	d_printf(ERRMSG_INVALID_OPTION, (char)opt, opt);
	usage();
    }
  }

  lp_load(servicesf,True,False,False);       

  argv_new = (const char **)poptGetArgs(pc);

  argc_new = argc;
  for (i=0; i<argc; i++) {
    if (argv_new[i] == NULL) {
      argc_new = i;
      break;
    }
  }
  	 
  if (argc_new < 2) {
    usage();
    return -1;
  }

  func = get_func(argv_new[1]);

  if (func == 0)
    return -1;

  if (argc_new < 3) {
    if (func == VALIDATEF) {
      validate_usage();
      return -1;
    }
    if (func == HELPF) {
      usage();
      return 0;
    }
  }
        
  if (func == HELPF) {
     switch(get_func(argv_new[2])) { 
      case FILEF:
	    file_usage();
	    break;
      case SHAREF:
	    share_usage();
	    break;
      case SESSIONF:
	    session_usage();
	    break;
      case SERVERF:
	    server_usage();
	    break;
      case DOMAINF:
	    domain_usage();
	    break;
      case PRINTQF:
	    printq_usage();
	    break;
      case USERF:
	    user_usage();
	    break;
      case GROUPF:
	    group_usage();
	    break;
      case VALIDATEF:
	    validate_usage();
	    break;
      case SERVICEF:
        service_usage();
        break;
      case ADMINF:
        admin_usage();
        break;
      case GROUPMEMBERF:
	    groupmember_usage();
	    break;
      case PASSWORDF:
        password_usage();
        break;
      case HELPF:
	    usage();
	    break;
      default:
	    d_printf(ERRMSG_INVALID_HELP_OPTION);
	    usage();
    }
    return 0;
  }

  if (argc_new > 2) {
    /* parse next parm (argument 2) - i.e. sub function */
    subfunc = get_subfunc(argv_new[2]);
  }
  if (func == 0) return -1;

  if (requester_name)
    pstrcpy(global_requester_name, requester_name);
  else
    get_myname(global_requester_name);

  if (user_name)
    pstrcpy(global_user_name, user_name);
  else if (getenv("LOGNAME"))
    pstrcpy(global_user_name,getenv("LOGNAME"));

  fstrcpy(global_workgroup, my_workgroup ? my_workgroup :lp_workgroup());

  if (dest_host)
    pstrcpy(host, dest_host);

  if((have_ip) && (host[0]))
    d_printf(ERRMSG_BOTH_SERVER_IPADDRESS);

  while (!got_pass) {    /* BB if nulluser operation. why bother to ask for pword BB */
    p = getpass(PASSWORD_PROMPT);
    if (p) {
      pstrcpy(password, p);
      got_pass = 1;
    }
  }

  load_interfaces();

  switch (func) {
    case FILEF:
      if(argc_new <= 3) {
	    if (subfunc == OTHER_SF)
	        rc = net_file(subfunc, argv_new[2]);
	    else
	        rc = net_file(subfunc,NULL);
      } else 
	    rc = net_file(subfunc,argv_new[3]);
      break;
    case SHAREF:
      if (argc_new == 2)
	    rc = net_share(subfunc, NULL, NULL, 0);
      else
	    rc = net_share(subfunc,argv_new[3], comment, maxusers);
      break;
    case SESSIONF:
      if (argc_new <= 3)
	    rc = net_session(subfunc,NULL);
      else
	    rc = net_session(subfunc,argv_new[3]);
      break;
    case SERVERF:
      rc = net_server(target_workgroup, subfunc);
      break;
    case DOMAINF:
      if(subfunc != LIST_SF)
	    d_printf(ERRMSG_INVALID_DOMAIN_ACTION);
      rc = net_domain();
      break;
    case USERF:
      if (argc_new == 2)
	    rc = net_user(subfunc, NULL, NULL, -1);
      else if(argc_new == 3)
	    rc = net_user(subfunc,NULL, NULL, -1);
      else if(argc_new > 3)
	    rc = net_user(subfunc,argv_new[3], comment, flagsarg);
      break;
    case GROUPF:
      if (argc_new == 2)
	    rc = net_group(subfunc, NULL, NULL);
      else if(argc_new == 3)
	    rc = net_group(subfunc,NULL, NULL);
      else if(argc_new > 3)
	    rc = net_group(subfunc,argv_new[3], comment);
      break;
    case GROUPMEMBERF:
      if (argc_new == 4)
	    rc = net_groupmember(subfunc, argv_new[3], NULL);
      else if (argc_new == 5)
	    rc = net_groupmember(subfunc, argv_new[3], argv_new[4]);
      else {
	    groupmember_usage();
	    rc = -1;
      }
      break;
    case VALIDATEF:
      rc = net_validate(global_user_name);
      break;
    case SERVICEF:
      rc = net_service(subfunc, argv_new[3], argv_new[4]);
      break;
    case ADMINF:
      if(argc_new < 3)
      {
          admin_usage();
          rc = -1;
      }
      else if (argc_new == 3) {
          rc = net_admin(argv_new[2], NULL, NULL);
      } else if (argc_new == 4) {
          rc = net_admin(argv_new[2], argv_new[3], NULL);
      } else {
          rc = net_admin(argv_new[2], argv_new[3], argv_new[4]);
      }
      break;
    case PASSWORDF:
      if(argc_new != 5)
      {
        password_usage();
        rc = -1;
      } else {
        rc = net_password(argv_new[2], argv_new[3], argv_new[4]);
      }
      break;
    case PRINTQF:
      if (argc_new <= 3)
	    rc = net_printq(subfunc, NULL, jobid);
      else
	    rc = net_printq(subfunc, argv_new[3], jobid);
      break;
    default:
      usage();
      return -1;
  }	
  DEBUG(1,("return code = %d\n", rc));
  return rc;
}