fake_ntlm.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 2010, Mandy Wu, <mandy.wu@intel.com>
00009  * Copyright (C) 2011 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
00010  *
00011  * This software is licensed as described in the file COPYING, which
00012  * you should have received as part of this distribution. The terms
00013  * are also available at https://curl.haxx.se/docs/copyright.html.
00014  *
00015  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00016  * copies of the Software, and permit persons to whom the Software is
00017  * furnished to do so, under the terms of the COPYING file.
00018  *
00019  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00020  * KIND, either express or implied.
00021  *
00022  ***************************************************************************/
00023 #include "server_setup.h"
00024 
00025 /*
00026  * This is a fake ntlm_auth, which is used for testing NTLM single-sign-on.
00027  * When DEBUGBUILD is defined, libcurl invoke this tool instead of real winbind
00028  * daemon helper /usr/bin/ntlm_auth. This tool will accept commands and
00029  * responses with a pre-written string saved in test case test2005.
00030  */
00031 
00032 #define ENABLE_CURLX_PRINTF
00033 #include "curlx.h" /* from the private lib dir */
00034 #include "getpart.h"
00035 #include "util.h"
00036 
00037 /* include memdebug.h last */
00038 #include "memdebug.h"
00039 
00040 #ifndef DEFAULT_LOGFILE
00041 #define DEFAULT_LOGFILE "log/fake_ntlm.log"
00042 #endif
00043 
00044 const char *serverlogfile = DEFAULT_LOGFILE;
00045 
00046 /*
00047  * Returns an allocated buffer with printable representation of input
00048  * buffer contents or returns NULL on out of memory condition.
00049  */
00050 static char *printable(char *inbuf, size_t inlength)
00051 {
00052   char *outbuf;
00053   char *newbuf;
00054   size_t newsize;
00055   size_t outsize;
00056   size_t outincr = 0;
00057   size_t i, o = 0;
00058 
00059 #define HEX_FMT_STR  "[0x%02X]"
00060 #define HEX_STR_LEN  6
00061 #define NOTHING_STR  "[NOTHING]"
00062 #define NOTHING_LEN  9
00063 
00064   if(!inlength)
00065     inlength = strlen(inbuf);
00066 
00067   if(inlength) {
00068     outincr = ((inlength/2) < (HEX_STR_LEN+1)) ? HEX_STR_LEN+1 : inlength/2;
00069     outsize = inlength + outincr;
00070   }
00071   else
00072     outsize = NOTHING_LEN + 1;
00073 
00074   outbuf = malloc(outsize);
00075   if(!outbuf)
00076     return NULL;
00077 
00078   if(!inlength) {
00079     snprintf(&outbuf[0], outsize, "%s", NOTHING_STR);
00080     return outbuf;
00081   }
00082 
00083   for(i=0; i<inlength; i++) {
00084 
00085     if(o > outsize - (HEX_STR_LEN + 1)) {
00086       newsize = outsize + outincr;
00087       newbuf = realloc(outbuf, newsize);
00088       if(!newbuf) {
00089         free(outbuf);
00090         return NULL;
00091       }
00092       outbuf = newbuf;
00093       outsize = newsize;
00094     }
00095 
00096     if((inbuf[i] > 0x20) && (inbuf[i] < 0x7F)) {
00097       outbuf[o] = inbuf[i];
00098       o++;
00099     }
00100     else {
00101       snprintf(&outbuf[o], outsize - o, HEX_FMT_STR, inbuf[i]);
00102       o += HEX_STR_LEN;
00103     }
00104 
00105   }
00106   outbuf[o] = '\0';
00107 
00108   return outbuf;
00109 }
00110 
00111 int main(int argc, char *argv[])
00112 {
00113   char buf[1024];
00114   FILE *stream;
00115   char *filename;
00116   int error;
00117   char *type1_input = NULL, *type3_input = NULL;
00118   char *type1_output = NULL, *type3_output = NULL;
00119   size_t size = 0;
00120   long testnum;
00121   const char *env;
00122   int arg = 1;
00123   char *helper_user = (char *)"unknown";
00124   char *helper_proto = (char *)"unknown";
00125   char *helper_domain = (char *)"unknown";
00126   bool use_cached_creds = FALSE;
00127   char *msgbuf;
00128 
00129   buf[0] = '\0';
00130 
00131   while(argc > arg) {
00132     if(!strcmp("--use-cached-creds", argv[arg])) {
00133       use_cached_creds = TRUE;
00134       arg++;
00135     }
00136     else if(!strcmp("--helper-protocol", argv[arg])) {
00137       arg++;
00138       if(argc > arg)
00139         helper_proto = argv[arg++];
00140     }
00141     else if(!strcmp("--username", argv[arg])) {
00142       arg++;
00143       if(argc > arg)
00144         helper_user = argv[arg++];
00145     }
00146     else if(!strcmp("--domain", argv[arg])) {
00147       arg++;
00148       if(argc > arg)
00149         helper_domain = argv[arg++];
00150     }
00151     else {
00152       puts("Usage: fake_ntlm [option]\n"
00153            " --use-cached-creds\n"
00154            " --helper-protocol [protocol]\n"
00155            " --username [username]\n"
00156            " --domain [domain]");
00157       exit(1);
00158     }
00159   }
00160 
00161   logmsg("fake_ntlm (user: %s) (proto: %s) (domain: %s) (cached creds: %s)",
00162          helper_user, helper_proto, helper_domain,
00163          (use_cached_creds) ? "yes" : "no");
00164 
00165   env = getenv("CURL_NTLM_AUTH_TESTNUM");
00166   if(env) {
00167     char *endptr;
00168     long lnum = strtol(env, &endptr, 10);
00169     if((endptr != env + strlen(env)) || (lnum < 1L)) {
00170       logmsg("Test number not valid in CURL_NTLM_AUTH_TESTNUM");
00171       exit(1);
00172     }
00173     testnum = lnum;
00174   }
00175   else {
00176     logmsg("Test number not specified in CURL_NTLM_AUTH_TESTNUM");
00177     exit(1);
00178   }
00179 
00180   env = getenv("CURL_NTLM_AUTH_SRCDIR");
00181   if(env) {
00182     path = env;
00183   }
00184 
00185   filename = test2file(testnum);
00186   stream=fopen(filename, "rb");
00187   if(!stream) {
00188     error = errno;
00189     logmsg("fopen() failed with error: %d %s", error, strerror(error));
00190     logmsg("Error opening file: %s", filename);
00191     logmsg("Couldn't open test file %ld", testnum);
00192     exit(1);
00193   }
00194   else {
00195     /* get the ntlm_auth input/output */
00196     error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
00197     fclose(stream);
00198     if(error || size == 0) {
00199       logmsg("getpart() type 1 input failed with error: %d", error);
00200       exit(1);
00201     }
00202   }
00203 
00204   stream=fopen(filename, "rb");
00205   if(!stream) {
00206     error = errno;
00207     logmsg("fopen() failed with error: %d %s", error, strerror(error));
00208     logmsg("Error opening file: %s", filename);
00209     logmsg("Couldn't open test file %ld", testnum);
00210     exit(1);
00211   }
00212   else {
00213     size = 0;
00214     error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
00215     fclose(stream);
00216     if(error || size == 0) {
00217       logmsg("getpart() type 3 input failed with error: %d", error);
00218       exit(1);
00219     }
00220   }
00221 
00222   while(fgets(buf, sizeof(buf), stdin)) {
00223     if(strcmp(buf, type1_input) == 0) {
00224       stream=fopen(filename, "rb");
00225       if(!stream) {
00226         error = errno;
00227         logmsg("fopen() failed with error: %d %s", error, strerror(error));
00228         logmsg("Error opening file: %s", filename);
00229         logmsg("Couldn't open test file %ld", testnum);
00230         exit(1);
00231       }
00232       else {
00233         size = 0;
00234         error = getpart(&type1_output, &size, "ntlm_auth_type1", "output",
00235                         stream);
00236         fclose(stream);
00237         if(error || size == 0) {
00238           logmsg("getpart() type 1 output failed with error: %d", error);
00239           exit(1);
00240         }
00241       }
00242       printf("%s", type1_output);
00243       fflush(stdout);
00244     }
00245     else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
00246       stream=fopen(filename, "rb");
00247       if(!stream) {
00248         error = errno;
00249         logmsg("fopen() failed with error: %d %s", error, strerror(error));
00250         logmsg("Error opening file: %s", filename);
00251         logmsg("Couldn't open test file %ld", testnum);
00252         exit(1);
00253       }
00254       else {
00255         size = 0;
00256         error = getpart(&type3_output, &size, "ntlm_auth_type3", "output",
00257                         stream);
00258         fclose(stream);
00259         if(error || size == 0) {
00260           logmsg("getpart() type 3 output failed with error: %d", error);
00261           exit(1);
00262         }
00263       }
00264       printf("%s", type3_output);
00265       fflush(stdout);
00266     }
00267     else {
00268       printf("Unknown request\n");
00269       msgbuf = printable(buf, 0);
00270       if(msgbuf) {
00271         logmsg("invalid input: '%s'\n", msgbuf);
00272         free(msgbuf);
00273       }
00274       else
00275         logmsg("OOM formatting invalid input: '%s'\n", buf);
00276       exit(1);
00277     }
00278   }
00279   return 1;
00280 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02