2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 2004
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "smb_server/smb_server.h"
22 #include "libcli/raw/libcliraw.h"
23 #include "libcli/raw/raw_proto.h"
24 #include "param/param.h"
28 sign an outgoing packet
30 void smbsrv_sign_packet(struct smbsrv_request *req)
33 /* enable this when packet signing is preventing you working out why valgrind
34 says that data is uninitialised */
35 file_save("pkt.dat", req->out.buffer, req->out.size);
38 switch (req->smb_conn->signing.signing_state) {
39 case SMB_SIGNING_ENGINE_OFF:
42 case SMB_SIGNING_ENGINE_BSRSPYL:
43 /* mark the packet as signed - BEFORE we sign it...*/
44 mark_packet_signed(&req->out);
46 /* I wonder what BSRSPYL stands for - but this is what MS
48 memcpy((req->out.hdr + HDR_SS_FIELD), "BSRSPYL ", 8);
51 case SMB_SIGNING_ENGINE_ON:
53 sign_outgoing_message(&req->out,
54 &req->smb_conn->signing.mac_key,
64 setup the signing key for a connection. Called after authentication succeeds
67 bool smbsrv_setup_signing(struct smbsrv_connection *smb_conn,
68 DATA_BLOB *session_key,
71 if (!set_smb_signing_common(&smb_conn->signing)) {
74 return smbcli_simple_set_signing(smb_conn,
75 &smb_conn->signing, session_key, response);
78 void smbsrv_signing_restart(struct smbsrv_connection *smb_conn,
79 DATA_BLOB *session_key,
82 if (!smb_conn->signing.seen_valid) {
83 DEBUG(5, ("Client did not send a valid signature on "
84 "SPNEGO session setup - ignored, expect good next time\n"));
85 /* force things back on (most clients do not sign this packet)... */
86 smbsrv_setup_signing(smb_conn, session_key, response);
87 smb_conn->signing.next_seq_num = 2;
88 if (smb_conn->signing.mandatory_signing) {
89 DEBUG(5, ("Configured for mandatory signing, 'good packet seen' forced on\n"));
90 /* if this is mandatory, then
91 * pretend we have seen a
92 * valid packet, so we don't
94 smb_conn->signing.seen_valid = true;
99 bool smbsrv_init_signing(struct smbsrv_connection *smb_conn)
101 smb_conn->signing.mac_key = data_blob(NULL, 0);
102 if (!smbcli_set_signing_off(&smb_conn->signing)) {
106 switch (lp_server_signing(smb_conn->lp_ctx)) {
107 case SMB_SIGNING_OFF:
108 smb_conn->signing.allow_smb_signing = false;
110 case SMB_SIGNING_SUPPORTED:
111 smb_conn->signing.allow_smb_signing = true;
113 case SMB_SIGNING_REQUIRED:
114 smb_conn->signing.allow_smb_signing = true;
115 smb_conn->signing.mandatory_signing = true;
117 case SMB_SIGNING_AUTO:
118 if (lp_server_role(smb_conn->lp_ctx) == ROLE_DOMAIN_CONTROLLER) {
119 smb_conn->signing.allow_smb_signing = true;
121 smb_conn->signing.allow_smb_signing = false;
129 allocate a sequence number to a request
131 static void req_signing_alloc_seq_num(struct smbsrv_request *req)
133 req->seq_num = req->smb_conn->signing.next_seq_num;
135 if (req->smb_conn->signing.signing_state != SMB_SIGNING_ENGINE_OFF) {
136 req->smb_conn->signing.next_seq_num += 2;
141 called for requests that do not produce a reply of their own
143 void smbsrv_signing_no_reply(struct smbsrv_request *req)
145 if (req->smb_conn->signing.signing_state != SMB_SIGNING_ENGINE_OFF) {
146 req->smb_conn->signing.next_seq_num--;
150 /***********************************************************
151 SMB signing - Simple implementation - check a MAC sent by client
152 ************************************************************/
154 * Check a packet supplied by the server.
155 * @return false if we had an established signing connection
156 * which had a back checksum, true otherwise
158 bool smbsrv_signing_check_incoming(struct smbsrv_request *req)
162 req_signing_alloc_seq_num(req);
164 switch (req->smb_conn->signing.signing_state)
166 case SMB_SIGNING_ENGINE_OFF:
168 case SMB_SIGNING_ENGINE_BSRSPYL:
169 case SMB_SIGNING_ENGINE_ON:
171 if (req->in.size < (HDR_SS_FIELD + 8)) {
174 good = check_signed_incoming_message(&req->in,
175 &req->smb_conn->signing.mac_key,
178 return signing_good(&req->smb_conn->signing,
179 req->seq_num+1, good);