s3-auth use gensec directly rather than via auth_generic_state
[kai/samba.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 "ntlmssp_wrap.h"
25 #include "auth.h"
26 #include "auth/gensec/gensec.h"
27
28 NTSTATUS auth_generic_server_start(TALLOC_CTX *mem_ctx,
29                                    const char *oid,
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 gensec_security *gensec_security = NULL;
39         NTSTATUS status;
40
41         status = auth_generic_prepare(talloc_tos(), remote_address, &gensec_security);
42         if (!NT_STATUS_IS_OK(status)) {
43                 DEBUG(0, (__location__ ": auth_generic_prepare failed: %s\n",
44                           nt_errstr(status)));
45                 return status;
46         }
47
48         if (do_sign) {
49                 gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
50         }
51         if (do_seal) {
52                 gensec_want_feature(gensec_security, GENSEC_FEATURE_SIGN);
53                 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
54         }
55
56         if (is_dcerpc) {
57                 gensec_want_feature(gensec_security, GENSEC_FEATURE_DCE_STYLE);
58         }
59
60         status = gensec_start_mech_by_oid(gensec_security, oid);
61         if (!NT_STATUS_IS_OK(status)) {
62                 DEBUG(0, (__location__ ": auth_generic_start failed: %s\n",
63                           nt_errstr(status)));
64                 TALLOC_FREE(gensec_security);
65                 return status;
66         }
67
68         status = gensec_update(gensec_security, mem_ctx, NULL, *token_in, token_out);
69         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
70                 DEBUG(2, (__location__ ": gensec_update failed: %s\n",
71                           nt_errstr(status)));
72                 TALLOC_FREE(gensec_security);
73                 return status;
74         }
75
76         /* steal gensec context to the caller */
77         *ctx = talloc_move(mem_ctx, &gensec_security);
78         return NT_STATUS_OK;
79 }
80
81 NTSTATUS auth_generic_server_authtype_start(TALLOC_CTX *mem_ctx,
82                                             uint8_t auth_type, uint8_t auth_level,
83                                             DATA_BLOB *token_in,
84                                             DATA_BLOB *token_out,
85                                             const struct tsocket_address *remote_address,
86                                             struct gensec_security **ctx)
87 {
88         struct gensec_security *gensec_security = NULL;
89         NTSTATUS status;
90
91         status = auth_generic_prepare(talloc_tos(), remote_address, &gensec_security);
92         if (!NT_STATUS_IS_OK(status)) {
93                 DEBUG(0, (__location__ ": auth_generic_prepare failed: %s\n",
94                           nt_errstr(status)));
95                 return status;
96         }
97
98         status = gensec_start_mech_by_authtype(gensec_security, auth_type, auth_level);
99         if (!NT_STATUS_IS_OK(status)) {
100                 DEBUG(0, (__location__ ": auth_generic_start failed: %s\n",
101                           nt_errstr(status)));
102                 TALLOC_FREE(gensec_security);
103                 return status;
104         }
105
106         status = gensec_update(gensec_security, mem_ctx, NULL, *token_in, token_out);
107         if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
108                 DEBUG(2, (__location__ ": gensec_update failed: %s\n",
109                           nt_errstr(status)));
110                 TALLOC_FREE(gensec_security);
111                 return status;
112         }
113
114         /* steal gensec context to the caller */
115         *ctx = talloc_move(mem_ctx, &gensec_security);
116         return NT_STATUS_OK;
117 }
118
119 NTSTATUS auth_generic_server_step(struct gensec_security *gensec_security,
120                              TALLOC_CTX *mem_ctx,
121                              DATA_BLOB *token_in,
122                              DATA_BLOB *token_out)
123 {
124         NTSTATUS status;
125
126         /* this has to be done as root in order to verify the password */
127         become_root();
128         status = gensec_update(gensec_security, mem_ctx, NULL, *token_in, token_out);
129         unbecome_root();
130
131         return status;
132 }
133
134 NTSTATUS auth_generic_server_check_flags(struct gensec_security *gensec_security,
135                                     bool do_sign, bool do_seal)
136 {
137         if (do_sign && !gensec_have_feature(gensec_security, GENSEC_FEATURE_SIGN)) {
138                 DEBUG(1, (__location__ "Integrity was requested but client "
139                           "failed to negotiate signing.\n"));
140                 return NT_STATUS_ACCESS_DENIED;
141         }
142
143         if (do_seal && !gensec_have_feature(gensec_security, GENSEC_FEATURE_SEAL)) {
144                 DEBUG(1, (__location__ "Privacy was requested but client "
145                           "failed to negotiate sealing.\n"));
146                 return NT_STATUS_ACCESS_DENIED;
147         }
148
149         return NT_STATUS_OK;
150 }
151
152 NTSTATUS auth_generic_server_get_user_info(struct gensec_security *gensec_security,
153                                       TALLOC_CTX *mem_ctx,
154                                       struct auth_session_info **session_info)
155 {
156         NTSTATUS status;
157
158         status = gensec_session_info(gensec_security, mem_ctx, session_info);
159         if (!NT_STATUS_IS_OK(status)) {
160                 DEBUG(1, (__location__ ": Failed to get authenticated user "
161                           "info: %s\n", nt_errstr(status)));
162                 return status;
163         }
164
165         DEBUG(5, (__location__ "OK: user: %s domain: %s\n",
166                   (*session_info)->info->account_name,
167                   (*session_info)->info->domain_name));
168
169         return NT_STATUS_OK;
170 }