auth:creds: Introduce CRED_SMB_CONF
[samba.git] / auth / credentials / credentials.h
1 /* 
2    samba -- Unix SMB/CIFS implementation.
3
4    Client credentials structure
5
6    Copyright (C) Jelmer Vernooij 2004-2006
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22 #ifndef __CREDENTIALS_H__
23 #define __CREDENTIALS_H__
24
25 #include "../lib/util/time.h"
26 #include "../lib/util/data_blob.h"
27 #include "librpc/gen_ndr/misc.h"
28
29 struct cli_credentials;
30 struct ccache_container;
31 struct tevent_context;
32 struct netlogon_creds_CredentialState;
33 struct ldb_context;
34 struct ldb_message;
35 struct loadparm_context;
36 struct ccache_container;
37 struct gssapi_creds_container;
38 struct smb_krb5_context;
39 struct keytab_container;
40 struct db_context;
41
42 /* In order of priority */
43 enum credentials_obtained { 
44         CRED_UNINITIALISED = 0,  /* We don't even have a guess yet */
45         CRED_SMB_CONF,           /* Current value should be used, which comes from smb.conf */
46         CRED_CALLBACK,           /* Callback should be used to obtain value */
47         CRED_GUESS_ENV,          /* Current value should be used, which was guessed */
48         CRED_GUESS_FILE,         /* A guess from a file (or file pointed at in env variable) */
49         CRED_CALLBACK_RESULT,    /* Value was obtained from a callback */
50         CRED_SPECIFIED           /* Was explicitly specified on the command-line */
51 };
52
53 enum credentials_use_kerberos {
54         CRED_AUTO_USE_KERBEROS = 0, /* Default, we try kerberos if available */
55         CRED_DONT_USE_KERBEROS,     /* Sometimes trying kerberos just does 'bad things', so don't */
56         CRED_MUST_USE_KERBEROS      /* Sometimes administrators are paranoid, so always do kerberos */
57 };
58
59 enum credentials_krb_forwardable {
60         CRED_AUTO_KRB_FORWARDABLE = 0, /* Default, follow library defaults */
61         CRED_NO_KRB_FORWARDABLE,       /* not forwardable */
62         CRED_FORCE_KRB_FORWARDABLE     /* forwardable */
63 };
64
65 #define CLI_CRED_NTLM2       0x01
66 #define CLI_CRED_NTLMv2_AUTH 0x02
67 #define CLI_CRED_LANMAN_AUTH 0x04
68 #define CLI_CRED_NTLM_AUTH   0x08
69 #define CLI_CRED_CLEAR_AUTH  0x10   /* TODO:  Push cleartext auth with this flag */
70
71 const char *cli_credentials_get_workstation(struct cli_credentials *cred);
72 bool cli_credentials_set_workstation(struct cli_credentials *cred, 
73                                      const char *val, 
74                                      enum credentials_obtained obtained);
75 bool cli_credentials_is_anonymous(struct cli_credentials *cred);
76 struct cli_credentials *cli_credentials_init(TALLOC_CTX *mem_ctx);
77 void cli_credentials_set_anonymous(struct cli_credentials *cred);
78 bool cli_credentials_wrong_password(struct cli_credentials *cred);
79 const char *cli_credentials_get_password(struct cli_credentials *cred);
80 void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, 
81                                               const char **username, 
82                                               const char **domain);
83 NTSTATUS cli_credentials_get_ntlm_response(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, 
84                                            int *flags,
85                                            DATA_BLOB challenge,
86                                            const NTTIME *server_timestamp,
87                                            DATA_BLOB target_info,
88                                            DATA_BLOB *_lm_response, DATA_BLOB *_nt_response, 
89                                            DATA_BLOB *_lm_session_key, DATA_BLOB *_session_key);
90 const char *cli_credentials_get_realm(struct cli_credentials *cred);
91 const char *cli_credentials_get_username(struct cli_credentials *cred);
92 int cli_credentials_get_krb5_context(struct cli_credentials *cred, 
93                                      struct loadparm_context *lp_ctx,
94                                      struct smb_krb5_context **smb_krb5_context);
95 int cli_credentials_get_ccache(struct cli_credentials *cred, 
96                                struct tevent_context *event_ctx,
97                                struct loadparm_context *lp_ctx,
98                                struct ccache_container **ccc,
99                                const char **error_string);
100 int cli_credentials_get_named_ccache(struct cli_credentials *cred, 
101                                      struct tevent_context *event_ctx,
102                                      struct loadparm_context *lp_ctx,
103                                      char *ccache_name,
104                                      struct ccache_container **ccc, const char **error_string);
105 bool cli_credentials_failed_kerberos_login(struct cli_credentials *cred,
106                                            const char *principal,
107                                            unsigned int *count);
108 int cli_credentials_get_keytab(struct cli_credentials *cred, 
109                                struct loadparm_context *lp_ctx,
110                                struct keytab_container **_ktc);
111 const char *cli_credentials_get_domain(struct cli_credentials *cred);
112 struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred);
113 void cli_credentials_set_machine_account_pending(struct cli_credentials *cred,
114                                                  struct loadparm_context *lp_ctx);
115 void cli_credentials_set_conf(struct cli_credentials *cred, 
116                               struct loadparm_context *lp_ctx);
117 char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx);
118 int cli_credentials_get_server_gss_creds(struct cli_credentials *cred, 
119                                          struct loadparm_context *lp_ctx,
120                                          struct gssapi_creds_container **_gcc);
121 int cli_credentials_get_client_gss_creds(struct cli_credentials *cred, 
122                                          struct tevent_context *event_ctx,
123                                          struct loadparm_context *lp_ctx,
124                                          struct gssapi_creds_container **_gcc,
125                                          const char **error_string);
126 void cli_credentials_set_forced_sasl_mech(struct cli_credentials *creds,
127                                           const char *sasl_mech);
128 void cli_credentials_set_kerberos_state(struct cli_credentials *creds, 
129                                         enum credentials_use_kerberos use_kerberos);
130 void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
131                                          enum credentials_krb_forwardable krb_forwardable);
132 bool cli_credentials_set_domain(struct cli_credentials *cred, 
133                                 const char *val, 
134                                 enum credentials_obtained obtained);
135 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
136                                          const char *(*domain_cb) (struct cli_credentials *));
137 bool cli_credentials_set_username(struct cli_credentials *cred, 
138                                   const char *val, enum credentials_obtained obtained);
139 bool cli_credentials_set_username_callback(struct cli_credentials *cred,
140                                   const char *(*username_cb) (struct cli_credentials *));
141 bool cli_credentials_set_principal(struct cli_credentials *cred, 
142                                    const char *val, 
143                                    enum credentials_obtained obtained);
144 bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
145                                   const char *(*principal_cb) (struct cli_credentials *));
146 bool cli_credentials_set_password(struct cli_credentials *cred, 
147                                   const char *val, 
148                                   enum credentials_obtained obtained);
149 struct cli_credentials *cli_credentials_init_anon(TALLOC_CTX *mem_ctx);
150 void cli_credentials_parse_string(struct cli_credentials *credentials, const char *data, enum credentials_obtained obtained);
151 struct samr_Password *cli_credentials_get_nt_hash(struct cli_credentials *cred,
152                                                   TALLOC_CTX *mem_ctx);
153 struct samr_Password *cli_credentials_get_old_nt_hash(struct cli_credentials *cred,
154                                                       TALLOC_CTX *mem_ctx);
155 bool cli_credentials_set_realm(struct cli_credentials *cred, 
156                                const char *val, 
157                                enum credentials_obtained obtained);
158 void cli_credentials_set_secure_channel_type(struct cli_credentials *cred,
159                                      enum netr_SchannelType secure_channel_type);
160 void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
161                                                              time_t last_change_time);
162 void cli_credentials_set_netlogon_creds(
163         struct cli_credentials *cred,
164         const struct netlogon_creds_CredentialState *netlogon_creds);
165 NTSTATUS cli_credentials_set_krb5_context(struct cli_credentials *cred, 
166                                           struct smb_krb5_context *smb_krb5_context);
167 NTSTATUS cli_credentials_set_stored_principal(struct cli_credentials *cred,
168                                               struct loadparm_context *lp_ctx,
169                                               const char *serviceprincipal);
170 NTSTATUS cli_credentials_set_machine_account(struct cli_credentials *cred,
171                                              struct loadparm_context *lp_ctx);
172 /**
173  * Fill in credentials for the machine trust account, from the
174  * secrets.ldb or passed in handle to secrets.tdb (perhaps in CTDB).
175  *
176  * This version is used in parts of the code that can link in the
177  * CTDB dbwrap backend, by passing down the already open handle.
178  *
179  * @param cred Credentials structure to fill in
180  * @param db_ctx dbwrap context for secrets.tdb
181  * @retval NTSTATUS error detailing any failure
182  */
183 NTSTATUS cli_credentials_set_machine_account_db_ctx(struct cli_credentials *cred,
184                                                     struct loadparm_context *lp_ctx,
185                                                     struct db_context *db_ctx);
186
187 bool cli_credentials_authentication_requested(struct cli_credentials *cred);
188 void cli_credentials_guess(struct cli_credentials *cred,
189                            struct loadparm_context *lp_ctx);
190 bool cli_credentials_set_bind_dn(struct cli_credentials *cred, 
191                                  const char *bind_dn);
192 const char *cli_credentials_get_bind_dn(struct cli_credentials *cred);
193 bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained);
194 char *cli_credentials_get_unparsed_name(struct cli_credentials *credentials, TALLOC_CTX *mem_ctx);
195 bool cli_credentials_set_password_callback(struct cli_credentials *cred,
196                                            const char *(*password_cb) (struct cli_credentials *));
197 enum netr_SchannelType cli_credentials_get_secure_channel_type(struct cli_credentials *cred);
198 time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred);
199 void cli_credentials_set_kvno(struct cli_credentials *cred,
200                               int kvno);
201 bool cli_credentials_set_utf16_password(struct cli_credentials *cred,
202                                         const DATA_BLOB *password_utf16,
203                                         enum credentials_obtained obtained);
204 bool cli_credentials_set_old_utf16_password(struct cli_credentials *cred,
205                                             const DATA_BLOB *password_utf16);
206 void cli_credentials_set_password_will_be_nt_hash(struct cli_credentials *cred,
207                                                   bool val);
208 bool cli_credentials_set_nt_hash(struct cli_credentials *cred,
209                                  const struct samr_Password *nt_hash, 
210                                  enum credentials_obtained obtained);
211 bool cli_credentials_set_old_nt_hash(struct cli_credentials *cred,
212                                      const struct samr_Password *nt_hash);
213 bool cli_credentials_set_ntlm_response(struct cli_credentials *cred,
214                                        const DATA_BLOB *lm_response, 
215                                        const DATA_BLOB *nt_response, 
216                                        enum credentials_obtained obtained);
217 int cli_credentials_set_keytab_name(struct cli_credentials *cred, 
218                                     struct loadparm_context *lp_ctx,
219                                     const char *keytab_name, 
220                                     enum credentials_obtained obtained);
221 void cli_credentials_set_gensec_features(struct cli_credentials *creds, uint32_t gensec_features);
222 uint32_t cli_credentials_get_gensec_features(struct cli_credentials *creds);
223 int cli_credentials_set_ccache(struct cli_credentials *cred, 
224                                struct loadparm_context *lp_ctx,
225                                const char *name, 
226                                enum credentials_obtained obtained,
227                                const char **error_string);
228 bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained);
229 bool cli_credentials_parse_password_fd(struct cli_credentials *credentials, 
230                                        int fd, enum credentials_obtained obtained);
231 void cli_credentials_invalidate_ccache(struct cli_credentials *cred, 
232                                        enum credentials_obtained obtained);
233 void cli_credentials_set_salt_principal(struct cli_credentials *cred, const char *principal);
234 void cli_credentials_set_impersonate_principal(struct cli_credentials *cred,
235                                                const char *principal,
236                                                const char *self_service);
237 void cli_credentials_set_target_service(struct cli_credentials *cred, const char *principal);
238 const char *cli_credentials_get_salt_principal(struct cli_credentials *cred);
239 const char *cli_credentials_get_impersonate_principal(struct cli_credentials *cred);
240 const char *cli_credentials_get_self_service(struct cli_credentials *cred);
241 const char *cli_credentials_get_target_service(struct cli_credentials *cred);
242 enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds);
243 const char *cli_credentials_get_forced_sasl_mech(struct cli_credentials *cred);
244 enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds);
245 NTSTATUS cli_credentials_set_secrets(struct cli_credentials *cred, 
246                                      struct loadparm_context *lp_ctx,
247                                      struct ldb_context *ldb,
248                                      const char *base,
249                                      const char *filter, 
250                                      char **error_string);
251  int cli_credentials_get_kvno(struct cli_credentials *cred);
252
253 bool cli_credentials_set_username_callback(struct cli_credentials *cred,
254                                   const char *(*username_cb) (struct cli_credentials *));
255
256 /**
257  * Obtain the client principal for this credentials context.
258  * @param cred credentials context
259  * @retval The username set on this context.
260  * @note Return value will never be NULL except by programmer error.
261  */
262 char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained);
263 bool cli_credentials_set_principal(struct cli_credentials *cred, 
264                                    const char *val, 
265                                    enum credentials_obtained obtained);
266 bool cli_credentials_set_principal_callback(struct cli_credentials *cred,
267                                   const char *(*principal_cb) (struct cli_credentials *));
268
269 /**
270  * Obtain the 'old' password for this credentials context (used for join accounts).
271  * @param cred credentials context
272  * @retval If set, the cleartext password, otherwise NULL
273  */
274 const char *cli_credentials_get_old_password(struct cli_credentials *cred);
275 bool cli_credentials_set_old_password(struct cli_credentials *cred, 
276                                       const char *val, 
277                                       enum credentials_obtained obtained);
278 bool cli_credentials_set_domain_callback(struct cli_credentials *cred,
279                                          const char *(*domain_cb) (struct cli_credentials *));
280 bool cli_credentials_set_realm_callback(struct cli_credentials *cred,
281                                         const char *(*realm_cb) (struct cli_credentials *));
282 bool cli_credentials_set_workstation_callback(struct cli_credentials *cred,
283                                               const char *(*workstation_cb) (struct cli_credentials *));
284
285 void cli_credentials_set_callback_data(struct cli_credentials *cred,
286                                        void *callback_data);
287 void *_cli_credentials_callback_data(struct cli_credentials *cred);
288 #define cli_credentials_callback_data(_cred, _type) \
289         talloc_get_type_abort(_cli_credentials_callback_data(_cred), _type)
290 #define cli_credentials_callback_data_void(_cred) \
291         _cli_credentials_callback_data(_cred)
292
293 /**
294  * Return attached NETLOGON credentials 
295  */
296 struct netlogon_creds_CredentialState *cli_credentials_get_netlogon_creds(struct cli_credentials *cred);
297
298 NTSTATUS netlogon_creds_session_encrypt(
299         struct netlogon_creds_CredentialState *state,
300         DATA_BLOB data);
301
302 #endif /* __CREDENTIALS_H__ */