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