r3737: - Get rid of the register_subsystem() and register_backend() functions.
[samba.git] / source4 / auth / auth_builtin.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Generic authentication types
4    Copyright (C) Andrew Bartlett         2001-2002
5    Copyright (C) Jelmer Vernooij              2002
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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "auth/auth.h"
24
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_AUTH
27
28 /**
29  * Return a guest logon for guest users (username = "")
30  *
31  * Typically used as the first module in the auth chain, this allows
32  * guest logons to be dealt with in one place.  Non-guest logons 'fail'
33  * and pass onto the next module.
34  **/
35
36 static NTSTATUS check_guest_security(const struct auth_context *auth_context,
37                                      void *my_private_data, 
38                                      TALLOC_CTX *mem_ctx,
39                                      const struct auth_usersupplied_info *user_info, 
40                                      struct auth_serversupplied_info **server_info)
41 {
42         /* mark this as 'not for me' */
43         NTSTATUS nt_status = NT_STATUS_NOT_IMPLEMENTED;
44
45         if (!(user_info->internal_username.str 
46               && *user_info->internal_username.str)) {
47                 nt_status = make_server_info_guest(discard_const(auth_context), 
48                                                    server_info);
49         }
50
51         return nt_status;
52 }
53
54 /* Guest modules initialisation */
55
56 static NTSTATUS auth_init_guest(struct auth_context *auth_context, 
57                                 const char *options, 
58                                 struct auth_methods **auth_method) 
59 {
60         if (!make_auth_methods(auth_context, auth_method))
61                 return NT_STATUS_NO_MEMORY;
62
63         (*auth_method)->auth = check_guest_security;
64         (*auth_method)->name = "guest";
65         return NT_STATUS_OK;
66 }
67
68 #ifdef DEVELOPER
69 /** 
70  * Return an error based on username
71  *
72  * This function allows the testing of obsure errors, as well as the generation
73  * of NT_STATUS -> DOS error mapping tables.
74  *
75  * This module is of no value to end-users.
76  *
77  * The password is ignored.
78  *
79  * @return An NTSTATUS value based on the username
80  **/
81
82 static NTSTATUS check_name_to_ntstatus_security(const struct auth_context *auth_context,
83                                                 void *my_private_data, 
84                                                 TALLOC_CTX *mem_ctx,
85                                                 const struct auth_usersupplied_info *user_info, 
86                                                 struct auth_serversupplied_info **server_info)
87 {
88         NTSTATUS nt_status;
89         fstring user;
90         long error_num;
91         fstrcpy(user, user_info->smb_name.str);
92         
93         if (strncasecmp("NT_STATUS", user, strlen("NT_STATUS")) == 0) {
94                 return nt_status_string_to_code(user);
95         }
96
97         error_num = strtoul(user, NULL, 16);
98         
99         DEBUG(5,("check_name_to_ntstatus_security: Error for user %s was 0x%lx\n", user, error_num));
100
101         nt_status = NT_STATUS(error_num);
102         
103         return nt_status;
104 }
105
106 /** Module initialisation function */
107
108 static NTSTATUS auth_init_name_to_ntstatus(struct auth_context *auth_context, 
109                                            const char *param, 
110                                            struct auth_methods **auth_method) 
111 {
112         if (!make_auth_methods(auth_context, auth_method))
113                 return NT_STATUS_NO_MEMORY;
114
115         (*auth_method)->auth = check_name_to_ntstatus_security;
116         (*auth_method)->name = "name_to_ntstatus";
117         return NT_STATUS_OK;
118 }
119
120 /** 
121  * Return a 'fixed' challenge instead of a variable one.
122  *
123  * The idea of this function is to make packet snifs consistant
124  * with a fixed challenge, so as to aid debugging.
125  *
126  * This module is of no value to end-users.
127  *
128  * This module does not actually authenticate the user, but
129  * just pretenteds to need a specified challenge.  
130  * This module removes *all* security from the challenge-response system
131  *
132  * @return NT_STATUS_UNSUCCESSFUL
133  **/
134
135 static NTSTATUS check_fixed_challenge_security(const struct auth_context *auth_context,
136                                                void *my_private_data, 
137                                                TALLOC_CTX *mem_ctx,
138                                                const struct auth_usersupplied_info *user_info, 
139                                                struct auth_serversupplied_info **server_info)
140 {
141         return NT_STATUS_NOT_IMPLEMENTED;
142 }
143
144 /****************************************************************************
145  Get the challenge out of a password server.
146 ****************************************************************************/
147
148 static DATA_BLOB auth_get_fixed_challenge(const struct auth_context *auth_context,
149                                           void **my_private_data, 
150                                           TALLOC_CTX *mem_ctx)
151 {
152         const char *challenge = "I am a teapot";   
153         return data_blob(challenge, 8);
154 }
155
156
157 /** Module initailisation function */
158
159 static NTSTATUS auth_init_fixed_challenge(struct auth_context *auth_context, 
160                                           const char *param, 
161                                           struct auth_methods **auth_method) 
162 {
163         if (!make_auth_methods(auth_context, auth_method))
164                 return NT_STATUS_NO_MEMORY;
165
166         (*auth_method)->auth = check_fixed_challenge_security;
167         (*auth_method)->get_chal = auth_get_fixed_challenge;
168         (*auth_method)->name = "fixed_challenge";
169         return NT_STATUS_OK;
170 }
171 #endif /* DEVELOPER */
172
173 NTSTATUS auth_builtin_init(void)
174 {
175         NTSTATUS ret;
176         struct auth_operations ops;
177
178         ops.name = "guest";
179         ops.init = auth_init_guest;
180         ret = auth_register(&ops);
181         if (!NT_STATUS_IS_OK(ret)) {
182                 DEBUG(0,("Failed to register '%s' auth backend!\n",
183                         ops.name));
184                 return ret;
185         }
186
187 #ifdef DEVELOPER
188         ops.name = "name_to_ntstatus";
189         ops.init = auth_init_name_to_ntstatus;
190         ret = auth_register(&ops);
191         if (!NT_STATUS_IS_OK(ret)) {
192                 DEBUG(0,("Failed to register '%s' auth backend!\n",
193                         ops.name));
194                 return ret;
195         }
196
197         ops.name = "fixed_challenge";
198         ops.init = auth_init_fixed_challenge;
199         ret = auth_register(&ops);
200         if (!NT_STATUS_IS_OK(ret)) {
201                 DEBUG(0,("Failed to register '%s' auth backend!\n",
202                         ops.name));
203                 return ret;
204         }
205 #endif /* DEVELOPER */
206         return ret;
207 }