first public release of samba4 code
[jelmer/samba4-debian.git] / source / nmbd / nmbd_winsproxy.c
1 /* 
2    Unix SMB/CIFS implementation.
3    NBT netbios routines and daemon - version 2
4
5    Copyright (C) Jeremy Allison 1994-1998
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 /****************************************************************************
26 Function called when the name lookup succeeded.
27 ****************************************************************************/
28
29 static void wins_proxy_name_query_request_success( struct subnet_record *subrec,
30                         struct userdata_struct *userdata,
31                         struct nmb_name *nmbname, struct in_addr ip, struct res_rec *rrec)
32 {
33   struct packet_struct *original_packet;
34   struct subnet_record *orig_broadcast_subnet;
35   struct name_record *namerec;
36   uint16 nb_flags;
37   int num_ips;
38   int i;
39   int ttl = 3600; /* By default one hour in the cache. */
40   struct in_addr *iplist;
41
42   /* Extract the original packet and the original broadcast subnet from
43      the userdata. */
44
45   memcpy( (char *)&orig_broadcast_subnet, userdata->data, sizeof(struct subnet_record *) );
46   memcpy( (char *)&original_packet, &userdata->data[sizeof(struct subnet_record *)],
47           sizeof(struct packet_struct *) );
48
49   nb_flags = get_nb_flags( rrec->rdata );
50
51   num_ips = rrec->rdlength / 6;
52   if(num_ips == 0)
53   {
54     DEBUG(0,("wins_proxy_name_query_request_success: Invalid number of IP records (0) \
55 returned for name %s.\n", nmb_namestr(nmbname) ));
56     return;
57   }
58
59   if(num_ips == 1)
60     iplist = &ip;
61   else
62   {
63     if((iplist = (struct in_addr *)malloc( num_ips * sizeof(struct in_addr) )) == NULL)
64     {
65       DEBUG(0,("wins_proxy_name_query_request_success: malloc fail !\n"));
66       return;
67     }
68
69     for(i = 0; i < num_ips; i++)
70       putip( (char *)&iplist[i], (char *)&rrec->rdata[ (i*6) + 2]);
71   }
72
73   /* Add the queried name to the original subnet as a WINS_PROXY_NAME. */
74
75   if(rrec == PERMANENT_TTL)
76     ttl = lp_max_ttl();
77
78   namerec = add_name_to_subnet( orig_broadcast_subnet, nmbname->name,
79                                 nmbname->name_type, nb_flags, ttl,
80                                 WINS_PROXY_NAME, num_ips, iplist );
81
82   if(iplist != &ip)
83     SAFE_FREE(iplist);
84
85   /*
86    * Check that none of the IP addresses we are returning is on the
87    * same broadcast subnet as the original requesting packet. If it
88    * is then don't reply (although we still need to add the name
89    * to the cache) as the actual machine will be replying also
90    * and we don't want two replies to a broadcast query.
91    */
92
93   if(namerec && original_packet->packet.nmb.header.nm_flags.bcast)
94   {
95     for( i = 0; i < namerec->data.num_ips; i++)
96     {
97       if( same_net( namerec->data.ip[i],
98                     orig_broadcast_subnet->myip,
99                     orig_broadcast_subnet->mask_ip ) )
100       {
101         DEBUG( 5, ( "wins_proxy_name_query_request_success: name %s is a WINS \
102 proxy name and is also on the same subnet (%s) as the requestor. \
103 Not replying.\n",
104                     nmb_namestr(&namerec->name),
105                     orig_broadcast_subnet->subnet_name ) );
106         return;
107       }
108     }
109   }
110
111   /* Finally reply to the original name query. */
112   reply_netbios_packet(original_packet,                /* Packet to reply to. */
113                        0,                              /* Result code. */
114                        NMB_QUERY,                      /* nmbd type code. */
115                        NMB_NAME_QUERY_OPCODE,          /* opcode. */
116                        ttl,                            /* ttl. */
117                        rrec->rdata,                    /* data to send. */
118                        rrec->rdlength);                /* data length. */
119 }
120
121 /****************************************************************************
122 Function called when the name lookup failed.
123 ****************************************************************************/
124
125 static void wins_proxy_name_query_request_fail(struct subnet_record *subrec,
126                                     struct response_record *rrec,
127                                     struct nmb_name *question_name, int fail_code)
128 {
129   DEBUG(4,("wins_proxy_name_query_request_fail: WINS server returned error code %d for lookup \
130 of name %s.\n", fail_code, nmb_namestr(question_name) ));
131 }
132
133 /****************************************************************************
134 Function to make a deep copy of the userdata we will need when the WINS
135 proxy query returns.
136 ****************************************************************************/
137
138 static struct userdata_struct *wins_proxy_userdata_copy_fn(struct userdata_struct *userdata)
139 {
140   struct packet_struct *p, *copy_of_p;
141   struct userdata_struct *new_userdata = 
142         (struct userdata_struct *)malloc( userdata->userdata_len );
143
144   if(new_userdata == NULL)
145     return NULL;
146
147   new_userdata->copy_fn = userdata->copy_fn;
148   new_userdata->free_fn = userdata->free_fn;
149   new_userdata->userdata_len = userdata->userdata_len;
150
151   /* Copy the subnet_record pointer. */
152   memcpy( new_userdata->data, userdata->data, sizeof(struct subnet_record *) );
153
154   /* Extract the pointer to the packet struct */
155   memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)],
156          sizeof(struct packet_struct *) );
157
158   /* Do a deep copy of the packet. */
159   if((copy_of_p = copy_packet(p)) == NULL)
160   {
161     SAFE_FREE(new_userdata);
162     return NULL;
163   }
164
165   /* Lock the copy. */
166   copy_of_p->locked = True;
167
168   memcpy( &new_userdata->data[sizeof(struct subnet_record *)], (char *)&copy_of_p,
169           sizeof(struct packet_struct *) );
170
171   return new_userdata;
172 }
173
174 /****************************************************************************
175 Function to free the deep copy of the userdata we used when the WINS
176 proxy query returned.
177 ****************************************************************************/
178
179 static void wins_proxy_userdata_free_fn(struct userdata_struct *userdata)
180 {
181   struct packet_struct *p;
182
183   /* Extract the pointer to the packet struct */
184   memcpy((char *)&p, &userdata->data[sizeof(struct subnet_record *)],
185          sizeof(struct packet_struct *));
186
187   /* Unlock the packet. */
188   p->locked = False;
189
190   free_packet(p);
191   ZERO_STRUCTP(userdata);
192   SAFE_FREE(userdata);
193 }
194
195 /****************************************************************************
196  Make a WINS query on behalf of a broadcast client name query request.
197 ****************************************************************************/
198
199 void make_wins_proxy_name_query_request( struct subnet_record *subrec, 
200                                          struct packet_struct *incoming_packet,
201                                          struct nmb_name *question_name)
202 {
203   long *ud[(sizeof(struct userdata_struct) + sizeof(struct subrec *) + 
204           sizeof(struct packet_struct *))/sizeof(long *) + 1];
205   struct userdata_struct *userdata = (struct userdata_struct *)ud;
206
207   memset(ud, '\0', sizeof(ud));
208  
209   userdata->copy_fn = wins_proxy_userdata_copy_fn;
210   userdata->free_fn = wins_proxy_userdata_free_fn;
211   userdata->userdata_len = sizeof(ud);
212   memcpy( userdata->data, (char *)&subrec, sizeof(struct subnet_record *));
213   memcpy( &userdata->data[sizeof(struct subnet_record *)], (char *)&incoming_packet,
214           sizeof(struct packet_struct *));
215
216   /* Now use the unicast subnet to query the name with the WINS server. */
217   query_name( unicast_subnet, question_name->name, question_name->name_type,
218               wins_proxy_name_query_request_success,
219               wins_proxy_name_query_request_fail,
220               userdata);
221 }