2 Unix SMB/CIFS implementation.
4 Winbind status program.
6 Copyright (C) Tim Potter 2000-2002
7 Copyright (C) Andrew Bartlett 2003
8 Copyright (C) Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it> 2000
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #define DBGC_CLASS DBGC_WINBIND
30 #define SQUID_BUFFER_SIZE 2010
39 extern int winbindd_fd;
41 static const char *helper_protocol;
42 static const char *username;
43 static const char *domain;
44 static const char *workstation;
45 static const char *hex_challenge;
46 static const char *hex_lm_response;
47 static const char *hex_nt_response;
48 static unsigned char *challenge;
49 static size_t challenge_len;
50 static unsigned char *lm_response;
51 static size_t lm_response_len;
52 static unsigned char *nt_response;
53 static size_t nt_response_len;
54 static int request_lm_key;
55 static int request_nt_key;
57 static char *password;
59 static char winbind_separator(void)
61 struct winbindd_response response;
68 ZERO_STRUCT(response);
70 /* Send off request */
72 if (winbindd_request(WINBINDD_INFO, NULL, &response) !=
74 d_printf("could not obtain winbind separator!\n");
78 sep = response.data.info.winbind_separator;
82 d_printf("winbind separator was NULL!\n");
89 static const char *get_winbind_domain(void)
91 struct winbindd_response response;
93 static fstring winbind_domain;
94 if (*winbind_domain) {
95 return winbind_domain;
98 ZERO_STRUCT(response);
100 /* Send off request */
102 if (winbindd_request(WINBINDD_DOMAIN_NAME, NULL, &response) !=
103 NSS_STATUS_SUCCESS) {
104 d_printf("could not obtain winbind domain name!\n");
108 fstrcpy(winbind_domain, response.data.domain_name);
110 return winbind_domain;
114 static const char *get_winbind_netbios_name(void)
116 struct winbindd_response response;
118 static fstring winbind_netbios_name;
120 if (*winbind_netbios_name) {
121 return winbind_netbios_name;
124 ZERO_STRUCT(response);
126 /* Send off request */
128 if (winbindd_request(WINBINDD_NETBIOS_NAME, NULL, &response) !=
129 NSS_STATUS_SUCCESS) {
130 d_printf("could not obtain winbind netbios name!\n");
134 fstrcpy(winbind_netbios_name, response.data.netbios_name);
136 return winbind_netbios_name;
140 /* Authenticate a user with a plaintext password */
142 static BOOL check_plaintext_auth(const char *user, const char *pass, BOOL stdout_diagnostics)
144 struct winbindd_request request;
145 struct winbindd_response response;
148 /* Send off request */
150 ZERO_STRUCT(request);
151 ZERO_STRUCT(response);
153 fstrcpy(request.data.auth.user, user);
154 fstrcpy(request.data.auth.pass, pass);
156 result = winbindd_request(WINBINDD_PAM_AUTH, &request, &response);
158 /* Display response */
160 if (stdout_diagnostics) {
161 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
162 d_printf("Reading winbind reply failed! (0x01)\n");
165 d_printf("%s (0x%x)\n",
166 response.data.auth.nt_status_string,
167 response.data.auth.nt_status);
169 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
170 DEBUG(1, ("Reading winbind reply failed! (0x01)\n"));
173 DEBUG(3, ("%s (0x%x)\n",
174 response.data.auth.nt_status_string,
175 response.data.auth.nt_status));
178 return (result == NSS_STATUS_SUCCESS);
181 static NTSTATUS winbind_pw_check(struct ntlmssp_state *ntlmssp_state)
183 struct winbindd_request request;
184 struct winbindd_response response;
186 /* Send off request */
188 ZERO_STRUCT(request);
189 ZERO_STRUCT(response);
191 fstrcpy(request.data.auth_crap.user, ntlmssp_state->user);
193 fstrcpy(request.data.auth_crap.domain, ntlmssp_state->domain);
194 fstrcpy(request.data.auth_crap.workstation, ntlmssp_state->workstation);
196 memcpy(request.data.auth_crap.chal, ntlmssp_state->chal.data,
197 MIN(ntlmssp_state->chal.length, 8));
199 memcpy(request.data.auth_crap.lm_resp, ntlmssp_state->lm_resp.data,
200 MIN(ntlmssp_state->lm_resp.length, sizeof(request.data.auth_crap.lm_resp)));
202 memcpy(request.data.auth_crap.nt_resp, ntlmssp_state->nt_resp.data,
203 MIN(ntlmssp_state->nt_resp.length, sizeof(request.data.auth_crap.nt_resp)));
205 request.data.auth_crap.lm_resp_len = ntlmssp_state->lm_resp.length;
206 request.data.auth_crap.nt_resp_len = ntlmssp_state->nt_resp.length;
208 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
210 /* Display response */
212 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
213 return NT_STATUS_UNSUCCESSFUL;
216 return NT_STATUS(response.data.auth.nt_status);
219 static void manage_squid_ntlmssp_request(enum squid_mode squid_mode,
220 char *buf, int length)
222 static NTLMSSP_STATE *ntlmssp_state = NULL;
223 DATA_BLOB request, reply;
226 if (strlen(buf) < 2) {
227 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
228 x_fprintf(x_stdout, "BH\n");
232 if (strlen(buf) > 3) {
233 request = base64_decode_data_blob(buf + 3);
234 } else if (strcmp(buf, "YR") == 0) {
235 request = data_blob(NULL, 0);
237 ntlmssp_server_end(&ntlmssp_state);
239 DEBUG(1, ("NTLMSSP query [%s] invalid", buf));
240 x_fprintf(x_stdout, "BH\n");
244 if (!ntlmssp_state) {
245 ntlmssp_server_start(&ntlmssp_state);
246 ntlmssp_state->check_password = winbind_pw_check;
247 ntlmssp_state->get_domain = get_winbind_domain;
248 ntlmssp_state->get_global_myname = get_winbind_netbios_name;
251 DEBUG(10, ("got NTLMSSP packet:\n"));
252 dump_data(10, request.data, request.length);
254 nt_status = ntlmssp_server_update(ntlmssp_state, request, &reply);
256 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
257 char *reply_base64 = base64_encode_data_blob(reply);
258 x_fprintf(x_stdout, "TT %s\n", reply_base64);
259 SAFE_FREE(reply_base64);
260 data_blob_free(&reply);
261 DEBUG(10, ("NTLMSSP challenge\n"));
262 } else if (!NT_STATUS_IS_OK(nt_status)) {
263 x_fprintf(x_stdout, "NA %s\n", nt_errstr(nt_status));
264 DEBUG(10, ("NTLMSSP %s\n", nt_errstr(nt_status)));
266 x_fprintf(x_stdout, "AF %s\\%s\n", ntlmssp_state->domain, ntlmssp_state->user);
267 DEBUG(10, ("NTLMSSP OK!\n"));
270 data_blob_free(&request);
273 static void manage_squid_basic_request(enum squid_mode squid_mode,
274 char *buf, int length)
279 pass=memchr(buf,' ',length);
281 DEBUG(2, ("Password not found. Denying access\n"));
282 x_fprintf(x_stderr, "ERR\n");
288 if (squid_mode == SQUID_2_5_BASIC) {
289 rfc1738_unescape(user);
290 rfc1738_unescape(pass);
293 if (check_plaintext_auth(user, pass, False)) {
294 x_fprintf(x_stdout, "OK\n");
296 x_fprintf(x_stdout, "ERR\n");
300 static void manage_squid_request(enum squid_mode squid_mode)
302 char buf[SQUID_BUFFER_SIZE+1];
307 /* this is not a typo - x_fgets doesn't work too well under squid */
308 if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
309 DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
310 strerror(ferror(stdin))));
311 exit(1); /* BIIG buffer */
314 c=memchr(buf,'\n',sizeof(buf)-1);
323 DEBUG(2, ("Oversized message\n"));
324 x_fprintf(x_stderr, "ERR\n");
329 DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
331 if (buf[0] == '\0') {
332 DEBUG(2, ("Invalid Request\n"));
333 x_fprintf(x_stderr, "ERR\n");
337 if (squid_mode == SQUID_2_5_BASIC || squid_mode == SQUID_2_4_BASIC) {
338 manage_squid_basic_request(squid_mode, buf, length);
339 } else if (squid_mode == SQUID_2_5_NTLMSSP) {
340 manage_squid_ntlmssp_request(squid_mode, buf, length);
345 static void squid_stream(enum squid_mode squid_mode) {
346 /* initialize FDescs */
347 x_setbuf(x_stdout, NULL);
348 x_setbuf(x_stderr, NULL);
350 manage_squid_request(squid_mode);
355 /* Authenticate a user with a challenge/response */
357 static BOOL check_auth_crap(void)
359 struct winbindd_request request;
360 struct winbindd_response response;
363 static uint8 zeros[16];
366 /* Send off request */
368 ZERO_STRUCT(request);
369 ZERO_STRUCT(response);
372 request.data.auth_crap.flags |= WINBIND_PAM_LMKEY;
375 request.data.auth_crap.flags |= WINBIND_PAM_NTKEY;
377 fstrcpy(request.data.auth_crap.user, username);
379 fstrcpy(request.data.auth_crap.domain, domain);
380 fstrcpy(request.data.auth_crap.workstation, workstation);
382 memcpy(request.data.auth_crap.chal, challenge, MIN(challenge_len, 8));
384 memcpy(request.data.auth_crap.lm_resp, lm_response, MIN(lm_response_len, sizeof(request.data.auth_crap.lm_resp)));
386 memcpy(request.data.auth_crap.nt_resp, nt_response, MIN(nt_response_len, sizeof(request.data.auth_crap.nt_resp)));
388 request.data.auth_crap.lm_resp_len = lm_response_len;
389 request.data.auth_crap.nt_resp_len = nt_response_len;
391 result = winbindd_request(WINBINDD_PAM_AUTH_CRAP, &request, &response);
393 /* Display response */
395 if ((result != NSS_STATUS_SUCCESS) && (response.data.auth.nt_status == 0)) {
396 d_printf("Reading winbind reply failed! (0x01)\n");
399 d_printf("%s (0x%x)\n",
400 response.data.auth.nt_status_string,
401 response.data.auth.nt_status);
403 if (response.data.auth.nt_status == 0) {
405 && (memcmp(zeros, response.data.auth.first_8_lm_hash,
406 sizeof(response.data.auth.first_8_lm_hash)) != 0)) {
407 hex_encode(response.data.auth.first_8_lm_hash,
408 sizeof(response.data.auth.first_8_lm_hash),
410 d_printf("LM_KEY: %s\n", lm_key);
414 && (memcmp(zeros, response.data.auth.nt_session_key,
415 sizeof(response.data.auth.nt_session_key)) != 0)) {
416 hex_encode(response.data.auth.nt_session_key,
417 sizeof(response.data.auth.nt_session_key),
419 d_printf("NT_KEY: %s\n", nt_key);
424 return result == NSS_STATUS_SUCCESS;
442 int main(int argc, const char **argv)
447 struct poptOption long_options[] = {
450 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
451 { "username", 0, POPT_ARG_STRING, &username, OPT_USERNAME, "username"},
452 { "domain", 0, POPT_ARG_STRING, &domain, OPT_DOMAIN, "domain name"},
453 { "workstation", 0, POPT_ARG_STRING, &domain, OPT_WORKSTATION, "workstation"},
454 { "challenge", 0, POPT_ARG_STRING, &hex_challenge, OPT_CHALLENGE, "challenge (HEX encoded)"},
455 { "lm-response", 0, POPT_ARG_STRING, &hex_lm_response, OPT_LM, "LM Response to the challenge (HEX encoded)"},
456 { "nt-response", 0, POPT_ARG_STRING, &hex_nt_response, OPT_NT, "NT or NTLMv2 Response to the challenge (HEX encoded)"},
457 { "password", 0, POPT_ARG_STRING, &password, OPT_PASSWORD, "User's plaintext password"},
458 { "request-lm-key", 0, POPT_ARG_NONE, &request_lm_key, OPT_LM_KEY, "Retreive LM session key"},
459 { "request-nt-key", 0, POPT_ARG_NONE, &request_nt_key, OPT_NT_KEY, "Retreive NT session key"},
460 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
461 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_configfile },
462 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_version},
466 /* Samba client initialisation */
472 pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
474 /* Parse command line options */
477 poptPrintHelp(pc, stderr, 0);
481 pc = poptGetContext(NULL, argc, (const char **)argv, long_options,
482 POPT_CONTEXT_KEEP_FIRST);
484 while((opt = poptGetNextOpt(pc)) != -1) {
487 challenge_len = strlen(hex_challenge);
488 challenge = smb_xmalloc((challenge_len+1)/2);
489 if ((challenge_len = strhex_to_str(challenge, challenge_len, hex_challenge)) != 8) {
490 fprintf(stderr, "hex decode of %s failed (only got %u bytes)!\n",
491 hex_challenge, challenge_len);
496 lm_response_len = strlen(hex_lm_response);
497 lm_response = smb_xmalloc((lm_response_len+1)/2);
498 if ((lm_response_len = strhex_to_str(lm_response, lm_response_len, hex_lm_response)) != 24) {
499 fprintf(stderr, "hex decode of %s failed!\n", hex_lm_response);
504 nt_response_len = strlen(hex_nt_response);
505 nt_response = smb_xmalloc((nt_response_len+1)/2);
506 if ((nt_response_len = strhex_to_str(nt_response, nt_response_len, hex_nt_response)) < 24) {
507 fprintf(stderr, "hex decode of %s failed!\n", hex_nt_response);
514 if (helper_protocol) {
515 if (strcmp(helper_protocol, "squid-2.5-ntlmssp")== 0) {
516 squid_stream(SQUID_2_5_NTLMSSP);
517 } else if (strcmp(helper_protocol, "squid-2.5-basic")== 0) {
518 squid_stream(SQUID_2_5_BASIC);
519 } else if (strcmp(helper_protocol, "squid-2.4-basic")== 0) {
520 squid_stream(SQUID_2_4_BASIC);
522 fprintf(stderr, "unknown helper protocol [%s]\n", helper_protocol);
527 if (domain == NULL) {
528 domain = get_winbind_domain();
531 if (workstation == NULL) {
536 if (!check_auth_crap()) {
539 } else if (password) {
541 snprintf(user, sizeof(user)-1, "%s%c%s", domain, winbind_separator(), username);
542 if (!check_plaintext_auth(user, password, True)) {