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