r1687: Fix bogus requirement for SMB signing on guest connections.
[samba.git] / source / 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 smbsrv_request *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 smbsrv_request *req, union smb_sesssetup *sess)
42 {
43         NTSTATUS status;
44         struct auth_usersupplied_info *user_info = NULL;
45         struct auth_serversupplied_info *server_info = NULL;
46         struct auth_session_info *session_info;
47         DATA_BLOB null_blob;
48
49         if (!req->smb_conn->negotiate.done_sesssetup) {
50                 req->smb_conn->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_conn->negotiate.auth_context->check_ntlm_password(req->smb_conn->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         status = make_session_info(server_info, &session_info);
71         if (!NT_STATUS_IS_OK(status)) {
72                 return nt_status_squash(status);
73         }
74
75         sess->old.out.action = 0;
76         sess->old.out.vuid = smbsrv_register_session(req->smb_conn, session_info, NULL);
77         if (sess->old.out.vuid == UID_FIELD_INVALID) {
78                 return NT_STATUS_ACCESS_DENIED;
79         }
80         sesssetup_common_strings(req, 
81                                  &sess->old.out.os,
82                                  &sess->old.out.lanman,
83                                  &sess->old.out.domain);
84
85         req->session = smbsrv_session_find(req->smb_conn, sess->old.out.vuid);
86
87         return NT_STATUS_OK;
88 }
89
90
91 /*
92   handler for NT1 style session setup
93 */
94 static NTSTATUS sesssetup_nt1(struct smbsrv_request *req, union smb_sesssetup *sess)
95 {
96         NTSTATUS status;
97         struct auth_usersupplied_info *user_info = NULL;
98         struct auth_serversupplied_info *server_info = NULL;
99         struct auth_session_info *session_info;
100
101         if (!req->smb_conn->negotiate.done_sesssetup) {
102                 req->smb_conn->negotiate.max_send = sess->nt1.in.bufsize;
103                 req->smb_conn->negotiate.client_caps = sess->nt1.in.capabilities;
104         }
105
106         status = make_user_info_for_reply_enc(&user_info, 
107                                               sess->nt1.in.user, sess->nt1.in.domain,
108                                               sess->nt1.in.password1,
109                                               sess->nt1.in.password2);
110         if (!NT_STATUS_IS_OK(status)) {
111                 return NT_STATUS_ACCESS_DENIED;
112         }
113
114         status = req->smb_conn->negotiate
115                 .auth_context->check_ntlm_password(req->smb_conn->negotiate
116                                                     .auth_context, 
117                                                    user_info, 
118                                                    &server_info);
119         if (!NT_STATUS_IS_OK(status)) {
120                 return nt_status_squash(status);
121         }
122
123         status = make_session_info(server_info, &session_info);
124         if (!NT_STATUS_IS_OK(status)) {
125                 return nt_status_squash(status);
126         }
127
128         sess->nt1.out.action = 0;
129         sess->nt1.out.vuid = smbsrv_register_session(req->smb_conn, session_info, NULL);
130         if (sess->nt1.out.vuid == UID_FIELD_INVALID) {
131                 return NT_STATUS_ACCESS_DENIED;
132         }
133         sesssetup_common_strings(req, 
134                                  &sess->nt1.out.os,
135                                  &sess->nt1.out.lanman,
136                                  &sess->nt1.out.domain);
137         
138         req->session = smbsrv_session_find(req->smb_conn, sess->nt1.out.vuid);
139         if (!session_info->server_info->guest) {
140                 srv_setup_signing(req->smb_conn, &session_info->session_key, &sess->nt1.in.password2);
141         }
142
143         return NT_STATUS_OK;
144 }
145
146
147 /*
148   handler for SPNEGO style session setup
149 */
150 static NTSTATUS sesssetup_spnego(struct smbsrv_request *req, union smb_sesssetup *sess)
151 {
152         /* defer this one for now */
153         return NT_STATUS_INVALID_LEVEL;
154 }
155
156 /*
157   backend for sessionsetup call - this takes all 3 variants of the call
158 */
159 NTSTATUS sesssetup_backend(struct smbsrv_request *req, 
160                            union smb_sesssetup *sess)
161 {
162         NTSTATUS status = NT_STATUS_INVALID_LEVEL;
163
164         switch (sess->generic.level) {
165                 case RAW_SESSSETUP_OLD:
166                         status = sesssetup_old(req, sess);
167                         break;
168                 case RAW_SESSSETUP_NT1:
169                         status = sesssetup_nt1(req, sess);
170                         break;
171                 case RAW_SESSSETUP_SPNEGO:
172                         status = sesssetup_spnego(req, sess);
173                         break;
174         }
175
176         if (NT_STATUS_IS_OK(status)) {
177                 req->smb_conn->negotiate.done_sesssetup = True;
178         }
179
180         return status;
181 }
182
183