[GLUE] Rsync SAMBA_3_2_0 SVN r25598 in order to create the v3-2-test branch.
[ira/wip.git] / source / libsmb / clidgram.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client dgram calls
4    Copyright (C) Andrew Tridgell 1994-1998
5    Copyright (C) Richard Sharpe 2001
6    Copyright (C) John Terpstra 2001
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23
24 /*
25  * cli_send_mailslot, send a mailslot for client code ...
26  */
27
28 BOOL cli_send_mailslot(struct messaging_context *msg_ctx,
29                        BOOL unique, const char *mailslot,
30                        uint16 priority,
31                        char *buf, int len,
32                        const char *srcname, int src_type, 
33                        const char *dstname, int dest_type,
34                        struct in_addr dest_ip)
35 {
36         struct packet_struct p;
37         struct dgram_packet *dgram = &p.packet.dgram;
38         char *ptr, *p2;
39         char tmp[4];
40         pid_t nmbd_pid;
41
42         if ((nmbd_pid = pidfile_pid("nmbd")) == 0) {
43                 DEBUG(3, ("No nmbd found\n"));
44                 return False;
45         }
46
47         memset((char *)&p, '\0', sizeof(p));
48
49         /*
50          * Next, build the DGRAM ...
51          */
52
53         /* DIRECT GROUP or UNIQUE datagram. */
54         dgram->header.msg_type = unique ? 0x10 : 0x11; 
55         dgram->header.flags.node_type = M_NODE;
56         dgram->header.flags.first = True;
57         dgram->header.flags.more = False;
58         dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) +
59                 ((unsigned)sys_getpid()%(unsigned)100);
60         /* source ip is filled by nmbd */
61         dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
62         dgram->header.packet_offset = 0;
63   
64         make_nmb_name(&dgram->source_name,srcname,src_type);
65         make_nmb_name(&dgram->dest_name,dstname,dest_type);
66
67         ptr = &dgram->data[0];
68
69         /* Setup the smb part. */
70         ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
71         memcpy(tmp,ptr,4);
72         set_message(ptr,17,strlen(mailslot) + 1 + len,True);
73         memcpy(ptr,tmp,4);
74
75         SCVAL(ptr,smb_com,SMBtrans);
76         SSVAL(ptr,smb_vwv1,len);
77         SSVAL(ptr,smb_vwv11,len);
78         SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
79         SSVAL(ptr,smb_vwv13,3);
80         SSVAL(ptr,smb_vwv14,1);
81         SSVAL(ptr,smb_vwv15,priority);
82         SSVAL(ptr,smb_vwv16,2);
83         p2 = smb_buf(ptr);
84         fstrcpy(p2,mailslot);
85         p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
86         if (!p2) {
87                 return False;
88         }
89
90         memcpy(p2,buf,len);
91         p2 += len;
92
93         dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
94
95         p.packet_type = DGRAM_PACKET;
96         p.ip = dest_ip;
97         p.timestamp = time(NULL);
98
99         DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
100                  mailslot, nmb_namestr(&dgram->source_name)));
101         DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name),
102                     inet_ntoa(dest_ip)));
103
104         return NT_STATUS_IS_OK(messaging_send_buf(msg_ctx,
105                                                   pid_to_procid(nmbd_pid),
106                                                   MSG_SEND_PACKET,
107                                                   (uint8 *)&p, sizeof(p)));
108 }
109
110 /*
111  * cli_get_response: Get a response ...
112  */
113 BOOL cli_get_response(const char *mailslot, char *buf, int bufsiz)
114 {
115         struct packet_struct *p;
116
117         p = receive_unexpected(DGRAM_PACKET, 0, mailslot);
118
119         if (p == NULL)
120                 return False;
121
122         memcpy(buf, &p->packet.dgram.data[92],
123                MIN(bufsiz, p->packet.dgram.datasize-92));
124
125         return True;
126 }
127
128 /*
129  * cli_get_backup_list: Send a get backup list request ...
130  */
131
132 static char cli_backup_list[1024];
133
134 int cli_get_backup_list(struct messaging_context *msg_ctx,
135                         const char *myname, const char *send_to_name)
136 {
137         pstring outbuf;
138         char *p;
139         struct in_addr sendto_ip;
140
141         if (!resolve_name(send_to_name, &sendto_ip, 0x1d)) {
142
143                 DEBUG(0, ("Could not resolve name: %s<1D>\n", send_to_name));
144                 return False;
145
146         }
147
148         memset(cli_backup_list, '\0', sizeof(cli_backup_list));
149         memset(outbuf, '\0', sizeof(outbuf));
150
151         p = outbuf;
152
153         SCVAL(p, 0, ANN_GetBackupListReq);
154         p++;
155
156         SCVAL(p, 0, 1); /* Count pointer ... */
157         p++;
158
159         SIVAL(p, 0, 1); /* The sender's token ... */
160         p += 4;
161
162         cli_send_mailslot(msg_ctx, True, "\\MAILSLOT\\BROWSE", 1, outbuf, 
163                           PTR_DIFF(p, outbuf), myname, 0, send_to_name, 
164                           0x1d, sendto_ip);
165
166         /* We should check the error and return if we got one */
167
168         /* Now, get the response ... */
169
170         cli_get_response("\\MAILSLOT\\BROWSE",
171                          cli_backup_list, sizeof(cli_backup_list));
172
173         return True;
174
175 }
176
177 /*
178  * cli_get_backup_server: Get the backup list and retrieve a server from it
179  */
180
181 int cli_get_backup_server(struct messaging_context *msg_ctx,
182                           char *my_name, char *target, char *servername,
183                           int namesize)
184 {
185
186   /* Get the backup list first. We could pull this from the cache later */
187
188   cli_get_backup_list(msg_ctx, my_name, target);  /* FIXME: Check the response */
189
190   if (!cli_backup_list[0]) { /* Empty list ... try again */
191
192     cli_get_backup_list(msg_ctx, my_name, target);
193
194   }
195
196   strncpy(servername, cli_backup_list, MIN(16, namesize));
197
198   return True;
199
200 }