1/* 2 Unix SMB/CIFS implementation. 3 4 a composite API for finding a DC and its name 5 6 Copyright (C) Volker Lendecke 2005 7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006 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 3 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, see <http://www.gnu.org/licenses/>. 21*/ 22 23#include "include/includes.h" 24#include "lib/messaging/irpc.h" 25#include "librpc/gen_ndr/ndr_irpc.h" 26#include "librpc/gen_ndr/samr.h" 27#include "libcli/composite/composite.h" 28#include "libcli/libcli.h" 29#include "libcli/resolve/resolve.h" 30#include "libcli/finddcs.h" 31 32struct finddcs_state { 33 struct composite_context *ctx; 34 struct messaging_context *msg_ctx; 35 36 const char *my_netbios_name; 37 const char *domain_name; 38 struct dom_sid *domain_sid; 39 40 struct nbtd_getdcname r; 41 struct nbt_name_status node_status; 42 43 struct smb_iconv_convenience *iconv_convenience; 44 45 int num_dcs; 46 struct nbt_dc_name *dcs; 47 uint16_t nbt_port; 48}; 49 50static void finddcs_name_resolved(struct composite_context *ctx); 51static void finddcs_getdc_replied(struct irpc_request *ireq); 52static void fallback_node_status(struct finddcs_state *state); 53static void fallback_node_status_replied(struct nbt_name_request *name_req); 54 55/* 56 * Setup and send off the a normal name resolution for the target name. 57 * 58 * The domain_sid parameter is optional, and is used in the subsequent getdc request. 59 * 60 * This will try a GetDC request, but this may not work. It will try 61 * a node status as a fallback, then return no name (but still include 62 * the IP) 63 */ 64 65struct composite_context *finddcs_send(TALLOC_CTX *mem_ctx, 66 const char *my_netbios_name, 67 uint16_t nbt_port, 68 const char *domain_name, 69 int name_type, 70 struct dom_sid *domain_sid, 71 struct smb_iconv_convenience *iconv_convenience, 72 struct resolve_context *resolve_ctx, 73 struct tevent_context *event_ctx, 74 struct messaging_context *msg_ctx) 75{ 76 struct composite_context *c, *creq; 77 struct finddcs_state *state; 78 struct nbt_name name; 79 80 c = composite_create(mem_ctx, event_ctx); 81 if (c == NULL) return NULL; 82 83 state = talloc(c, struct finddcs_state); 84 if (composite_nomem(state, c)) return c; 85 c->private_data = state; 86 87 state->ctx = c; 88 89 state->nbt_port = nbt_port; 90 state->my_netbios_name = talloc_strdup(state, my_netbios_name); 91 state->domain_name = talloc_strdup(state, domain_name); 92 state->iconv_convenience = iconv_convenience; 93 if (composite_nomem(state->domain_name, c)) return c; 94 95 if (domain_sid) { 96 state->domain_sid = talloc_reference(state, domain_sid); 97 if (composite_nomem(state->domain_sid, c)) return c; 98 } else { 99 state->domain_sid = NULL; 100 } 101 102 state->msg_ctx = msg_ctx; 103 104 make_nbt_name(&name, state->domain_name, name_type); 105 creq = resolve_name_send(resolve_ctx, state, &name, event_ctx); 106 composite_continue(c, creq, finddcs_name_resolved, state); 107 return c; 108} 109 110/* Having got an name query answer, fire off a GetDC request, so we 111 * can find the target's all-important name. (Kerberos and some 112 * netlogon operations are quite picky about names) 113 * 114 * The name is a courtesy, if we don't find it, don't completely fail. 115 * 116 * However, if the nbt server is down, fall back to a node status 117 * request 118 */ 119static void finddcs_name_resolved(struct composite_context *ctx) 120{ 121 struct finddcs_state *state = 122 talloc_get_type(ctx->async.private_data, struct finddcs_state); 123 struct irpc_request *ireq; 124 struct server_id *nbt_servers; 125 const char *address; 126 127 state->ctx->status = resolve_name_recv(ctx, state, &address); 128 if (!composite_is_ok(state->ctx)) return; 129 130 /* TODO: This should try and find all the DCs, and give the 131 * caller them in the order they responded */ 132 133 state->num_dcs = 1; 134 state->dcs = talloc_array(state, struct nbt_dc_name, state->num_dcs); 135 if (composite_nomem(state->dcs, state->ctx)) return; 136 137 state->dcs[0].address = talloc_steal(state->dcs, address); 138 139 /* Try and find the nbt server. Fallback to a node status 140 * request if we can't make this happen The nbt server just 141 * might not be running, or we may not have a messaging 142 * context (not root etc) */ 143 if (!state->msg_ctx) { 144 fallback_node_status(state); 145 return; 146 } 147 148 nbt_servers = irpc_servers_byname(state->msg_ctx, state, "nbt_server"); 149 if ((nbt_servers == NULL) || (nbt_servers[0].id == 0)) { 150 fallback_node_status(state); 151 return; 152 } 153 154 state->r.in.domainname = state->domain_name; 155 state->r.in.ip_address = state->dcs[0].address; 156 state->r.in.my_computername = state->my_netbios_name; 157 state->r.in.my_accountname = talloc_asprintf(state, "%s$", state->my_netbios_name); 158 if (composite_nomem(state->r.in.my_accountname, state->ctx)) return; 159 state->r.in.account_control = ACB_WSTRUST; 160 state->r.in.domain_sid = state->domain_sid; 161 162 ireq = irpc_call_send(state->msg_ctx, nbt_servers[0], 163 &ndr_table_irpc, NDR_NBTD_GETDCNAME, 164 &state->r, state); 165 if (!ireq) { 166 fallback_node_status(state); 167 return; 168 } 169 170 composite_continue_irpc(state->ctx, ireq, finddcs_getdc_replied, state); 171} 172 173/* Called when the GetDC request returns */ 174static void finddcs_getdc_replied(struct irpc_request *ireq) 175{ 176 struct finddcs_state *state = 177 talloc_get_type(ireq->async.private_data, struct finddcs_state); 178 179 state->ctx->status = irpc_call_recv(ireq); 180 if (!composite_is_ok(state->ctx)) return; 181 182 state->dcs[0].name = talloc_steal(state->dcs, state->r.out.dcname); 183 composite_done(state->ctx); 184} 185 186/* The GetDC request might not be available (such as occours when the 187 * NBT server is down). Fallback to a node status. It is the best 188 * hope we have... */ 189static void fallback_node_status(struct finddcs_state *state) 190{ 191 struct nbt_name_socket *nbtsock; 192 struct nbt_name_request *name_req; 193 194 state->node_status.in.name.name = "*"; 195 state->node_status.in.name.type = NBT_NAME_CLIENT; 196 state->node_status.in.name.scope = NULL; 197 state->node_status.in.dest_addr = state->dcs[0].address; 198 state->node_status.in.dest_port = state->nbt_port; 199 state->node_status.in.timeout = 1; 200 state->node_status.in.retries = 2; 201 202 nbtsock = nbt_name_socket_init(state, state->ctx->event_ctx, 203 state->iconv_convenience); 204 if (composite_nomem(nbtsock, state->ctx)) return; 205 206 name_req = nbt_name_status_send(nbtsock, &state->node_status); 207 if (composite_nomem(name_req, state->ctx)) return; 208 209 composite_continue_nbt(state->ctx, 210 name_req, 211 fallback_node_status_replied, 212 state); 213} 214 215/* We have a node status reply (or perhaps a timeout) */ 216static void fallback_node_status_replied(struct nbt_name_request *name_req) 217{ 218 int i; 219 struct finddcs_state *state = talloc_get_type(name_req->async.private_data, struct finddcs_state); 220 state->ctx->status = nbt_name_status_recv(name_req, state, &state->node_status); 221 if (!composite_is_ok(state->ctx)) return; 222 223 for (i=0; i < state->node_status.out.status.num_names; i++) { 224 int j; 225 if (state->node_status.out.status.names[i].type == NBT_NAME_SERVER) { 226 char *name = talloc_strndup(state->dcs, state->node_status.out.status.names[0].name, 15); 227 /* Strip space padding */ 228 if (name) { 229 j = MIN(strlen(name), 15); 230 for (; j > 0 && name[j - 1] == ' '; j--) { 231 name[j - 1] = '\0'; 232 } 233 } 234 state->dcs[0].name = name; 235 composite_done(state->ctx); 236 return; 237 } 238 } 239 composite_error(state->ctx, NT_STATUS_NO_LOGON_SERVERS); 240} 241 242NTSTATUS finddcs_recv(struct composite_context *c, TALLOC_CTX *mem_ctx, 243 int *num_dcs, struct nbt_dc_name **dcs) 244{ 245 NTSTATUS status = composite_wait(c); 246 if (NT_STATUS_IS_OK(status)) { 247 struct finddcs_state *state = 248 talloc_get_type(c->private_data, struct finddcs_state); 249 *num_dcs = state->num_dcs; 250 *dcs = talloc_steal(mem_ctx, state->dcs); 251 } 252 talloc_free(c); 253 return status; 254} 255 256NTSTATUS finddcs(TALLOC_CTX *mem_ctx, 257 const char *my_netbios_name, 258 uint16_t nbt_port, 259 const char *domain_name, int name_type, 260 struct dom_sid *domain_sid, 261 struct smb_iconv_convenience *iconv_convenience, 262 struct resolve_context *resolve_ctx, 263 struct tevent_context *event_ctx, 264 struct messaging_context *msg_ctx, 265 int *num_dcs, struct nbt_dc_name **dcs) 266{ 267 struct composite_context *c = finddcs_send(mem_ctx, 268 my_netbios_name, 269 nbt_port, 270 domain_name, name_type, 271 domain_sid, 272 iconv_convenience, 273 resolve_ctx, 274 event_ctx, msg_ctx); 275 return finddcs_recv(c, mem_ctx, num_dcs, dcs); 276} 277