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