r2611: Try to make Samba4's ntlm_auth more consistant with Samba 3.0.
[samba.git] / source / utils / ntlm_auth.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind status program.
5
6    Copyright (C) Tim Potter      2000-2003
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003-2004
8    Copyright (C) Francesco Chemolli <kinkie@kame.usr.dsi.unimi.it> 2000 
9
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.
14    
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.
19    
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.
23 */
24
25 #include "includes.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
29
30 #define SQUID_BUFFER_SIZE 2010
31
32 enum stdio_helper_mode {
33         SQUID_2_4_BASIC,
34         SQUID_2_5_BASIC,
35         SQUID_2_5_NTLMSSP,
36         NTLMSSP_CLIENT_1,
37         GSS_SPNEGO_CLIENT,
38         GSS_SPNEGO_SERVER,
39         NTLM_SERVER_1,
40         NUM_HELPER_MODES
41 };
42
43 #define NTLM_AUTH_FLAG_USER_SESSION_KEY     0x0004
44 #define NTLM_AUTH_FLAG_LMKEY                0x0008
45
46
47 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode, 
48                                       char *buf, int length, void **private);
49
50 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode, 
51                                         char *buf, int length, void **private);
52
53 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode, 
54                                    char *buf, int length, void **private);
55
56 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode, 
57                                           char *buf, int length, void **private);
58
59 static void manage_squid_request(enum stdio_helper_mode helper_mode, 
60                                  stdio_helper_function fn, void *private);
61
62 static const struct {
63         enum stdio_helper_mode mode;
64         const char *name;
65         stdio_helper_function fn;
66 } stdio_helper_protocols[] = {
67         { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
68         { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
69         { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_gensec_request},
70         { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_request},
71         { GSS_SPNEGO_SERVER, "gss-spnego", manage_gensec_request},
72         { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_request},
73         { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
74         { NUM_HELPER_MODES, NULL, NULL}
75 };
76
77 extern int winbindd_fd;
78
79 const char *opt_username;
80 const char *opt_domain;
81 const char *opt_workstation;
82 const char *opt_password;
83
84
85 /* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
86    form DOMAIN/user into a domain and a user */
87
88 static BOOL parse_ntlm_auth_domain_user(const char *domuser, fstring domain, 
89                                      fstring user)
90 {
91
92         char *p = strchr(domuser,*lp_winbind_separator());
93
94         if (!p) {
95                 return False;
96         }
97         
98         fstrcpy(user, p+1);
99         fstrcpy(domain, domuser);
100         domain[PTR_DIFF(p, domuser)] = 0;
101
102         return True;
103 }
104
105 /* Authenticate a user with a plaintext password */
106
107 static BOOL check_plaintext_auth(const char *user, const char *pass, 
108                                  BOOL stdout_diagnostics)
109 {
110         return (strcmp(pass, opt_password) == 0);
111 }
112
113 /* authenticate a user with an encrypted username/password */
114
115 static NTSTATUS local_pw_check_specified(const char *username, 
116                                          const char *domain, 
117                                          const char *workstation,
118                                          const DATA_BLOB *challenge, 
119                                          const DATA_BLOB *lm_response, 
120                                          const DATA_BLOB *nt_response, 
121                                          uint32 flags, 
122                                          DATA_BLOB *lm_session_key, 
123                                          DATA_BLOB *user_session_key, 
124                                          char **error_string, 
125                                          char **unix_name) 
126 {
127         NTSTATUS nt_status;
128         uint8_t lm_pw[16], nt_pw[16];
129         uint8_t *lm_pwd, *nt_pwd;
130         TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
131         if (!mem_ctx) {
132                 nt_status = NT_STATUS_NO_MEMORY;
133         } else {
134                 
135                 E_md4hash(opt_password, nt_pw);
136                 if (E_deshash(opt_password, lm_pw)) {
137                         lm_pwd = lm_pw;
138                 } else {
139                         lm_pwd = NULL;
140                 }
141                 nt_pwd = nt_pw;
142                 
143                 
144                 nt_status = ntlm_password_check(mem_ctx, 
145                                                 challenge,
146                                                 lm_response,
147                                                 nt_response,
148                                                 NULL, NULL,
149                                                 username,
150                                                 username,
151                                                 domain,
152                                                 lm_pwd, nt_pwd, user_session_key, lm_session_key);
153                 
154                 if (NT_STATUS_IS_OK(nt_status)) {
155                         if (unix_name) {
156                                 asprintf(unix_name, 
157                                          "%s%c%s", domain,
158                                          *lp_winbind_separator(), 
159                                          username);
160                         }
161                 } else {
162                         DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n", 
163                                   domain, username, workstation, 
164                                   nt_errstr(nt_status)));
165                 }
166                 talloc_destroy(mem_ctx);
167         }
168         if (error_string) {
169                 *error_string = strdup(nt_errstr(nt_status));
170         }
171         return nt_status;
172         
173         
174 }
175
176 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode, 
177                                        char *buf, int length, void **private) 
178 {
179         char *user, *pass;      
180         user=buf;
181         
182         pass=memchr(buf,' ',length);
183         if (!pass) {
184                 DEBUG(2, ("Password not found. Denying access\n"));
185                 x_fprintf(x_stdout, "ERR\n");
186                 return;
187         }
188         *pass='\0';
189         pass++;
190         
191         if (stdio_helper_mode == SQUID_2_5_BASIC) {
192                 rfc1738_unescape(user);
193                 rfc1738_unescape(pass);
194         }
195         
196         if (check_plaintext_auth(user, pass, False)) {
197                 x_fprintf(x_stdout, "OK\n");
198         } else {
199                 x_fprintf(x_stdout, "ERR\n");
200         }
201 }
202
203 /* This is a bit hairy, but the basic idea is to do a password callback
204    to the calling application.  The callback comes from within gensec */
205
206 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode, 
207                                          char *buf, int length, void **private)  
208 {
209         DATA_BLOB in;
210         struct gensec_security **gensec_state = (struct gensec_security **)private;
211         if (strlen(buf) < 2) {
212                 DEBUG(1, ("query [%s] invalid", buf));
213                 x_fprintf(x_stdout, "BH\n");
214                 return;
215         }
216
217         if (strlen(buf) > 3) {
218                 in = base64_decode_data_blob(buf + 3);
219         } else {
220                 in = data_blob(NULL, 0);
221         }
222
223         if (strncmp(buf, "PW ", 3) == 0) {
224
225                 (*gensec_state)->password_callback_private = talloc_strndup((*gensec_state)->mem_ctx, 
226                                                                             (const char *)in.data, in.length);
227                 
228                 if ((*gensec_state)->password_callback_private == NULL) {
229                         DEBUG(1, ("Out of memory\n"));
230                         x_fprintf(x_stdout, "BH\n");
231                         data_blob_free(&in);
232                         return;
233                 }
234
235                 x_fprintf(x_stdout, "OK\n");
236                 data_blob_free(&in);
237                 return;
238         }
239         DEBUG(1, ("Asked for (and expected) a password\n"));
240         x_fprintf(x_stdout, "BH\n");
241         data_blob_free(&in);
242 }
243
244 /* 
245  * Callback for gensec, to ask the calling application for a password.  Uses the above function
246  * for the stdio part of this.
247  */
248
249 static NTSTATUS get_password(struct gensec_security *gensec_security, TALLOC_CTX *mem_ctx, 
250                              char **password) 
251 {
252         *password = NULL;
253         
254         /* Ask for a password */
255         x_fprintf(x_stdout, "PW\n");
256         gensec_security->password_callback_private = NULL;
257
258         manage_squid_request(NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, &gensec_security);
259         *password = (char *)gensec_security->password_callback_private;
260         if (*password) {
261                 return NT_STATUS_OK;
262         } else {
263                 return NT_STATUS_INVALID_PARAMETER;
264         }
265 }
266
267 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode, 
268                                   char *buf, int length, void **private) 
269 {
270         DATA_BLOB in;
271         DATA_BLOB out = data_blob(NULL, 0);
272         char *out_base64 = NULL;
273         const char *reply_arg = NULL;
274         struct gensec_security **gensec_state = (struct gensec_security **)private;
275         NTSTATUS nt_status;
276         BOOL first = False;
277         const char *reply_code;
278         
279         if (strlen(buf) < 2) {
280                 DEBUG(1, ("query [%s] invalid", buf));
281                 x_fprintf(x_stdout, "BH\n");
282                 return;
283         }
284
285         if (strlen(buf) > 3) {
286                 in = base64_decode_data_blob(buf + 3);
287         } else {
288                 in = data_blob(NULL, 0);
289         }
290
291         if (strncmp(buf, "YR", 2) == 0) {
292                 if (gensec_state && *gensec_state) {
293                         gensec_end(gensec_state);
294                         *gensec_state = NULL;
295                 }
296         } else if ( (strncmp(buf, "OK", 2) == 0)) {
297                 /* do nothing */
298                 data_blob_free(&in);
299                 return;
300         } else if ( (strncmp(buf, "TT ", 3) != 0) &&
301                     (strncmp(buf, "KK ", 3) != 0) &&
302                     (strncmp(buf, "AF ", 3) != 0) &&
303                     (strncmp(buf, "NA ", 3) != 0) && 
304                     (strncmp(buf, "PW ", 3) != 0)) {
305                 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
306                 x_fprintf(x_stdout, "BH\n");
307                 data_blob_free(&in);
308                 return;
309         }
310
311         /* setup gensec */
312         if (!(gensec_state && *gensec_state)) {
313                 switch (stdio_helper_mode) {
314                 case GSS_SPNEGO_CLIENT:
315                 case NTLMSSP_CLIENT_1:
316                         /* setup the client side */
317                         
318                         if (!NT_STATUS_IS_OK(gensec_client_start(gensec_state))) {
319                                 exit(1);
320                         }
321                         gensec_set_username(*gensec_state, opt_username);
322                         gensec_set_domain(*gensec_state, opt_domain);           
323                         if (opt_password) {
324                                 if (!NT_STATUS_IS_OK(gensec_set_password(*gensec_state, opt_password))) {
325                                         DEBUG(1, ("Out of memory\n"));
326                                         x_fprintf(x_stdout, "BH\n");
327                                         data_blob_free(&in);
328                                         return;
329                                 }
330                         } else {
331                                 gensec_set_password_callback(*gensec_state, get_password, NULL);
332                         }
333                         
334                         break;
335                 case GSS_SPNEGO_SERVER:
336                 case SQUID_2_5_NTLMSSP:
337                         if (!NT_STATUS_IS_OK(gensec_server_start(gensec_state))) {
338                                 exit(1);
339                         }
340                         break;
341                 default:
342                         abort();
343                 }
344
345                 switch (stdio_helper_mode) {
346                 case GSS_SPNEGO_CLIENT:
347                 case GSS_SPNEGO_SERVER:
348                         nt_status = gensec_start_mech_by_oid(*gensec_state, OID_SPNEGO);
349                         break;
350                 case NTLMSSP_CLIENT_1:
351                 case SQUID_2_5_NTLMSSP:
352                         nt_status = gensec_start_mech_by_oid(*gensec_state, OID_NTLMSSP);
353                         break;
354                 default:
355                         abort();
356                 }
357
358                 if (!NT_STATUS_IS_OK(nt_status)) {
359                         DEBUG(1, ("SPENGO login failed to initialise: %s\n", nt_errstr(nt_status)));
360                         x_fprintf(x_stdout, "BH\n");
361                         return;
362                 }
363                 if (!in.length) {
364                         first = True;
365                 }
366         }
367         
368         if (strncmp(buf, "PW ", 3) == 0) {
369
370                 if (!NT_STATUS_IS_OK(gensec_set_password(*gensec_state, 
371                                                          talloc_strndup((*gensec_state)->mem_ctx, 
372                                                                         (const char *)in.data, 
373                                                                         in.length)))) {
374                         DEBUG(1, ("Out of memory\n"));
375                         x_fprintf(x_stdout, "BH\n");
376                         data_blob_free(&in);
377                         return;
378                 }
379
380                 x_fprintf(x_stdout, "OK\n");
381                 data_blob_free(&in);
382                 return;
383         }
384
385         /* update */
386
387         nt_status = gensec_update(*gensec_state, NULL, in, &out);
388         
389         /* don't leak 'bad password'/'no such user' info to the network client */
390         nt_status = nt_status_squash(nt_status);
391
392         if (out.length) {
393                 out_base64 = base64_encode_data_blob(out);
394         } else {
395                 out_base64 = NULL;
396         }
397         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
398                 reply_arg = "*";
399                 if (first) {
400                         reply_code = "YR";
401                 } else if ((*gensec_state)->gensec_role == GENSEC_CLIENT) { 
402                         reply_code = "KK";
403                 } else if ((*gensec_state)->gensec_role == GENSEC_SERVER) { 
404                         reply_code = "TT";
405                 } else {
406                         abort();
407                 }
408
409
410         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
411                 reply_code = "BH";
412                 reply_arg = nt_errstr(nt_status);
413                 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
414         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
415                 reply_code = "BH";
416                 reply_arg = nt_errstr(nt_status);
417                 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
418         } else if (!NT_STATUS_IS_OK(nt_status)) {
419                 reply_code = "NA";
420                 reply_arg = nt_errstr(nt_status);
421                 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
422         } else if /* OK */ ((*gensec_state)->gensec_role == GENSEC_SERVER) {
423                 struct auth_session_info *session_info;
424
425                 nt_status = gensec_session_info(*gensec_state, &session_info);
426                 if (!NT_STATUS_IS_OK(nt_status)) {
427                         reply_code = "BH";
428                         reply_arg = nt_errstr(nt_status);
429                         DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
430                 } else {
431
432                         reply_code = "AF";
433                         reply_arg = talloc_asprintf((*gensec_state)->mem_ctx, 
434                                                     "%s%s%s", session_info->server_info->domain, 
435                                                     lp_winbind_separator(), session_info->server_info->account_name);
436                         talloc_destroy(session_info->mem_ctx);
437                 }
438         } else if ((*gensec_state)->gensec_role == GENSEC_CLIENT) {
439                 reply_code = "AF";
440                 reply_arg = NULL;
441         } else {
442                 abort();
443         }
444
445         switch (stdio_helper_mode) {
446         case GSS_SPNEGO_SERVER:
447                 x_fprintf(x_stdout, "%s %s %s\n", reply_code, 
448                           out_base64 ? out_base64 : "*", 
449                           reply_arg ? reply_arg : "*");
450
451         default:
452                 if (out_base64) {
453                         x_fprintf(x_stdout, "%s %s\n", reply_code, out_base64);
454                 } else if (reply_arg) {
455                         x_fprintf(x_stdout, "%s %s\n", reply_code, reply_arg);
456                 } else {
457                         x_fprintf(x_stdout, "%s\n", reply_code);
458                 }
459         }
460
461         SAFE_FREE(out_base64);
462         return;
463 }
464
465 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode, 
466                                          char *buf, int length, void **private) 
467 {
468         char *request, *parameter;      
469         static DATA_BLOB challenge;
470         static DATA_BLOB lm_response;
471         static DATA_BLOB nt_response;
472         static char *full_username;
473         static char *username;
474         static char *domain;
475         static char *plaintext_password;
476         static BOOL ntlm_server_1_user_session_key;
477         static BOOL ntlm_server_1_lm_session_key;
478         
479         if (strequal(buf, ".")) {
480                 if (!full_username && !username) {      
481                         x_fprintf(x_stdout, "Error: No username supplied!\n");
482                 } else if (plaintext_password) {
483                         /* handle this request as plaintext */
484                         if (!full_username) {
485                                 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(), username) == -1) {
486                                         x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
487                                         return;
488                                 }
489                         }
490                         if (check_plaintext_auth(full_username, plaintext_password, False)) {
491                                 x_fprintf(x_stdout, "Authenticated: Yes\n");
492                         } else {
493                                 x_fprintf(x_stdout, "Authenticated: No\n");
494                         }
495                 } else if (!lm_response.data && !nt_response.data) {
496                         x_fprintf(x_stdout, "Error: No password supplied!\n");
497                 } else if (!challenge.data) {   
498                         x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
499                 } else {
500                         char *error_string = NULL;
501                         DATA_BLOB lm_key;
502                         DATA_BLOB user_session_key;
503                         uint32 flags = 0;
504
505                         if (full_username && !username) {
506                                 fstring fstr_user;
507                                 fstring fstr_domain;
508                                 
509                                 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
510                                         /* username might be 'tainted', don't print into our new-line deleimianted stream */
511                                         x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
512                                 }
513                                 SAFE_FREE(username);
514                                 SAFE_FREE(domain);
515                                 username = smb_xstrdup(fstr_user);
516                                 domain = smb_xstrdup(fstr_domain);
517                         }
518
519                         if (!domain) {
520                                 domain = smb_xstrdup(lp_workgroup());
521                         }
522
523                         if (ntlm_server_1_lm_session_key) 
524                                 flags |= NTLM_AUTH_FLAG_LMKEY;
525                         
526                         if (ntlm_server_1_user_session_key) 
527                                 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
528
529                         if (!NT_STATUS_IS_OK(
530                                     local_pw_check_specified(username, 
531                                                               domain, 
532                                                               lp_netbios_name(),
533                                                               &challenge, 
534                                                               &lm_response, 
535                                                               &nt_response, 
536                                                               flags, 
537                                                               &lm_key, 
538                                                               &user_session_key,
539                                                               &error_string,
540                                                               NULL))) {
541
542                                 x_fprintf(x_stdout, "Authenticated: No\n");
543                                 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
544                                 SAFE_FREE(error_string);
545                         } else {
546                                 static char zeros[16];
547                                 char *hex_lm_key;
548                                 char *hex_user_session_key;
549
550                                 x_fprintf(x_stdout, "Authenticated: Yes\n");
551
552                                 if (ntlm_server_1_lm_session_key 
553                                     && lm_key.length 
554                                     && (memcmp(zeros, lm_key.data, 
555                                                                 lm_key.length) != 0)) {
556                                         hex_encode(lm_key.data,
557                                                    lm_key.length,
558                                                    &hex_lm_key);
559                                         x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
560                                         SAFE_FREE(hex_lm_key);
561                                 }
562
563                                 if (ntlm_server_1_user_session_key 
564                                     && user_session_key.length 
565                                     && (memcmp(zeros, user_session_key.data, 
566                                                user_session_key.length) != 0)) {
567                                         hex_encode(user_session_key.data, 
568                                                    user_session_key.length, 
569                                                    &hex_user_session_key);
570                                         x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
571                                         SAFE_FREE(hex_user_session_key);
572                                 }
573                         }
574                 }
575                 /* clear out the state */
576                 challenge = data_blob(NULL, 0);
577                 nt_response = data_blob(NULL, 0);
578                 lm_response = data_blob(NULL, 0);
579                 SAFE_FREE(full_username);
580                 SAFE_FREE(username);
581                 SAFE_FREE(domain);
582                 SAFE_FREE(plaintext_password);
583                 ntlm_server_1_user_session_key = False;
584                 ntlm_server_1_lm_session_key = False;
585                 x_fprintf(x_stdout, ".\n");
586
587                 return;
588         }
589
590         request = buf;
591
592         /* Indicates a base64 encoded structure */
593         parameter = strstr(request, ":: ");
594         if (!parameter) {
595                 parameter = strstr(request, ": ");
596                 
597                 if (!parameter) {
598                         DEBUG(0, ("Parameter not found!\n"));
599                         x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
600                         return;
601                 }
602                 
603                 parameter[0] ='\0';
604                 parameter++;
605                 parameter[0] ='\0';
606                 parameter++;
607
608         } else {
609                 parameter[0] ='\0';
610                 parameter++;
611                 parameter[0] ='\0';
612                 parameter++;
613                 parameter[0] ='\0';
614                 parameter++;
615
616                 base64_decode_inplace(parameter);
617         }
618
619         if (strequal(request, "LANMAN-Challenge")) {
620                 challenge = strhex_to_data_blob(parameter);
621                 if (challenge.length != 8) {
622                         x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n", 
623                                   parameter,
624                                   (int)challenge.length);
625                         challenge = data_blob(NULL, 0);
626                 }
627         } else if (strequal(request, "NT-Response")) {
628                 nt_response = strhex_to_data_blob(parameter);
629                 if (nt_response.length < 24) {
630                         x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n", 
631                                   parameter,
632                                   (int)nt_response.length);
633                         nt_response = data_blob(NULL, 0);
634                 }
635         } else if (strequal(request, "LANMAN-Response")) {
636                 lm_response = strhex_to_data_blob(parameter);
637                 if (lm_response.length != 24) {
638                         x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n", 
639                                   parameter,
640                                   (int)lm_response.length);
641                         lm_response = data_blob(NULL, 0);
642                 }
643         } else if (strequal(request, "Password")) {
644                 plaintext_password = smb_xstrdup(parameter);
645         } else if (strequal(request, "NT-Domain")) {
646                 domain = smb_xstrdup(parameter);
647         } else if (strequal(request, "Username")) {
648                 username = smb_xstrdup(parameter);
649         } else if (strequal(request, "Full-Username")) {
650                 full_username = smb_xstrdup(parameter);
651         } else if (strequal(request, "Request-User-Session-Key")) {
652                 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
653         } else if (strequal(request, "Request-LanMan-Session-Key")) {
654                 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
655         } else {
656                 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
657         }
658 }
659
660 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn, void *private) 
661 {
662         char buf[SQUID_BUFFER_SIZE+1];
663         int length;
664         char *c;
665         static BOOL err;
666
667         /* this is not a typo - x_fgets doesn't work too well under squid */
668         if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
669                 if (ferror(stdin)) {
670                         DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
671                                   strerror(ferror(stdin))));
672                         
673                         exit(1);    /* BIIG buffer */
674                 }
675                 exit(0);
676         }
677     
678         c=memchr(buf,'\n',sizeof(buf)-1);
679         if (c) {
680                 *c = '\0';
681                 length = c-buf;
682         } else {
683                 err = 1;
684                 return;
685         }
686         if (err) {
687                 DEBUG(2, ("Oversized message\n"));
688                 x_fprintf(x_stderr, "ERR\n");
689                 err = 0;
690                 return;
691         }
692
693         DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
694
695         if (buf[0] == '\0') {
696                 DEBUG(2, ("Invalid Request\n"));
697                 x_fprintf(x_stderr, "ERR\n");
698                 return;
699         }
700         
701         fn(helper_mode, buf, length, private);
702 }
703
704 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
705         void *private = NULL;
706         /* initialize FDescs */
707         x_setbuf(x_stdout, NULL);
708         x_setbuf(x_stderr, NULL);
709         while(1) {
710                 manage_squid_request(stdio_mode, fn, &private);
711         }
712 }
713
714
715 /* Main program */
716
717 enum {
718         OPT_USERNAME = 1000,
719         OPT_DOMAIN,
720         OPT_WORKSTATION,
721         OPT_CHALLENGE,
722         OPT_RESPONSE,
723         OPT_LM,
724         OPT_NT,
725         OPT_PASSWORD,
726         OPT_LM_KEY,
727         OPT_USER_SESSION_KEY,
728         OPT_DIAGNOSTICS,
729         OPT_REQUIRE_MEMBERSHIP
730 };
731
732  int main(int argc, const char **argv)
733 {
734         static const char *helper_protocol;
735         int opt;
736
737         poptContext pc;
738
739         /* NOTE: DO NOT change this interface without considering the implications!
740            This is an external interface, which other programs will use to interact 
741            with this helper.
742         */
743
744         /* We do not use single-letter command abbreviations, because they harm future 
745            interface stability. */
746
747         struct poptOption long_options[] = {
748                 POPT_AUTOHELP
749                 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
750                 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
751                 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
752                 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},             
753                 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},            
754                 POPT_COMMON_SAMBA
755                 POPT_TABLEEND
756         };
757
758         /* Samba client initialisation */
759
760         setup_logging("ntlm_auth", DEBUG_STDOUT);
761
762         if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
763                 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
764                         dyn_CONFIGFILE, strerror(errno));
765                 exit(1);
766         }
767
768         /* Parse options */
769
770         pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
771
772         /* Parse command line options */
773
774         if (argc == 1) {
775                 poptPrintHelp(pc, stderr, 0);
776                 return 1;
777         }
778
779         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
780                             POPT_CONTEXT_KEEP_FIRST);
781
782         while((opt = poptGetNextOpt(pc)) != -1) {
783                 if (opt < -1) {
784                         break;
785                 }
786         }
787         if (opt < -1) {
788                 fprintf(stderr, "%s: %s\n",
789                         poptBadOption(pc, POPT_BADOPTION_NOALIAS),
790                         poptStrerror(opt));
791                 return 1;
792         }
793
794         if (opt_domain == NULL) {
795                 opt_domain = lp_workgroup();
796         }
797
798         if (helper_protocol) {
799                 int i;
800                 for (i=0; i<NUM_HELPER_MODES; i++) {
801                         if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
802                                 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
803                                 exit(0);
804                         }
805                 }
806                 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
807
808                 for (i=0; i<NUM_HELPER_MODES; i++) {
809                         x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
810                 }
811
812                 exit(1);
813         }
814
815         if (!opt_username) {
816                 x_fprintf(x_stderr, "username must be specified!\n\n");
817                 poptPrintHelp(pc, stderr, 0);
818                 exit(1);
819         }
820
821         if (opt_workstation == NULL) {
822                 opt_workstation = lp_netbios_name();
823         }
824
825         if (!opt_password) {
826                 opt_password = getpass("password: ");
827         }
828
829         {
830                 char *user;
831
832                 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(), opt_username);
833                 if (!check_plaintext_auth(user, opt_password, True)) {
834                         return 1;
835                 }
836         }
837
838         /* Exit code */
839
840         poptFreeContext(pc);
841         return 0;
842 }