root/source4/utils/ntlm_auth.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mux_printf
  2. parse_ntlm_auth_domain_user
  3. base64_decode_data_blob
  4. base64_encode_data_blob
  5. base64_decode_inplace
  6. check_plaintext_auth
  7. local_pw_check_specified
  8. manage_squid_basic_request
  9. manage_gensec_get_pw_request
  10. get_password
  11. in_list
  12. gensec_want_feature_list
  13. manage_gensec_request
  14. manage_ntlm_server_1_request
  15. manage_squid_request
  16. squid_stream
  17. main

   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 3 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, see <http://www.gnu.org/licenses/>.
  22 */
  23 
  24 #include "includes.h"
  25 #include "system/filesys.h"
  26 #include "lib/cmdline/popt_common.h"
  27 #include "lib/ldb/include/ldb.h"
  28 #include "auth/credentials/credentials.h"
  29 #include "auth/gensec/gensec.h"
  30 #include "auth/auth.h"
  31 #include "librpc/gen_ndr/ndr_netlogon.h"
  32 #include "auth/auth_sam.h"
  33 #include "auth/ntlm/ntlm_check.h"
  34 #include "libcli/auth/libcli_auth.h"
  35 #include "libcli/security/security.h"
  36 #include "lib/events/events.h"
  37 #include "lib/messaging/messaging.h"
  38 #include "lib/messaging/irpc.h"
  39 #include "auth/ntlmssp/ntlmssp.h"
  40 #include "param/param.h"
  41 
  42 #define INITIAL_BUFFER_SIZE 300
  43 #define MAX_BUFFER_SIZE 63000
  44 
  45 enum stdio_helper_mode {
  46         SQUID_2_4_BASIC,
  47         SQUID_2_5_BASIC,
  48         SQUID_2_5_NTLMSSP,
  49         NTLMSSP_CLIENT_1,
  50         GSS_SPNEGO_CLIENT,
  51         GSS_SPNEGO_SERVER,
  52         NTLM_SERVER_1,
  53         NUM_HELPER_MODES
  54 };
  55 
  56 #define NTLM_AUTH_FLAG_USER_SESSION_KEY     0x0004
  57 #define NTLM_AUTH_FLAG_LMKEY                0x0008
  58 
  59 
  60 typedef void (*stdio_helper_function)(enum stdio_helper_mode stdio_helper_mode, 
  61                                       struct loadparm_context *lp_ctx,
  62                                       char *buf, int length, void **private1,
  63                                       unsigned int mux_id, void **private2);
  64 
  65 static void manage_squid_basic_request (enum stdio_helper_mode stdio_helper_mode, 
  66                                         struct loadparm_context *lp_ctx,
  67                                         char *buf, int length, void **private1,
  68                                         unsigned int mux_id, void **private2);
  69 
  70 static void manage_gensec_request (enum stdio_helper_mode stdio_helper_mode, 
  71                                    struct loadparm_context *lp_ctx,
  72                                    char *buf, int length, void **private1,
  73                                    unsigned int mux_id, void **private2);
  74 
  75 static void manage_ntlm_server_1_request (enum stdio_helper_mode stdio_helper_mode, 
  76                                           struct loadparm_context *lp_ctx,
  77                                           char *buf, int length, void **private1,
  78                                           unsigned int mux_id, void **private2);
  79 
  80 static void manage_squid_request(struct loadparm_context *lp_ctx,
  81                                  enum stdio_helper_mode helper_mode, 
  82                                  stdio_helper_function fn, void **private2);
  83 
  84 static const struct {
  85         enum stdio_helper_mode mode;
  86         const char *name;
  87         stdio_helper_function fn;
  88 } stdio_helper_protocols[] = {
  89         { SQUID_2_4_BASIC, "squid-2.4-basic", manage_squid_basic_request},
  90         { SQUID_2_5_BASIC, "squid-2.5-basic", manage_squid_basic_request},
  91         { SQUID_2_5_NTLMSSP, "squid-2.5-ntlmssp", manage_gensec_request},
  92         { GSS_SPNEGO_CLIENT, "gss-spnego-client", manage_gensec_request},
  93         { GSS_SPNEGO_SERVER, "gss-spnego", manage_gensec_request},
  94         { NTLMSSP_CLIENT_1, "ntlmssp-client-1", manage_gensec_request},
  95         { NTLM_SERVER_1, "ntlm-server-1", manage_ntlm_server_1_request},
  96         { NUM_HELPER_MODES, NULL, NULL}
  97 };
  98 
  99 extern int winbindd_fd;
 100 
 101 static const char *opt_username;
 102 static const char *opt_domain;
 103 static const char *opt_workstation;
 104 static const char *opt_password;
 105 static int opt_multiplex;
 106 static int use_cached_creds;
 107 
 108 
 109 static void mux_printf(unsigned int mux_id, const char *format, ...) PRINTF_ATTRIBUTE(2, 3);
     /* [<][>][^][v][top][bottom][index][help] */
 110 
 111 static void mux_printf(unsigned int mux_id, const char *format, ...)
 112 {
 113         va_list ap;
 114 
 115         if (opt_multiplex) {
 116                 x_fprintf(x_stdout, "%d ", mux_id);
 117         }
 118 
 119         va_start(ap, format);
 120         x_vfprintf(x_stdout, format, ap);
 121         va_end(ap);
 122 }
 123 
 124 
 125 
 126 /* Copy of parse_domain_user from winbindd_util.c.  Parse a string of the
 127    form DOMAIN/user into a domain and a user */
 128 
 129 static bool parse_ntlm_auth_domain_user(const char *domuser, char **domain, 
     /* [<][>][^][v][top][bottom][index][help] */
 130                                                                                 char **user, char winbind_separator)
 131 {
 132 
 133         char *p = strchr(domuser, winbind_separator);
 134 
 135         if (!p) {
 136                 return false;
 137         }
 138         
 139         *user = smb_xstrdup(p+1);
 140         *domain = smb_xstrdup(domuser);
 141         (*domain)[PTR_DIFF(p, domuser)] = 0;
 142 
 143         return true;
 144 }
 145 
 146 /**
 147  * Decode a base64 string into a DATA_BLOB - simple and slow algorithm
 148  **/
 149 static DATA_BLOB base64_decode_data_blob(TALLOC_CTX *mem_ctx, const char *s)
     /* [<][>][^][v][top][bottom][index][help] */
 150 {
 151         DATA_BLOB ret = data_blob_talloc(mem_ctx, s, strlen(s)+1);
 152         ret.length = ldb_base64_decode((char *)ret.data);
 153         return ret;
 154 }
 155 
 156 /**
 157  * Encode a base64 string into a talloc()ed string caller to free.
 158  **/
 159 static char *base64_encode_data_blob(TALLOC_CTX *mem_ctx, DATA_BLOB data)
     /* [<][>][^][v][top][bottom][index][help] */
 160 {
 161         return ldb_base64_encode(mem_ctx, (const char *)data.data, data.length);
 162 }
 163 
 164 /**
 165  * Decode a base64 string in-place - wrapper for the above
 166  **/
 167 static void base64_decode_inplace(char *s)
     /* [<][>][^][v][top][bottom][index][help] */
 168 {
 169         ldb_base64_decode(s);
 170 }
 171 
 172 
 173 
 174 /* Authenticate a user with a plaintext password */
 175 
 176 static bool check_plaintext_auth(const char *user, const char *pass, 
     /* [<][>][^][v][top][bottom][index][help] */
 177                                  bool stdout_diagnostics)
 178 {
 179         return (strcmp(pass, opt_password) == 0);
 180 }
 181 
 182 /* authenticate a user with an encrypted username/password */
 183 
 184 static NTSTATUS local_pw_check_specified(struct loadparm_context *lp_ctx,
     /* [<][>][^][v][top][bottom][index][help] */
 185                                          const char *username, 
 186                                          const char *domain, 
 187                                          const char *workstation,
 188                                          const DATA_BLOB *challenge, 
 189                                          const DATA_BLOB *lm_response, 
 190                                          const DATA_BLOB *nt_response, 
 191                                          uint32_t flags, 
 192                                          DATA_BLOB *lm_session_key, 
 193                                          DATA_BLOB *user_session_key, 
 194                                          char **error_string, 
 195                                          char **unix_name) 
 196 {
 197         NTSTATUS nt_status;
 198         struct samr_Password lm_pw, nt_pw;
 199         struct samr_Password *lm_pwd, *nt_pwd;
 200         TALLOC_CTX *mem_ctx = talloc_init("local_pw_check_specified");
 201         if (!mem_ctx) {
 202                 nt_status = NT_STATUS_NO_MEMORY;
 203         } else {
 204                 
 205                 E_md4hash(opt_password, nt_pw.hash);
 206                 if (E_deshash(opt_password, lm_pw.hash)) {
 207                         lm_pwd = &lm_pw;
 208                 } else {
 209                         lm_pwd = NULL;
 210                 }
 211                 nt_pwd = &nt_pw;
 212                 
 213                 
 214                 nt_status = ntlm_password_check(mem_ctx, 
 215                                                 lp_lanman_auth(lp_ctx),
 216                                                 lp_ntlm_auth(lp_ctx),
 217                                                 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
 218                                                 MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
 219                                                 challenge,
 220                                                 lm_response,
 221                                                 nt_response,
 222                                                 username,
 223                                                 username,
 224                                                 domain,
 225                                                 lm_pwd, nt_pwd, user_session_key, lm_session_key);
 226                 
 227                 if (NT_STATUS_IS_OK(nt_status)) {
 228                         if (unix_name) {
 229                                 asprintf(unix_name, 
 230                                          "%s%c%s", domain,
 231                                          *lp_winbind_separator(lp_ctx), 
 232                                          username);
 233                         }
 234                 } else {
 235                         DEBUG(3, ("Login for user [%s]\\[%s]@[%s] failed due to [%s]\n", 
 236                                   domain, username, workstation, 
 237                                   nt_errstr(nt_status)));
 238                 }
 239                 talloc_free(mem_ctx);
 240         }
 241         if (error_string) {
 242                 *error_string = strdup(nt_errstr(nt_status));
 243         }
 244         return nt_status;
 245         
 246         
 247 }
 248 
 249 static void manage_squid_basic_request(enum stdio_helper_mode stdio_helper_mode, 
     /* [<][>][^][v][top][bottom][index][help] */
 250                                        struct loadparm_context *lp_ctx,
 251                                        char *buf, int length, void **private1,
 252                                        unsigned int mux_id, void **private2) 
 253 {
 254         char *user, *pass;      
 255         user=buf;
 256         
 257         pass = memchr(buf, ' ', length);
 258         if (!pass) {
 259                 DEBUG(2, ("Password not found. Denying access\n"));
 260                 mux_printf(mux_id, "ERR\n");
 261                 return;
 262         }
 263         *pass='\0';
 264         pass++;
 265         
 266         if (stdio_helper_mode == SQUID_2_5_BASIC) {
 267                 rfc1738_unescape(user);
 268                 rfc1738_unescape(pass);
 269         }
 270         
 271         if (check_plaintext_auth(user, pass, false)) {
 272                 mux_printf(mux_id, "OK\n");
 273         } else {
 274                 mux_printf(mux_id, "ERR\n");
 275         }
 276 }
 277 
 278 /* This is a bit hairy, but the basic idea is to do a password callback
 279    to the calling application.  The callback comes from within gensec */
 280 
 281 static void manage_gensec_get_pw_request(enum stdio_helper_mode stdio_helper_mode, 
     /* [<][>][^][v][top][bottom][index][help] */
 282                                          struct loadparm_context *lp_ctx,
 283                                          char *buf, int length, void **private1,
 284                                          unsigned int mux_id, void **password)  
 285 {
 286         DATA_BLOB in;
 287         if (strlen(buf) < 2) {
 288                 DEBUG(1, ("query [%s] invalid", buf));
 289                 mux_printf(mux_id, "BH Query invalid\n");
 290                 return;
 291         }
 292 
 293         if (strlen(buf) > 3) {
 294                 in = base64_decode_data_blob(NULL, buf + 3);
 295         } else {
 296                 in = data_blob(NULL, 0);
 297         }
 298 
 299         if (strncmp(buf, "PW ", 3) == 0) {
 300 
 301                 *password = talloc_strndup(*private1 /* hopefully the right gensec context, useful to use for talloc */,
 302                                            (const char *)in.data, in.length);
 303                 
 304                 if (*password == NULL) {
 305                         DEBUG(1, ("Out of memory\n"));
 306                         mux_printf(mux_id, "BH Out of memory\n");
 307                         data_blob_free(&in);
 308                         return;
 309                 }
 310 
 311                 mux_printf(mux_id, "OK\n");
 312                 data_blob_free(&in);
 313                 return;
 314         }
 315         DEBUG(1, ("Asked for (and expected) a password\n"));
 316         mux_printf(mux_id, "BH Expected a password\n");
 317         data_blob_free(&in);
 318 }
 319 
 320 /** 
 321  * Callback for password credentials.  This is not async, and when
 322  * GENSEC and the credentials code is made async, it will look rather
 323  * different.
 324  */
 325 
 326 static const char *get_password(struct cli_credentials *credentials) 
     /* [<][>][^][v][top][bottom][index][help] */
 327 {
 328         char *password = NULL;
 329         
 330         /* Ask for a password */
 331         mux_printf((unsigned int)(uintptr_t)credentials->priv_data, "PW\n");
 332         credentials->priv_data = NULL;
 333 
 334         manage_squid_request(cmdline_lp_ctx, NUM_HELPER_MODES /* bogus */, manage_gensec_get_pw_request, (void **)&password);
 335         return password;
 336 }
 337 
 338 /**
 339  Check if a string is part of a list.
 340 **/
 341 static bool in_list(const char *s, const char *list, bool casesensitive)
     /* [<][>][^][v][top][bottom][index][help] */
 342 {
 343         char *tok;
 344         size_t tok_len = 1024;
 345         const char *p=list;
 346 
 347         if (!list)
 348                 return false;
 349 
 350         tok = (char *)malloc(tok_len);
 351         if (!tok) {
 352                 return false;
 353         }
 354 
 355         while (next_token(&p, tok, LIST_SEP, tok_len)) {
 356                 if ((casesensitive?strcmp:strcasecmp_m)(tok,s) == 0) {
 357                         free(tok);
 358                         return true;
 359                 }
 360         }
 361         free(tok);
 362         return false;
 363 }
 364 
 365 static void gensec_want_feature_list(struct gensec_security *state, char* feature_list)
     /* [<][>][^][v][top][bottom][index][help] */
 366 {
 367         if (in_list("NTLMSSP_FEATURE_SESSION_KEY", feature_list, true)) {
 368                 DEBUG(10, ("want GENSEC_FEATURE_SESSION_KEY\n"));
 369                 gensec_want_feature(state, GENSEC_FEATURE_SESSION_KEY);
 370         }
 371         if (in_list("NTLMSSP_FEATURE_SIGN", feature_list, true)) {
 372                 DEBUG(10, ("want GENSEC_FEATURE_SIGN\n"));
 373                 gensec_want_feature(state, GENSEC_FEATURE_SIGN);
 374         }
 375         if (in_list("NTLMSSP_FEATURE_SEAL", feature_list, true)) {
 376                 DEBUG(10, ("want GENSEC_FEATURE_SEAL\n"));
 377                 gensec_want_feature(state, GENSEC_FEATURE_SEAL);
 378         }
 379 }
 380 
 381 static void manage_gensec_request(enum stdio_helper_mode stdio_helper_mode, 
     /* [<][>][^][v][top][bottom][index][help] */
 382                                   struct loadparm_context *lp_ctx,
 383                                   char *buf, int length, void **private1,
 384                                   unsigned int mux_id, void **private2) 
 385 {
 386         DATA_BLOB in;
 387         DATA_BLOB out = data_blob(NULL, 0);
 388         char *out_base64 = NULL;
 389         const char *reply_arg = NULL;
 390         struct gensec_ntlm_state {
 391                 struct gensec_security *gensec_state;
 392                 const char *set_password;
 393         };
 394         struct gensec_ntlm_state *state;
 395         struct tevent_context *ev;
 396         struct messaging_context *msg;
 397 
 398         NTSTATUS nt_status;
 399         bool first = false;
 400         const char *reply_code;
 401         struct cli_credentials *creds;
 402 
 403         static char *want_feature_list = NULL;
 404         static DATA_BLOB session_key;
 405 
 406         TALLOC_CTX *mem_ctx;
 407 
 408         if (*private1) {
 409                 state = (struct gensec_ntlm_state *)*private1;
 410         } else {
 411                 state = talloc_zero(NULL, struct gensec_ntlm_state);
 412                 if (!state) {
 413                         mux_printf(mux_id, "BH No Memory\n");
 414                         exit(1);
 415                 }
 416                 *private1 = state;
 417                 if (opt_password) {
 418                         state->set_password = opt_password;
 419                 }
 420         }
 421         
 422         if (strlen(buf) < 2) {
 423                 DEBUG(1, ("query [%s] invalid", buf));
 424                 mux_printf(mux_id, "BH Query invalid\n");
 425                 return;
 426         }
 427 
 428         if (strlen(buf) > 3) {
 429                 if(strncmp(buf, "SF ", 3) == 0) {
 430                         DEBUG(10, ("Setting flags to negotiate\n"));
 431                         talloc_free(want_feature_list);
 432                         want_feature_list = talloc_strndup(state, buf+3, strlen(buf)-3);
 433                         mux_printf(mux_id, "OK\n");
 434                         return;
 435                 }
 436                 in = base64_decode_data_blob(NULL, buf + 3);
 437         } else {
 438                 in = data_blob(NULL, 0);
 439         }
 440 
 441         if (strncmp(buf, "YR", 2) == 0) {
 442                 if (state->gensec_state) {
 443                         talloc_free(state->gensec_state);
 444                         state->gensec_state = NULL;
 445                 }
 446         } else if ( (strncmp(buf, "OK", 2) == 0)) {
 447                 /* Just return BH, like ntlm_auth from Samba 3 does. */
 448                 mux_printf(mux_id, "BH Command expected\n");
 449                 data_blob_free(&in);
 450                 return;
 451         } else if ( (strncmp(buf, "TT ", 3) != 0) &&
 452                     (strncmp(buf, "KK ", 3) != 0) &&
 453                     (strncmp(buf, "AF ", 3) != 0) &&
 454                     (strncmp(buf, "NA ", 3) != 0) && 
 455                     (strncmp(buf, "UG", 2) != 0) && 
 456                     (strncmp(buf, "PW ", 3) != 0) &&
 457                     (strncmp(buf, "GK", 2) != 0) &&
 458                     (strncmp(buf, "GF", 2) != 0)) {
 459                 DEBUG(1, ("SPNEGO request [%s] invalid\n", buf));
 460                 mux_printf(mux_id, "BH SPNEGO request invalid\n");
 461                 data_blob_free(&in);
 462                 return;
 463         }
 464 
 465         ev = s4_event_context_init(state);
 466         if (!ev) {
 467                 exit(1);
 468         }
 469         /* setup gensec */
 470         if (!(state->gensec_state)) {
 471                 switch (stdio_helper_mode) {
 472                 case GSS_SPNEGO_CLIENT:
 473                 case NTLMSSP_CLIENT_1:
 474                         /* setup the client side */
 475 
 476                         nt_status = gensec_client_start(NULL, &state->gensec_state, ev, 
 477                                                         lp_gensec_settings(NULL, lp_ctx));
 478                         if (!NT_STATUS_IS_OK(nt_status)) {
 479                                 exit(1);
 480                         }
 481 
 482                         break;
 483                 case GSS_SPNEGO_SERVER:
 484                 case SQUID_2_5_NTLMSSP:
 485                 {
 486                         const char *winbind_method[] = { "winbind", NULL };
 487                         struct auth_context *auth_context;
 488 
 489                         msg = messaging_client_init(state, lp_messaging_path(state, lp_ctx), 
 490                                                     lp_iconv_convenience(lp_ctx), ev);
 491                         if (!msg) {
 492                                 exit(1);
 493                         }
 494                         nt_status = auth_context_create_methods(mem_ctx, 
 495                                                                 winbind_method,
 496                                                                 ev, 
 497                                                                 msg, 
 498                                                                 lp_ctx,
 499                                                                 &auth_context);
 500         
 501                         if (!NT_STATUS_IS_OK(nt_status)) {
 502                                 exit(1);
 503                         }
 504                         
 505                         if (!NT_STATUS_IS_OK(gensec_server_start(state, ev, 
 506                                                                  lp_gensec_settings(state, lp_ctx), 
 507                                                                  auth_context, &state->gensec_state))) {
 508                                 exit(1);
 509                         }
 510                         break;
 511                 }
 512                 default:
 513                         abort();
 514                 }
 515 
 516                 creds = cli_credentials_init(state->gensec_state);
 517                 cli_credentials_set_conf(creds, lp_ctx);
 518                 if (opt_username) {
 519                         cli_credentials_set_username(creds, opt_username, CRED_SPECIFIED);
 520                 }
 521                 if (opt_domain) {
 522                         cli_credentials_set_domain(creds, opt_domain, CRED_SPECIFIED);
 523                 }
 524                 if (state->set_password) {
 525                         cli_credentials_set_password(creds, state->set_password, CRED_SPECIFIED);
 526                 } else {
 527                         cli_credentials_set_password_callback(creds, get_password);
 528                         creds->priv_data = (void*)(uintptr_t)mux_id;
 529                 }
 530                 if (opt_workstation) {
 531                         cli_credentials_set_workstation(creds, opt_workstation, CRED_SPECIFIED);
 532                 }
 533                 
 534                 switch (stdio_helper_mode) {
 535                 case GSS_SPNEGO_SERVER:
 536                 case SQUID_2_5_NTLMSSP:
 537                         cli_credentials_set_machine_account(creds, lp_ctx);
 538                         break;
 539                 default:
 540                         break;
 541                 }
 542 
 543                 gensec_set_credentials(state->gensec_state, creds);
 544                 gensec_want_feature_list(state->gensec_state, want_feature_list);
 545 
 546                 switch (stdio_helper_mode) {
 547                 case GSS_SPNEGO_CLIENT:
 548                 case GSS_SPNEGO_SERVER:
 549                         nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_SPNEGO);
 550                         if (!in.length) {
 551                                 first = true;
 552                         }
 553                         break;
 554                 case NTLMSSP_CLIENT_1:
 555                         if (!in.length) {
 556                                 first = true;
 557                         }
 558                         /* fall through */
 559                 case SQUID_2_5_NTLMSSP:
 560                         nt_status = gensec_start_mech_by_oid(state->gensec_state, GENSEC_OID_NTLMSSP);
 561                         break;
 562                 default:
 563                         abort();
 564                 }
 565 
 566                 if (!NT_STATUS_IS_OK(nt_status)) {
 567                         DEBUG(1, ("GENSEC mech failed to start: %s\n", nt_errstr(nt_status)));
 568                         mux_printf(mux_id, "BH GENSEC mech failed to start\n");
 569                         return;
 570                 }
 571 
 572         }
 573 
 574         /* update */
 575         mem_ctx = talloc_named(NULL, 0, "manage_gensec_request internal mem_ctx");
 576         
 577         if (strncmp(buf, "PW ", 3) == 0) {
 578                 state->set_password = talloc_strndup(state,
 579                                                      (const char *)in.data, 
 580                                                      in.length);
 581                 
 582                 cli_credentials_set_password(gensec_get_credentials(state->gensec_state),
 583                                              state->set_password,
 584                                              CRED_SPECIFIED);
 585                 mux_printf(mux_id, "OK\n");
 586                 data_blob_free(&in);
 587                 talloc_free(mem_ctx);
 588                 return;
 589         }
 590 
 591         if (strncmp(buf, "UG", 2) == 0) {
 592                 int i;
 593                 char *grouplist = NULL;
 594                 struct auth_session_info *session_info;
 595 
 596                 nt_status = gensec_session_info(state->gensec_state, &session_info); 
 597                 if (!NT_STATUS_IS_OK(nt_status)) {
 598                         DEBUG(1, ("gensec_session_info failed: %s\n", nt_errstr(nt_status)));
 599                         mux_printf(mux_id, "BH %s\n", nt_errstr(nt_status));
 600                         data_blob_free(&in);
 601                         talloc_free(mem_ctx);
 602                         return;
 603                 }
 604                 
 605                 /* get the string onto the context */
 606                 grouplist = talloc_strdup(mem_ctx, "");
 607                 
 608                 for (i=0; i<session_info->security_token->num_sids; i++) {
 609                         struct security_token *token = session_info->security_token; 
 610                         const char *sidstr = dom_sid_string(session_info, 
 611                                                             token->sids[i]);
 612                         grouplist = talloc_asprintf_append_buffer(grouplist, "%s,", sidstr);
 613                 }
 614 
 615                 mux_printf(mux_id, "GL %s\n", grouplist);
 616                 talloc_free(session_info);
 617                 data_blob_free(&in);
 618                 talloc_free(mem_ctx);
 619                 return;
 620         }
 621 
 622         if (strncmp(buf, "GK", 2) == 0) {
 623                 char *base64_key;
 624                 DEBUG(10, ("Requested session key\n"));
 625                 nt_status = gensec_session_key(state->gensec_state, &session_key);
 626                 if(!NT_STATUS_IS_OK(nt_status)) {
 627                         DEBUG(1, ("gensec_session_key failed: %s\n", nt_errstr(nt_status)));
 628                         mux_printf(mux_id, "BH No session key\n");
 629                         talloc_free(mem_ctx);
 630                         return;
 631                 } else {
 632                         base64_key = base64_encode_data_blob(state, session_key);
 633                         mux_printf(mux_id, "GK %s\n", base64_key);
 634                         talloc_free(base64_key);
 635                 }
 636                 talloc_free(mem_ctx);
 637                 return;
 638         }
 639 
 640         if (strncmp(buf, "GF", 2) == 0) {
 641                 struct gensec_ntlmssp_state *gensec_ntlmssp_state;
 642                 uint32_t neg_flags;
 643 
 644                 gensec_ntlmssp_state = talloc_get_type(state->gensec_state->private_data, 
 645                                 struct gensec_ntlmssp_state);
 646                 neg_flags = gensec_ntlmssp_state->neg_flags;
 647 
 648                 DEBUG(10, ("Requested negotiated feature flags\n"));
 649                 mux_printf(mux_id, "GF 0x%08x\n", neg_flags);
 650                 return;
 651         }
 652 
 653         nt_status = gensec_update(state->gensec_state, mem_ctx, in, &out);
 654         
 655         /* don't leak 'bad password'/'no such user' info to the network client */
 656         nt_status = auth_nt_status_squash(nt_status);
 657 
 658         if (out.length) {
 659                 out_base64 = base64_encode_data_blob(mem_ctx, out);
 660         } else {
 661                 out_base64 = NULL;
 662         }
 663 
 664         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
 665                 reply_arg = "*";
 666                 if (first) {
 667                         reply_code = "YR";
 668                 } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) { 
 669                         reply_code = "KK";
 670                 } else if (state->gensec_state->gensec_role == GENSEC_SERVER) { 
 671                         reply_code = "TT";
 672                 } else {
 673                         abort();
 674                 }
 675 
 676 
 677         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
 678                 reply_code = "BH NT_STATUS_ACCESS_DENIED";
 679                 reply_arg = nt_errstr(nt_status);
 680                 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
 681         } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_UNSUCCESSFUL)) {
 682                 reply_code = "BH NT_STATUS_UNSUCCESSFUL";
 683                 reply_arg = nt_errstr(nt_status);
 684                 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
 685         } else if (!NT_STATUS_IS_OK(nt_status)) {
 686                 reply_code = "NA";
 687                 reply_arg = nt_errstr(nt_status);
 688                 DEBUG(1, ("GENSEC login failed: %s\n", nt_errstr(nt_status)));
 689         } else if /* OK */ (state->gensec_state->gensec_role == GENSEC_SERVER) {
 690                 struct auth_session_info *session_info;
 691 
 692                 nt_status = gensec_session_info(state->gensec_state, &session_info);
 693                 if (!NT_STATUS_IS_OK(nt_status)) {
 694                         reply_code = "BH Failed to retrive session info";
 695                         reply_arg = nt_errstr(nt_status);
 696                         DEBUG(1, ("GENSEC failed to retreive the session info: %s\n", nt_errstr(nt_status)));
 697                 } else {
 698 
 699                         reply_code = "AF";
 700                         reply_arg = talloc_asprintf(state->gensec_state, 
 701                                                     "%s%s%s", session_info->server_info->domain_name, 
 702                                                     lp_winbind_separator(lp_ctx), session_info->server_info->account_name);
 703                         talloc_free(session_info);
 704                 }
 705         } else if (state->gensec_state->gensec_role == GENSEC_CLIENT) {
 706                 reply_code = "AF";
 707                 reply_arg = out_base64;
 708         } else {
 709                 abort();
 710         }
 711 
 712         switch (stdio_helper_mode) {
 713         case GSS_SPNEGO_SERVER:
 714                 mux_printf(mux_id, "%s %s %s\n", reply_code, 
 715                           out_base64 ? out_base64 : "*", 
 716                           reply_arg ? reply_arg : "*");
 717                 break;
 718         default:
 719                 if (out_base64) {
 720                         mux_printf(mux_id, "%s %s\n", reply_code, out_base64);
 721                 } else if (reply_arg) {
 722                         mux_printf(mux_id, "%s %s\n", reply_code, reply_arg);
 723                 } else {
 724                         mux_printf(mux_id, "%s\n", reply_code);
 725                 }
 726         }
 727 
 728         talloc_free(mem_ctx);
 729         return;
 730 }
 731 
 732 static void manage_ntlm_server_1_request(enum stdio_helper_mode stdio_helper_mode, 
     /* [<][>][^][v][top][bottom][index][help] */
 733                                          struct loadparm_context *lp_ctx,
 734                                          char *buf, int length, void **private1,
 735                                          unsigned int mux_id, void **private2) 
 736 {
 737         char *request, *parameter;      
 738         static DATA_BLOB challenge;
 739         static DATA_BLOB lm_response;
 740         static DATA_BLOB nt_response;
 741         static char *full_username;
 742         static char *username;
 743         static char *domain;
 744         static char *plaintext_password;
 745         static bool ntlm_server_1_user_session_key;
 746         static bool ntlm_server_1_lm_session_key;
 747         
 748         if (strequal(buf, ".")) {
 749                 if (!full_username && !username) {      
 750                         mux_printf(mux_id, "Error: No username supplied!\n");
 751                 } else if (plaintext_password) {
 752                         /* handle this request as plaintext */
 753                         if (!full_username) {
 754                                 if (asprintf(&full_username, "%s%c%s", domain, *lp_winbind_separator(lp_ctx), username) == -1) {
 755                                         mux_printf(mux_id, "Error: Out of memory in asprintf!\n.\n");
 756                                         return;
 757                                 }
 758                         }
 759                         if (check_plaintext_auth(full_username, plaintext_password, false)) {
 760                                 mux_printf(mux_id, "Authenticated: Yes\n");
 761                         } else {
 762                                 mux_printf(mux_id, "Authenticated: No\n");
 763                         }
 764                 } else if (!lm_response.data && !nt_response.data) {
 765                         mux_printf(mux_id, "Error: No password supplied!\n");
 766                 } else if (!challenge.data) {   
 767                         mux_printf(mux_id, "Error: No lanman-challenge supplied!\n");
 768                 } else {
 769                         char *error_string = NULL;
 770                         DATA_BLOB lm_key;
 771                         DATA_BLOB user_session_key;
 772                         uint32_t flags = 0;
 773 
 774                         if (full_username && !username) {
 775                                 SAFE_FREE(username);
 776                                 SAFE_FREE(domain);
 777                                 if (!parse_ntlm_auth_domain_user(full_username, &username, 
 778                                                                                                  &domain, 
 779                                                                                                  *lp_winbind_separator(lp_ctx))) {
 780                                         /* username might be 'tainted', don't print into our new-line deleimianted stream */
 781                                         mux_printf(mux_id, "Error: Could not parse into domain and username\n");
 782                                 }
 783                         }
 784 
 785                         if (!domain) {
 786                                 domain = smb_xstrdup(lp_workgroup(lp_ctx));
 787                         }
 788 
 789                         if (ntlm_server_1_lm_session_key) 
 790                                 flags |= NTLM_AUTH_FLAG_LMKEY;
 791                         
 792                         if (ntlm_server_1_user_session_key) 
 793                                 flags |= NTLM_AUTH_FLAG_USER_SESSION_KEY;
 794 
 795                         if (!NT_STATUS_IS_OK(
 796                                     local_pw_check_specified(lp_ctx,
 797                                                              username, 
 798                                                               domain, 
 799                                                               lp_netbios_name(lp_ctx),
 800                                                               &challenge, 
 801                                                               &lm_response, 
 802                                                               &nt_response, 
 803                                                               flags, 
 804                                                               &lm_key, 
 805                                                               &user_session_key,
 806                                                               &error_string,
 807                                                               NULL))) {
 808 
 809                                 mux_printf(mux_id, "Authenticated: No\n");
 810                                 mux_printf(mux_id, "Authentication-Error: %s\n.\n", error_string);
 811                                 SAFE_FREE(error_string);
 812                         } else {
 813                                 static char zeros[16];
 814                                 char *hex_lm_key;
 815                                 char *hex_user_session_key;
 816 
 817                                 mux_printf(mux_id, "Authenticated: Yes\n");
 818 
 819                                 if (ntlm_server_1_lm_session_key 
 820                                     && lm_key.length 
 821                                     && (memcmp(zeros, lm_key.data, 
 822                                                                 lm_key.length) != 0)) {
 823                                         hex_encode(lm_key.data,
 824                                                    lm_key.length,
 825                                                    &hex_lm_key);
 826                                         mux_printf(mux_id, "LANMAN-Session-Key: %s\n", hex_lm_key);
 827                                         SAFE_FREE(hex_lm_key);
 828                                 }
 829 
 830                                 if (ntlm_server_1_user_session_key 
 831                                     && user_session_key.length 
 832                                     && (memcmp(zeros, user_session_key.data, 
 833                                                user_session_key.length) != 0)) {
 834                                         hex_encode(user_session_key.data, 
 835                                                    user_session_key.length, 
 836                                                    &hex_user_session_key);
 837                                         mux_printf(mux_id, "User-Session-Key: %s\n", hex_user_session_key);
 838                                         SAFE_FREE(hex_user_session_key);
 839                                 }
 840                         }
 841                 }
 842                 /* clear out the state */
 843                 challenge = data_blob(NULL, 0);
 844                 nt_response = data_blob(NULL, 0);
 845                 lm_response = data_blob(NULL, 0);
 846                 SAFE_FREE(full_username);
 847                 SAFE_FREE(username);
 848                 SAFE_FREE(domain);
 849                 SAFE_FREE(plaintext_password);
 850                 ntlm_server_1_user_session_key = false;
 851                 ntlm_server_1_lm_session_key = false;
 852                 mux_printf(mux_id, ".\n");
 853 
 854                 return;
 855         }
 856 
 857         request = buf;
 858 
 859         /* Indicates a base64 encoded structure */
 860         parameter = strstr(request, ":: ");
 861         if (!parameter) {
 862                 parameter = strstr(request, ": ");
 863                 
 864                 if (!parameter) {
 865                         DEBUG(0, ("Parameter not found!\n"));
 866                         mux_printf(mux_id, "Error: Parameter not found!\n.\n");
 867                         return;
 868                 }
 869                 
 870                 parameter[0] ='\0';
 871                 parameter++;
 872                 parameter[0] ='\0';
 873                 parameter++;
 874 
 875         } else {
 876                 parameter[0] ='\0';
 877                 parameter++;
 878                 parameter[0] ='\0';
 879                 parameter++;
 880                 parameter[0] ='\0';
 881                 parameter++;
 882 
 883                 base64_decode_inplace(parameter);
 884         }
 885 
 886         if (strequal(request, "LANMAN-Challenge")) {
 887                 challenge = strhex_to_data_blob(NULL, parameter);
 888                 if (challenge.length != 8) {
 889                         mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 8)\n.\n", 
 890                                   parameter,
 891                                   (int)challenge.length);
 892                         challenge = data_blob(NULL, 0);
 893                 }
 894         } else if (strequal(request, "NT-Response")) {
 895                 nt_response = strhex_to_data_blob(NULL, parameter);
 896                 if (nt_response.length < 24) {
 897                         mux_printf(mux_id, "Error: hex decode of %s failed! (only got %d bytes, needed at least 24)\n.\n", 
 898                                   parameter,
 899                                   (int)nt_response.length);
 900                         nt_response = data_blob(NULL, 0);
 901                 }
 902         } else if (strequal(request, "LANMAN-Response")) {
 903                 lm_response = strhex_to_data_blob(NULL, parameter);
 904                 if (lm_response.length != 24) {
 905                         mux_printf(mux_id, "Error: hex decode of %s failed! (got %d bytes, expected 24)\n.\n", 
 906                                   parameter,
 907                                   (int)lm_response.length);
 908                         lm_response = data_blob(NULL, 0);
 909                 }
 910         } else if (strequal(request, "Password")) {
 911                 plaintext_password = smb_xstrdup(parameter);
 912         } else if (strequal(request, "NT-Domain")) {
 913                 domain = smb_xstrdup(parameter);
 914         } else if (strequal(request, "Username")) {
 915                 username = smb_xstrdup(parameter);
 916         } else if (strequal(request, "Full-Username")) {
 917                 full_username = smb_xstrdup(parameter);
 918         } else if (strequal(request, "Request-User-Session-Key")) {
 919                 ntlm_server_1_user_session_key = strequal(parameter, "Yes");
 920         } else if (strequal(request, "Request-LanMan-Session-Key")) {
 921                 ntlm_server_1_lm_session_key = strequal(parameter, "Yes");
 922         } else {
 923                 mux_printf(mux_id, "Error: Unknown request %s\n.\n", request);
 924         }
 925 }
 926 
 927 static void manage_squid_request(struct loadparm_context *lp_ctx, enum stdio_helper_mode helper_mode, 
     /* [<][>][^][v][top][bottom][index][help] */
 928                                  stdio_helper_function fn, void **private2) 
 929 {
 930         char *buf;
 931         char tmp[INITIAL_BUFFER_SIZE+1];
 932         unsigned int mux_id = 0;
 933         int length, buf_size = 0;
 934         char *c;
 935         struct mux_private {
 936                 unsigned int max_mux;
 937                 void **private_pointers;
 938         };
 939 
 940         static struct mux_private *mux_private;
 941         static void *normal_private;
 942         void **private1;
 943 
 944         buf = talloc_strdup(NULL, "");
 945 
 946         if (buf == NULL) {
 947                 DEBUG(0, ("Failed to allocate memory for reading the input "
 948                           "buffer.\n"));
 949                 x_fprintf(x_stdout, "ERR\n");
 950                 return;
 951         }
 952 
 953         do {
 954                 /* this is not a typo - x_fgets doesn't work too well under
 955                  * squid */
 956                 if (fgets(tmp, INITIAL_BUFFER_SIZE, stdin) == NULL) {
 957                         if (ferror(stdin)) {
 958                                 DEBUG(1, ("fgets() failed! dying..... errno=%d "
 959                                           "(%s)\n", ferror(stdin),
 960                                           strerror(ferror(stdin))));
 961 
 962                                 exit(1);    /* BIIG buffer */
 963                         }
 964                         exit(0);
 965                 }
 966 
 967                 buf = talloc_strdup_append_buffer(buf, tmp);
 968                 buf_size += INITIAL_BUFFER_SIZE;
 969 
 970                 if (buf_size > MAX_BUFFER_SIZE) {
 971                         DEBUG(0, ("Invalid Request (too large)\n"));
 972                         x_fprintf(x_stdout, "ERR\n");
 973                         talloc_free(buf);
 974                         return;
 975                 }
 976 
 977                 c = strchr(buf, '\n');
 978         } while (c == NULL);
 979 
 980         *c = '\0';
 981         length = c-buf;
 982 
 983         DEBUG(10, ("Got '%s' from squid (length: %d).\n",buf,length));
 984 
 985         if (buf[0] == '\0') {
 986                 DEBUG(0, ("Invalid Request (empty)\n"));
 987                 x_fprintf(x_stdout, "ERR\n");
 988                 talloc_free(buf);
 989                 return;
 990         }
 991 
 992         if (opt_multiplex) {
 993                 if (sscanf(buf, "%u ", &mux_id) != 1) {
 994                         DEBUG(0, ("Invalid Request - no multiplex id\n"));
 995                         x_fprintf(x_stdout, "ERR\n");
 996                         talloc_free(buf);
 997                         return;
 998                 }
 999                 if (!mux_private) {
1000                         mux_private = talloc(NULL, struct mux_private);
1001                         mux_private->max_mux = 0;
1002                         mux_private->private_pointers = NULL;
1003                 }
1004                 
1005                 c=strchr(buf,' ');
1006                 if (!c) {
1007                         DEBUG(0, ("Invalid Request - no data after multiplex id\n"));
1008                         x_fprintf(x_stdout, "ERR\n");
1009                         talloc_free(buf);
1010                         return;
1011                 }
1012                 c++;
1013                 if (mux_id >= mux_private->max_mux) {
1014                         unsigned int prev_max = mux_private->max_mux;
1015                         mux_private->max_mux = mux_id + 1;
1016                         mux_private->private_pointers
1017                                 = talloc_realloc(mux_private, 
1018                                                    mux_private->private_pointers, 
1019                                                    void *, mux_private->max_mux);
1020                         memset(&mux_private->private_pointers[prev_max], '\0',  
1021                                (sizeof(*mux_private->private_pointers) * (mux_private->max_mux - prev_max))); 
1022                 };
1023 
1024                 private1 = &mux_private->private_pointers[mux_id];
1025         } else {
1026                 c = buf;
1027                 private1 = &normal_private;
1028         }
1029 
1030         fn(helper_mode, lp_ctx, c, length, private1, mux_id, private2);
1031         talloc_free(buf);
1032 }
1033 
1034 static void squid_stream(struct loadparm_context *lp_ctx, 
     /* [<][>][^][v][top][bottom][index][help] */
1035                          enum stdio_helper_mode stdio_mode, 
1036                          stdio_helper_function fn) {
1037         /* initialize FDescs */
1038         x_setbuf(x_stdout, NULL);
1039         x_setbuf(x_stderr, NULL);
1040         while(1) {
1041                 manage_squid_request(lp_ctx, stdio_mode, fn, NULL);
1042         }
1043 }
1044 
1045 
1046 /* Main program */
1047 
1048 enum {
1049         OPT_USERNAME = 1000,
1050         OPT_DOMAIN,
1051         OPT_WORKSTATION,
1052         OPT_CHALLENGE,
1053         OPT_RESPONSE,
1054         OPT_LM,
1055         OPT_NT,
1056         OPT_PASSWORD,
1057         OPT_LM_KEY,
1058         OPT_USER_SESSION_KEY,
1059         OPT_DIAGNOSTICS,
1060         OPT_REQUIRE_MEMBERSHIP,
1061         OPT_MULTIPLEX,
1062         OPT_USE_CACHED_CREDS,
1063 };
1064 
1065 int main(int argc, const char **argv)
     /* [<][>][^][v][top][bottom][index][help] */
1066 {
1067         static const char *helper_protocol;
1068         int opt;
1069 
1070         poptContext pc;
1071 
1072         /* NOTE: DO NOT change this interface without considering the implications!
1073            This is an external interface, which other programs will use to interact 
1074            with this helper.
1075         */
1076 
1077         /* We do not use single-letter command abbreviations, because they harm future 
1078            interface stability. */
1079 
1080         struct poptOption long_options[] = {
1081                 POPT_AUTOHELP
1082                 { "helper-protocol", 0, POPT_ARG_STRING, &helper_protocol, OPT_DOMAIN, "operate as a stdio-based helper", "helper protocol to use"},
1083                 { "domain", 0, POPT_ARG_STRING, &opt_domain, OPT_DOMAIN, "domain name"},
1084                 { "workstation", 0, POPT_ARG_STRING, &opt_workstation, OPT_WORKSTATION, "workstation"},
1085                 { "username", 0, POPT_ARG_STRING, &opt_username, OPT_PASSWORD, "Username"},             
1086                 { "password", 0, POPT_ARG_STRING, &opt_password, OPT_PASSWORD, "User's plaintext password"},            
1087                 { "multiplex", 0, POPT_ARG_NONE, &opt_multiplex, OPT_MULTIPLEX, "Multiplex Mode"},
1088                 { "use-cached-creds", 0, POPT_ARG_NONE, &use_cached_creds, OPT_USE_CACHED_CREDS, "silently ignored for compatibility reasons"},
1089                 POPT_COMMON_SAMBA
1090                 POPT_COMMON_VERSION
1091                 { NULL }
1092         };
1093 
1094         /* Samba client initialisation */
1095 
1096         setup_logging(NULL, DEBUG_STDERR);
1097 
1098         /* Parse options */
1099 
1100         pc = poptGetContext("ntlm_auth", argc, argv, long_options, 0);
1101 
1102         /* Parse command line options */
1103 
1104         if (argc == 1) {
1105                 poptPrintHelp(pc, stderr, 0);
1106                 return 1;
1107         }
1108 
1109         pc = poptGetContext(NULL, argc, (const char **)argv, long_options, 
1110                             POPT_CONTEXT_KEEP_FIRST);
1111 
1112         while((opt = poptGetNextOpt(pc)) != -1) {
1113                 if (opt < -1) {
1114                         break;
1115                 }
1116         }
1117         if (opt < -1) {
1118                 fprintf(stderr, "%s: %s\n",
1119                         poptBadOption(pc, POPT_BADOPTION_NOALIAS),
1120                         poptStrerror(opt));
1121                 return 1;
1122         }
1123 
1124         gensec_init(cmdline_lp_ctx);
1125 
1126         if (opt_domain == NULL) {
1127                 opt_domain = lp_workgroup(cmdline_lp_ctx);
1128         }
1129 
1130         if (helper_protocol) {
1131                 int i;
1132                 for (i=0; i<NUM_HELPER_MODES; i++) {
1133                         if (strcmp(helper_protocol, stdio_helper_protocols[i].name) == 0) {
1134                                 squid_stream(cmdline_lp_ctx, stdio_helper_protocols[i].mode, stdio_helper_protocols[i].fn);
1135                                 exit(0);
1136                         }
1137                 }
1138                 x_fprintf(x_stderr, "unknown helper protocol [%s]\n\nValid helper protools:\n\n", helper_protocol);
1139 
1140                 for (i=0; i<NUM_HELPER_MODES; i++) {
1141                         x_fprintf(x_stderr, "%s\n", stdio_helper_protocols[i].name);
1142                 }
1143 
1144                 exit(1);
1145         }
1146 
1147         if (!opt_username) {
1148                 x_fprintf(x_stderr, "username must be specified!\n\n");
1149                 poptPrintHelp(pc, stderr, 0);
1150                 exit(1);
1151         }
1152 
1153         if (opt_workstation == NULL) {
1154                 opt_workstation = lp_netbios_name(cmdline_lp_ctx);
1155         }
1156 
1157         if (!opt_password) {
1158                 opt_password = getpass("password: ");
1159         }
1160 
1161         {
1162                 char *user;
1163 
1164                 asprintf(&user, "%s%c%s", opt_domain, *lp_winbind_separator(cmdline_lp_ctx), opt_username);
1165                 if (!check_plaintext_auth(user, opt_password, true)) {
1166                         return 1;
1167                 }
1168         }
1169 
1170         /* Exit code */
1171 
1172         poptFreeContext(pc);
1173         return 0;
1174 }

/* [<][>][^][v][top][bottom][index][help] */