s3:registry: remove usage of reg_objects from net_rpc_printer.c
[Samba.git] / source3 / libsmb / clidgram.c
blob04964bd3f0e4a6f2b40c713f4a99f5f1d66a6e30
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 "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "libsmb/clidgram.h"
26 #include "libsmb/nmblib.h"
27 #include "messages.h"
28 #include "librpc/gen_ndr/samr.h"
31 * cli_send_mailslot, send a mailslot for client code ...
34 static bool cli_prep_mailslot(bool unique, const char *mailslot,
35 uint16 priority,
36 char *buf, int len,
37 const char *srcname, int src_type,
38 const char *dstname, int dest_type,
39 const struct sockaddr_storage *dest_ss,
40 int dgm_id,
41 struct packet_struct *p)
43 struct dgram_packet *dgram = &p->packet.dgram;
44 char *ptr, *p2;
45 char tmp[4];
46 char addr[INET6_ADDRSTRLEN];
48 ZERO_STRUCTP(p);
51 * Next, build the DGRAM ...
54 /* DIRECT GROUP or UNIQUE datagram. */
55 dgram->header.msg_type = unique ? 0x10 : 0x11;
56 dgram->header.flags.node_type = M_NODE;
57 dgram->header.flags.first = True;
58 dgram->header.flags.more = False;
59 dgram->header.dgm_id = dgm_id;
60 /* source ip is filled by nmbd */
61 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
62 dgram->header.packet_offset = 0;
64 make_nmb_name(&dgram->source_name,srcname,src_type);
65 make_nmb_name(&dgram->dest_name,dstname,dest_type);
67 ptr = &dgram->data[0];
69 /* Setup the smb part. */
70 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
71 memcpy(tmp,ptr,4);
73 if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
74 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
75 return False;
78 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
79 memcpy(ptr,tmp,4);
81 SCVAL(ptr,smb_com,SMBtrans);
82 SSVAL(ptr,smb_vwv1,len);
83 SSVAL(ptr,smb_vwv11,len);
84 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
85 SSVAL(ptr,smb_vwv13,3);
86 SSVAL(ptr,smb_vwv14,1);
87 SSVAL(ptr,smb_vwv15,priority);
88 SSVAL(ptr,smb_vwv16,2);
89 p2 = smb_buf(ptr);
90 fstrcpy(p2,mailslot);
91 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
92 if (!p2) {
93 return False;
96 memcpy(p2,buf,len);
97 p2 += len;
99 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
101 p->packet_type = DGRAM_PACKET;
102 p->ip = ((const struct sockaddr_in *)dest_ss)->sin_addr;
103 p->timestamp = time(NULL);
105 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
106 mailslot, nmb_namestr(&dgram->source_name)));
107 print_sockaddr(addr, sizeof(addr), dest_ss);
109 DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr));
111 return true;
114 static char *mailslot_name(TALLOC_CTX *mem_ctx, struct in_addr dc_ip)
116 return talloc_asprintf(mem_ctx, "%s%X",
117 NBT_MAILSLOT_GETDC, dc_ip.s_addr);
120 static bool prep_getdc_request(const struct sockaddr_storage *dc_ss,
121 const char *domain_name,
122 const struct dom_sid *sid,
123 uint32_t nt_version,
124 const char *my_mailslot,
125 int dgm_id,
126 struct packet_struct *p)
128 TALLOC_CTX *frame = talloc_stackframe();
129 const char *my_acct_name;
130 struct nbt_netlogon_packet packet;
131 struct NETLOGON_SAM_LOGON_REQUEST *s;
132 enum ndr_err_code ndr_err;
133 DATA_BLOB blob = data_blob_null;
134 struct dom_sid my_sid;
135 bool ret = false;
137 ZERO_STRUCT(packet);
138 ZERO_STRUCT(my_sid);
140 if (sid != NULL) {
141 my_sid = *sid;
144 my_acct_name = talloc_asprintf(talloc_tos(), "%s$", lp_netbios_name());
145 if (my_acct_name == NULL) {
146 goto fail;
149 packet.command = LOGON_SAM_LOGON_REQUEST;
150 s = &packet.req.logon;
152 s->request_count = 0;
153 s->computer_name = lp_netbios_name();
154 s->user_name = my_acct_name;
155 s->mailslot_name = my_mailslot;
156 s->acct_control = ACB_WSTRUST;
157 s->sid = my_sid;
158 s->nt_version = nt_version;
159 s->lmnt_token = 0xffff;
160 s->lm20_token = 0xffff;
162 if (DEBUGLEVEL >= 10) {
163 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
166 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &packet,
167 (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
168 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
169 goto fail;
172 ret = cli_prep_mailslot(false, NBT_MAILSLOT_NTLOGON, 0,
173 (char *)blob.data, blob.length,
174 lp_netbios_name(), 0, domain_name, 0x1c,
175 dc_ss, dgm_id, p);
176 fail:
177 TALLOC_FREE(frame);
178 return ret;
181 static bool parse_getdc_response(
182 struct packet_struct *packet,
183 TALLOC_CTX *mem_ctx,
184 const char *domain_name,
185 uint32_t *nt_version,
186 const char **dc_name,
187 struct netlogon_samlogon_response **samlogon_response)
189 DATA_BLOB blob;
190 struct netlogon_samlogon_response *r;
191 union dgram_message_body p;
192 enum ndr_err_code ndr_err;
193 NTSTATUS status;
195 const char *returned_dc = NULL;
196 const char *returned_domain = NULL;
198 blob = data_blob_const(packet->packet.dgram.data,
199 packet->packet.dgram.datasize);
200 if (blob.length < 4) {
201 DEBUG(1, ("invalid length: %d\n", (int)blob.length));
202 return false;
205 if (RIVAL(blob.data,0) != DGRAM_SMB) {
206 DEBUG(1, ("invalid packet\n"));
207 return false;
210 blob.data += 4;
211 blob.length -= 4;
213 ndr_err = ndr_pull_union_blob_all(&blob, mem_ctx, &p, DGRAM_SMB,
214 (ndr_pull_flags_fn_t)ndr_pull_dgram_smb_packet);
215 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
216 DEBUG(1, ("failed to parse packet\n"));
217 return false;
220 if (p.smb.smb_command != SMB_TRANSACTION) {
221 DEBUG(1, ("invalid smb_command: %d\n", p.smb.smb_command));
222 return false;
225 if (DEBUGLEVEL >= 10) {
226 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
229 blob = p.smb.body.trans.data;
231 r = talloc_zero(mem_ctx, struct netlogon_samlogon_response);
232 if (!r) {
233 return false;
236 status = pull_netlogon_samlogon_response(&blob, r, r);
237 if (!NT_STATUS_IS_OK(status)) {
238 TALLOC_FREE(r);
239 return false;
242 map_netlogon_samlogon_response(r);
244 /* do we still need this ? */
245 *nt_version = r->ntver;
247 returned_domain = r->data.nt5_ex.domain_name;
248 returned_dc = r->data.nt5_ex.pdc_name;
250 if (!strequal(returned_domain, domain_name)) {
251 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
252 domain_name, returned_domain));
253 TALLOC_FREE(r);
254 return false;
257 if (*returned_dc == '\\') returned_dc += 1;
258 if (*returned_dc == '\\') returned_dc += 1;
260 *dc_name = talloc_strdup(mem_ctx, returned_dc);
261 if (!*dc_name) {
262 TALLOC_FREE(r);
263 return false;
266 if (samlogon_response) {
267 *samlogon_response = r;
268 } else {
269 TALLOC_FREE(r);
272 DEBUG(10, ("GetDC gave name %s for domain %s\n",
273 *dc_name, returned_domain));
275 return True;
278 struct nbt_getdc_state {
279 struct tevent_context *ev;
280 struct messaging_context *msg_ctx;
281 struct nb_packet_reader *reader;
282 const char *my_mailslot;
283 pid_t nmbd_pid;
285 const struct sockaddr_storage *dc_addr;
286 const char *domain_name;
287 const struct dom_sid *sid;
288 uint32_t nt_version;
289 const char *dc_name;
290 struct netlogon_samlogon_response *samlogon_response;
292 struct packet_struct p;
295 static void nbt_getdc_got_reader(struct tevent_req *subreq);
296 static void nbt_getdc_got_response(struct tevent_req *subreq);
298 struct tevent_req *nbt_getdc_send(TALLOC_CTX *mem_ctx,
299 struct tevent_context *ev,
300 struct messaging_context *msg_ctx,
301 const struct sockaddr_storage *dc_addr,
302 const char *domain_name,
303 const struct dom_sid *sid,
304 uint32_t nt_version)
306 struct tevent_req *req, *subreq;
307 struct nbt_getdc_state *state;
308 uint16_t dgm_id;
310 req = tevent_req_create(mem_ctx, &state, struct nbt_getdc_state);
311 if (req == NULL) {
312 return NULL;
314 state->ev = ev;
315 state->msg_ctx = msg_ctx;
316 state->dc_addr = dc_addr;
317 state->domain_name = domain_name;
318 state->sid = sid;
319 state->nt_version = nt_version;
321 if (dc_addr->ss_family != AF_INET) {
322 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
323 return tevent_req_post(req, ev);
325 state->my_mailslot = mailslot_name(
326 state, ((const struct sockaddr_in *)dc_addr)->sin_addr);
327 if (tevent_req_nomem(state->my_mailslot, req)) {
328 return tevent_req_post(req, ev);
330 state->nmbd_pid = pidfile_pid("nmbd");
331 if (state->nmbd_pid == 0) {
332 DEBUG(3, ("No nmbd found\n"));
333 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
334 return tevent_req_post(req, ev);
337 generate_random_buffer((uint8_t *)(void *)&dgm_id, sizeof(dgm_id));
339 if (!prep_getdc_request(dc_addr, domain_name, sid, nt_version,
340 state->my_mailslot, dgm_id & 0x7fff,
341 &state->p)) {
342 DEBUG(3, ("prep_getdc_request failed\n"));
343 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
344 return tevent_req_post(req, ev);
347 subreq = nb_packet_reader_send(state, ev, DGRAM_PACKET, -1,
348 state->my_mailslot);
349 if (tevent_req_nomem(subreq, req)) {
350 return tevent_req_post(req, ev);
352 tevent_req_set_callback(subreq, nbt_getdc_got_reader, req);
353 return req;
356 static void nbt_getdc_got_reader(struct tevent_req *subreq)
358 struct tevent_req *req = tevent_req_callback_data(
359 subreq, struct tevent_req);
360 struct nbt_getdc_state *state = tevent_req_data(
361 req, struct nbt_getdc_state);
362 NTSTATUS status;
364 status = nb_packet_reader_recv(subreq, state, &state->reader);
365 TALLOC_FREE(subreq);
366 if (tevent_req_nterror(req, status)) {
367 DEBUG(10, ("nb_packet_reader_recv returned %s\n",
368 nt_errstr(status)));
369 return;
372 status = messaging_send_buf(
373 state->msg_ctx, pid_to_procid(state->nmbd_pid),
374 MSG_SEND_PACKET, (uint8_t *)&state->p, sizeof(state->p));
376 if (tevent_req_nterror(req, status)) {
377 DEBUG(10, ("messaging_send_buf returned %s\n",
378 nt_errstr(status)));
379 return;
381 subreq = nb_packet_read_send(state, state->ev, state->reader);
382 if (tevent_req_nomem(subreq, req)) {
383 return;
385 tevent_req_set_callback(subreq, nbt_getdc_got_response, req);
388 static void nbt_getdc_got_response(struct tevent_req *subreq)
390 struct tevent_req *req = tevent_req_callback_data(
391 subreq, struct tevent_req);
392 struct nbt_getdc_state *state = tevent_req_data(
393 req, struct nbt_getdc_state);
394 struct packet_struct *p;
395 NTSTATUS status;
396 bool ret;
398 status = nb_packet_read_recv(subreq, &p);
399 TALLOC_FREE(subreq);
400 if (tevent_req_nterror(req, status)) {
401 return;
404 ret = parse_getdc_response(p, state, state->domain_name,
405 &state->nt_version, &state->dc_name,
406 &state->samlogon_response);
407 free_packet(p);
408 if (!ret) {
409 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
410 return;
412 tevent_req_done(req);
415 NTSTATUS nbt_getdc_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
416 uint32_t *nt_version, const char **dc_name,
417 struct netlogon_samlogon_response **samlogon_response)
419 struct nbt_getdc_state *state = tevent_req_data(
420 req, struct nbt_getdc_state);
421 NTSTATUS status;
423 if (tevent_req_is_nterror(req, &status)) {
424 return status;
426 if (nt_version != NULL) {
427 *nt_version = state->nt_version;
429 if (dc_name != NULL) {
430 *dc_name = talloc_move(mem_ctx, &state->dc_name);
432 if (samlogon_response != NULL) {
433 *samlogon_response = talloc_move(
434 mem_ctx, &state->samlogon_response);
436 return NT_STATUS_OK;
439 NTSTATUS nbt_getdc(struct messaging_context *msg_ctx,
440 const struct sockaddr_storage *dc_addr,
441 const char *domain_name,
442 const struct dom_sid *sid,
443 uint32_t nt_version,
444 TALLOC_CTX *mem_ctx,
445 uint32_t *pnt_version,
446 const char **dc_name,
447 struct netlogon_samlogon_response **samlogon_response)
449 TALLOC_CTX *frame = talloc_stackframe();
450 struct tevent_context *ev;
451 struct tevent_req *req;
452 NTSTATUS status = NT_STATUS_NO_MEMORY;
454 ev = tevent_context_init(frame);
455 if (ev == NULL) {
456 goto fail;
458 req = nbt_getdc_send(ev, ev, msg_ctx, dc_addr, domain_name,
459 sid, nt_version);
460 if (req == NULL) {
461 goto fail;
463 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
464 goto fail;
466 status = nbt_getdc_recv(req, mem_ctx, pnt_version, dc_name,
467 samlogon_response);
468 fail:
469 TALLOC_FREE(frame);
470 return status;