s3-ntlmssp Add mem_ctx argument to auth_ntlmssp_update
[mat/samba.git] / source3 / rpc_server / dcesrv_ntlmssp.c
1 /*
2  *  NTLMSSP Acceptor
3  *  DCERPC Server functions
4  *  Copyright (C) Simo Sorce 2010.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20
21 #include "includes.h"
22 #include "rpc_server/dcesrv_ntlmssp.h"
23 #include "../libcli/auth/ntlmssp.h"
24 #include "ntlmssp_wrap.h"
25 #include "auth.h"
26
27 NTSTATUS ntlmssp_server_auth_start(TALLOC_CTX *mem_ctx,
28                                    bool do_sign,
29                                    bool do_seal,
30                                    bool is_dcerpc,
31                                    DATA_BLOB *token_in,
32                                    DATA_BLOB *token_out,
33                                    const struct tsocket_address *remote_address,
34                                    struct auth_ntlmssp_state **ctx)
35 {
36         struct auth_ntlmssp_state *a = NULL;
37         NTSTATUS status;
38
39         status = auth_ntlmssp_start(remote_address, &a);
40         if (!NT_STATUS_IS_OK(status)) {
41                 DEBUG(0, (__location__ ": auth_ntlmssp_start failed: %s\n",
42                           nt_errstr(status)));
43                 return status;
44         }
45
46         /* Clear flags, then set them according to requested flags */
47         auth_ntlmssp_and_flags(a, ~(NTLMSSP_NEGOTIATE_SIGN |
48                                         NTLMSSP_NEGOTIATE_SEAL));
49
50         if (do_sign) {
51                 auth_ntlmssp_or_flags(a, NTLMSSP_NEGOTIATE_SIGN);
52         }
53         if (do_seal) {
54                 /* Always implies both sign and seal for ntlmssp */
55                 auth_ntlmssp_or_flags(a, NTLMSSP_NEGOTIATE_SIGN |
56                                          NTLMSSP_NEGOTIATE_SEAL);
57         }
58
59         status = auth_ntlmssp_update(a, mem_ctx, *token_in, token_out);
60         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
61                 DEBUG(0, (__location__ ": auth_ntlmssp_update failed: %s\n",
62                           nt_errstr(status)));
63                 goto done;
64         }
65
66         /* steal ntlmssp context too */
67         *ctx = talloc_move(mem_ctx, &a);
68
69         status = NT_STATUS_OK;
70
71 done:
72         if (!NT_STATUS_IS_OK(status)) {
73                 TALLOC_FREE(a);
74         }
75
76         return status;
77 }
78
79 NTSTATUS ntlmssp_server_step(struct auth_ntlmssp_state *ctx,
80                              TALLOC_CTX *mem_ctx,
81                              DATA_BLOB *token_in,
82                              DATA_BLOB *token_out)
83 {
84         NTSTATUS status;
85
86         /* this has to be done as root in order to verify the password */
87         become_root();
88         status = auth_ntlmssp_update(ctx, mem_ctx, *token_in, token_out);
89         unbecome_root();
90
91         return status;
92 }
93
94 NTSTATUS ntlmssp_server_check_flags(struct auth_ntlmssp_state *ctx,
95                                     bool do_sign, bool do_seal)
96 {
97         if (do_sign && !auth_ntlmssp_negotiated_sign(ctx)) {
98                 DEBUG(1, (__location__ "Integrity was requested but client "
99                           "failed to negotiate signing.\n"));
100                 return NT_STATUS_ACCESS_DENIED;
101         }
102
103         if (do_seal && !auth_ntlmssp_negotiated_seal(ctx)) {
104                 DEBUG(1, (__location__ "Privacy was requested but client "
105                           "failed to negotiate sealing.\n"));
106                 return NT_STATUS_ACCESS_DENIED;
107         }
108
109         return NT_STATUS_OK;
110 }
111
112 NTSTATUS ntlmssp_server_get_user_info(struct auth_ntlmssp_state *ctx,
113                                       TALLOC_CTX *mem_ctx,
114                                       struct auth_session_info **session_info)
115 {
116         NTSTATUS status;
117
118         status = auth_ntlmssp_steal_session_info(mem_ctx, ctx, session_info);
119         if (!NT_STATUS_IS_OK(status)) {
120                 DEBUG(1, (__location__ ": Failed to get authenticated user "
121                           "info: %s\n", nt_errstr(status)));
122                 return status;
123         }
124
125         DEBUG(5, (__location__ "OK: user: %s domain: %s\n",
126                   (*session_info)->info->account_name,
127                   (*session_info)->info->domain_name));
128
129         return NT_STATUS_OK;
130 }