The IRIX compiler does not like embedded unnamed unions
[obnox/samba/samba-obnox.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 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 most (all?) 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_netlogon_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         const char *p;
69         struct nbt_netlogon_response netlogon;
70         NTSTATUS status;
71
72         status = dgram_mailslot_netlogon_parse_response(dgmslot, packet, packet,
73                                                         &netlogon);
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         /* We asked for version 1 only */
81         if (netlogon.response_type == NETLOGON_SAMLOGON
82             && netlogon.data.samlogon.ntver != NETLOGON_NT_VERSION_1) {
83                 status = NT_STATUS_INVALID_NETWORK_RESPONSE;
84                 goto done;
85         }
86
87         p = netlogon.data.samlogon.data.nt4.server;
88
89         DEBUG(10, ("NTLOGON_SAM_LOGON_REPLY: server: %s, user: %s, "
90                    "domain: %s\n", p,
91                    netlogon.data.samlogon.data.nt4.user_name,
92                    netlogon.data.samlogon.data.nt4.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
104         status = NT_STATUS_OK;
105
106  done:
107         irpc_send_reply(s->msg, status);
108 }
109
110 static NTSTATUS nbtd_getdcname(struct irpc_message *msg, 
111                                struct nbtd_getdcname *req)
112 {
113         struct nbtd_server *server =
114                 talloc_get_type(msg->private, struct nbtd_server);
115         struct nbtd_interface *iface = nbtd_find_request_iface(server, req->in.ip_address, true);
116         struct getdc_state *s;
117         struct nbt_netlogon_packet p;
118         struct NETLOGON_SAM_LOGON_REQUEST *r;
119         struct nbt_name src, dst;
120         struct socket_address *dest;
121         struct dgram_mailslot_handler *handler;
122         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
123
124         DEBUG(0, ("nbtd_getdcname called\n"));
125
126         s = talloc(msg, struct getdc_state);
127         NT_STATUS_HAVE_NO_MEMORY(s);
128
129         s->msg = msg;
130         s->req = req;
131         
132         handler = dgram_mailslot_temp(iface->dgmsock, NBT_MAILSLOT_GETDC,
133                                       getdc_recv_netlogon_reply, s);
134         NT_STATUS_HAVE_NO_MEMORY(handler);
135         
136         ZERO_STRUCT(p);
137         p.command = LOGON_SAM_LOGON_REQUEST;
138         r = &p.req.logon;
139         r->request_count = 0;
140         r->computer_name = req->in.my_computername;
141         r->user_name = req->in.my_accountname;
142         r->mailslot_name = handler->mailslot_name;
143         r->acct_control = req->in.account_control;
144         r->sid = *req->in.domain_sid;
145         r->nt_version = NETLOGON_NT_VERSION_1;
146         r->lmnt_token = 0xffff;
147         r->lm20_token = 0xffff;
148
149         make_nbt_name_client(&src, req->in.my_computername);
150         make_nbt_name(&dst, req->in.domainname, 0x1c);
151
152         dest = socket_address_from_strings(msg, iface->dgmsock->sock->backend_name, 
153                                            req->in.ip_address, 138);
154         NT_STATUS_HAVE_NO_MEMORY(dest);
155
156         status = dgram_mailslot_netlogon_send(iface->dgmsock, 
157                                               &dst, dest,
158                                               NBT_MAILSLOT_NETLOGON, 
159                                               &src, &p);
160         if (!NT_STATUS_IS_OK(status)) {
161                 DEBUG(0, ("dgram_mailslot_ntlogon_send failed: %s\n",
162                           nt_errstr(status)));
163                 return status;
164         }
165
166         msg->defer_reply = true;
167         return NT_STATUS_OK;
168 }
169
170
171 /*
172   register the irpc handlers for the nbt server
173 */
174 void nbtd_register_irpc(struct nbtd_server *nbtsrv)
175 {
176         NTSTATUS status;
177         struct task_server *task = nbtsrv->task;
178
179         status = IRPC_REGISTER(task->msg_ctx, irpc, NBTD_INFORMATION, 
180                                nbtd_information, nbtsrv);
181         if (!NT_STATUS_IS_OK(status)) {
182                 task_server_terminate(task, "nbtd failed to setup monitoring");
183                 return;
184         }
185
186         status = IRPC_REGISTER(task->msg_ctx, irpc, NBTD_GETDCNAME,
187                                nbtd_getdcname, nbtsrv);
188         if (!NT_STATUS_IS_OK(status)) {
189                 task_server_terminate(task, "nbtd failed to setup getdcname "
190                                       "handler");
191                 return;
192         }
193
194         status = IRPC_REGISTER(task->msg_ctx, irpc, NBTD_PROXY_WINS_CHALLENGE,
195                                nbtd_proxy_wins_challenge, nbtsrv);
196         if (!NT_STATUS_IS_OK(status)) {
197                 task_server_terminate(task, "nbtd failed to setup wins challenge "
198                                       "handler");
199                 return;
200         }
201
202         status = IRPC_REGISTER(task->msg_ctx, irpc, NBTD_PROXY_WINS_RELEASE_DEMAND,
203                                nbtd_proxy_wins_release_demand, nbtsrv);
204         if (!NT_STATUS_IS_OK(status)) {
205                 task_server_terminate(task, "nbtd failed to setup wins release demand "
206                                       "handler");
207                 return;
208         }
209 }