r15942: Remove the sync internal ldb calls altogether.
[jelmer/samba4-debian.git] / source / nbt_server / irpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    irpc services for the NBT server
5
6    Copyright (C) Andrew Tridgell        2005
7    Copyright (C) Volker Lendecke        2005
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 #include "includes.h"
25 #include "smbd/service_task.h"
26 #include "smbd/service.h"
27 #include "nbt_server/nbt_server.h"
28 #include "nbt_server/wins/winsserver.h"
29 #include "librpc/gen_ndr/ndr_irpc.h"
30 #include "lib/socket/socket.h"
31 #include "libcli/resolve/resolve.h"
32 #include "librpc/gen_ndr/ndr_nbt.h"
33
34 /*
35   serve out the nbt statistics
36 */
37 static NTSTATUS nbtd_information(struct irpc_message *msg, 
38                                  struct nbtd_information *r)
39 {
40         struct nbtd_server *server = talloc_get_type(msg->private, struct nbtd_server);
41
42         switch (r->in.level) {
43         case NBTD_INFO_STATISTICS:
44                 r->out.info.stats = &server->stats;
45                 break;
46         }
47
48         return NT_STATUS_OK;
49 }
50
51
52 /*
53   winbind needs to be able to do a getdc request, but some windows
54   servers always send the reply to port 138, regardless of the request
55   port. To cope with this we use a irpc request to the NBT server
56   which has port 138 open, and thus can receive the replies
57 */
58 struct getdc_state {
59         struct irpc_message *msg;
60         struct nbtd_getdcname *req;
61 };
62
63 static void getdc_recv_ntlogon_reply(struct dgram_mailslot_handler *dgmslot, 
64                                      struct nbt_dgram_packet *packet, 
65                                      struct socket_address *src)
66 {
67         struct getdc_state *s =
68                 talloc_get_type(dgmslot->private, struct getdc_state);
69
70         struct nbt_ntlogon_packet ntlogon;
71         NTSTATUS status;
72
73         status = dgram_mailslot_ntlogon_parse(dgmslot, packet, packet,
74                                               &ntlogon);
75         if (!NT_STATUS_IS_OK(status)) {
76                 DEBUG(5, ("dgram_mailslot_ntlogon_parse failed: %s\n",
77                           nt_errstr(status)));
78                 goto done;
79         }
80
81         status = NT_STATUS_NO_LOGON_SERVERS;
82
83         DEBUG(10, ("reply: command=%d\n", ntlogon.command));
84
85         switch (ntlogon.command) {
86         case NTLOGON_SAM_LOGON:
87                 DEBUG(0, ("Huh -- got NTLOGON_SAM_LOGON as reply\n"));
88                 break;
89         case NTLOGON_SAM_LOGON_REPLY:
90         case NTLOGON_SAM_LOGON_REPLY15: {
91                 const char *p = ntlogon.req.reply.server;
92
93                 DEBUG(10, ("NTLOGON_SAM_LOGON_REPLY: server: %s, user: %s, "
94                            "domain: %s\n", p, ntlogon.req.reply.user_name,
95                            ntlogon.req.reply.domain));
96
97                 if (*p == '\\') p += 1;
98                 if (*p == '\\') p += 1;
99
100                 s->req->out.dcname = talloc_strdup(s->req, p);
101                 if (s->req->out.dcname == NULL) {
102                         DEBUG(0, ("talloc failed\n"));
103                         status = NT_STATUS_NO_MEMORY;
104                         goto done;
105                 }
106                 status = NT_STATUS_OK;
107                 break;
108         }
109         default:
110                 DEBUG(0, ("Got unknown packet: %d\n", ntlogon.command));
111                 break;
112         }
113
114  done:
115         irpc_send_reply(s->msg, status);
116 }
117
118 static NTSTATUS nbtd_getdcname(struct irpc_message *msg, 
119                                struct nbtd_getdcname *req)
120 {
121         struct nbtd_server *server =
122                 talloc_get_type(msg->private, struct nbtd_server);
123         struct nbtd_interface *iface = nbtd_find_request_iface(server, req->in.ip_address, True);
124         struct getdc_state *s;
125         struct nbt_ntlogon_packet p;
126         struct nbt_ntlogon_sam_logon *r;
127         struct nbt_name src, dst;
128         struct socket_address *dest;
129         struct dgram_mailslot_handler *handler;
130         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
131
132         DEBUG(0, ("nbtd_getdcname called\n"));
133
134         s = talloc(msg, struct getdc_state);
135         NT_STATUS_HAVE_NO_MEMORY(s);
136
137         s->msg = msg;
138         s->req = req;
139         
140         handler = dgram_mailslot_temp(iface->dgmsock, NBT_MAILSLOT_GETDC,
141                                       getdc_recv_ntlogon_reply, s);
142         NT_STATUS_HAVE_NO_MEMORY(handler);
143         
144         ZERO_STRUCT(p);
145         p.command = NTLOGON_SAM_LOGON;
146         r = &p.req.logon;
147         r->request_count = 0;
148         r->computer_name = req->in.my_computername;
149         r->user_name = req->in.my_accountname;
150         r->mailslot_name = handler->mailslot_name;
151         r->acct_control = req->in.account_control;
152         r->sid = *req->in.domain_sid;
153         r->nt_version = 1;
154         r->lmnt_token = 0xffff;
155         r->lm20_token = 0xffff;
156
157         make_nbt_name_client(&src, req->in.my_computername);
158         make_nbt_name(&dst, req->in.domainname, 0x1c);
159
160         dest = socket_address_from_strings(msg, iface->dgmsock->sock->backend_name, 
161                                            req->in.ip_address, 138);
162         NT_STATUS_HAVE_NO_MEMORY(dest);
163
164         status = dgram_mailslot_ntlogon_send(iface->dgmsock, DGRAM_DIRECT_GROUP,
165                                              &dst, dest,
166                                              &src, &p);
167         if (!NT_STATUS_IS_OK(status)) {
168                 DEBUG(0, ("dgram_mailslot_ntlogon_send failed: %s\n",
169                           nt_errstr(status)));
170                 return status;
171         }
172
173         msg->defer_reply = True;
174         return NT_STATUS_OK;
175 }
176
177
178 /*
179   register the irpc handlers for the nbt server
180 */
181 void nbtd_register_irpc(struct nbtd_server *nbtsrv)
182 {
183         NTSTATUS status;
184         struct task_server *task = nbtsrv->task;
185
186         status = IRPC_REGISTER(task->msg_ctx, irpc, NBTD_INFORMATION, 
187                                nbtd_information, nbtsrv);
188         if (!NT_STATUS_IS_OK(status)) {
189                 task_server_terminate(task, "nbtd failed to setup monitoring");
190                 return;
191         }
192
193         status = IRPC_REGISTER(task->msg_ctx, irpc, NBTD_GETDCNAME,
194                                nbtd_getdcname, nbtsrv);
195         if (!NT_STATUS_IS_OK(status)) {
196                 task_server_terminate(task, "nbtd failed to setup getdcname "
197                                       "handler");
198                 return;
199         }
200
201         status = IRPC_REGISTER(task->msg_ctx, irpc, NBTD_PROXY_WINS_CHALLENGE,
202                                nbtd_proxy_wins_challenge, nbtsrv);
203         if (!NT_STATUS_IS_OK(status)) {
204                 task_server_terminate(task, "nbtd failed to setup wins challenge "
205                                       "handler");
206                 return;
207         }
208
209         status = IRPC_REGISTER(task->msg_ctx, irpc, NBTD_PROXY_WINS_RELEASE_DEMAND,
210                                nbtd_proxy_wins_release_demand, nbtsrv);
211         if (!NT_STATUS_IS_OK(status)) {
212                 task_server_terminate(task, "nbtd failed to setup wins release demand "
213                                       "handler");
214                 return;
215         }
216 }