r818: added server side SMB signing to Samba4
[bbaumbach/samba-autobuild/.git] / source4 / smb_server / sesssetup.c
1 /* 
2    Unix SMB/CIFS implementation.
3    handle SMBsessionsetup
4    Copyright (C) Andrew Tridgell 1998-2001
5    Copyright (C) Andrew Bartlett      2001
6    Copyright (C) Jim McDonough        2002
7    Copyright (C) Luke Howard          2003
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
26 /*
27   setup the OS, Lanman and domain portions of a session setup reply
28 */
29 static void sesssetup_common_strings(struct request_context *req,
30                                      char **os, char **lanman, char **domain)
31 {
32         (*os) = talloc_asprintf(req->mem_ctx, "Unix");
33         (*lanman) = talloc_asprintf(req->mem_ctx, "Samba %s", SAMBA_VERSION_STRING);
34         (*domain) = talloc_asprintf(req->mem_ctx, "%s", lp_workgroup());
35 }
36
37
38 /*
39   handler for old style session setup
40 */
41 static NTSTATUS sesssetup_old(struct request_context *req, union smb_sesssetup *sess)
42 {
43         NTSTATUS status;
44         auth_usersupplied_info *user_info = NULL;
45         auth_serversupplied_info *server_info = NULL;
46         DATA_BLOB null_blob;
47         DATA_BLOB session_key;
48
49         if (!req->smb->negotiate.done_sesssetup) {
50                 req->smb->negotiate.max_send = sess->old.in.bufsize;
51         }
52
53         null_blob.length = 0;
54
55         status = make_user_info_for_reply_enc(&user_info, 
56                                               sess->old.in.user, sess->old.in.domain,
57                                               sess->old.in.password,
58                                               null_blob);
59         if (!NT_STATUS_IS_OK(status)) {
60                 return NT_STATUS_ACCESS_DENIED;
61         }
62
63         status = req->smb->negotiate.auth_context->check_ntlm_password(req->smb->negotiate.auth_context, 
64                                                                        user_info, 
65                                                                        &server_info);
66         if (!NT_STATUS_IS_OK(status)) {
67                 return nt_status_squash(status);
68         }
69
70         if (server_info->user_session_key.data) {
71                 session_key = data_blob(server_info->user_session_key.data, server_info->user_session_key.length);
72         } else {
73                 session_key = data_blob(NULL, 0);
74         }
75
76         sess->old.out.action = 0;
77         sess->old.out.vuid = register_vuid(req->smb, server_info, &session_key, sess->old.in.user);
78         sesssetup_common_strings(req, 
79                                  &sess->old.out.os,
80                                  &sess->old.out.lanman,
81                                  &sess->old.out.domain);
82
83         return NT_STATUS_OK;
84 }
85
86
87 /*
88   handler for NT1 style session setup
89 */
90 static NTSTATUS sesssetup_nt1(struct request_context *req, union smb_sesssetup *sess)
91 {
92         NTSTATUS status;
93         auth_usersupplied_info *user_info = NULL;
94         auth_serversupplied_info *server_info = NULL;
95         DATA_BLOB session_key;
96
97         if (!req->smb->negotiate.done_sesssetup) {
98                 req->smb->negotiate.max_send = sess->nt1.in.bufsize;
99                 req->smb->negotiate.client_caps = sess->nt1.in.capabilities;
100         }
101
102         status = make_user_info_for_reply_enc(&user_info, 
103                                               sess->nt1.in.user, sess->nt1.in.domain,
104                                               sess->nt1.in.password1,
105                                               sess->nt1.in.password2);
106         if (!NT_STATUS_IS_OK(status)) {
107                 return NT_STATUS_ACCESS_DENIED;
108         }
109
110         status = req->smb->negotiate.auth_context->check_ntlm_password(req->smb->negotiate.auth_context, 
111                                                                        user_info, 
112                                                                        &server_info);
113         if (!NT_STATUS_IS_OK(status)) {
114                 return nt_status_squash(status);
115         }
116
117         if (server_info->user_session_key.data) {
118                 session_key = data_blob(server_info->user_session_key.data, server_info->user_session_key.length);
119         } else {
120                 session_key = data_blob(NULL, 0);
121         }
122
123         sess->nt1.out.action = 0;
124         sess->nt1.out.vuid = register_vuid(req->smb, server_info, &session_key, sess->old.in.user);
125         sesssetup_common_strings(req, 
126                                  &sess->nt1.out.os,
127                                  &sess->nt1.out.lanman,
128                                  &sess->nt1.out.domain);
129
130         srv_setup_signing(req->smb, &session_key, &sess->nt1.in.password2);
131
132         return NT_STATUS_OK;
133 }
134
135
136 /*
137   handler for SPNEGO style session setup
138 */
139 static NTSTATUS sesssetup_spnego(struct request_context *req, union smb_sesssetup *sess)
140 {
141         /* defer this one for now */
142         return NT_STATUS_INVALID_LEVEL;
143 }
144
145 /*
146   backend for sessionsetup call - this takes all 3 varients of the call
147 */
148 NTSTATUS sesssetup_backend(struct request_context *req, 
149                            union smb_sesssetup *sess)
150 {
151         switch (sess->generic.level) {
152                 case RAW_SESSSETUP_OLD:
153                         return sesssetup_old(req, sess);
154                 case RAW_SESSSETUP_NT1:
155                         return sesssetup_nt1(req, sess);
156                 case RAW_SESSSETUP_SPNEGO:
157                         return sesssetup_spnego(req, sess);
158         }
159
160         req->smb->negotiate.done_sesssetup = True;
161
162         return NT_STATUS_INVALID_LEVEL;
163 }
164
165