s3-auth Add auth_samba4 module
[samba.git] / source3 / auth / auth_samba4.c
1 /*
2    Unix SMB/CIFS implementation.
3    Authenticate against Samba4's auth subsystem
4    Copyright (C) Volker Lendecke 2008
5    Copyright (C) Andrew Bartlett 2010
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 "source3/include/auth.h"
23 #include "source4/auth/auth.h"
24 #include "auth/auth_sam_reply.h"
25 #include "param/param.h"
26 #include "source4/lib/events/events.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_AUTH
30
31 static NTSTATUS check_samba4_security(const struct auth_context *auth_context,
32                                       void *my_private_data,
33                                       TALLOC_CTX *mem_ctx,
34                                       const struct auth_usersupplied_info *user_info,
35                                       struct auth_serversupplied_info **server_info)
36 {
37         TALLOC_CTX *frame = talloc_stackframe();
38         struct netr_SamInfo3 *info3 = NULL;
39         NTSTATUS nt_status;
40         struct auth_user_info_dc *user_info_dc;
41         struct auth4_context *auth4_context;
42         struct loadparm_context *lp_ctx;
43         const char *config_file;
44
45         lp_ctx = loadparm_init(frame);
46         if (lp_ctx == NULL) {
47                 DEBUG(10, ("loadparm_init failed\n"));
48                 talloc_free(frame);
49                 return NT_STATUS_INVALID_SERVER_STATE;
50         }
51
52         if (lp_loaded()) {
53                 config_file = lp_configfile();
54         }
55         if (!config_file || !config_file[0]) {
56                 config_file = get_dyn_CONFIGFILE();
57         }
58
59         if (!lpcfg_load(lp_ctx, config_file)) {
60                 DEBUG(1, ("s4 lpcfg_load() of s3 config file %s failed", config_file));
61                 talloc_free(frame);
62                 return NT_STATUS_INVALID_SERVER_STATE;
63         }
64
65         /* We create a private tevent context here to avoid nested loops in
66          * the s3 one, as that may not be expected */
67         nt_status = auth_context_create(mem_ctx,
68                                         s4_event_context_init(frame), NULL, 
69                                         lp_ctx,
70                                         &auth4_context);
71         NT_STATUS_NOT_OK_RETURN(nt_status);
72                 
73         nt_status = auth_context_set_challenge(auth4_context, auth_context->challenge.data, "auth_samba4");
74         NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, auth4_context);
75
76         nt_status = auth_check_password(auth4_context, auth4_context, user_info, &user_info_dc);
77         NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, auth4_context);
78         
79         nt_status = auth_convert_user_info_dc_saminfo3(mem_ctx,
80                                                        user_info_dc,
81                                                        &info3);
82         if (NT_STATUS_IS_OK(nt_status)) {
83                 /* We need the strings from the server_info to be valid as long as the info3 is around */
84                 talloc_steal(info3, user_info_dc);
85         }
86         talloc_free(auth4_context);
87
88         if (!NT_STATUS_IS_OK(nt_status)) {
89                 goto done;
90         }
91
92         nt_status = make_server_info_info3(mem_ctx, user_info->client.account_name,
93                                            user_info->mapped.domain_name, server_info,
94                                         info3);
95         if (!NT_STATUS_IS_OK(nt_status)) {
96                 DEBUG(10, ("make_server_info_info3 failed: %s\n",
97                            nt_errstr(nt_status)));
98                 TALLOC_FREE(frame);
99                 return nt_status;
100         }
101
102         nt_status = NT_STATUS_OK;
103
104  done:
105         TALLOC_FREE(frame);
106         return nt_status;
107 }
108
109 /* module initialisation */
110 static NTSTATUS auth_init_samba4(struct auth_context *auth_context,
111                                     const char *param,
112                                     auth_methods **auth_method)
113 {
114         struct auth_methods *result;
115
116         result = TALLOC_ZERO_P(auth_context, struct auth_methods);
117         if (result == NULL) {
118                 return NT_STATUS_NO_MEMORY;
119         }
120         result->name = "samba4";
121         result->auth = check_samba4_security;
122
123         *auth_method = result;
124         return NT_STATUS_OK;
125 }
126
127 NTSTATUS auth_samba4_init(void)
128 {
129         smb_register_auth(AUTH_INTERFACE_VERSION, "samba4",
130                           auth_init_samba4);
131         return NT_STATUS_OK;
132 }