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