242695e0fd68ce14d5f19925c37a04521bc7a2a4
[mat/samba.git] / source3 / rpc_server / dcesrv_ntlmssp.c
1 /*
2  *  NTLMSSP Acceptor
3  *  DCERPC Server functions
4  *  Copyright (C) Simo Sorce 2010.
5  *  Copyright (C) Andrew Bartlett 2011.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "includes.h"
23 #include "rpc_server/dcesrv_ntlmssp.h"
24 #include "../auth/ntlmssp/ntlmssp.h"
25 #include "ntlmssp_wrap.h"
26 #include "auth.h"
27 #include "auth/gensec/gensec.h"
28
29 NTSTATUS ntlmssp_server_auth_start(TALLOC_CTX *mem_ctx,
30                                    bool do_sign,
31                                    bool do_seal,
32                                    bool is_dcerpc,
33                                    DATA_BLOB *token_in,
34                                    DATA_BLOB *token_out,
35                                    const struct tsocket_address *remote_address,
36                                    struct gensec_security **ctx)
37 {
38         struct auth_ntlmssp_state *a = NULL;
39         NTSTATUS status;
40
41         status = auth_ntlmssp_prepare(remote_address, &a);
42         if (!NT_STATUS_IS_OK(status)) {
43                 DEBUG(0, (__location__ ": auth_ntlmssp_prepare failed: %s\n",
44                           nt_errstr(status)));
45                 return status;
46         }
47
48         if (do_sign) {
49                 gensec_want_feature(a->gensec_security, GENSEC_FEATURE_SIGN);
50         }
51         if (do_seal) {
52                 /* Always implies both sign and seal for ntlmssp */
53                 gensec_want_feature(a->gensec_security, GENSEC_FEATURE_SEAL);
54         }
55
56         status = auth_ntlmssp_start(a);
57         if (!NT_STATUS_IS_OK(status)) {
58                 DEBUG(0, (__location__ ": auth_ntlmssp_start failed: %s\n",
59                           nt_errstr(status)));
60                 return status;
61         }
62
63         status = gensec_update(a->gensec_security, mem_ctx, NULL, *token_in, token_out);
64         if (!NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
65                 DEBUG(0, (__location__ ": auth_ntlmssp_update failed: %s\n",
66                           nt_errstr(status)));
67                 goto done;
68         }
69
70         /* steal ntlmssp context too */
71         *ctx = talloc_move(mem_ctx, &a->gensec_security);
72
73         status = NT_STATUS_OK;
74
75 done:
76         TALLOC_FREE(a);
77
78         return status;
79 }
80
81 NTSTATUS ntlmssp_server_step(struct gensec_security *gensec_security,
82                              TALLOC_CTX *mem_ctx,
83                              DATA_BLOB *token_in,
84                              DATA_BLOB *token_out)
85 {
86         NTSTATUS status;
87
88         /* this has to be done as root in order to verify the password */
89         become_root();
90         status = gensec_update(gensec_security, mem_ctx, NULL, *token_in, token_out);
91         unbecome_root();
92
93         return status;
94 }
95
96 NTSTATUS ntlmssp_server_check_flags(struct gensec_security *gensec_security,
97                                     bool do_sign, bool do_seal)
98 {
99         if (do_sign && !gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
100                 DEBUG(1, (__location__ "Integrity was requested but client "
101                           "failed to negotiate signing.\n"));
102                 return NT_STATUS_ACCESS_DENIED;
103         }
104
105         if (do_seal && !gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
106                 DEBUG(1, (__location__ "Privacy was requested but client "
107                           "failed to negotiate sealing.\n"));
108                 return NT_STATUS_ACCESS_DENIED;
109         }
110
111         return NT_STATUS_OK;
112 }
113
114 NTSTATUS ntlmssp_server_get_user_info(struct gensec_security *gensec_security,
115                                       TALLOC_CTX *mem_ctx,
116                                       struct auth_session_info **session_info)
117 {
118         NTSTATUS status;
119
120         status = gensec_session_info(gensec_security, mem_ctx, session_info);
121         if (!NT_STATUS_IS_OK(status)) {
122                 DEBUG(1, (__location__ ": Failed to get authenticated user "
123                           "info: %s\n", nt_errstr(status)));
124                 return status;
125         }
126
127         DEBUG(5, (__location__ "OK: user: %s domain: %s\n",
128                   (*session_info)->info->account_name,
129                   (*session_info)->info->domain_name));
130
131         return NT_STATUS_OK;
132 }