not all versions of env like more than one argument...
[Samba.git] / source3 / libsmb / clidgram.c
blobc8185f67bafed266d23c293f689cc8dfd1f02305
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
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.
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.
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/>.
22 #include "includes.h"
23 #include "librpc/gen_ndr/messaging.h"
26 * cli_send_mailslot, send a mailslot for client code ...
29 static bool cli_send_mailslot(struct messaging_context *msg_ctx,
30 bool unique, const char *mailslot,
31 uint16 priority,
32 char *buf, int len,
33 const char *srcname, int src_type,
34 const char *dstname, int dest_type,
35 const struct sockaddr_storage *dest_ss)
37 struct packet_struct p;
38 struct dgram_packet *dgram = &p.packet.dgram;
39 char *ptr, *p2;
40 char tmp[4];
41 pid_t nmbd_pid;
42 char addr[INET6_ADDRSTRLEN];
44 if ((nmbd_pid = pidfile_pid("nmbd")) == 0) {
45 DEBUG(3, ("No nmbd found\n"));
46 return False;
49 if (dest_ss->ss_family != AF_INET) {
50 DEBUG(3, ("cli_send_mailslot: can't send to IPv6 address.\n"));
51 return false;
54 memset((char *)&p, '\0', sizeof(p));
57 * Next, build the DGRAM ...
60 /* DIRECT GROUP or UNIQUE datagram. */
61 dgram->header.msg_type = unique ? 0x10 : 0x11;
62 dgram->header.flags.node_type = M_NODE;
63 dgram->header.flags.first = True;
64 dgram->header.flags.more = False;
65 dgram->header.dgm_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) +
66 ((unsigned)sys_getpid()%(unsigned)100);
67 /* source ip is filled by nmbd */
68 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
69 dgram->header.packet_offset = 0;
71 make_nmb_name(&dgram->source_name,srcname,src_type);
72 make_nmb_name(&dgram->dest_name,dstname,dest_type);
74 ptr = &dgram->data[0];
76 /* Setup the smb part. */
77 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
78 memcpy(tmp,ptr,4);
80 if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
81 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
82 return False;
85 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
86 memcpy(ptr,tmp,4);
88 SCVAL(ptr,smb_com,SMBtrans);
89 SSVAL(ptr,smb_vwv1,len);
90 SSVAL(ptr,smb_vwv11,len);
91 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
92 SSVAL(ptr,smb_vwv13,3);
93 SSVAL(ptr,smb_vwv14,1);
94 SSVAL(ptr,smb_vwv15,priority);
95 SSVAL(ptr,smb_vwv16,2);
96 p2 = smb_buf(ptr);
97 fstrcpy(p2,mailslot);
98 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
99 if (!p2) {
100 return False;
103 memcpy(p2,buf,len);
104 p2 += len;
106 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
108 p.packet_type = DGRAM_PACKET;
109 p.ip = ((const struct sockaddr_in *)dest_ss)->sin_addr;
110 p.timestamp = time(NULL);
112 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
113 mailslot, nmb_namestr(&dgram->source_name)));
114 print_sockaddr(addr, sizeof(addr), dest_ss);
116 DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr));
118 return NT_STATUS_IS_OK(messaging_send_buf(msg_ctx,
119 pid_to_procid(nmbd_pid),
120 MSG_SEND_PACKET,
121 (uint8 *)&p, sizeof(p)));
124 static const char *mailslot_name(TALLOC_CTX *mem_ctx, struct in_addr dc_ip)
126 return talloc_asprintf(mem_ctx, "%s%X",
127 NBT_MAILSLOT_GETDC, dc_ip.s_addr);
130 bool send_getdc_request(TALLOC_CTX *mem_ctx,
131 struct messaging_context *msg_ctx,
132 struct sockaddr_storage *dc_ss,
133 const char *domain_name,
134 const DOM_SID *sid,
135 uint32_t nt_version)
137 struct in_addr dc_ip;
138 const char *my_acct_name = NULL;
139 const char *my_mailslot = NULL;
140 struct nbt_netlogon_packet packet;
141 struct NETLOGON_SAM_LOGON_REQUEST *s;
142 enum ndr_err_code ndr_err;
143 DATA_BLOB blob;
144 struct dom_sid my_sid;
146 ZERO_STRUCT(packet);
147 ZERO_STRUCT(my_sid);
149 if (dc_ss->ss_family != AF_INET) {
150 return false;
153 if (sid) {
154 my_sid = *sid;
157 dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
158 my_mailslot = mailslot_name(mem_ctx, dc_ip);
159 if (!my_mailslot) {
160 return false;
163 my_acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
164 if (!my_acct_name) {
165 return false;
168 packet.command = LOGON_SAM_LOGON_REQUEST;
169 s = &packet.req.logon;
171 s->request_count = 0;
172 s->computer_name = global_myname();
173 s->user_name = my_acct_name;
174 s->mailslot_name = my_mailslot;
175 s->acct_control = ACB_WSTRUST;
176 s->sid = my_sid;
177 s->nt_version = nt_version;
178 s->lmnt_token = 0xffff;
179 s->lm20_token = 0xffff;
181 if (DEBUGLEVEL >= 10) {
182 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
185 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, &packet,
186 (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
187 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
188 return false;
191 return cli_send_mailslot(msg_ctx,
192 false, NBT_MAILSLOT_NTLOGON, 0,
193 (char *)blob.data, blob.length,
194 global_myname(), 0, domain_name, 0x1c,
195 dc_ss);
198 bool receive_getdc_response(TALLOC_CTX *mem_ctx,
199 struct sockaddr_storage *dc_ss,
200 const char *domain_name,
201 uint32_t *nt_version,
202 const char **dc_name,
203 struct netlogon_samlogon_response **_r)
205 struct packet_struct *packet;
206 const char *my_mailslot = NULL;
207 struct in_addr dc_ip;
208 DATA_BLOB blob;
209 struct netlogon_samlogon_response r;
210 union dgram_message_body p;
211 enum ndr_err_code ndr_err;
212 NTSTATUS status;
214 const char *returned_dc = NULL;
215 const char *returned_domain = NULL;
217 if (dc_ss->ss_family != AF_INET) {
218 return false;
221 dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
223 my_mailslot = mailslot_name(mem_ctx, dc_ip);
224 if (!my_mailslot) {
225 return false;
228 packet = receive_unexpected(DGRAM_PACKET, 0, my_mailslot);
230 if (packet == NULL) {
231 DEBUG(5, ("Did not receive packet for %s\n", my_mailslot));
232 return False;
235 DEBUG(5, ("Received packet for %s\n", my_mailslot));
237 blob = data_blob_const(packet->packet.dgram.data,
238 packet->packet.dgram.datasize);
240 if (blob.length < 4) {
241 DEBUG(0,("invalid length: %d\n", (int)blob.length));
242 return false;
245 if (RIVAL(blob.data,0) != DGRAM_SMB) {
246 DEBUG(0,("invalid packet\n"));
247 return false;
250 blob.data += 4;
251 blob.length -= 4;
253 ndr_err = ndr_pull_union_blob_all(&blob, mem_ctx, NULL, &p, DGRAM_SMB,
254 (ndr_pull_flags_fn_t)ndr_pull_dgram_smb_packet);
255 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
256 DEBUG(0,("failed to parse packet\n"));
257 return false;
260 if (p.smb.smb_command != SMB_TRANSACTION) {
261 DEBUG(0,("invalid smb_command: %d\n", p.smb.smb_command));
262 return false;
265 if (DEBUGLEVEL >= 10) {
266 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
269 blob = p.smb.body.trans.data;
271 ZERO_STRUCT(r);
273 status = pull_netlogon_samlogon_response(&blob, mem_ctx, NULL, &r);
274 if (!NT_STATUS_IS_OK(status)) {
275 return false;
278 map_netlogon_samlogon_response(&r);
280 /* do we still need this ? */
281 *nt_version = r.ntver;
283 returned_domain = r.data.nt5_ex.domain_name;
284 returned_dc = r.data.nt5_ex.pdc_name;
286 if (!strequal(returned_domain, domain_name)) {
287 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
288 domain_name, returned_domain));
289 return false;
292 *dc_name = talloc_strdup(mem_ctx, returned_dc);
293 if (!*dc_name) {
294 return false;
297 if (**dc_name == '\\') *dc_name += 1;
298 if (**dc_name == '\\') *dc_name += 1;
300 if (_r) {
301 *_r = (struct netlogon_samlogon_response *)talloc_memdup(
302 mem_ctx, &r, sizeof(struct netlogon_samlogon_response));
303 if (!*_r) {
304 return false;
308 DEBUG(10, ("GetDC gave name %s for domain %s\n",
309 *dc_name, returned_domain));
311 return True;