LIBCLI_SMB2: Depend on cli_smb_common rather than libsmb.
[ab/samba.git/.git] / source3 / rpc_server / dcesrv_auth_generic.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_auth_generic.h"
24 #include "auth.h"
25 #include "auth/gensec/gensec.h"
26
27 NTSTATUS auth_generic_server_authtype_start(TALLOC_CTX *mem_ctx,
28                                             uint8_t auth_type, uint8_t auth_level,
29                                             DATA_BLOB *token_in,
30                                             DATA_BLOB *token_out,
31                                             const struct tsocket_address *remote_address,
32                                             struct gensec_security **ctx)
33 {
34         struct gensec_security *gensec_security = NULL;
35         NTSTATUS status;
36
37         status = auth_generic_prepare(talloc_tos(), remote_address, &gensec_security);
38         if (!NT_STATUS_IS_OK(status)) {
39                 DEBUG(0, (__location__ ": auth_generic_prepare failed: %s\n",
40                           nt_errstr(status)));
41                 return status;
42         }
43
44         status = gensec_start_mech_by_authtype(gensec_security, auth_type, auth_level);
45         if (!NT_STATUS_IS_OK(status)) {
46                 DEBUG(0, (__location__ ": auth_generic_start failed: %s\n",
47                           nt_errstr(status)));
48                 TALLOC_FREE(gensec_security);
49                 return status;
50         }
51
52         status = gensec_update(gensec_security, mem_ctx, NULL, *token_in, token_out);
53         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
54                 DEBUG(2, (__location__ ": gensec_update failed: %s\n",
55                           nt_errstr(status)));
56                 TALLOC_FREE(gensec_security);
57                 return status;
58         }
59
60         /* steal gensec context to the caller */
61         *ctx = talloc_move(mem_ctx, &gensec_security);
62         return NT_STATUS_OK;
63 }
64
65 NTSTATUS auth_generic_server_step(struct gensec_security *gensec_security,
66                              TALLOC_CTX *mem_ctx,
67                              DATA_BLOB *token_in,
68                              DATA_BLOB *token_out)
69 {
70         NTSTATUS status;
71
72         /* this has to be done as root in order to verify the password */
73         become_root();
74         status = gensec_update(gensec_security, mem_ctx, NULL, *token_in, token_out);
75         unbecome_root();
76
77         return status;
78 }
79
80 NTSTATUS auth_generic_server_check_flags(struct gensec_security *gensec_security,
81                                     bool do_sign, bool do_seal)
82 {
83         if (do_sign && !gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
84                 DEBUG(1, (__location__ "Integrity was requested but client "
85                           "failed to negotiate signing.\n"));
86                 return NT_STATUS_ACCESS_DENIED;
87         }
88
89         if (do_seal && !gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
90                 DEBUG(1, (__location__ "Privacy was requested but client "
91                           "failed to negotiate sealing.\n"));
92                 return NT_STATUS_ACCESS_DENIED;
93         }
94
95         return NT_STATUS_OK;
96 }
97
98 NTSTATUS auth_generic_server_get_user_info(struct gensec_security *gensec_security,
99                                       TALLOC_CTX *mem_ctx,
100                                       struct auth_session_info **session_info)
101 {
102         NTSTATUS status;
103
104         status = gensec_session_info(gensec_security, mem_ctx, session_info);
105         if (!NT_STATUS_IS_OK(status)) {
106                 DEBUG(1, (__location__ ": Failed to get authenticated user "
107                           "info: %s\n", nt_errstr(status)));
108                 return status;
109         }
110
111         DEBUG(5, (__location__ "OK: user: %s domain: %s\n",
112                   (*session_info)->info->account_name,
113                   (*session_info)->info->domain_name));
114
115         return NT_STATUS_OK;
116 }