s4:samba-tool: add simple command "group list"
[Samba/vl.git] / source3 / libsmb / auth_generic.c
blobdbd87fff2bcafedb698614627a435508d9c6a865
1 /*
2 NLTMSSP wrappers
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Andrew Bartlett 2001-2003,2011
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.
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.
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/>.
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 #include "librpc/crypto/gse.h"
30 NTSTATUS auth_generic_set_username(struct auth_generic_state *ans,
31 const char *user)
33 cli_credentials_set_username(ans->credentials, user, CRED_SPECIFIED);
34 return NT_STATUS_OK;
37 NTSTATUS auth_generic_set_domain(struct auth_generic_state *ans,
38 const char *domain)
40 cli_credentials_set_domain(ans->credentials, domain, CRED_SPECIFIED);
41 return NT_STATUS_OK;
44 NTSTATUS auth_generic_set_password(struct auth_generic_state *ans,
45 const char *password)
47 cli_credentials_set_password(ans->credentials, password, CRED_SPECIFIED);
48 return NT_STATUS_OK;
51 NTSTATUS auth_generic_client_prepare(TALLOC_CTX *mem_ctx, struct auth_generic_state **auth_generic_state)
53 struct auth_generic_state *ans;
54 NTSTATUS nt_status;
55 size_t idx = 0;
56 struct gensec_settings *gensec_settings;
57 struct loadparm_context *lp_ctx;
59 ans = talloc_zero(mem_ctx, struct auth_generic_state);
60 if (!ans) {
61 DEBUG(0,("auth_generic_start: talloc failed!\n"));
62 return NT_STATUS_NO_MEMORY;
65 lp_ctx = loadparm_init_s3(ans, loadparm_s3_context());
66 if (lp_ctx == NULL) {
67 DEBUG(10, ("loadparm_init_s3 failed\n"));
68 TALLOC_FREE(ans);
69 return NT_STATUS_INVALID_SERVER_STATE;
72 gensec_settings = lpcfg_gensec_settings(ans, lp_ctx);
73 if (lp_ctx == NULL) {
74 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
75 TALLOC_FREE(ans);
76 return NT_STATUS_NO_MEMORY;
79 gensec_settings->backends = talloc_zero_array(gensec_settings,
80 struct gensec_security_ops *, 4);
81 if (gensec_settings->backends == NULL) {
82 TALLOC_FREE(ans);
83 return NT_STATUS_NO_MEMORY;
86 gensec_settings->backends[idx++] = &gensec_ntlmssp3_client_ops;
88 #if defined(HAVE_KRB5)
89 gensec_settings->backends[idx++] = &gensec_gse_krb5_security_ops;
90 #endif
92 gensec_init();
93 gensec_settings->backends[idx++] = gensec_security_by_oid(NULL,
94 GENSEC_OID_SPNEGO);
96 nt_status = gensec_client_start(ans, &ans->gensec_security, gensec_settings);
98 if (!NT_STATUS_IS_OK(nt_status)) {
99 TALLOC_FREE(ans);
100 return nt_status;
103 ans->credentials = cli_credentials_init(ans);
104 if (!ans->credentials) {
105 TALLOC_FREE(ans);
106 return NT_STATUS_NO_MEMORY;
109 cli_credentials_guess(ans->credentials, lp_ctx);
111 talloc_unlink(ans, lp_ctx);
112 talloc_unlink(ans, gensec_settings);
114 *auth_generic_state = ans;
115 return NT_STATUS_OK;
118 NTSTATUS auth_generic_client_start(struct auth_generic_state *ans, const char *oid)
120 NTSTATUS status;
122 /* Transfer the credentials to gensec */
123 status = gensec_set_credentials(ans->gensec_security, ans->credentials);
124 if (!NT_STATUS_IS_OK(status)) {
125 DEBUG(1, ("Failed to set GENSEC credentials: %s\n",
126 nt_errstr(status)));
127 return status;
129 talloc_unlink(ans, ans->credentials);
130 ans->credentials = NULL;
132 status = gensec_start_mech_by_oid(ans->gensec_security,
133 oid);
134 if (!NT_STATUS_IS_OK(status)) {
135 return status;
138 return NT_STATUS_OK;
141 NTSTATUS auth_generic_client_start_by_authtype(struct auth_generic_state *ans,
142 uint8_t auth_type,
143 uint8_t auth_level)
145 NTSTATUS status;
147 /* Transfer the credentials to gensec */
148 status = gensec_set_credentials(ans->gensec_security, ans->credentials);
149 if (!NT_STATUS_IS_OK(status)) {
150 DEBUG(1, ("Failed to set GENSEC credentials: %s\n",
151 nt_errstr(status)));
152 return status;
154 talloc_unlink(ans, ans->credentials);
155 ans->credentials = NULL;
157 status = gensec_start_mech_by_authtype(ans->gensec_security,
158 auth_type, auth_level);
159 if (!NT_STATUS_IS_OK(status)) {
160 return status;
163 return NT_STATUS_OK;