s4-auth Extend python bindings to allow ldb and message to be specified
[sfrench/samba-autobuild/.git] / source4 / auth / auth.h
1 /*
2    Unix SMB/CIFS implementation.
3    Standardised Authentication types
4    Copyright (C) Andrew Bartlett   2001
5    Copyright (C) Stefan Metzmacher 2005
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 #ifndef _SAMBA_AUTH_H
22 #define _SAMBA_AUTH_H
23
24 #include "librpc/gen_ndr/ndr_krb5pac.h"
25 #include "../auth/common_auth.h"
26
27 extern const char *krbtgt_attrs[];
28 extern const char *server_attrs[];
29 extern const char *user_attrs[];
30
31 union netr_Validation;
32 struct netr_SamBaseInfo;
33 struct netr_SamInfo3;
34 struct loadparm_context;
35
36 /* modules can use the following to determine if the interface has changed
37  * please increment the version number after each interface change
38  * with a comment and maybe update struct auth_critical_sizes.
39  */
40 /* version 1 - version from samba 3.0 - metze */
41 /* version 2 - initial samba4 version - metze */
42 /* version 3 - subsequent samba4 version - abartlet */
43 /* version 4 - subsequent samba4 version - metze */
44 /* version 0 - till samba4 is stable - metze */
45 #define AUTH_INTERFACE_VERSION 0
46
47 #define AUTH_SESSION_INFO_DEFAULT_GROUPS     0x01 /* Add the user to the default world and network groups */
48 #define AUTH_SESSION_INFO_AUTHENTICATED      0x02 /* Add the user to the 'authenticated users' group */
49 #define AUTH_SESSION_INFO_SIMPLE_PRIVILEGES  0x04 /* Use a trivial map between users and privilages, rather than a DB */
50
51 struct auth_serversupplied_info
52 {
53         struct dom_sid *account_sid;
54         struct dom_sid *primary_group_sid;
55
56         size_t n_domain_groups;
57         struct dom_sid **domain_groups;
58
59         DATA_BLOB user_session_key;
60         DATA_BLOB lm_session_key;
61
62         const char *account_name;
63         const char *domain_name;
64
65         const char *full_name;
66         const char *logon_script;
67         const char *profile_path;
68         const char *home_directory;
69         const char *home_drive;
70         const char *logon_server;
71
72         NTTIME last_logon;
73         NTTIME last_logoff;
74         NTTIME acct_expiry;
75         NTTIME last_password_change;
76         NTTIME allow_password_change;
77         NTTIME force_password_change;
78
79         uint16_t logon_count;
80         uint16_t bad_password_count;
81
82         uint32_t acct_flags;
83
84         bool authenticated;
85
86         struct PAC_SIGNATURE_DATA pac_srv_sig, pac_kdc_sig;
87 };
88
89 struct auth_method_context;
90 struct auth_check_password_request;
91 struct auth_context;
92 struct auth_session_info;
93 struct ldb_dn;
94
95 struct auth_operations {
96         const char *name;
97
98         /* If you are using this interface, then you are probably
99          * getting something wrong.  This interface is only for
100          * security=server, and makes a number of compromises to allow
101          * that.  It is not compatible with being a PDC.  */
102
103         NTSTATUS (*get_challenge)(struct auth_method_context *ctx, TALLOC_CTX *mem_ctx, uint8_t chal[8]);
104
105         /* Given the user supplied info, check if this backend want to handle the password checking */
106
107         NTSTATUS (*want_check)(struct auth_method_context *ctx, TALLOC_CTX *mem_ctx,
108                                const struct auth_usersupplied_info *user_info);
109
110         /* Given the user supplied info, check a password */
111
112         NTSTATUS (*check_password)(struct auth_method_context *ctx, TALLOC_CTX *mem_ctx,
113                                    const struct auth_usersupplied_info *user_info,
114                                    struct auth_serversupplied_info **server_info);
115
116         /* Lookup a 'server info' return based only on the principal */
117         NTSTATUS (*get_server_info_principal)(TALLOC_CTX *mem_ctx,
118                                               struct auth_context *auth_context,
119                                               const char *principal,
120                                               struct ldb_dn *user_dn,
121                                               struct auth_serversupplied_info **server_info);
122 };
123
124 struct auth_method_context {
125         struct auth_method_context *prev, *next;
126         struct auth_context *auth_ctx;
127         const struct auth_operations *ops;
128         int depth;
129         void *private_data;
130 };
131
132 struct auth_context {
133         struct {
134                 /* Who set this up in the first place? */
135                 const char *set_by;
136
137                 bool may_be_modified;
138
139                 DATA_BLOB data;
140         } challenge;
141
142         /* methods, in the order they should be called */
143         struct auth_method_context *methods;
144
145         /* the event context to use for calls that can block */
146         struct tevent_context *event_ctx;
147
148         /* the messaging context which can be used by backends */
149         struct messaging_context *msg_ctx;
150
151         /* loadparm context */
152         struct loadparm_context *lp_ctx;
153
154         /* SAM database for this local machine - to fill in local groups, or to authenticate local NTLM users */
155         struct ldb_context *sam_ctx;
156
157         NTSTATUS (*check_password)(struct auth_context *auth_ctx,
158                                    TALLOC_CTX *mem_ctx,
159                                    const struct auth_usersupplied_info *user_info,
160                                    struct auth_serversupplied_info **server_info);
161
162         NTSTATUS (*get_challenge)(struct auth_context *auth_ctx, uint8_t chal[8]);
163
164         bool (*challenge_may_be_modified)(struct auth_context *auth_ctx);
165
166         NTSTATUS (*set_challenge)(struct auth_context *auth_ctx, const uint8_t chal[8], const char *set_by);
167
168         NTSTATUS (*get_server_info_principal)(TALLOC_CTX *mem_ctx,
169                                                  struct auth_context *auth_ctx,
170                                                  const char *principal,
171                                                  struct ldb_dn *user_dn,
172                                                  struct auth_serversupplied_info **server_info);
173
174         NTSTATUS (*generate_session_info)(TALLOC_CTX *mem_ctx,
175                                           struct auth_context *auth_context,
176                                           struct auth_serversupplied_info *server_info,
177                                           uint32_t session_info_flags,
178                                           struct auth_session_info **session_info);
179 };
180
181 /* this structure is used by backends to determine the size of some critical types */
182 struct auth_critical_sizes {
183         int interface_version;
184         int sizeof_auth_operations;
185         int sizeof_auth_methods;
186         int sizeof_auth_context;
187         int sizeof_auth_usersupplied_info;
188         int sizeof_auth_serversupplied_info;
189 };
190
191  NTSTATUS encrypt_user_info(TALLOC_CTX *mem_ctx, struct auth_context *auth_context,
192                            enum auth_password_state to_state,
193                            const struct auth_usersupplied_info *user_info_in,
194                            const struct auth_usersupplied_info **user_info_encrypted);
195
196 #include "auth/session.h"
197 #include "auth/system_session_proto.h"
198
199 struct ldb_message;
200 struct ldb_context;
201 struct gensec_security;
202
203 NTSTATUS auth_get_challenge(struct auth_context *auth_ctx, uint8_t chal[8]);
204 NTSTATUS authsam_account_ok(TALLOC_CTX *mem_ctx,
205                             struct ldb_context *sam_ctx,
206                             uint32_t logon_parameters,
207                             struct ldb_dn *domain_dn,
208                             struct ldb_message *msg,
209                             const char *logon_workstation,
210                             const char *name_for_logs,
211                             bool allow_domain_trust,
212                             bool password_change);
213 NTSTATUS authsam_expand_nested_groups(struct ldb_context *sam_ctx,
214                                       struct ldb_val *dn_val, const bool only_childs, const char *filter,
215                                       TALLOC_CTX *res_sids_ctx, struct dom_sid ***res_sids,
216                                       unsigned int *num_res_sids);
217 struct auth_session_info *system_session(struct loadparm_context *lp_ctx);
218 NTSTATUS authsam_make_server_info(TALLOC_CTX *mem_ctx, struct ldb_context *sam_ctx,
219                                            const char *netbios_name,
220                                            const char *domain_name,
221                                            struct ldb_dn *domain_dn,
222                                            struct ldb_message *msg,
223                                            DATA_BLOB user_sess_key, DATA_BLOB lm_sess_key,
224                                   struct auth_serversupplied_info **_server_info);
225 NTSTATUS auth_system_session_info(TALLOC_CTX *parent_ctx,
226                                            struct loadparm_context *lp_ctx,
227                                            struct auth_session_info **_session_info) ;
228 NTSTATUS auth_nt_status_squash(NTSTATUS nt_status);
229
230 NTSTATUS auth_context_create_methods(TALLOC_CTX *mem_ctx, const char **methods,
231                                      struct tevent_context *ev,
232                                      struct messaging_context *msg,
233                                      struct loadparm_context *lp_ctx,
234                                      struct ldb_context *sam_ctx,
235                                      struct auth_context **auth_ctx);
236 const char **auth_methods_from_lp(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx);
237
238 NTSTATUS auth_context_create(TALLOC_CTX *mem_ctx,
239                              struct tevent_context *ev,
240                              struct messaging_context *msg,
241                              struct loadparm_context *lp_ctx,
242                              struct auth_context **auth_ctx);
243 NTSTATUS auth_context_create_from_ldb(TALLOC_CTX *mem_ctx, struct ldb_context *ldb, struct auth_context **auth_ctx);
244
245 NTSTATUS auth_check_password(struct auth_context *auth_ctx,
246                              TALLOC_CTX *mem_ctx,
247                              const struct auth_usersupplied_info *user_info,
248                              struct auth_serversupplied_info **server_info);
249 NTSTATUS auth_init(void);
250 NTSTATUS auth_register(const struct auth_operations *ops);
251 NTSTATUS server_service_auth_init(void);
252 NTSTATUS authenticate_username_pw(TALLOC_CTX *mem_ctx,
253                                   struct tevent_context *ev,
254                                   struct messaging_context *msg,
255                                   struct loadparm_context *lp_ctx,
256                                   const char *nt4_domain,
257                                   const char *nt4_username,
258                                   const char *password,
259                                   const uint32_t logon_parameters,
260                                   struct auth_session_info **session_info);
261
262 struct tevent_req *auth_check_password_send(TALLOC_CTX *mem_ctx,
263                                             struct tevent_context *ev,
264                                             struct auth_context *auth_ctx,
265                                             const struct auth_usersupplied_info *user_info);
266 NTSTATUS auth_check_password_recv(struct tevent_req *req,
267                                   TALLOC_CTX *mem_ctx,
268                                   struct auth_serversupplied_info **server_info);
269
270 bool auth_challenge_may_be_modified(struct auth_context *auth_ctx);
271 NTSTATUS auth_context_set_challenge(struct auth_context *auth_ctx, const uint8_t chal[8], const char *set_by);
272
273 NTSTATUS auth_get_server_info_principal(TALLOC_CTX *mem_ctx,
274                                         struct auth_context *auth_ctx,
275                                         const char *principal,
276                                         struct ldb_dn *user_dn,
277                                         struct auth_serversupplied_info **server_info);
278
279 NTSTATUS samba_server_gensec_start(TALLOC_CTX *mem_ctx,
280                                    struct tevent_context *event_ctx,
281                                    struct messaging_context *msg_ctx,
282                                    struct loadparm_context *lp_ctx,
283                                    struct cli_credentials *server_credentials,
284                                    const char *target_service,
285                                    struct gensec_security **gensec_context);
286
287 #endif /* _SMBAUTH_H_ */