r2546: Remove another strupper_m() that we don't need.
[kai/samba.git] / source4 / 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-server", 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                 if (out_base64) {
448                         x_fprintf(x_stdout, "%s %s %s\n", reply_code, out_base64, reply_arg);
449                 } else if (reply_arg) {
450                         x_fprintf(x_stdout, "%s %s\n", reply_code, reply_arg);
451                 } else {
452                         x_fprintf(x_stdout, "%s\n", reply_code);
453                 }
454         default:
455                 if (out_base64) {
456                         x_fprintf(x_stdout, "%s %s\n", reply_code, out_base64);
457                 } else if (reply_arg) {
458                         x_fprintf(x_stdout, "%s %s\n", reply_code, reply_arg);
459                 } else {
460                         x_fprintf(x_stdout, "%s\n", reply_code);
461                 }
462         }
463
464         SAFE_FREE(out_base64);
465         return;
466 }
467
468 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode, 
469                                          char *buf, int length, void **private) 
470 {
471         char *request, *parameter;      
472         static DATA_BLOB challenge;
473         static DATA_BLOB lm_response;
474         static DATA_BLOB nt_response;
475         static char *full_username;
476         static char *username;
477         static char *domain;
478         static char *plaintext_password;
479         static BOOL ntlm_server_1_user_session_key;
480         static BOOL ntlm_server_1_lm_session_key;
481         
482         if (strequal(buf, ".")) {
483                 if (!full_username && !username) {      
484                         x_fprintf(x_stdout, "Error: No username supplied!\n");
485                 } else if (plaintext_password) {
486                         /* handle this request as plaintext */
487                         if (!full_username) {
488                                 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(), username) == -1) {
489                                         x_fprintf(x_stdout, "Error: Out of memory in asprintf!\n.\n");
490                                         return;
491                                 }
492                         }
493                         if (check_plaintext_auth(full_username, plaintext_password, False)) {
494                                 x_fprintf(x_stdout, "Authenticated: Yes\n");
495                         } else {
496                                 x_fprintf(x_stdout, "Authenticated: No\n");
497                         }
498                 } else if (!lm_response.data && !nt_response.data) {
499                         x_fprintf(x_stdout, "Error: No password supplied!\n");
500                 } else if (!challenge.data) {   
501                         x_fprintf(x_stdout, "Error: No lanman-challenge supplied!\n");
502                 } else {
503                         char *error_string = NULL;
504                         DATA_BLOB lm_key;
505                         DATA_BLOB user_session_key;
506                         uint32 flags = 0;
507
508                         if (full_username && !username) {
509                                 fstring fstr_user;
510                                 fstring fstr_domain;
511                                 
512                                 if (!parse_ntlm_auth_domain_user(full_username, fstr_user, fstr_domain)) {
513                                         /* username might be 'tainted', don't print into our new-line deleimianted stream */
514                                         x_fprintf(x_stdout, "Error: Could not parse into domain and username\n");
515                                 }
516                                 SAFE_FREE(username);
517                                 SAFE_FREE(domain);
518                                 username = smb_xstrdup(fstr_user);
519                                 domain = smb_xstrdup(fstr_domain);
520                         }
521
522                         if (!domain) {
523                                 domain = smb_xstrdup(lp_workgroup());
524                         }
525
526                         if (ntlm_server_1_lm_session_key) 
527                                 flags |= NTLM_AUTH_FLAG_LMKEY;
528                         
529                         if (ntlm_server_1_user_session_key) 
530                                 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
531
532                         if (!NT_STATUS_IS_OK(
533                                     local_pw_check_specified(username, 
534                                                               domain, 
535                                                               lp_netbios_name(),
536                                                               &challenge, 
537                                                               &lm_response, 
538                                                               &nt_response, 
539                                                               flags, 
540                                                               &lm_key, 
541                                                               &user_session_key,
542                                                               &error_string,
543                                                               NULL))) {
544
545                                 x_fprintf(x_stdout, "Authenticated: No\n");
546                                 x_fprintf(x_stdout, "Authentication-Error: %s\n.\n", error_string);
547                                 SAFE_FREE(error_string);
548                         } else {
549                                 static char zeros[16];
550                                 char *hex_lm_key;
551                                 char *hex_user_session_key;
552
553                                 x_fprintf(x_stdout, "Authenticated: Yes\n");
554
555                                 if (ntlm_server_1_lm_session_key 
556                                     && lm_key.length 
557                                     && (memcmp(zeros, lm_key.data, 
558                                                                 lm_key.length) != 0)) {
559                                         hex_encode(lm_key.data,
560                                                    lm_key.length,
561                                                    &hex_lm_key);
562                                         x_fprintf(x_stdout, "LANMAN-Session-Key: %s\n", hex_lm_key);
563                                         SAFE_FREE(hex_lm_key);
564                                 }
565
566                                 if (ntlm_server_1_user_session_key 
567                                     && user_session_key.length 
568                                     && (memcmp(zeros, user_session_key.data, 
569                                                user_session_key.length) != 0)) {
570                                         hex_encode(user_session_key.data, 
571                                                    user_session_key.length, 
572                                                    &hex_user_session_key);
573                                         x_fprintf(x_stdout, "User-Session-Key: %s\n", hex_user_session_key);
574                                         SAFE_FREE(hex_user_session_key);
575                                 }
576                         }
577                 }
578                 /* clear out the state */
579                 challenge = data_blob(NULL, 0);
580                 nt_response = data_blob(NULL, 0);
581                 lm_response = data_blob(NULL, 0);
582                 SAFE_FREE(full_username);
583                 SAFE_FREE(username);
584                 SAFE_FREE(domain);
585                 SAFE_FREE(plaintext_password);
586                 ntlm_server_1_user_session_key = False;
587                 ntlm_server_1_lm_session_key = False;
588                 x_fprintf(x_stdout, ".\n");
589
590                 return;
591         }
592
593         request = buf;
594
595         /* Indicates a base64 encoded structure */
596         parameter = strstr(request, ":: ");
597         if (!parameter) {
598                 parameter = strstr(request, ": ");
599                 
600                 if (!parameter) {
601                         DEBUG(0, ("Parameter not found!\n"));
602                         x_fprintf(x_stdout, "Error: Parameter not found!\n.\n");
603                         return;
604                 }
605                 
606                 parameter[0] ='\0';
607                 parameter++;
608                 parameter[0] ='\0';
609                 parameter++;
610
611         } else {
612                 parameter[0] ='\0';
613                 parameter++;
614                 parameter[0] ='\0';
615                 parameter++;
616                 parameter[0] ='\0';
617                 parameter++;
618
619                 base64_decode_inplace(parameter);
620         }
621
622         if (strequal(request, "LANMAN-Challenge")) {
623                 challenge = strhex_to_data_blob(parameter);
624                 if (challenge.length != 8) {
625                         x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n", 
626                                   parameter,
627                                   (int)challenge.length);
628                         challenge = data_blob(NULL, 0);
629                 }
630         } else if (strequal(request, "NT-Response")) {
631                 nt_response = strhex_to_data_blob(parameter);
632                 if (nt_response.length < 24) {
633                         x_fprintf(x_stdout, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n", 
634                                   parameter,
635                                   (int)nt_response.length);
636                         nt_response = data_blob(NULL, 0);
637                 }
638         } else if (strequal(request, "LANMAN-Response")) {
639                 lm_response = strhex_to_data_blob(parameter);
640                 if (lm_response.length != 24) {
641                         x_fprintf(x_stdout, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n", 
642                                   parameter,
643                                   (int)lm_response.length);
644                         lm_response = data_blob(NULL, 0);
645                 }
646         } else if (strequal(request, "Password")) {
647                 plaintext_password = smb_xstrdup(parameter);
648         } else if (strequal(request, "NT-Domain")) {
649                 domain = smb_xstrdup(parameter);
650         } else if (strequal(request, "Username")) {
651                 username = smb_xstrdup(parameter);
652         } else if (strequal(request, "Full-Username")) {
653                 full_username = smb_xstrdup(parameter);
654         } else if (strequal(request, "Request-User-Session-Key")) {
655                 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
656         } else if (strequal(request, "Request-LanMan-Session-Key")) {
657                 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
658         } else {
659                 x_fprintf(x_stdout, "Error: Unknown request %s\n.\n", request);
660         }
661 }
662
663 static void manage_squid_request(enum stdio_helper_mode helper_mode, stdio_helper_function fn, void *private) 
664 {
665         char buf[SQUID_BUFFER_SIZE+1];
666         int length;
667         char *c;
668         static BOOL err;
669
670         /* this is not a typo - x_fgets doesn't work too well under squid */
671         if (fgets(buf, sizeof(buf)-1, stdin) == NULL) {
672                 if (ferror(stdin)) {
673                         DEBUG(1, ("fgets() failed! dying..... errno=%d (%s)\n", ferror(stdin),
674                                   strerror(ferror(stdin))));
675                         
676                         exit(1);    /* BIIG buffer */
677                 }
678                 exit(0);
679         }
680     
681         c=memchr(buf,'\n',sizeof(buf)-1);
682         if (c) {
683                 *c = '\0';
684                 length = c-buf;
685         } else {
686                 err = 1;
687                 return;
688         }
689         if (err) {
690                 DEBUG(2, ("Oversized message\n"));
691                 x_fprintf(x_stderr, "ERR\n");
692                 err = 0;
693                 return;
694         }
695
696         DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
697
698         if (buf[0] == '\0') {
699                 DEBUG(2, ("Invalid Request\n"));
700                 x_fprintf(x_stderr, "ERR\n");
701                 return;
702         }
703         
704         fn(helper_mode, buf, length, private);
705 }
706
707 static void squid_stream(enum stdio_helper_mode stdio_mode, stdio_helper_function fn) {
708         void *private = NULL;
709         /* initialize FDescs */
710         x_setbuf(x_stdout, NULL);
711         x_setbuf(x_stderr, NULL);
712         while(1) {
713                 manage_squid_request(stdio_mode, fn, &private);
714         }
715 }
716
717
718 /* Main program */
719
720 enum {
721         OPT_USERNAME = 1000,
722         OPT_DOMAIN,
723         OPT_WORKSTATION,
724         OPT_CHALLENGE,
725         OPT_RESPONSE,
726         OPT_LM,
727         OPT_NT,
728         OPT_PASSWORD,
729         OPT_LM_KEY,
730         OPT_USER_SESSION_KEY,
731         OPT_DIAGNOSTICS,
732         OPT_REQUIRE_MEMBERSHIP
733 };
734
735  int main(int argc, const char **argv)
736 {
737         static const char *helper_protocol;
738         int opt;
739
740         poptContext pc;
741
742         /* NOTE: DO NOT change this interface without considering the implications!
743            This is an external interface, which other programs will use to interact 
744            with this helper.
745         */
746
747         /* We do not use single-letter command abbreviations, because they harm future 
748            interface stability. */
749
750         struct poptOption long_options[] = {
751                 POPT_AUTOHELP
752                 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
753                 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
754                 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
755                 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},             
756                 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},            
757                 POPT_COMMON_SAMBA
758                 POPT_TABLEEND
759         };
760
761         /* Samba client initialisation */
762
763         setup_logging("ntlm_auth", DEBUG_STDOUT);
764
765         if (!lp_load(dyn_CONFIGFILE, True, False, False)) {
766                 d_fprintf(stderr, "wbinfo: error opening config file %s. Error was %s\n",
767                         dyn_CONFIGFILE, strerror(errno));
768                 exit(1);
769         }
770
771         /* Parse options */
772
773         pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
774
775         /* Parse command line options */
776
777         if (argc == 1) {
778                 poptPrintHelp(pc, stderr, 0);
779                 return 1;
780         }
781
782         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
783                             POPT_CONTEXT_KEEP_FIRST);
784
785         while((opt = poptGetNextOpt(pc)) != -1) {
786                 if (opt < -1) {
787                         break;
788                 }
789         }
790         if (opt < -1) {
791                 fprintf(stderr, "%s: %s\n",
792                         poptBadOption(pc, POPT_BADOPTION_NOALIAS),
793                         poptStrerror(opt));
794                 return 1;
795         }
796
797         if (opt_domain == NULL) {
798                 opt_domain = lp_workgroup();
799         }
800
801         if (helper_protocol) {
802                 int i;
803                 for (i=0; i<NUM_HELPER_MODES; i++) {
804                         if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
805                                 squid_stream(stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
806                                 exit(0);
807                         }
808                 }
809                 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
810
811                 for (i=0; i<NUM_HELPER_MODES; i++) {
812                         x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
813                 }
814
815                 exit(1);
816         }
817
818         if (!opt_username) {
819                 x_fprintf(x_stderr, "username must be specified!\n\n");
820                 poptPrintHelp(pc, stderr, 0);
821                 exit(1);
822         }
823
824         if (opt_workstation == NULL) {
825                 opt_workstation = lp_netbios_name();
826         }
827
828         if (!opt_password) {
829                 opt_password = getpass("password: ");
830         }
831
832         {
833                 char *user;
834
835                 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(), opt_username);
836                 if (!check_plaintext_auth(user, opt_password, True)) {
837                         return 1;
838                 }
839         }
840
841         /* Exit code */
842
843         poptFreeContext(pc);
844         return 0;
845 }