Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/sess.c |
| 3 | * |
| 4 | * SMB/CIFS session setup handling routines |
| 5 | * |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 6 | * Copyright (c) International Business Machines Corp., 2006, 2007 |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 7 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 8 | * |
| 9 | * This library is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Lesser General Public License as published |
| 11 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This library 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 |
| 17 | * the GNU Lesser General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Lesser General Public License |
| 20 | * along with this library; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include "cifspdu.h" |
| 25 | #include "cifsglob.h" |
| 26 | #include "cifsproto.h" |
| 27 | #include "cifs_unicode.h" |
| 28 | #include "cifs_debug.h" |
| 29 | #include "ntlmssp.h" |
| 30 | #include "nterr.h" |
Steve French | 9c53588 | 2006-06-01 05:09:10 +0000 | [diff] [blame] | 31 | #include <linux/utsname.h> |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 32 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 33 | extern void SMBNTencrypt(unsigned char *passwd, unsigned char *c8, |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 34 | unsigned char *p24); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 35 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 36 | static __u32 cifs_ssetup_hdr(struct cifsSesInfo *ses, SESSION_SETUP_ANDX *pSMB) |
| 37 | { |
| 38 | __u32 capabilities = 0; |
| 39 | |
| 40 | /* init fields common to all four types of SessSetup */ |
| 41 | /* note that header is initialized to zero in header_assemble */ |
| 42 | pSMB->req.AndXCommand = 0xFF; |
| 43 | pSMB->req.MaxBufferSize = cpu_to_le16(ses->server->maxBuf); |
| 44 | pSMB->req.MaxMpxCount = cpu_to_le16(ses->server->maxReq); |
| 45 | |
| 46 | /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */ |
| 47 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 48 | /* BB verify whether signing required on neg or just on auth frame |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 49 | (and NTLM case) */ |
| 50 | |
| 51 | capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS | |
| 52 | CAP_LARGE_WRITE_X | CAP_LARGE_READ_X; |
| 53 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 54 | if (ses->server->secMode & |
| 55 | (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 56 | pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE; |
| 57 | |
| 58 | if (ses->capabilities & CAP_UNICODE) { |
| 59 | pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE; |
| 60 | capabilities |= CAP_UNICODE; |
| 61 | } |
| 62 | if (ses->capabilities & CAP_STATUS32) { |
| 63 | pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS; |
| 64 | capabilities |= CAP_STATUS32; |
| 65 | } |
| 66 | if (ses->capabilities & CAP_DFS) { |
| 67 | pSMB->req.hdr.Flags2 |= SMBFLG2_DFS; |
| 68 | capabilities |= CAP_DFS; |
| 69 | } |
| 70 | if (ses->capabilities & CAP_UNIX) { |
| 71 | capabilities |= CAP_UNIX; |
| 72 | } |
| 73 | |
| 74 | /* BB check whether to init vcnum BB */ |
| 75 | return capabilities; |
| 76 | } |
| 77 | |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 78 | static void unicode_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses, |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 79 | const struct nls_table *nls_cp) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 80 | { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 81 | char *bcc_ptr = *pbcc_area; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 82 | int bytes_ret = 0; |
| 83 | |
| 84 | /* BB FIXME add check that strings total less |
| 85 | than 335 or will need to send them as arrays */ |
| 86 | |
Steve French | 0223cf0 | 2006-06-27 19:50:57 +0000 | [diff] [blame] | 87 | /* unicode strings, must be word aligned before the call */ |
| 88 | /* if ((long) bcc_ptr % 2) { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 89 | *bcc_ptr = 0; |
| 90 | bcc_ptr++; |
Steve French | 0223cf0 | 2006-06-27 19:50:57 +0000 | [diff] [blame] | 91 | } */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 92 | /* copy user */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 93 | if (ses->userName == NULL) { |
Steve French | 6e659c6 | 2006-11-08 23:10:46 +0000 | [diff] [blame] | 94 | /* null user mount */ |
| 95 | *bcc_ptr = 0; |
| 96 | *(bcc_ptr+1) = 0; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 97 | } else { /* 300 should be long enough for any conceivable user name */ |
| 98 | bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->userName, |
| 99 | 300, nls_cp); |
| 100 | } |
| 101 | bcc_ptr += 2 * bytes_ret; |
| 102 | bcc_ptr += 2; /* account for null termination */ |
| 103 | /* copy domain */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 104 | if (ses->domainName == NULL) { |
Steve French | 6e659c6 | 2006-11-08 23:10:46 +0000 | [diff] [blame] | 105 | /* Sending null domain better than using a bogus domain name (as |
| 106 | we did briefly in 2.6.18) since server will use its default */ |
| 107 | *bcc_ptr = 0; |
| 108 | *(bcc_ptr+1) = 0; |
| 109 | bytes_ret = 0; |
| 110 | } else |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 111 | bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, ses->domainName, |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 112 | 256, nls_cp); |
| 113 | bcc_ptr += 2 * bytes_ret; |
| 114 | bcc_ptr += 2; /* account for null terminator */ |
| 115 | |
| 116 | /* Copy OS version */ |
| 117 | bytes_ret = cifs_strtoUCS((__le16 *)bcc_ptr, "Linux version ", 32, |
| 118 | nls_cp); |
| 119 | bcc_ptr += 2 * bytes_ret; |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 120 | bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, init_utsname()->release, |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 121 | 32, nls_cp); |
| 122 | bcc_ptr += 2 * bytes_ret; |
| 123 | bcc_ptr += 2; /* trailing null */ |
| 124 | |
| 125 | bytes_ret = cifs_strtoUCS((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS, |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 126 | 32, nls_cp); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 127 | bcc_ptr += 2 * bytes_ret; |
| 128 | bcc_ptr += 2; /* trailing null */ |
| 129 | |
| 130 | *pbcc_area = bcc_ptr; |
| 131 | } |
| 132 | |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 133 | static void ascii_ssetup_strings(char **pbcc_area, struct cifsSesInfo *ses, |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 134 | const struct nls_table *nls_cp) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 135 | { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 136 | char *bcc_ptr = *pbcc_area; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 137 | |
| 138 | /* copy user */ |
| 139 | /* BB what about null user mounts - check that we do this BB */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 140 | /* copy user */ |
| 141 | if (ses->userName == NULL) { |
| 142 | /* BB what about null user mounts - check that we do this BB */ |
| 143 | } else { /* 300 should be long enough for any conceivable user name */ |
| 144 | strncpy(bcc_ptr, ses->userName, 300); |
| 145 | } |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 146 | /* BB improve check for overflow */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 147 | bcc_ptr += strnlen(ses->userName, 300); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 148 | *bcc_ptr = 0; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 149 | bcc_ptr++; /* account for null termination */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 150 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 151 | /* copy domain */ |
| 152 | |
| 153 | if (ses->domainName != NULL) { |
| 154 | strncpy(bcc_ptr, ses->domainName, 256); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 155 | bcc_ptr += strnlen(ses->domainName, 256); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 156 | } /* else we will send a null domain name |
Steve French | 6e659c6 | 2006-11-08 23:10:46 +0000 | [diff] [blame] | 157 | so the server will default to its own domain */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 158 | *bcc_ptr = 0; |
| 159 | bcc_ptr++; |
| 160 | |
| 161 | /* BB check for overflow here */ |
| 162 | |
| 163 | strcpy(bcc_ptr, "Linux version "); |
| 164 | bcc_ptr += strlen("Linux version "); |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 165 | strcpy(bcc_ptr, init_utsname()->release); |
| 166 | bcc_ptr += strlen(init_utsname()->release) + 1; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 167 | |
| 168 | strcpy(bcc_ptr, CIFS_NETWORK_OPSYS); |
| 169 | bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1; |
| 170 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 171 | *pbcc_area = bcc_ptr; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 174 | static int decode_unicode_ssetup(char **pbcc_area, int bleft, |
| 175 | struct cifsSesInfo *ses, |
| 176 | const struct nls_table *nls_cp) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 177 | { |
| 178 | int rc = 0; |
| 179 | int words_left, len; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 180 | char *data = *pbcc_area; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 184 | cFYI(1, ("bleft %d", bleft)); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 185 | |
| 186 | |
Steve French | 8e6f195 | 2007-01-22 01:19:30 +0000 | [diff] [blame] | 187 | /* SMB header is unaligned, so cifs servers word align start of |
| 188 | Unicode strings */ |
| 189 | data++; |
| 190 | bleft--; /* Windows servers do not always double null terminate |
| 191 | their final Unicode string - in which case we |
| 192 | now will not attempt to decode the byte of junk |
| 193 | which follows it */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 194 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 195 | words_left = bleft / 2; |
| 196 | |
| 197 | /* save off server operating system */ |
| 198 | len = UniStrnlen((wchar_t *) data, words_left); |
| 199 | |
| 200 | /* We look for obvious messed up bcc or strings in response so we do not go off |
| 201 | the end since (at least) WIN2K and Windows XP have a major bug in not null |
| 202 | terminating last Unicode string in response */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 203 | if (len >= words_left) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 204 | return rc; |
| 205 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 206 | if (ses->serverOS) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 207 | kfree(ses->serverOS); |
| 208 | /* UTF-8 string will not grow more than four times as big as UCS-16 */ |
| 209 | ses->serverOS = kzalloc(4 * len, GFP_KERNEL); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 210 | if (ses->serverOS != NULL) { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 211 | cifs_strfromUCS_le(ses->serverOS, (__le16 *)data, len, |
| 212 | nls_cp); |
| 213 | } |
| 214 | data += 2 * (len + 1); |
| 215 | words_left -= len + 1; |
| 216 | |
| 217 | /* save off server network operating system */ |
| 218 | len = UniStrnlen((wchar_t *) data, words_left); |
| 219 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 220 | if (len >= words_left) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 221 | return rc; |
| 222 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 223 | if (ses->serverNOS) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 224 | kfree(ses->serverNOS); |
| 225 | ses->serverNOS = kzalloc(4 * len, GFP_KERNEL); /* BB this is wrong length FIXME BB */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 226 | if (ses->serverNOS != NULL) { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 227 | cifs_strfromUCS_le(ses->serverNOS, (__le16 *)data, len, |
| 228 | nls_cp); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 229 | if (strncmp(ses->serverNOS, "NT LAN Manager 4", 16) == 0) { |
| 230 | cFYI(1, ("NT4 server")); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 231 | ses->flags |= CIFS_SES_NT4; |
| 232 | } |
| 233 | } |
| 234 | data += 2 * (len + 1); |
| 235 | words_left -= len + 1; |
| 236 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 237 | /* save off server domain */ |
| 238 | len = UniStrnlen((wchar_t *) data, words_left); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 239 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 240 | if (len > words_left) |
| 241 | return rc; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 242 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 243 | if (ses->serverDomain) |
| 244 | kfree(ses->serverDomain); |
| 245 | ses->serverDomain = kzalloc(2 * (len + 1), GFP_KERNEL); /* BB FIXME wrong length */ |
| 246 | if (ses->serverDomain != NULL) { |
| 247 | cifs_strfromUCS_le(ses->serverDomain, (__le16 *)data, len, |
| 248 | nls_cp); |
| 249 | ses->serverDomain[2*len] = 0; |
| 250 | ses->serverDomain[(2*len) + 1] = 0; |
| 251 | } |
| 252 | data += 2 * (len + 1); |
| 253 | words_left -= len + 1; |
| 254 | |
| 255 | cFYI(1, ("words left: %d", words_left)); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 256 | |
| 257 | return rc; |
| 258 | } |
| 259 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 260 | static int decode_ascii_ssetup(char **pbcc_area, int bleft, |
| 261 | struct cifsSesInfo *ses, |
| 262 | const struct nls_table *nls_cp) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 263 | { |
| 264 | int rc = 0; |
| 265 | int len; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 266 | char *bcc_ptr = *pbcc_area; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 267 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 268 | cFYI(1, ("decode sessetup ascii. bleft %d", bleft)); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 269 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 270 | len = strnlen(bcc_ptr, bleft); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 271 | if (len >= bleft) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 272 | return rc; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 273 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 274 | if (ses->serverOS) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 275 | kfree(ses->serverOS); |
| 276 | |
| 277 | ses->serverOS = kzalloc(len + 1, GFP_KERNEL); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 278 | if (ses->serverOS) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 279 | strncpy(ses->serverOS, bcc_ptr, len); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 280 | if (strncmp(ses->serverOS, "OS/2", 4) == 0) { |
| 281 | cFYI(1, ("OS/2 server")); |
Steve French | 9ac00b7 | 2006-09-30 04:13:17 +0000 | [diff] [blame] | 282 | ses->flags |= CIFS_SES_OS2; |
| 283 | } |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 284 | |
| 285 | bcc_ptr += len + 1; |
| 286 | bleft -= len + 1; |
| 287 | |
| 288 | len = strnlen(bcc_ptr, bleft); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 289 | if (len >= bleft) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 290 | return rc; |
| 291 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 292 | if (ses->serverNOS) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 293 | kfree(ses->serverNOS); |
| 294 | |
| 295 | ses->serverNOS = kzalloc(len + 1, GFP_KERNEL); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 296 | if (ses->serverNOS) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 297 | strncpy(ses->serverNOS, bcc_ptr, len); |
| 298 | |
| 299 | bcc_ptr += len + 1; |
| 300 | bleft -= len + 1; |
| 301 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 302 | len = strnlen(bcc_ptr, bleft); |
| 303 | if (len > bleft) |
| 304 | return rc; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 305 | |
Steve French | 9ac00b7 | 2006-09-30 04:13:17 +0000 | [diff] [blame] | 306 | /* No domain field in LANMAN case. Domain is |
| 307 | returned by old servers in the SMB negprot response */ |
| 308 | /* BB For newer servers which do not support Unicode, |
| 309 | but thus do return domain here we could add parsing |
| 310 | for it later, but it is not very important */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 311 | cFYI(1, ("ascii: bytes left %d", bleft)); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 312 | |
| 313 | return rc; |
| 314 | } |
| 315 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 316 | int |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 317 | CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses, int first_time, |
| 318 | const struct nls_table *nls_cp) |
| 319 | { |
| 320 | int rc = 0; |
| 321 | int wct; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 322 | struct smb_hdr *smb_buf; |
| 323 | char *bcc_ptr; |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 324 | char *str_area; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 325 | SESSION_SETUP_ANDX *pSMB; |
| 326 | __u32 capabilities; |
| 327 | int count; |
| 328 | int resp_buf_type = 0; |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 329 | struct kvec iov[2]; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 330 | enum securityEnum type; |
| 331 | __u16 action; |
| 332 | int bytes_remaining; |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 333 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 334 | if (ses == NULL) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 335 | return -EINVAL; |
| 336 | |
| 337 | type = ses->server->secType; |
Steve French | f40c562 | 2006-06-28 00:13:38 +0000 | [diff] [blame] | 338 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 339 | cFYI(1, ("sess setup type %d", type)); |
| 340 | if (type == LANMAN) { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 341 | #ifndef CONFIG_CIFS_WEAK_PW_HASH |
| 342 | /* LANMAN and plaintext are less secure and off by default. |
| 343 | So we make this explicitly be turned on in kconfig (in the |
| 344 | build) and turned on at runtime (changed from the default) |
| 345 | in proc/fs/cifs or via mount parm. Unfortunately this is |
| 346 | needed for old Win (e.g. Win95), some obscure NAS and OS/2 */ |
| 347 | return -EOPNOTSUPP; |
| 348 | #endif |
| 349 | wct = 10; /* lanman 2 style sessionsetup */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 350 | } else if ((type == NTLM) || (type == NTLMv2)) { |
Steve French | 9312f67 | 2006-06-04 22:21:07 +0000 | [diff] [blame] | 351 | /* For NTLMv2 failures eventually may need to retry NTLM */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 352 | wct = 13; /* old style NTLM sessionsetup */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 353 | } else /* same size: negotiate or auth, NTLMSSP or extended security */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 354 | wct = 12; |
| 355 | |
| 356 | rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses, |
| 357 | (void **)&smb_buf); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 358 | if (rc) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 359 | return rc; |
| 360 | |
| 361 | pSMB = (SESSION_SETUP_ANDX *)smb_buf; |
| 362 | |
| 363 | capabilities = cifs_ssetup_hdr(ses, pSMB); |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 364 | |
| 365 | /* we will send the SMB in two pieces, |
| 366 | a fixed length beginning part, and a |
| 367 | second part which will include the strings |
| 368 | and rest of bcc area, in order to avoid having |
| 369 | to do a large buffer 17K allocation */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 370 | iov[0].iov_base = (char *)pSMB; |
| 371 | iov[0].iov_len = smb_buf->smb_buf_length + 4; |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 372 | |
| 373 | /* 2000 big enough to fit max user, domain, NOS name etc. */ |
| 374 | str_area = kmalloc(2000, GFP_KERNEL); |
| 375 | bcc_ptr = str_area; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 376 | |
Steve French | 9ac00b7 | 2006-09-30 04:13:17 +0000 | [diff] [blame] | 377 | ses->flags &= ~CIFS_SES_LANMAN; |
| 378 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 379 | if (type == LANMAN) { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 380 | #ifdef CONFIG_CIFS_WEAK_PW_HASH |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 381 | char lnm_session_key[CIFS_SESS_KEY_SIZE]; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 382 | |
| 383 | /* no capabilities flags in old lanman negotiation */ |
| 384 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 385 | pSMB->old_req.PasswordLength = cpu_to_le16(CIFS_SESS_KEY_SIZE); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 386 | /* BB calculate hash with password */ |
| 387 | /* and copy into bcc */ |
| 388 | |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 389 | calc_lanman_hash(ses, lnm_session_key); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 390 | ses->flags |= CIFS_SES_LANMAN; |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 391 | /* #ifdef CONFIG_CIFS_DEBUG2 |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 392 | cifs_dump_mem("cryptkey: ",ses->server->cryptKey, |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 393 | CIFS_SESS_KEY_SIZE); |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 394 | #endif */ |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 395 | memcpy(bcc_ptr, (char *)lnm_session_key, CIFS_SESS_KEY_SIZE); |
| 396 | bcc_ptr += CIFS_SESS_KEY_SIZE; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 397 | |
| 398 | /* can not sign if LANMAN negotiated so no need |
| 399 | to calculate signing key? but what if server |
| 400 | changed to do higher than lanman dialect and |
| 401 | we reconnected would we ever calc signing_key? */ |
| 402 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 403 | cFYI(1, ("Negotiating LANMAN setting up strings")); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 404 | /* Unicode not allowed for LANMAN dialects */ |
| 405 | ascii_ssetup_strings(&bcc_ptr, ses, nls_cp); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 406 | #endif |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 407 | } else if (type == NTLM) { |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 408 | char ntlm_session_key[CIFS_SESS_KEY_SIZE]; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 409 | |
| 410 | pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities); |
| 411 | pSMB->req_no_secext.CaseInsensitivePasswordLength = |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 412 | cpu_to_le16(CIFS_SESS_KEY_SIZE); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 413 | pSMB->req_no_secext.CaseSensitivePasswordLength = |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 414 | cpu_to_le16(CIFS_SESS_KEY_SIZE); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 415 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 416 | /* calculate session key */ |
| 417 | SMBNTencrypt(ses->password, ses->server->cryptKey, |
| 418 | ntlm_session_key); |
| 419 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 420 | if (first_time) /* should this be moved into common code |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 421 | with similar ntlmv2 path? */ |
Steve French | b609f06 | 2007-07-09 07:55:14 +0000 | [diff] [blame] | 422 | cifs_calculate_mac_key(&ses->server->mac_signing_key, |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 423 | ntlm_session_key, ses->password); |
| 424 | /* copy session key */ |
| 425 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 426 | memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 427 | bcc_ptr += CIFS_SESS_KEY_SIZE; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 428 | memcpy(bcc_ptr, (char *)ntlm_session_key, CIFS_SESS_KEY_SIZE); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 429 | bcc_ptr += CIFS_SESS_KEY_SIZE; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 430 | if (ses->capabilities & CAP_UNICODE) { |
Steve French | 0223cf0 | 2006-06-27 19:50:57 +0000 | [diff] [blame] | 431 | /* unicode strings must be word aligned */ |
| 432 | if (iov[0].iov_len % 2) { |
| 433 | *bcc_ptr = 0; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 434 | bcc_ptr++; |
| 435 | } |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 436 | unicode_ssetup_strings(&bcc_ptr, ses, nls_cp); |
Steve French | 0223cf0 | 2006-06-27 19:50:57 +0000 | [diff] [blame] | 437 | } else |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 438 | ascii_ssetup_strings(&bcc_ptr, ses, nls_cp); |
| 439 | } else if (type == NTLMv2) { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 440 | char *v2_sess_key = |
Steve French | 6d027cf | 2006-06-05 16:26:05 +0000 | [diff] [blame] | 441 | kmalloc(sizeof(struct ntlmv2_resp), GFP_KERNEL); |
Steve French | f64b23a | 2006-06-05 05:27:37 +0000 | [diff] [blame] | 442 | |
| 443 | /* BB FIXME change all users of v2_sess_key to |
| 444 | struct ntlmv2_resp */ |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 445 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 446 | if (v2_sess_key == NULL) { |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 447 | cifs_small_buf_release(smb_buf); |
| 448 | return -ENOMEM; |
| 449 | } |
| 450 | |
| 451 | pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities); |
| 452 | |
| 453 | /* LM2 password would be here if we supported it */ |
| 454 | pSMB->req_no_secext.CaseInsensitivePasswordLength = 0; |
| 455 | /* cpu_to_le16(LM2_SESS_KEY_SIZE); */ |
| 456 | |
| 457 | pSMB->req_no_secext.CaseSensitivePasswordLength = |
Steve French | f64b23a | 2006-06-05 05:27:37 +0000 | [diff] [blame] | 458 | cpu_to_le16(sizeof(struct ntlmv2_resp)); |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 459 | |
| 460 | /* calculate session key */ |
Steve French | 1717ffc | 2006-06-08 05:41:32 +0000 | [diff] [blame] | 461 | setup_ntlmv2_rsp(ses, v2_sess_key, nls_cp); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 462 | if (first_time) /* should this be moved into common code |
| 463 | with similar ntlmv2 path? */ |
Steve French | 7c7b25b | 2006-06-01 19:20:10 +0000 | [diff] [blame] | 464 | /* cifs_calculate_ntlmv2_mac_key(ses->server->mac_signing_key, |
| 465 | response BB FIXME, v2_sess_key); */ |
| 466 | |
| 467 | /* copy session key */ |
| 468 | |
| 469 | /* memcpy(bcc_ptr, (char *)ntlm_session_key,LM2_SESS_KEY_SIZE); |
| 470 | bcc_ptr += LM2_SESS_KEY_SIZE; */ |
Steve French | 3870253 | 2007-07-08 15:40:40 +0000 | [diff] [blame] | 471 | memcpy(bcc_ptr, (char *)v2_sess_key, |
| 472 | sizeof(struct ntlmv2_resp)); |
Steve French | f64b23a | 2006-06-05 05:27:37 +0000 | [diff] [blame] | 473 | bcc_ptr += sizeof(struct ntlmv2_resp); |
| 474 | kfree(v2_sess_key); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 475 | if (ses->capabilities & CAP_UNICODE) { |
| 476 | if (iov[0].iov_len % 2) { |
Steve French | 0223cf0 | 2006-06-27 19:50:57 +0000 | [diff] [blame] | 477 | *bcc_ptr = 0; |
| 478 | } bcc_ptr++; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 479 | unicode_ssetup_strings(&bcc_ptr, ses, nls_cp); |
Steve French | 0223cf0 | 2006-06-27 19:50:57 +0000 | [diff] [blame] | 480 | } else |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 481 | ascii_ssetup_strings(&bcc_ptr, ses, nls_cp); |
| 482 | } else /* NTLMSSP or SPNEGO */ { |
| 483 | pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC; |
| 484 | capabilities |= CAP_EXTENDED_SECURITY; |
| 485 | pSMB->req.Capabilities = cpu_to_le32(capabilities); |
| 486 | /* BB set password lengths */ |
| 487 | } |
| 488 | |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 489 | count = (long) bcc_ptr - (long) str_area; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 490 | smb_buf->smb_buf_length += count; |
| 491 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 492 | BCC_LE(smb_buf) = cpu_to_le16(count); |
| 493 | |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 494 | iov[1].iov_base = str_area; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 495 | iov[1].iov_len = count; |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 496 | rc = SendReceive2(xid, ses, iov, 2 /* num_iovecs */, &resp_buf_type, 0); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 497 | /* SMB request buf freed in SendReceive2 */ |
| 498 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 499 | cFYI(1, ("ssetup rc from sendrecv2 is %d", rc)); |
| 500 | if (rc) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 501 | goto ssetup_exit; |
| 502 | |
| 503 | pSMB = (SESSION_SETUP_ANDX *)iov[0].iov_base; |
| 504 | smb_buf = (struct smb_hdr *)iov[0].iov_base; |
| 505 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 506 | if ((smb_buf->WordCount != 3) && (smb_buf->WordCount != 4)) { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 507 | rc = -EIO; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 508 | cERROR(1, ("bad word count %d", smb_buf->WordCount)); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 509 | goto ssetup_exit; |
| 510 | } |
| 511 | action = le16_to_cpu(pSMB->resp.Action); |
| 512 | if (action & GUEST_LOGIN) |
Steve French | 189acaa | 2006-06-23 02:33:48 +0000 | [diff] [blame] | 513 | cFYI(1, ("Guest login")); /* BB mark SesInfo struct? */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 514 | ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */ |
| 515 | cFYI(1, ("UID = %d ", ses->Suid)); |
| 516 | /* response can have either 3 or 4 word count - Samba sends 3 */ |
| 517 | /* and lanman response is 3 */ |
| 518 | bytes_remaining = BCC(smb_buf); |
| 519 | bcc_ptr = pByteArea(smb_buf); |
| 520 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 521 | if (smb_buf->WordCount == 4) { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 522 | __u16 blob_len; |
| 523 | blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength); |
| 524 | bcc_ptr += blob_len; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 525 | if (blob_len > bytes_remaining) { |
| 526 | cERROR(1, ("bad security blob length %d", blob_len)); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 527 | rc = -EINVAL; |
| 528 | goto ssetup_exit; |
| 529 | } |
| 530 | bytes_remaining -= blob_len; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 531 | } |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 532 | |
| 533 | /* BB check if Unicode and decode strings */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 534 | if (smb_buf->Flags2 & SMBFLG2_UNICODE) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 535 | rc = decode_unicode_ssetup(&bcc_ptr, bytes_remaining, |
| 536 | ses, nls_cp); |
| 537 | else |
Steve French | 63135e0 | 2007-07-17 17:34:02 +0000 | [diff] [blame] | 538 | rc = decode_ascii_ssetup(&bcc_ptr, bytes_remaining, |
| 539 | ses, nls_cp); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 540 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 541 | ssetup_exit: |
Steve French | 750d115 | 2006-06-27 06:28:30 +0000 | [diff] [blame] | 542 | kfree(str_area); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 543 | if (resp_buf_type == CIFS_SMALL_BUFFER) { |
| 544 | cFYI(1, ("ssetup freeing small buf %p", iov[0].iov_base)); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 545 | cifs_small_buf_release(iov[0].iov_base); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 546 | } else if (resp_buf_type == CIFS_LARGE_BUFFER) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 547 | cifs_buf_release(iov[0].iov_base); |
| 548 | |
| 549 | return rc; |
| 550 | } |