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
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
|
/*
Unix SMB/Netbios implementation.
Version 1.9.
NBT netbios routines and daemon - version 2
Copyright (C) Andrew Tridgell 1994-1995
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.
Revision History:
14 jan 96: lkcl@pires.co.uk
added multiple workgroup domain master support
*/
#include "includes.h"
extern int ClientNMB;
extern int ClientDGRAM;
#define FIND_SELF 0x01
#define FIND_WINS 0x02
#define FIND_LOCAL 0x04
extern int DEBUGLEVEL;
extern pstring scope;
extern BOOL CanRecurse;
extern pstring myname;
extern struct in_addr ipzero;
extern struct in_addr ipgrp;
extern struct subnet_record *subnetlist;
#define WINS_LIST "wins.dat"
#define GET_TTL(ttl) ((ttl)?MIN(ttl,lp_max_ttl()):lp_max_ttl())
/****************************************************************************
finds the appropriate subnet structure. directed packets (non-bcast) are
assumed to come from a point-to-point (P or M node), and so the subnet we
return in this instance is the WINS 'pseudo-subnet' with ip 255.255.255.255
****************************************************************************/
static struct subnet_record *find_req_subnet(struct in_addr ip, BOOL bcast)
{
if (bcast)
{
/* identify the subnet the broadcast request came from */
return find_subnet(*iface_bcast(ip));
}
/* find the subnet under the pseudo-ip of 255.255.255.255 */
return find_subnet(ipgrp);
}
/****************************************************************************
true if two netbios names are equal
****************************************************************************/
static BOOL name_equal(struct nmb_name *n1,struct nmb_name *n2)
{
if (n1->name_type != n2->name_type) return(False);
return(strequal(n1->name,n2->name) && strequal(n1->scope,n2->scope));
}
/****************************************************************************
add a netbios name into the namelist
**************************************************************************/
static void add_name(struct subnet_record *d, struct name_record *n)
{
struct name_record *n2;
if (!d) return;
if (!d->namelist)
{
d->namelist = n;
n->prev = NULL;
n->next = NULL;
return;
}
for (n2 = d->namelist; n2->next; n2 = n2->next) ;
n2->next = n;
n->next = NULL;
n->prev = n2;
}
/****************************************************************************
remove a name from the namelist. The pointer must be an element just
retrieved
**************************************************************************/
void remove_name(struct subnet_record *d, struct name_record *n)
{
struct name_record *nlist;
if (!d) return;
nlist = d->namelist;
while (nlist && nlist != n) nlist = nlist->next;
if (nlist)
{
if (nlist->next) nlist->next->prev = nlist->prev;
if (nlist->prev) nlist->prev->next = nlist->next;
free(nlist);
}
}
/****************************************************************************
find a name in a namelist.
**************************************************************************/
static struct name_record *find_name(struct name_record *n,
struct nmb_name *name,
int search, struct in_addr ip)
{
struct name_record *ret;
for (ret = n; ret; ret = ret->next)
{
if (name_equal(&ret->name,name))
{
/* self search: self names only */
if ((search&FIND_SELF) == FIND_SELF && ret->source != SELF)
continue;
/* zero ip is either samba's ip or a way of finding a
name without needing to know the ip address */
if (zero_ip(ip) || ip_equal(ip, ret->ip))
{
return ret;
}
}
}
return NULL;
}
/****************************************************************************
find a name in the domain database namelist
search can be any of:
FIND_SELF - look exclusively for names the samba server has added for itself
FIND_LOCAL - look for names in the local subnet record.
FIND_WINS - look for names in the WINS record
**************************************************************************/
static struct name_record *find_name_search(struct subnet_record **d,
struct nmb_name *name,
int search, struct in_addr ip)
{
if (d == NULL) return NULL; /* bad error! */
if ((search & FIND_LOCAL) == FIND_LOCAL)
{
if (*d != NULL)
{
return find_name((*d)->namelist, name, search, ip);
}
else
{
DEBUG(4,("local find_name_search with a NULL subnet pointer\n"));
return NULL;
}
}
if ((search & FIND_WINS) != FIND_WINS) return NULL;
if (*d == NULL)
{
/* find WINS subnet record */
*d = find_subnet(ipgrp);
}
if (*d == NULL) return NULL;
return find_name((*d)->namelist, name, search, ip);
}
/****************************************************************************
dump a copy of the name table
**************************************************************************/
void dump_names(void)
{
struct name_record *n;
struct subnet_record *d;
fstring fname, fnamenew;
time_t t = time(NULL);
FILE *f;
strcpy(fname,lp_lockdir());
trim_string(fname,NULL,"/");
strcat(fname,"/");
strcat(fname,WINS_LIST);
strcpy(fnamenew,fname);
strcat(fnamenew,".");
f = fopen(fnamenew,"w");
if (!f)
{
DEBUG(4,("Can't open %s - %s\n",fnamenew,strerror(errno)));
}
DEBUG(3,("Dump of local name table:\n"));
for (d = subnetlist; d; d = d->next)
for (n = d->namelist; n; n = n->next)
{
if (f && ip_equal(d->bcast_ip, ipgrp) && n->source == REGISTER)
{
fstring data;
/* XXXX i have little imagination as to how to output nb_flags as
anything other than as a hexadecimal number :-) */
sprintf(data, "%s#%02x %s %ld %2x",
n->name.name,n->name.name_type, /* XXXX ignore the scope for now */
inet_ntoa(n->ip),
n->death_time,
n->nb_flags);
fprintf(f, "%s\n", data);
}
DEBUG(3,("%15s ", inet_ntoa(d->bcast_ip)));
DEBUG(3,("%15s ", inet_ntoa(d->mask_ip)));
DEBUG(3,("%s %15s TTL=%15d NBFLAGS=%2x\n",
namestr(&n->name),
inet_ntoa(n->ip),
n->death_time?n->death_time-t:0,
n->nb_flags));
}
fclose(f);
unlink(fname);
chmod(fnamenew,0644);
rename(fnamenew,fname);
DEBUG(3,("Wrote wins database %s\n",fname));
}
/****************************************************************************
load a netbios name database file
****************************************************************************/
void load_netbios_names(void)
{
struct subnet_record *d = find_subnet(ipgrp);
fstring fname;
FILE *f;
pstring line;
if (!d) return;
strcpy(fname,lp_lockdir());
trim_string(fname,NULL,"/");
strcat(fname,"/");
strcat(fname,WINS_LIST);
f = fopen(fname,"r");
if (!f) {
DEBUG(2,("Can't open wins database file %s\n",fname));
return;
}
while (!feof(f))
{
pstring name_str, ip_str, ttd_str, nb_flags_str;
pstring name;
int type = 0;
int nb_flags;
time_t ttd;
struct in_addr ipaddr;
enum name_source source;
char *ptr;
int count = 0;
char *p;
if (!fgets_slash(line,sizeof(pstring),f)) continue;
if (*line == '#') continue;
ptr = line;
if (next_token(&ptr,name_str ,NULL)) ++count;
if (next_token(&ptr,ip_str ,NULL)) ++count;
if (next_token(&ptr,ttd_str ,NULL)) ++count;
if (next_token(&ptr,nb_flags_str,NULL)) ++count;
if (count <= 0) continue;
if (count != 4) {
DEBUG(0,("Ill formed wins line"));
DEBUG(0,("[%s]: name#type ip nb_flags abs_time\n",line));
continue;
}
/* netbios name. # divides the name from the type (hex): netbios#xx */
strcpy(name,name_str);
p = strchr(name,'#');
if (p) {
*p = 0;
sscanf(p+1,"%x",&type);
}
/* decode the netbios flags (hex) and the time-to-die (seconds) */
sscanf(nb_flags_str,"%x",&nb_flags);
sscanf(ttd_str,"%ld",&ttd);
ipaddr = *interpret_addr2(ip_str);
if (ip_equal(ipaddr,ipzero)) {
source = SELF;
}
else
{
source = REGISTER;
}
DEBUG(4, ("add WINS line: %s#%02x %s %ld %2x\n",
name,type, inet_ntoa(ipaddr), ttd, nb_flags));
/* add all entries that have 60 seconds or more to live */
if (ttd - 10 < time(NULL) || ttd == 0)
{
time_t t = (ttd?ttd-time(NULL):0) / 3;
/* add netbios entry read from the wins.dat file. IF it's ok */
add_netbios_entry(d,name,type,nb_flags,t,source,ipaddr,True,True);
}
}
fclose(f);
}
/****************************************************************************
remove an entry from the name list
****************************************************************************/
void remove_netbios_name(struct subnet_record *d,
char *name,int type, enum name_source source,
struct in_addr ip)
{
struct nmb_name nn;
struct name_record *n;
int search = FIND_LOCAL;
/* if it's not a special browser name, search the WINS database */
if (type != 0x01 && type != 0x1d && type != 0x1e)
search |= FIND_WINS;
make_nmb_name(&nn, name, type, scope);
n = find_name_search(&d, &nn, search, ip);
if (n && n->source == source) remove_name(d,n);
}
/****************************************************************************
add an entry to the name list.
this is a multi-purpose function.
it adds samba's own names in to its records on each interface, keeping a
record of whether it is a master browser, domain master, or WINS server.
it also keeps a record of WINS entries (names of type 0x00, 0x20, 0x03 etc)
****************************************************************************/
struct name_record *add_netbios_entry(struct subnet_record *d,
char *name, int type, int nb_flags,
int ttl, enum name_source source, struct in_addr ip,
BOOL new_only,BOOL wins)
{
struct name_record *n;
struct name_record *n2=NULL;
int search = 0;
BOOL self = source == SELF;
/* add the name to the WINS list if the name comes from a directed query */
search |= wins ? FIND_WINS : FIND_LOCAL;
/* search for SELF names only */
search |= self ? FIND_SELF : 0;
if (!self)
{
if (wins)
{
if (type == 0x01 || type == 0x1d || type == 0x1e)
{
/* XXXX WINS server supposed to ignore special browser names. hm.
but is a primary domain controller supposed to ignore special
browser names? luke doesn't think so, but can't test it! :-)
*/
return NULL;
}
}
else /* !wins */
{
/* the only broadcast (non-WINS) names we are adding are ours (SELF) */
return NULL;
}
}
n = (struct name_record *)malloc(sizeof(*n));
if (!n) return(NULL);
bzero((char *)n,sizeof(*n));
make_nmb_name(&n->name,name,type,scope);
if ((n2 = find_name_search(&d, &n->name, search, new_only?ipzero:ip)))
{
free(n);
if (new_only || (n2->source==SELF && source!=SELF)) return n2;
n = n2;
}
if (ttl)
n->death_time = time(NULL)+ttl*3;
n->refresh_time = time(NULL)+GET_TTL(ttl);
n->ip = ip;
n->nb_flags = nb_flags;
n->source = source;
if (!n2) add_name(d,n);
DEBUG(3,("Added netbios name %s at %s ttl=%d nb_flags=%2x\n",
namestr(&n->name),inet_ntoa(ip),ttl,nb_flags));
return(n);
}
/****************************************************************************
remove an entry from the name list
****************************************************************************/
void remove_name_entry(struct subnet_record *d, char *name,int type)
{
/* XXXX BUG: if samba is offering WINS support, it should still broadcast
a de-registration packet to the local subnet before removing the
name from its local-subnet name database. */
if (lp_wins_support())
{
/* we are a WINS server. */
/* XXXX assume that if we are a WINS server that we are therefore
not pointing to another WINS server as well. this may later NOT
actually be true */
remove_netbios_name(d,name,type,SELF,ipzero);
}
else
{
/* not a WINS server: cannot just remove our own names: we have to
ask permission from the WINS server, or if no reply is received,
_then_ we can remove the name */
struct name_record n;
struct name_record *n2=NULL;
make_nmb_name(&n.name,name,type,scope);
if ((n2 = find_name_search(&d, &n.name, FIND_SELF, ipzero)))
{
/* check name isn't already being de-registered */
if (NAME_DEREG(n2->nb_flags))
return;
/* mark the name as in the process of deletion. */
n2->nb_flags &= NB_DEREG;
}
queue_netbios_pkt_wins(d,ClientNMB,NMB_REL,NAME_RELEASE,
name, type, 0, 0,
False, True, ipzero);
}
}
/****************************************************************************
add an entry to the name list
****************************************************************************/
void add_my_name_entry(struct subnet_record *d,char *name,int type,int nb_flags)
{
BOOL re_reg = False;
struct nmb_name n;
if (!d) return;
/* not that it particularly matters, but if the SELF name already exists,
it must be re-registered, rather than just registered */
make_nmb_name(&n, name, type, scope);
if (find_name(d->namelist, &n, SELF, ipzero))
re_reg = True;
/* always add our own entries */
add_netbios_entry(d,name,type,nb_flags,0,SELF,ipzero,False,lp_wins_support());
/* XXXX BUG: if samba is offering WINS support, it should still add the
name entry to a local-subnet name database. see rfc1001.txt 15.1.1 p28
regarding the point about M-nodes. */
if (!lp_wins_support())
{
/* samba isn't supporting WINS itself: register the name using broadcast
or with another WINS server.
XXXX note: we may support WINS and also know about other WINS servers
in the future.
*/
queue_netbios_pkt_wins(d,ClientNMB,
re_reg ? NMB_REG_REFRESH : NMB_REG, NAME_REGISTER,
name, type, nb_flags, GET_TTL(0),
False, True, ipzero);
}
}
/****************************************************************************
add the magic samba names, useful for finding samba servers
**************************************************************************/
void add_my_names(void)
{
BOOL wins = lp_wins_support();
struct subnet_record *d;
struct in_addr ip = ipzero;
/* each subnet entry, including WINS pseudo-subnet, has SELF names */
/* XXXX if there was a transport layer added to samba (ipx/spx etc) then
there would be yet _another_ for-loop, this time on the transport type
*/
for (d = subnetlist; d; d = d->next)
{
add_my_name_entry(d, myname,0x20,NB_ACTIVE);
add_my_name_entry(d, myname,0x03,NB_ACTIVE);
add_my_name_entry(d, myname,0x00,NB_ACTIVE);
add_my_name_entry(d, myname,0x1f,NB_ACTIVE);
add_netbios_entry(d,"*",0x0,NB_ACTIVE,0,SELF,ip,False,wins);
add_netbios_entry(d,"__SAMBA__",0x20,NB_ACTIVE,0,SELF,ip,False,wins);
add_netbios_entry(d,"__SAMBA__",0x00,NB_ACTIVE,0,SELF,ip,False,wins);
if (wins) {
/* the 0x1c name gets added by any WINS server it seems */
add_my_name_entry(d, my_workgroup(),0x1c,NB_ACTIVE|NB_GROUP);
}
}
}
/****************************************************************************
remove all the samba names... from a WINS server if necessary.
**************************************************************************/
void remove_my_names()
{
struct subnet_record *d;
for (d = subnetlist; d; d = d->next)
{
struct name_record *n, *next;
for (n = d->namelist; n; n = next)
{
next = n->next;
if (n->source == SELF)
{
/* get all SELF names removed from the WINS server's database */
/* XXXX note: problem occurs if this removes the wrong one! */
remove_name_entry(d,n->name.name, n->name.name_type);
}
}
}
}
/*******************************************************************
refresh my own names
******************************************************************/
void refresh_my_names(time_t t)
{
struct subnet_record *d;
for (d = subnetlist; d; d = d->next)
{
struct name_record *n;
for (n = d->namelist; n; n = n->next)
{
/* each SELF name has an individual time to be refreshed */
if (n->source == SELF && n->refresh_time < time(NULL))
{
add_my_name_entry(d,n->name.name,n->name.name_type,n->nb_flags);
}
}
}
}
/*******************************************************************
queries names occasionally. an over-cautious, non-trusting WINS server!
this function has been added because nmbd could be restarted. it
is generally a good idea to check all the names that have been
reloaded from file.
XXXX which names to poll and which not can be refined at a later date.
******************************************************************/
void query_refresh_names(void)
{
struct name_record *n;
struct subnet_record *d = find_subnet(ipgrp);
static time_t lasttime = 0;
time_t t = time(NULL);
int count = 0;
int name_refresh_time = NAME_POLL_REFRESH_TIME;
int max_count = name_refresh_time * 2 / NAME_POLL_INTERVAL;
if (max_count > 10) max_count = 10;
name_refresh_time = NAME_POLL_INTERVAL * max_count / 2;
/* if (!lp_poll_wins()) return; polling of registered names allowed */
if (!d) return;
if (t - lasttime < NAME_POLL_INTERVAL) return;
for (n = d->namelist; n; n = n->next)
{
/* only do unique, registered names */
if (n->source != REGISTER) continue;
if (!NAME_GROUP(n->nb_flags)) continue;
if (n->refresh_time < t)
{
DEBUG(3,("Polling name %s\n", namestr(&n->name)));
queue_netbios_packet(d,ClientNMB,NMB_QUERY,NAME_QUERY_CONFIRM,
n->name.name, n->name.name_type,
0,0,
False,False,n->ip);
count++;
}
if (count >= max_count)
{
/* don't do too many of these at once, but do enough to
cover everyone in the list */
return;
}
/* this name will be checked on again, if it's not removed */
n->refresh_time += name_refresh_time;
}
}
/*******************************************************************
expires old names in the namelist
******************************************************************/
void expire_names(time_t t)
{
struct name_record *n;
struct name_record *next;
struct subnet_record *d;
/* expire old names */
for (d = subnetlist; d; d = d->next)
{
for (n = d->namelist; n; n = next)
{
if (n->death_time && n->death_time < t)
{
DEBUG(3,("Removing dead name %s\n", namestr(&n->name)));
next = n->next;
if (n->prev) n->prev->next = n->next;
if (n->next) n->next->prev = n->prev;
if (d->namelist == n) d->namelist = n->next;
free(n);
}
else
{
next = n->next;
}
}
}
}
/****************************************************************************
response for a reg release received. samba has asked a WINS server if it
could release a name.
**************************************************************************/
void response_name_release(struct subnet_record *d, struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
char *name = nmb->question.question_name.name;
int type = nmb->question.question_name.name_type;
DEBUG(4,("response name release received\n"));
if (nmb->header.rcode == 0 && nmb->answers->rdata)
{
/* IMPORTANT: see expire_netbios_response_entries() */
struct in_addr found_ip;
putip((char*)&found_ip,&nmb->answers->rdata[2]);
if (ismyip(found_ip))
{
remove_netbios_name(d,name,type,SELF,found_ip);
}
}
else
{
DEBUG(2,("name release for %s rejected!\n",
namestr(&nmb->question.question_name)));
/* XXXX do we honestly care if our name release was rejected?
only if samba is issuing the release on behalf of some out-of-sync
server. if it's one of samba's SELF names, we don't care. */
}
}
/****************************************************************************
reply to a name release
****************************************************************************/
void reply_name_release(struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
struct in_addr ip;
int rcode=0;
int opcode = nmb->header.opcode;
int nb_flags = nmb->additional->rdata[0];
BOOL bcast = nmb->header.nm_flags.bcast;
struct name_record *n;
struct subnet_record *d = NULL;
char rdata[6];
int search = 0;
putip((char *)&ip,&nmb->additional->rdata[2]);
DEBUG(3,("Name release on name %s rcode=%d\n",
namestr(&nmb->question.question_name),rcode));
if (!(d = find_req_subnet(p->ip, bcast)))
{
DEBUG(3,("response packet: bcast %s not known\n",
inet_ntoa(p->ip)));
return;
}
if (bcast)
search &= FIND_LOCAL;
else
search &= FIND_WINS;
n = find_name_search(&d, &nmb->question.question_name,
search, ip);
/* XXXX under what conditions should we reject the removal?? */
if (n && n->nb_flags == nb_flags)
{
/* success = True;
rcode = 6; */
remove_name(d,n);
n = NULL;
}
if (bcast) return;
rdata[0] = nb_flags;
rdata[1] = 0;
putip(&rdata[2],(char *)&ip);
/* Send a NAME RELEASE RESPONSE */
reply_netbios_packet(p,nmb->header.name_trn_id,
rcode,opcode,True,
&nmb->question.question_name,
nmb->question.question_type,
nmb->question.question_class,
0,
rdata, 6);
}
/****************************************************************************
response for a reg request received
**************************************************************************/
void response_name_reg(struct subnet_record *d, struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
char *name = nmb->question.question_name.name;
int type = nmb->question.question_name.name_type;
BOOL bcast = nmb->header.nm_flags.bcast;
DEBUG(4,("response name registration received!\n"));
if (nmb->header.rcode == 0 && nmb->answers->rdata)
{
/* IMPORTANT: see expire_netbios_response_entries() */
int nb_flags = nmb->answers->rdata[0];
struct in_addr found_ip;
int ttl = nmb->answers->ttl;
enum name_source source = REGISTER;
putip((char*)&found_ip,&nmb->answers->rdata[2]);
if (ismyip(found_ip)) source = SELF;
add_netbios_entry(d, name,type,nb_flags,ttl,source,found_ip,True,!bcast);
}
else
{
struct work_record *work;
DEBUG(1,("name registration for %s rejected!\n",
namestr(&nmb->question.question_name)));
/* XXXX oh dear. we have problems. must deal with our name having
been rejected: e.g if it was our GROUP(1d) name, we must unbecome
a master browser. */
remove_netbios_name(d,name,type,SELF,ipzero);
if (!(work = find_workgroupstruct(d, name, False))) return;
if (AM_MASTER(work) && (type == 0x1d || type == 0x1b))
{
int remove_type = 0;
if (type == 0x1d) remove_type = SV_TYPE_MASTER_BROWSER;
if (type == 0x1b) remove_type = SV_TYPE_DOMAIN_MASTER;
become_nonmaster(d, work, remove_type);
}
}
}
/****************************************************************************
reply to a reg request
**************************************************************************/
void reply_name_reg(struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
struct nmb_name *question = &nmb->question.question_name;
struct nmb_name *reply_name = question;
char *qname = question->name;
int name_type = question->name_type;
int name_class = nmb->question.question_class;
BOOL bcast = nmb->header.nm_flags.bcast;
int ttl = GET_TTL(nmb->additional->ttl);
int nb_flags = nmb->additional->rdata[0];
BOOL group = NAME_GROUP(nb_flags);
int rcode = 0;
int opcode = nmb->header.opcode;
struct subnet_record *d = NULL;
struct name_record *n = NULL;
BOOL success = True;
BOOL recurse = True; /* true if samba replies yes/no: false if caller */
/* must challenge the current owner of the unique name */
char rdata[6];
struct in_addr ip, from_ip;
int search = 0;
putip((char *)&from_ip,&nmb->additional->rdata[2]);
ip = from_ip;
DEBUG(3,("Name registration for name %s at %s rcode=%d\n",
namestr(question),inet_ntoa(ip),rcode));
if (group)
{
/* apparently we should return 255.255.255.255 for group queries
(email from MS) */
ip = ipgrp;
}
if (!(d = find_req_subnet(p->ip, bcast)))
{
DEBUG(3,("response packet: bcast %s not known\n",
inet_ntoa(p->ip)));
return;
}
if (bcast)
search &= FIND_LOCAL;
else
search &= FIND_WINS;
/* see if the name already exists */
n = find_name_search(&d, question, search, from_ip);
if (n)
{
if (!group) /* unique names */
{
if (n->source == SELF || NAME_GROUP(n->nb_flags))
{
/* no-one can register one of samba's names, nor can they
register a name that's a group name as a unique name */
rcode = 6;
success = False;
}
else if(!ip_equal(ip, n->ip))
{
/* hm. this unique name doesn't belong to them. */
/* XXXX rfc1001.txt says:
* if we are doing secured WINS, we must send a Wait-Acknowledge
* packet (WACK) to the person who wants the name, then do a
* name query on the person who currently owns the unique name.
* if the current owner still says they own it, the person who wants
* the name can't have it. if they do not, or are not alive, they can.
*
* if we are doing non-secured WINS (which is much simpler) then
* we send a message to the person wanting the name saying 'he
* owns this name: i don't want to hear from you ever again
* until you've checked with him if you can have it!'. we then
* abandon the registration. once the person wanting the name
* has checked with the current owner, they will repeat the
* registration packet if the current owner is dead or doesn't
* want the name.
*/
/* non-secured WINS implementation: caller is responsible
for checking with current owner of name, then getting back
to us... IF current owner no longer owns the unique name */
/* XXXX please note also that samba cannot cope with
_receiving_ such redirecting, non-secured registration
packets. code to do this needs to be added.
*/
rcode = 0;
success = False;
recurse = False;
/* we inform on the current owner to the caller (which is
why it's non-secure */
reply_name = &n->name;
/* name_type = ?;
name_class = ?;
XXXX sorry, guys: i really can't see what name_type
and name_class should be set to according to rfc1001 */
}
else
{
n->ip = ip;
n->death_time = ttl?p->timestamp+ttl*3:0;
DEBUG(3,("%s owner: %s\n",namestr(&n->name),inet_ntoa(n->ip)));
}
}
else
{
/* refresh the name */
if (n->source != SELF)
{
n->death_time = ttl?p->timestamp + ttl*3:0;
}
}
/* XXXX bug reported by terryt@ren.pc.athabascau.ca */
/* names that people have checked for and not found get DNSFAILed.
we need to update the name record if someone then registers */
if (n->source == DNSFAIL)
n->source = REGISTER;
}
else
{
/* add the name to our name/subnet, or WINS, database */
n = add_netbios_entry(d,qname,name_type,nb_flags,ttl,REGISTER,ip,
True,!bcast);
}
/* if samba owns a unique name on a subnet, then it must respond and
disallow the attempted registration. if the registration is
successful by broadcast, only then is there no need to respond
(implicit registration: see rfc1001.txt 15.2.1).
*/
if (bcast || !success) return;
rdata[0] = nb_flags;
rdata[1] = 0;
putip(&rdata[2],(char *)&ip);
/* Send a NAME REGISTRATION RESPONSE (pos/neg)
or an END-NODE CHALLENGE REGISTRATION RESPONSE */
reply_netbios_packet(p,nmb->header.name_trn_id,
rcode,opcode,recurse,
reply_name, name_type, name_class,
ttl,
rdata, 6);
}
/****************************************************************************
reply to a name status query
****************************************************************************/
void reply_name_status(struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
char *qname = nmb->question.question_name.name;
int ques_type = nmb->question.question_name.name_type;
char rdata[MAX_DGRAM_SIZE];
char *countptr, *buf, *bufend;
int names_added;
struct name_record *n;
struct subnet_record *d = NULL;
BOOL bcast = nmb->header.nm_flags.bcast;
if (!(d = find_req_subnet(p->ip, bcast)))
{
DEBUG(3,("Name status req: bcast %s not known\n",
inet_ntoa(p->ip)));
return;
}
DEBUG(3,("Name status for name %s %s\n",
namestr(&nmb->question.question_name), inet_ntoa(p->ip)));
n = find_name_search(&d, &nmb->question.question_name,
FIND_SELF|FIND_LOCAL,
p->ip);
if (!n) return;
/* XXXX hack, we should calculate exactly how many will fit */
bufend = &rdata[MAX_DGRAM_SIZE] - 18;
countptr = buf = rdata;
buf += 1;
names_added = 0;
for (n = d->namelist ; n && buf < bufend; n = n->next)
{
int name_type = n->name.name_type;
if (n->source != SELF) continue;
/* start with first bit of putting info in buffer: the name */
bzero(buf,18);
sprintf(buf,"%-15.15s",n->name.name);
strupper(buf);
/* now check if we want to exclude other workgroup names
from the response. if we don't exclude them, windows clients
get confused and will respond with an error for NET VIEW */
if (name_type >= 0x1b && name_type <= 0x20 &&
ques_type >= 0x1b && ques_type <= 0x20)
{
if (!strequal(qname, n->name.name)) continue;
}
/* carry on putting name info in buffer */
buf[15] = name_type;
buf[16] = n->nb_flags;
buf += 18;
names_added++;
}
SCVAL(countptr,0,names_added);
/* XXXXXXX we should fill in more fields of the statistics structure */
bzero(buf,64);
{
extern int num_good_sends,num_good_receives;
SIVAL(buf,20,num_good_sends);
SIVAL(buf,24,num_good_receives);
}
SIVAL(buf,46,0xFFB8E5); /* undocumented - used by NT */
buf += 64;
/* Send a POSITIVE NAME STATUS RESPONSE */
reply_netbios_packet(p,nmb->header.name_trn_id,
0,0,True,
&nmb->question.question_name,
nmb->question.question_type,
nmb->question.question_class,
0,
rdata,PTR_DIFF(buf,rdata));
}
/***************************************************************************
reply to a name query
****************************************************************************/
struct name_record *search_for_name(struct subnet_record **d,
struct nmb_name *question,
struct in_addr ip, int Time, int search)
{
int name_type = question->name_type;
char *qname = question->name;
BOOL dns_type = name_type == 0x20 || name_type == 0;
struct name_record *n;
DEBUG(3,("Search for %s from %s - ", namestr(question), inet_ntoa(ip)));
/* first look up name in cache */
n = find_name_search(d,question,search,ip);
if (*d == NULL) return NULL;
/* now try DNS lookup. */
if (!n)
{
struct in_addr dns_ip;
unsigned long a;
/* only do DNS lookups if the query is for type 0x20 or type 0x0 */
if (!dns_type)
{
DEBUG(3,("types 0x20 0x1b 0x0 only: name not found\n"));
return NULL;
}
/* look it up with DNS */
a = interpret_addr(qname);
putip((char *)&dns_ip,(char *)&a);
if (!a)
{
/* no luck with DNS. We could possibly recurse here XXXX */
DEBUG(3,("no recursion.\n"));
/* add the fail to our WINS cache of names. give it 1 hour in the cache */
add_netbios_entry(*d,qname,name_type,NB_ACTIVE,60*60,DNSFAIL,dns_ip,
True, True);
return NULL;
}
/* add it to our WINS cache of names. give it 2 hours in the cache */
n = add_netbios_entry(*d,qname,name_type,NB_ACTIVE,2*60*60,DNS,dns_ip,
True,True);
/* failed to add it? yikes! */
if (!n) return NULL;
}
/* is our entry already dead? */
if (n->death_time)
{
if (n->death_time < Time) return False;
}
/* it may have been an earlier failure */
if (n->source == DNSFAIL)
{
DEBUG(3,("DNSFAIL\n"));
return NULL;
}
DEBUG(3,("OK %s\n",inet_ntoa(n->ip)));
return n;
}
/***************************************************************************
reply to a name query.
with broadcast name queries:
- only reply if the query is for one of YOUR names. all other machines on
the network will be doing the same thing (that is, only replying to a
broadcast query if they own it)
NOTE: broadcast name queries should only be sent out by a machine
if they HAVEN'T been configured to use WINS. this is generally bad news
in a wide area tcp/ip network and should be rectified by the systems
administrator. USE WINS! :-)
- the exception to this is if the query is for a Primary Domain Controller
type name (0x1b), in which case, a reply is sent.
- NEVER send a negative response to a broadcast query. no-one else will!
with directed name queries:
- if you are the WINS server, you are expected to respond with either
a negative response, a positive response, or a wait-for-acknowledgement
packet, and then later on a pos/neg response.
****************************************************************************/
void reply_name_query(struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
struct nmb_name *question = &nmb->question.question_name;
int name_type = question->name_type;
BOOL bcast = nmb->header.nm_flags.bcast;
int ttl=0;
int rcode = 0;
int nb_flags = 0;
struct in_addr retip;
char rdata[6];
struct subnet_record *d = NULL;
BOOL success = True;
struct name_record *n;
int search = 0;
if (name_type == 0x20 || name_type == 0x00 || name_type == 0x1b ||
name_type == 0x1f || name_type == 0x03 || name_type == 0x01 ||
name_type == 0x1c)
{
/* search for any of the non-'special browser' names, or for a PDC type
(0x1b) name in the WINS database.
XXXX should we include name type 0x1c: WINS server type?
*/
search |= FIND_WINS;
}
else
{
/* special browser name types e.g
^1^2__MSBROWSE__^2^1, GROUP(1d) and GROUP(1e)
name_type == 0x01 || name_type == 0x1d || name_type == 0x1e.
XXXX luke reckons we should be able to search for any SELF name
in the WINS database, if we are a primary domain controller.
*/
if (!(d = find_req_subnet(p->ip, bcast)))
{
DEBUG(3,("name query: bcast %s not known\n",
inet_ntoa(p->ip)));
success = False;
}
/* XXXX delete if shouldn't search for SELF names in WINS database */
search |= FIND_WINS;
}
if (bcast)
{
/* a name query has been made by a non-WINS configured host. search the
local interface database as well */
search |= FIND_LOCAL;
}
DEBUG(3,("Name query "));
if (search == 0)
{
/* eh? no criterion for searching database. help! */
success = False;
}
if (success && (n = search_for_name(&d,question,p->ip,p->timestamp, search)))
{
/* don't respond to broadcast queries unless the query is for
a name we own or it is for a Primary Domain Controller name */
if (bcast && n->source != SELF && name_type != 0x1b) {
if (!lp_wins_proxy() || same_net(p->ip,n->ip,*iface_nmask(p->ip))) {
/* never reply with a negative response to broadcast queries */
return;
}
}
/* name is directed query, or it's self, or it's a PDC type name, or
we're replying on behalf of a caller because they are on a different
subnet and cannot hear the broadcast. XXXX lp_wins_proxy should be
switched off in environments where broadcasts are forwarded */
/* XXXX note: for proxy servers, we should forward the query on to
another WINS server if the name is not in our database, or we are
not a WINS server ourselves
*/
ttl = n->death_time - p->timestamp;
retip = n->ip;
nb_flags = n->nb_flags;
}
else
{
if (bcast) return; /* never reply negative response to bcasts */
success = False;
}
/* if the IP is 0 then substitute my IP */
if (zero_ip(retip)) retip = *iface_ip(p->ip);
if (success)
{
rcode = 0;
DEBUG(3,("OK %s\n",inet_ntoa(retip)));
}
else
{
rcode = 3;
DEBUG(3,("UNKNOWN\n"));
}
if (success)
{
rdata[0] = nb_flags;
rdata[1] = 0;
putip(&rdata[2],(char *)&retip);
}
reply_netbios_packet(p,nmb->header.name_trn_id,
rcode,0,True,
&nmb->question.question_name,
nmb->question.question_type,
nmb->question.question_class,
ttl,
rdata, success ? 6 : 0);
}
/****************************************************************************
response from a name query server check. states of type NAME_QUERY_MST_SRV_CHK,
NAME_QUERY_SRV_CHK, and NAME_QUERY_FIND_MST dealt with here.
****************************************************************************/
static void response_server_check(struct nmb_name *ans_name,
struct response_record *n, struct subnet_record *d)
{
/* issue another state: this time to do a name status check */
enum state_type cmd = (n->state == NAME_QUERY_MST_SRV_CHK) ?
NAME_STATUS_MASTER_CHECK : NAME_STATUS_CHECK;
/* initiate a name status check on the server that replied */
queue_netbios_packet(d,ClientNMB,NMB_STATUS, cmd,
ans_name->name, ans_name->name_type,
0,0,
False,False,n->to_ip);
}
/****************************************************************************
response from a name status check. states of type NAME_STATUS_MASTER_CHECK
and NAME_STATUS_CHECK dealt with here.
****************************************************************************/
static void response_name_status_check(struct in_addr ip,
struct nmb_packet *nmb, BOOL bcast,
struct response_record *n, struct subnet_record *d)
{
/* NMB_STATUS arrives: contains workgroup name and server name required.
amongst other things. */
struct nmb_name name;
fstring serv_name;
if (interpret_node_status(d,nmb->answers->rdata,
&name,0x1d,serv_name,ip,bcast))
{
if (*serv_name)
{
sync_server(n->state,serv_name,
name.name,name.name_type, n->to_ip);
}
}
else
{
DEBUG(1,("No 0x1d name type in interpret_node_status()\n"));
}
}
/****************************************************************************
response from a name query to sync browse lists or to update our netbios
entry. states of type NAME_QUERY_SYNC and NAME_QUERY_CONFIRM
****************************************************************************/
static void response_name_query_sync(struct nmb_packet *nmb,
struct nmb_name *ans_name, BOOL bcast,
struct response_record *n, struct subnet_record *d)
{
DEBUG(4, ("Name query at %s ip %s - ",
namestr(&n->name), inet_ntoa(n->to_ip)));
if (!name_equal(&n->name, ans_name))
{
/* someone gave us the wrong name as a reply. oops. */
DEBUG(4,("unexpected name received: %s\n", namestr(ans_name)));
return;
}
if (nmb->header.rcode == 0 && nmb->answers->rdata)
{
int nb_flags = nmb->answers->rdata[0];
struct in_addr found_ip;
putip((char*)&found_ip,&nmb->answers->rdata[2]);
if (!ip_equal(n->to_ip, found_ip))
{
/* someone gave us the wrong ip as a reply. oops. */
DEBUG(4,("expected ip: %s\n", inet_ntoa(n->to_ip)));
DEBUG(4,("unexpected ip: %s\n", inet_ntoa(found_ip)));
return;
}
DEBUG(4, (" OK: %s\n", inet_ntoa(found_ip)));
if (n->state == NAME_QUERY_SYNC)
{
struct work_record *work = NULL;
if ((work = find_workgroupstruct(d, ans_name->name, False)))
{
/* the server is there: sync quick before it (possibly) dies! */
sync_browse_lists(d, work, ans_name->name, ans_name->name_type,
found_ip);
}
}
else
{
/* update our netbios name list (re-register it if necessary) */
add_netbios_entry(d, ans_name->name, ans_name->name_type,
nb_flags,GET_TTL(0),REGISTER,
found_ip,False,!bcast);
}
}
else
{
DEBUG(4, (" NEGATIVE RESPONSE!\n"));
if (n->state == NAME_QUERY_CONFIRM)
{
/* XXXX remove_netbios_entry()? */
/* lots of things we ought to do, here. if we get here,
then we're in a mess: our name database doesn't match
reality. sort it out
*/
remove_netbios_name(d,n->name.name, n->name.name_type,
REGISTER,n->to_ip);
}
}
}
/****************************************************************************
report the response record type
****************************************************************************/
static void debug_rr_type(int rr_type)
{
switch (rr_type)
{
case NMB_STATUS: DEBUG(3,("Name status ")); break;
case NMB_QUERY : DEBUG(3,("Name query ")); break;
case NMB_REG : DEBUG(3,("Name registration ")); break;
case NMB_REL : DEBUG(3,("Name release ")); break;
default : DEBUG(1,("wrong response packet type received")); break;
}
}
/****************************************************************************
report the response record nmbd state
****************************************************************************/
static void debug_state_type(int state)
{
/* report the state type to help debugging */
switch (state)
{
case NAME_QUERY_MST_SRV_CHK : DEBUG(4,("MASTER_SVR_CHECK\n")); break;
case NAME_QUERY_SRV_CHK : DEBUG(4,("NAME_QUERY_SRV_CHK\n")); break;
case NAME_QUERY_FIND_MST : DEBUG(4,("NAME_QUERY_FIND_MST\n")); break;
case NAME_STATUS_MASTER_CHECK: DEBUG(4,("NAME_STAT_MST_CHK\n")); break;
case NAME_STATUS_CHECK : DEBUG(4,("NAME_STATUS_CHECK\n")); break;
case NAME_QUERY_MST_CHK : DEBUG(4,("NAME_QUERY_MST_CHK\n")); break;
case NAME_REGISTER : DEBUG(4,("NAME_REGISTER\n")); break;
case NAME_RELEASE : DEBUG(4,("NAME_RELEASE\n")); break;
case NAME_QUERY_CONFIRM : DEBUG(4,("NAME_QUERY_CONFIRM\n")); break;
case NAME_QUERY_SYNC : DEBUG(4,("NAME_QUERY_SYNC\n")); break;
default: break;
}
}
/****************************************************************************
report any problems with the fact that a response has been received.
(responses for certain types of operations are only expected from one host)
****************************************************************************/
static BOOL response_problem_check(struct response_record *n,
struct nmb_packet *nmb, char *qname)
{
switch (nmb->answers->rr_type)
{
case NMB_REL:
{
if (n->num_msgs > 1)
{
DEBUG(1,("more than one release name response received!\n"));
return True;
}
break;
}
case NMB_REG:
{
if (n->num_msgs > 1)
{
DEBUG(1,("more than one register name response received!\n"));
return True;
}
break;
}
case NMB_QUERY:
{
if (n->num_msgs > 1)
{
if (nmb->header.rcode == 0 && nmb->answers->rdata)
{
int nb_flags = nmb->answers->rdata[0];
if ((!NAME_GROUP(nb_flags)))
{
/* oh dear. more than one person responded to a unique name.
there is either a network problem, a configuration problem
or a server is mis-behaving */
/* XXXX mark the name as in conflict, and then let the
person who just responded know that they must also mark it
as in conflict, and therefore must NOT use it.
see rfc1001.txt 15.1.3.5 */
/* this may cause problems for some early versions of nmbd */
switch (n->state)
{
case NAME_QUERY_MST_SRV_CHK:
case NAME_QUERY_SRV_CHK:
case NAME_QUERY_MST_CHK:
/* don't do case NAME_QUERY_FIND_MST: MSBROWSE isn't a unique name. */
{
if (!strequal(qname,n->name.name))
{
/* one subnet, one master browser per workgroup */
/* XXXX force an election? */
DEBUG(3,("more than one master browser replied!\n"));
return True;
}
break;
}
default: break;
}
DEBUG(3,("Unique Name conflict detected!\n"));
return True;
}
}
else
{
/* we have received a negative reply, having already received
at least one response (pos/neg). something's really wrong! */
DEBUG(3,("wierd name query problem detected!\n"));
return True;
}
}
}
}
return False;
}
/****************************************************************************
check that the response received is compatible with the response record
****************************************************************************/
static BOOL response_compatible(struct response_record *n,
struct nmb_packet *nmb)
{
switch (n->state)
{
case NAME_RELEASE:
{
if (nmb->answers->rr_type != NMB_REL)
{
DEBUG(1,("Name release reply has wrong answer rr_type\n"));
return False;
}
break;
}
case NAME_REGISTER:
{
if (nmb->answers->rr_type != NMB_REG)
{
DEBUG(1,("Name register reply has wrong answer rr_type\n"));
return False;
}
break;
}
case NAME_QUERY_CONFIRM:
case NAME_QUERY_SYNC:
case NAME_QUERY_MST_SRV_CHK:
case NAME_QUERY_SRV_CHK:
case NAME_QUERY_FIND_MST:
case NAME_QUERY_MST_CHK:
{
if (nmb->answers->rr_type != NMB_QUERY)
{
DEBUG(1,("Name query reply has wrong answer rr_type\n"));
return False;
}
break;
}
case NAME_STATUS_MASTER_CHECK:
case NAME_STATUS_CHECK:
{
if (nmb->answers->rr_type != NMB_STATUS)
{
DEBUG(1,("Name status reply has wrong answer rr_type\n"));
return False;
}
break;
}
default:
{
DEBUG(0,("unknown state type received in response_netbios_packet\n"));
break;
}
}
return True;
}
/****************************************************************************
process the response packet received
****************************************************************************/
static void response_process(struct subnet_record *d, struct packet_struct *p,
struct response_record *n, struct nmb_packet *nmb,
BOOL bcast, struct nmb_name *ans_name)
{
switch (n->state)
{
case NAME_RELEASE:
{
response_name_release(d, p);
break;
}
case NAME_REGISTER:
{
response_name_reg(d, p);
break;
}
case NAME_QUERY_MST_SRV_CHK:
case NAME_QUERY_SRV_CHK:
case NAME_QUERY_FIND_MST:
{
response_server_check(ans_name, n, d);
break;
}
case NAME_STATUS_MASTER_CHECK:
case NAME_STATUS_CHECK:
{
response_name_status_check(p->ip, nmb, bcast, n, d);
break;
}
case NAME_QUERY_CONFIRM:
case NAME_QUERY_SYNC:
{
response_name_query_sync(nmb, ans_name, bcast, n, d);
break;
}
case NAME_QUERY_MST_CHK:
{
/* no action required here. it's when NO responses are received
that we need to do something. see expire_name_query_entries() */
DEBUG(4, ("Master browser exists for %s at %s (just checking!)\n",
namestr(&n->name), inet_ntoa(n->to_ip)));
break;
}
default:
{
DEBUG(0,("unknown state type received in response_netbios_packet\n"));
break;
}
}
}
/****************************************************************************
response from a netbios packet.
****************************************************************************/
static void response_netbios_packet(struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
struct nmb_name *question = &nmb->question.question_name;
struct nmb_name *ans_name = NULL;
char *qname = question->name;
BOOL bcast = nmb->header.nm_flags.bcast;
struct response_record *n;
struct subnet_record *d = NULL;
if (!(n = find_response_record(&d,nmb->header.name_trn_id))) {
DEBUG(2,("unknown netbios response (received late or from nmblookup?)\n"));
return;
}
if (!d)
{
DEBUG(2,("response packet: subnet %s not known\n", inet_ntoa(p->ip)));
return;
}
if (!same_net(d->bcast_ip, d->mask_ip, p->ip)) /* copes with WINS 'subnet' */
{
DEBUG(2,("response from %s. ", inet_ntoa(p->ip)));
DEBUG(2,("expected on subnet %s. hmm.\n", inet_ntoa(d->bcast_ip)));
return;
}
if (nmb->answers == NULL)
{
/* hm. the packet received was a response, but with no answer. wierd! */
DEBUG(2,("NMB packet response from %s (bcast=%s) - UNKNOWN\n",
inet_ntoa(p->ip), BOOLSTR(bcast)));
return;
}
ans_name = &nmb->answers->rr_name;
DEBUG(3,("response for %s from %s (bcast=%s)\n",
namestr(ans_name), inet_ntoa(p->ip), BOOLSTR(bcast)));
debug_rr_type(nmb->answers->rr_type);
n->num_msgs++; /* count number of responses received */
n->repeat_count = 0; /* don't resend: see expire_netbios_packets() */
debug_state_type(n->state);
/* problem checking: multiple responses etc */
if (response_problem_check(n, nmb, qname))
return;
/* now check whether the state has received the correct type of response */
if (!response_compatible(n, nmb))
return;
/* now deal with the current state */
response_process(d, p, n, nmb, bcast, ans_name);
}
/****************************************************************************
process a nmb packet
****************************************************************************/
void process_nmb(struct packet_struct *p)
{
struct nmb_packet *nmb = &p->packet.nmb;
debug_nmb_packet(p);
switch (nmb->header.opcode)
{
case 8: /* what is this?? */
case NMB_REG:
case NMB_REG_REFRESH:
{
if (nmb->header.qdcount==0 || nmb->header.arcount==0) break;
if (nmb->header.response)
response_netbios_packet(p); /* response to registration dealt with here */
else
reply_name_reg(p);
break;
}
case 0:
{
if (nmb->header.response)
{
switch (nmb->question.question_type)
{
case 0x0:
{
response_netbios_packet(p);
break;
}
}
return;
}
else if (nmb->header.qdcount>0)
{
switch (nmb->question.question_type)
{
case NMB_QUERY:
{
reply_name_query(p);
break;
}
case NMB_STATUS:
{
reply_name_status(p);
break;
}
}
return;
}
break;
}
case NMB_REL:
{
if (nmb->header.qdcount==0 || nmb->header.arcount==0)
{
DEBUG(2,("netbios release packet rejected\n"));
break;
}
if (nmb->header.response)
response_netbios_packet(p); /* response to reply dealt with in here */
else
reply_name_release(p);
break;
}
}
}
|