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