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