191bac8a54a8cc332e7602fbe2b58bcf16cb6cc8
[garming/samba-autobuild/.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "nbt_server/nbt_server.h"
25
26
27 /*
28   reply to a SAM LOGON request
29  */
30 static void nbtd_ntlogon_sam_logon(struct dgram_mailslot_handler *dgmslot, 
31                                    struct nbt_dgram_packet *packet,
32                                    const struct nbt_peer_socket *src,
33                                    struct nbt_ntlogon_packet *ntlogon)
34 {
35         struct nbt_name *name = &packet->data.msg.dest_name;
36         struct nbt_ntlogon_packet reply;
37         struct nbt_ntlogon_sam_logon_reply *logon;
38
39         /* only answer sam logon requests on the PDC or LOGON names */
40         if (name->type != NBT_NAME_PDC && name->type != NBT_NAME_LOGON) {
41                 return;
42         }
43
44         /* setup a SAM LOGON reply */
45         ZERO_STRUCT(reply);
46         reply.command = NTLOGON_SAM_LOGON_REPLY;
47         logon = &reply.req.reply;
48
49         logon->server           = talloc_asprintf(packet, "\\\\%s", lp_netbios_name());
50         logon->user_name        = ntlogon->req.logon.user_name;
51         logon->domain           = lp_workgroup();
52         logon->nt_version       = 1;
53         logon->lmnt_token       = 0xFFFF;
54         logon->lm20_token       = 0xFFFF;
55
56         packet->data.msg.dest_name.type = 0;
57
58         dgram_mailslot_ntlogon_reply(dgmslot->dgmsock, 
59                                      packet, 
60                                      ntlogon->req.logon.mailslot_name,
61                                      &reply);
62 }
63
64 /*
65   handle incoming ntlogon mailslot requests
66 */
67 void nbtd_mailslot_ntlogon_handler(struct dgram_mailslot_handler *dgmslot, 
68                                    struct nbt_dgram_packet *packet, 
69                                    const struct nbt_peer_socket *src)
70 {
71         NTSTATUS status = NT_STATUS_NO_MEMORY;
72         struct nbtd_interface *iface = 
73                 talloc_get_type(dgmslot->private, struct nbtd_interface);
74         struct nbt_ntlogon_packet *ntlogon = 
75                 talloc(dgmslot, struct nbt_ntlogon_packet);
76         struct nbtd_iface_name *iname;
77         struct nbt_name *name = &packet->data.msg.dest_name;
78
79         if (ntlogon == NULL) goto failed;
80
81         /*
82           see if the we are listening on the destination netbios name
83         */
84         iname = nbtd_find_iname(iface, name, 0);
85         if (iname == NULL) {
86                 status = NT_STATUS_BAD_NETWORK_NAME;
87                 goto failed;
88         }
89
90         DEBUG(2,("ntlogon request to %s from %s:%d\n", 
91                  nbt_name_string(ntlogon, name), src->addr, src->port));
92         status = dgram_mailslot_ntlogon_parse(dgmslot, ntlogon, packet, ntlogon);
93         if (!NT_STATUS_IS_OK(status)) goto failed;
94
95         NDR_PRINT_DEBUG(nbt_ntlogon_packet, ntlogon);
96
97         switch (ntlogon->command) {
98         case NTLOGON_SAM_LOGON:
99                 nbtd_ntlogon_sam_logon(dgmslot, packet, src, ntlogon);
100                 break;
101         default:
102                 DEBUG(2,("unknown ntlogon op %d from %s:%d\n", 
103                          ntlogon->command, src->addr, src->port));
104                 break;
105         }
106
107         talloc_free(ntlogon);
108         return;
109
110 failed:
111         DEBUG(2,("nbtd ntlogon handler failed from %s:%d - %s\n",
112                  src->addr, src->port, nt_errstr(status)));
113         talloc_free(ntlogon);
114 }