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