r23792: convert Samba4 to GPLv3
[metze/samba/wip.git] / source4 / nbt_server / dgram / ntlogon.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    NBT datagram ntlogon server
5
6    Copyright (C) Andrew Tridgell        2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "nbt_server/nbt_server.h"
24 #include "lib/socket/socket.h"
25 #include "librpc/gen_ndr/ndr_nbt.h"
26
27 /*
28   reply to a SAM LOGON request
29  */
30 static void nbtd_ntlogon_sam_logon(struct dgram_mailslot_handler *dgmslot, 
31                                    struct nbtd_interface *iface,
32                                    struct nbt_dgram_packet *packet,
33                                    const struct socket_address *src,
34                                    struct nbt_ntlogon_packet *ntlogon)
35 {
36         struct nbt_name *name = &packet->data.msg.dest_name;
37         struct nbtd_interface *reply_iface = nbtd_find_reply_iface(iface, src->addr, False);
38         struct nbt_ntlogon_packet reply;
39         struct nbt_ntlogon_sam_logon_reply *logon;
40
41         /* only answer sam logon requests on the PDC or LOGON names */
42         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
43                 return;
44         }
45
46         /* setup a SAM LOGON reply */
47         ZERO_STRUCT(reply);
48         reply.command = NTLOGON_SAM_LOGON_REPLY;
49         logon = &reply.req.reply;
50
51         logon->server           = talloc_asprintf(packet, "\\\\%s", lp_netbios_name());
52         logon->user_name        = ntlogon->req.logon.user_name;
53         logon->domain           = lp_workgroup();
54         logon->nt_version       = 1;
55         logon->lmnt_token       = 0xFFFF;
56         logon->lm20_token       = 0xFFFF;
57
58         packet->data.msg.dest_name.type = 0;
59
60         dgram_mailslot_ntlogon_reply(reply_iface->dgmsock, 
61                                      packet, 
62                                      ntlogon->req.logon.mailslot_name,
63                                      &reply);
64 }
65
66 /*
67   handle incoming ntlogon mailslot requests
68 */
69 void nbtd_mailslot_ntlogon_handler(struct dgram_mailslot_handler *dgmslot, 
70                                    struct nbt_dgram_packet *packet, 
71                                    struct socket_address *src)
72 {
73         NTSTATUS status = NT_STATUS_NO_MEMORY;
74         struct nbtd_interface *iface = 
75                 talloc_get_type(dgmslot->private, struct nbtd_interface);
76         struct nbt_ntlogon_packet *ntlogon = 
77                 talloc(dgmslot, struct nbt_ntlogon_packet);
78         struct nbtd_iface_name *iname;
79         struct nbt_name *name = &packet->data.msg.dest_name;
80
81         if (ntlogon == NULL) goto failed;
82
83         /*
84           see if the we are listening on the destination netbios name
85         */
86         iname = nbtd_find_iname(iface, name, 0);
87         if (iname == NULL) {
88                 status = NT_STATUS_BAD_NETWORK_NAME;
89                 goto failed;
90         }
91
92         DEBUG(2,("ntlogon request to %s from %s:%d\n", 
93                  nbt_name_string(ntlogon, name), src->addr, src->port));
94         status = dgram_mailslot_ntlogon_parse(dgmslot, ntlogon, packet, ntlogon);
95         if (!NT_STATUS_IS_OK(status)) goto failed;
96
97         NDR_PRINT_DEBUG(nbt_ntlogon_packet, ntlogon);
98
99         switch (ntlogon->command) {
100         case NTLOGON_SAM_LOGON:
101                 nbtd_ntlogon_sam_logon(dgmslot, iface, packet, src, ntlogon);
102                 break;
103         default:
104                 DEBUG(2,("unknown ntlogon op %d from %s:%d\n", 
105                          ntlogon->command, src->addr, src->port));
106                 break;
107         }
108
109         talloc_free(ntlogon);
110         return;
111
112 failed:
113         DEBUG(2,("nbtd ntlogon handler failed from %s:%d to %s - %s\n",
114                  src->addr, src->port, nbt_name_string(ntlogon, name),
115                  nt_errstr(status)));
116         talloc_free(ntlogon);
117 }