42669f7232e6dcade075b1354aa4443e2306036e
[metze/samba/wip.git] / source3 / libsmb / auth_generic.c
1 /*
2    NLTMSSP wrappers
3
4    Copyright (C) Andrew Tridgell      2001
5    Copyright (C) Andrew Bartlett 2001-2003,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 #include "includes.h"
22 #include "auth/ntlmssp/ntlmssp.h"
23 #include "auth_generic.h"
24 #include "auth/gensec/gensec.h"
25 #include "auth/credentials/credentials.h"
26 #include "librpc/rpc/dcerpc.h"
27 #include "lib/param/param.h"
28
29 NTSTATUS auth_generic_set_username(struct auth_generic_state *ans,
30                                    const char *user)
31 {
32         cli_credentials_set_username(ans->credentials, user, CRED_SPECIFIED);
33         return NT_STATUS_OK;
34 }
35
36 NTSTATUS auth_generic_set_domain(struct auth_generic_state *ans,
37                                  const char *domain)
38 {
39         cli_credentials_set_domain(ans->credentials, domain, CRED_SPECIFIED);
40         return NT_STATUS_OK;
41 }
42
43 NTSTATUS auth_generic_set_password(struct auth_generic_state *ans,
44                                    const char *password)
45 {
46         cli_credentials_set_password(ans->credentials, password, CRED_SPECIFIED);
47         return NT_STATUS_OK;
48 }
49
50 NTSTATUS auth_generic_client_prepare(TALLOC_CTX *mem_ctx, struct auth_generic_state **auth_generic_state)
51 {
52         struct auth_generic_state *ans;
53         NTSTATUS nt_status;
54
55         struct gensec_settings *gensec_settings;
56         struct loadparm_context *lp_ctx;
57
58         ans = talloc_zero(mem_ctx, struct auth_generic_state);
59         if (!ans) {
60                 DEBUG(0,("auth_generic_start: talloc failed!\n"));
61                 return NT_STATUS_NO_MEMORY;
62         }
63
64         lp_ctx = loadparm_init_s3(ans, loadparm_s3_context());
65         if (lp_ctx == NULL) {
66                 DEBUG(10, ("loadparm_init_s3 failed\n"));
67                 TALLOC_FREE(ans);
68                 return NT_STATUS_INVALID_SERVER_STATE;
69         }
70
71         gensec_settings = lpcfg_gensec_settings(ans, lp_ctx);
72         if (lp_ctx == NULL) {
73                 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
74                 TALLOC_FREE(ans);
75                 return NT_STATUS_NO_MEMORY;
76         }
77
78         gensec_settings->backends = talloc_zero_array(gensec_settings, struct gensec_security_ops *, 2);
79         if (gensec_settings->backends == NULL) {
80                 TALLOC_FREE(ans);
81                 return NT_STATUS_NO_MEMORY;
82         }
83
84         gensec_settings->backends[0] = &gensec_ntlmssp3_client_ops;
85
86         nt_status = gensec_client_start(ans, &ans->gensec_security, gensec_settings);
87
88         if (!NT_STATUS_IS_OK(nt_status)) {
89                 TALLOC_FREE(ans);
90                 return nt_status;
91         }
92
93         ans->credentials = cli_credentials_init(ans);
94         if (!ans->credentials) {
95                 TALLOC_FREE(ans);
96                 return NT_STATUS_NO_MEMORY;
97         }
98
99         cli_credentials_guess(ans->credentials, lp_ctx);
100
101         talloc_unlink(ans, lp_ctx);
102         talloc_unlink(ans, gensec_settings);
103
104         *auth_generic_state = ans;
105         return NT_STATUS_OK;
106 }
107
108 NTSTATUS auth_generic_client_start(struct auth_generic_state *ans, const char *oid)
109 {
110         NTSTATUS status;
111
112         /* Transfer the credentials to gensec */
113         status = gensec_set_credentials(ans->gensec_security, ans->credentials);
114         if (!NT_STATUS_IS_OK(status)) {
115                 DEBUG(1, ("Failed to set GENSEC credentials: %s\n",
116                           nt_errstr(status)));
117                 return status;
118         }
119         talloc_unlink(ans, ans->credentials);
120         ans->credentials = NULL;
121
122         status = gensec_start_mech_by_oid(ans->gensec_security,
123                                           oid);
124         if (!NT_STATUS_IS_OK(status)) {
125                 return status;
126         }
127
128         return NT_STATUS_OK;
129 }
130
131 NTSTATUS auth_generic_client_start_by_authtype(struct auth_generic_state *ans,
132                                                uint8_t auth_type,
133                                                uint8_t auth_level)
134 {
135         NTSTATUS status;
136
137         /* Transfer the credentials to gensec */
138         status = gensec_set_credentials(ans->gensec_security, ans->credentials);
139         if (!NT_STATUS_IS_OK(status)) {
140                 DEBUG(1, ("Failed to set GENSEC credentials: %s\n",
141                           nt_errstr(status)));
142                 return status;
143         }
144         talloc_unlink(ans, ans->credentials);
145         ans->credentials = NULL;
146
147         status = gensec_start_mech_by_authtype(ans->gensec_security,
148                                                auth_type, auth_level);
149         if (!NT_STATUS_IS_OK(status)) {
150                 return status;
151         }
152
153         return NT_STATUS_OK;
154 }