Fixes to libsmbclient so it will work when browsing real Windows systems which
[sfrench/samba-autobuild/.git] / source3 / libsmb / clidgram.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    client dgram calls
5    Copyright (C) Andrew Tridgell 1994-1998
6    Copyright (C) Richard Sharpe 2001
7    Copyright (C) John Terpstra 2001
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #define NO_SYSLOG
25
26 #include "includes.h"
27
28 /*
29  * cli_send_mailslot, send a mailslot for client code ...
30  */
31
32 static int dgram_sock = -1;
33
34 int cli_send_mailslot(BOOL unique, char *mailslot, char *buf, int len,
35                       const char *srcname, int src_type, 
36                       const char *dstname, int dest_type,
37                       struct in_addr dest_ip, struct in_addr src_ip,
38                       int dest_port)
39 {
40   struct packet_struct p;
41   struct dgram_packet *dgram = &p.packet.dgram;
42   struct sockaddr_in sock_out;
43   char *ptr, *p2;
44   char tmp[4];
45   int name_size;
46
47   bzero((char *)&p, sizeof(p));
48
49   /* 
50    * First, check if we have an open socket to the dest IP 
51    */
52
53   if (dgram_sock < 1) {
54
55     if ((dgram_sock = open_socket_out(SOCK_DGRAM, &dest_ip, 138, LONG_CONNECT_TIMEOUT)) < 0) {
56
57       DEBUG(4, ("open_sock_out failed ..."));
58       return False;
59
60     }
61
62     /* Make it a broadcast socket ... */
63
64     set_socket_options(dgram_sock, "SO_BROADCAST");
65
66     /* Now, bind my addr to it ... */
67
68     bzero((char *)&sock_out, sizeof(sock_out));
69     sock_out.sin_addr.s_addr = INADDR_ANY;
70     sock_out.sin_port = htons(138);
71     sock_out.sin_family = AF_INET;
72
73     if (bind(dgram_sock, (struct sockaddr_in *)&sock_out, sizeof(sock_out)) < 0) {
74
75       /* Try again on any port ... */
76
77       sock_out.sin_port = INADDR_ANY;
78
79       if (bind(dgram_sock, (struct sockaddr_in *)&sock_out, sizeof(sock_out)) < 0) {
80
81         DEBUG(4, ("failed to bind socket to address ...\n"));
82         return False;
83
84       }
85
86     }
87
88   }
89
90   /* Now, figure out what socket name we were bound to. We want the port */
91
92   name_size = sizeof(sock_out);
93
94   getsockname(dgram_sock, (struct sockaddr_in *)&sock_out, &name_size);
95
96   fprintf(stderr, "Socket bound to IP:%s, port: %d\n", inet_ntoa(sock_out.sin_addr), ntohs(sock_out.sin_port));
97
98   /*
99    * Next, build the DGRAM ...
100    */
101
102   /* DIRECT GROUP or UNIQUE datagram. */
103   dgram->header.msg_type = unique ? 0x10 : 0x11; 
104   dgram->header.flags.node_type = M_NODE;
105   dgram->header.flags.first = True;
106   dgram->header.flags.more = False;
107   dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)sys_getpid()%(unsigned)100);
108   dgram->header.source_ip.s_addr = sock_out.sin_addr.s_addr;
109   fprintf(stderr, "Source IP = %0X\n", dgram->header.source_ip);
110   dgram->header.source_port = ntohs(sock_out.sin_port);
111   fprintf(stderr, "Source Port = %0X\n", dgram->header.source_port);
112   dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
113   dgram->header.packet_offset = 0;
114   
115   make_nmb_name(&dgram->source_name,srcname,src_type);
116   make_nmb_name(&dgram->dest_name,dstname,dest_type);
117
118   ptr = &dgram->data[0];
119
120   /* Setup the smb part. */
121   ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
122   memcpy(tmp,ptr,4);
123   set_message(ptr,17,17 + len,True);
124   memcpy(ptr,tmp,4);
125
126   CVAL(ptr,smb_com) = SMBtrans;
127   SSVAL(ptr,smb_vwv1,len);
128   SSVAL(ptr,smb_vwv11,len);
129   SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
130   SSVAL(ptr,smb_vwv13,3);
131   SSVAL(ptr,smb_vwv14,1);
132   SSVAL(ptr,smb_vwv15,1);
133   SSVAL(ptr,smb_vwv16,2);
134   p2 = smb_buf(ptr);
135   pstrcpy(p2,mailslot);
136   p2 = skip_string(p2,1);
137
138   memcpy(p2,buf,len);
139   p2 += len;
140
141   dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
142
143   p.ip = dest_ip;
144   p.port = dest_port;
145   p.fd = dgram_sock;
146   p.timestamp = time(NULL);
147   p.packet_type = DGRAM_PACKET;
148
149   DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
150                     nmb_namestr(&dgram->source_name), inet_ntoa(src_ip)));
151   DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
152
153   return send_packet(&p);
154
155 }
156
157 /*
158  * cli_get_response: Get a response ...
159  */
160 int cli_get_response(BOOL unique, char *mailslot, char *buf, int bufsiz)
161 {
162   struct packet_struct *packet;
163
164   packet = receive_dgram_packet(dgram_sock, 2, mailslot);
165
166   if (packet) { /* We got one, pull what we want out of the SMB data ... */
167
168     struct dgram_packet *dgram = &packet->packet.dgram;
169
170     /*
171      * We should probably parse the SMB, but for now, we will pull what
172      * from fixed, known locations ...
173      */
174
175     /* Copy the data to buffer, respecting sizes ... */
176
177     bcopy(&dgram->data[92], buf, MIN(bufsiz, (dgram->datasize - 92)));
178
179   }
180   else 
181     return -1;
182
183 }
184
185 /*
186  * cli_get_backup_list: Send a get backup list request ...
187  */
188
189 static char cli_backup_list[1024];
190
191 int cli_get_backup_list(const char *myname, const char *send_to_name)
192 {
193   char outbuf[15];
194   char *p;
195   struct in_addr sendto_ip, my_ip;
196
197   if (!resolve_name(send_to_name, &sendto_ip, 0x1d)) {
198
199     fprintf(stderr, "Could not resolve name: %s<1D>\n", send_to_name);
200
201   }
202
203   my_ip.s_addr = inet_addr("0.0.0.0");
204  
205   if (!resolve_name(myname, &my_ip, 0x00)) { /* FIXME: Call others here */
206
207     fprintf(stderr, "Could not resolve name: %s<00>\n", myname);
208
209   }
210
211   bzero(cli_backup_list, sizeof(cli_backup_list));
212   bzero(outbuf, sizeof(outbuf));
213
214   p = outbuf;
215
216   SCVAL(p, 0, ANN_GetBackupListReq);
217   p++;
218
219   SCVAL(p, 0, 1); /* Count pointer ... */
220   p++;
221
222   SIVAL(p, 0, 1); /* The sender's token ... */
223   p += 4;
224
225   cli_send_mailslot(True, "\\MAILSLOT\\BROWSE", outbuf, PTR_DIFF(p, outbuf),
226                     myname, 0, send_to_name, 0x1d, sendto_ip, my_ip, 138);
227
228   /* We should check the error and return if we got one */
229
230   /* Now, get the response ... */
231
232   cli_get_response(True, "\\MAILSLOT\\BROWSE", cli_backup_list, sizeof(cli_backup_list));
233
234   /* Should check the response here ... FIXME */
235
236 }
237
238 /*
239  * cli_get_backup_server: Get the backup list and retrieve a server from it
240  */
241
242 int cli_get_backup_server(char *my_name, char *target, char *servername, int namesize)
243 {
244
245   /* Get the backup list first. We could pull this from the cache later */
246
247   cli_get_backup_list(my_name, target);  /* FIXME: Check the response */
248
249   strncpy(servername, cli_backup_list, MIN(16, namesize));
250
251 }