In cli_error() return ENOENT when an ERROR_INVALID_NAME is received instead
[samba.git] / source3 / libsmb / unexpected.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    handle unexpected packets
5    Copyright (C) Andrew Tridgell 2000
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20    
21 */
22
23 #include "includes.h"
24
25 extern int DEBUGLEVEL;
26
27 static TDB_CONTEXT *tdbd = NULL;
28
29 /* the key type used in the unexpeceted packet database */
30 struct unexpected_key {
31         enum packet_type packet_type;
32         time_t timestamp;
33         int count;
34 };
35
36
37
38 /****************************************************************************
39  all unexpected packets are passed in here, to be stored in a unexpected
40  packet database. This allows nmblookup and other tools to receive packets
41  erroneoously sent to the wrong port by broken MS systems
42   **************************************************************************/
43 void unexpected_packet(struct packet_struct *p)
44 {
45         static int count;
46         TDB_DATA kbuf, dbuf;
47         struct unexpected_key key;
48         char buf[1024];
49         int len=0;
50
51         if (!tdbd) {
52                 tdbd = tdb_open(lock_path("unexpected.tdb"), 1, 
53                                TDB_CLEAR_IF_FIRST,
54                                O_RDWR | O_CREAT, 0644);
55                 if (!tdbd) {
56                         DEBUG(0,("Failed to open unexpected.tdb\n"));
57                         return;
58                 }
59         }
60
61         memset(buf,'\0',sizeof(buf));
62         
63         len = build_packet(buf, p);
64
65         key.packet_type = p->packet_type;
66         key.timestamp = p->timestamp;
67         key.count = count++;
68
69         kbuf.dptr = (char *)&key;
70         kbuf.dsize = sizeof(key);
71         dbuf.dptr = buf;
72         dbuf.dsize = len;
73
74         tdb_store(tdbd, kbuf, dbuf, TDB_REPLACE);
75 }
76
77
78 static time_t lastt;
79
80 /****************************************************************************
81 delete the record if it is too old
82   **************************************************************************/
83 static int traverse_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
84 {
85         struct unexpected_key key;
86
87         memcpy(&key, kbuf.dptr, sizeof(key));
88
89         if (lastt - key.timestamp > NMBD_UNEXPECTED_TIMEOUT) {
90                 tdb_delete(ttdb, kbuf);
91         }
92
93         return 0;
94 }
95
96
97 /****************************************************************************
98 delete all old unexpected packets
99   **************************************************************************/
100 void clear_unexpected(time_t t)
101 {
102         if (!tdbd) return;
103
104         if ((lastt != 0) && (t < lastt + NMBD_UNEXPECTED_TIMEOUT))
105                 return;
106
107         lastt = t;
108
109         tdb_traverse(tdbd, traverse_fn, NULL);
110 }
111
112
113 static struct packet_struct *matched_packet;
114 static int match_id;
115 static enum packet_type match_type;
116 static char *match_name;
117
118 /****************************************************************************
119 tdb traversal fn to find a matching 137 packet
120   **************************************************************************/
121 static int traverse_match(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
122 {
123         struct unexpected_key key;
124         struct packet_struct *p;
125
126         memcpy(&key, kbuf.dptr, sizeof(key));
127
128         if (key.packet_type != match_type) return 0;
129
130         p = parse_packet(dbuf.dptr, dbuf.dsize, match_type);
131
132         if ((match_type == NMB_PACKET && 
133              p->packet.nmb.header.name_trn_id == match_id) ||
134             (match_type == DGRAM_PACKET && 
135              match_mailslot_name(p, match_name))) {
136                 matched_packet = p;
137                 return -1;
138         }
139
140         free_packet(p);
141
142         return 0;
143 }
144
145
146 /****************************************************************************
147 check for a particular packet in the unexpected packet queue
148   **************************************************************************/
149 struct packet_struct *receive_unexpected(enum packet_type packet_type, int id, 
150                                          char *mailslot_name)
151 {
152         TDB_CONTEXT *tdb2;
153
154         tdb2 = tdb_open(lock_path("unexpected.tdb"), 0, 0, O_RDONLY, 0);
155         if (!tdb2) return NULL;
156
157         matched_packet = NULL;
158         match_id = id;
159         match_type = packet_type;
160         match_name = mailslot_name;
161
162         tdb_traverse(tdb2, traverse_match, NULL);
163
164         tdb_close(tdb2);
165
166         return matched_packet;
167 }