Пример #1
0
int main(int argc, char *argv[])
{
  char buf[1024];
  FILE *stream;
  char *filename;
  int error;
  char *type1_input = NULL, *type3_input = NULL;
  char *type1_output = NULL, *type3_output = NULL;
  size_t size = 0;
  long testnum;
  const char *env;
  int arg = 1;
  char *helper_user = (char *)"unknown";
  char *helper_proto = (char *)"unknown";
  char *helper_domain = (char *)"unknown";
  bool use_cached_creds = FALSE;
  char *msgbuf;

  buf[0] = '\0';

  while(argc > arg) {
    if(!strcmp("--use-cached-creds", argv[arg])) {
      use_cached_creds = TRUE;
      arg++;
    }
    else if(!strcmp("--helper-protocol", argv[arg])) {
      arg++;
      if(argc > arg)
        helper_proto = argv[arg++];
    }
    else if(!strcmp("--username", argv[arg])) {
      arg++;
      if(argc > arg)
        helper_user = argv[arg++];
    }
    else if(!strcmp("--domain", argv[arg])) {
      arg++;
      if(argc > arg)
        helper_domain = argv[arg++];
    }
    else {
      puts("Usage: fake_ntlm [option]\n"
           " --use-cached-creds\n"
           " --helper-protocol [protocol]\n"
           " --username [username]\n"
           " --domain [domain]");
      exit(1);
    }
  }

  logmsg("fake_ntlm (user: %s) (proto: %s) (domain: %s) (cached creds: %s)",
         helper_user, helper_proto, helper_domain,
         (use_cached_creds) ? "yes" : "no");

  env = getenv("CURL_NTLM_AUTH_TESTNUM");
  if (env) {
    char *endptr;
    long lnum = strtol(env, &endptr, 10);
    if((endptr != env + strlen(env)) || (lnum < 1L)) {
      logmsg("Test number not valid in CURL_NTLM_AUTH_TESTNUM");
      exit(1);
    }
    testnum = lnum;
  } else {
    logmsg("Test number not specified in CURL_NTLM_AUTH_TESTNUM");
    exit(1);
  }

  env = getenv("CURL_NTLM_AUTH_SRCDIR");
  if (env) {
    path = env;
  }

  filename = test2file(testnum);
  stream=fopen(filename, "rb");
  if(!stream) {
    error = ERRNO;
    logmsg("fopen() failed with error: %d %s", error, strerror(error));
    logmsg("Error opening file: %s", filename);
    logmsg("Couldn't open test file %ld", testnum);
    exit(1);
  }
  else {
    /* get the ntlm_auth input/output */
    error = getpart(&type1_input, &size, "ntlm_auth_type1", "input", stream);
    fclose(stream);
    if(error || size == 0) {
      logmsg("getpart() type 1 input failed with error: %d", error);
      exit(1);
    }
  }

  stream=fopen(filename, "rb");
  if(!stream) {
    error = ERRNO;
    logmsg("fopen() failed with error: %d %s", error, strerror(error));
    logmsg("Error opening file: %s", filename);
    logmsg("Couldn't open test file %ld", testnum);
    exit(1);
  }
  else {
    size = 0;
    error = getpart(&type3_input, &size, "ntlm_auth_type3", "input", stream);
    fclose(stream);
    if(error || size == 0) {
      logmsg("getpart() type 3 input failed with error: %d", error);
      exit(1);
    }
  }

  while(fgets(buf, sizeof(buf), stdin)) {
    if(strcmp(buf, type1_input) == 0) {
      stream=fopen(filename, "rb");
      if(!stream) {
        error = ERRNO;
        logmsg("fopen() failed with error: %d %s", error, strerror(error));
        logmsg("Error opening file: %s", filename);
        logmsg("Couldn't open test file %ld", testnum);
        exit(1);
      }
      else {
        size = 0;
        error = getpart(&type1_output, &size, "ntlm_auth_type1", "output", stream);
        fclose(stream);
        if(error || size == 0) {
          logmsg("getpart() type 1 output failed with error: %d", error);
          exit(1);
        }
      }
      printf("%s", type1_output);
      fflush(stdout);
    }
    else if(strncmp(buf, type3_input, strlen(type3_input)) == 0) {
      stream=fopen(filename, "rb");
      if(!stream) {
        error = ERRNO;
        logmsg("fopen() failed with error: %d %s", error, strerror(error));
        logmsg("Error opening file: %s", filename);
        logmsg("Couldn't open test file %ld", testnum);
        exit(1);
      }
      else {
        size = 0;
        error = getpart(&type3_output, &size, "ntlm_auth_type3", "output", stream);
        fclose(stream);
        if(error || size == 0) {
          logmsg("getpart() type 3 output failed with error: %d", error);
          exit(1);
        }
      }
      printf("%s", type3_output);
      fflush(stdout);
    }
    else {
      printf("Unknown request\n");
      msgbuf = printable(buf, 0);
      if(msgbuf) {
        logmsg("invalid input: '%s'\n", msgbuf);
        free(msgbuf);
      }
      else
        logmsg("OOM formatting invalid input: '%s'\n", buf);
      exit(1);
    }
  }
  return 1;
}
Пример #2
0
Файл: tftpd.c Проект: 2px/curl
/*
 * Validate file access.
 */
static int validate_access(struct testcase *test,
                           const char *filename, int mode)
{
  char *ptr;
  long testno, partno;
  int error;
  char partbuf[80]="data";

  logmsg("trying to get file: %s mode %x", filename, mode);

  if(!strncmp("verifiedserver", filename, 14)) {
    char weare[128];
    size_t count = snprintf(weare, sizeof(weare),
                            "WE ROOLZ: %ld\r\n", (long)getpid());

    logmsg("Are-we-friendly question received");
    test->buffer = strdup(weare);
    test->rptr = test->buffer; /* set read pointer */
    test->bufsize = count;    /* set total count */
    test->rcount = count;     /* set data left to read */
    return 0; /* fine */
  }

  /* find the last slash */
  ptr = strrchr(filename, '/');

  if(ptr) {
    char *file;

    ptr++; /* skip the slash */

    /* skip all non-numericals following the slash */
    while(*ptr && !ISDIGIT(*ptr))
      ptr++;

    /* get the number */
    testno = strtol(ptr, &ptr, 10);

    if(testno > 10000) {
      partno = testno % 10000;
      testno /= 10000;
    }
    else
      partno = 0;


    logmsg("requested test number %ld part %ld", testno, partno);

    test->testno = testno;

    (void)parse_servercmd(test);

    file = test2file(testno);

    if(0 != partno)
      snprintf(partbuf, sizeof(partbuf), "data%ld", partno);

    if(file) {
      FILE *stream=fopen(file, "rb");
      if(!stream) {
        error = errno;
        logmsg("fopen() failed with error: %d %s", error, strerror(error));
        logmsg("Error opening file: %s", file);
        logmsg("Couldn't open test file: %s", file);
        return EACCESS;
      }
      else {
        size_t count;
        error = getpart(&test->buffer, &count, "reply", partbuf, stream);
        fclose(stream);
        if(error) {
          logmsg("getpart() failed with error: %d", error);
          return EACCESS;
        }
        if(test->buffer) {
          test->rptr = test->buffer; /* set read pointer */
          test->bufsize = count;    /* set total count */
          test->rcount = count;     /* set data left to read */
        }
        else
          return EACCESS;
      }

    }
    else
      return EACCESS;
  }
  else {
    logmsg("no slash found in path");
    return EACCESS; /* failure */
  }

  logmsg("file opened and all is good");
  return 0;
}
Пример #3
0
Файл: tftpd.c Проект: 2px/curl
/* Based on the testno, parse the correct server commands. */
static int parse_servercmd(struct testcase *req)
{
  FILE *stream;
  char *filename;
  int error;

  filename = test2file(req->testno);

  stream=fopen(filename, "rb");
  if(!stream) {
    error = errno;
    logmsg("fopen() failed with error: %d %s", error, strerror(error));
    logmsg("  [1] Error opening file: %s", filename);
    logmsg("  Couldn't open test file %ld", req->testno);
    return 1; /* done */
  }
  else {
    char *orgcmd = NULL;
    char *cmd = NULL;
    size_t cmdsize = 0;
    int num=0;

    /* get the custom server control "commands" */
    error = getpart(&orgcmd, &cmdsize, "reply", "servercmd", stream);
    fclose(stream);
    if(error) {
      logmsg("getpart() failed with error: %d", error);
      return 1; /* done */
    }

    cmd = orgcmd;
    while(cmd && cmdsize) {
      char *check;
      if(1 == sscanf(cmd, "writedelay: %d", &num)) {
        logmsg("instructed to delay %d secs between packets", num);
        req->writedelay = num;
      }
      else {
        logmsg("Unknown <servercmd> instruction found: %s", cmd);
      }
      /* try to deal with CRLF or just LF */
      check = strchr(cmd, '\r');
      if(!check)
        check = strchr(cmd, '\n');

      if(check) {
        /* get to the letter following the newline */
        while((*check == '\r') || (*check == '\n'))
          check++;

        if(!*check)
          /* if we reached a zero, get out */
          break;
        cmd = check;
      }
      else
        break;
    }
    free(orgcmd);
  }

  return 0; /* OK! */
}
Пример #4
0
static void ir_parseline2(ir_parseline_t *ipl)
{
  channel_t *ch;
  char *nick;
  char *part3a;
  char *t;
  unsigned int i;

  caps(ipl->part[1]);

  /* NOTICE nick */
  if (!strcmp(ipl->part[1], "NOTICE")) {
    if (gnetwork->caps_nick && ipl->part[2]) {
      if (!strcmp(caps(ipl->part[2]), gnetwork->caps_nick)) {
        /* nickserv */
        identify_check(ipl->line);
#ifdef USE_RUBY
        if (do_myruby_notice(ipl->line) == 0)
#endif /* USE_RUBY */
          privmsgparse(0, 0, ipl->line);
      }
    }
  }

  /* PRIVMSG */
  if (!strcmp(ipl->part[1], "PRIVMSG")) {
#ifndef WITHOUT_BLOWFISH
    char *line2;

    line2 = test_fish_message(ipl->line, ipl->part[2], ipl->part[3], ipl->part[4]);
    if (line2) {
#ifdef USE_RUBY
      if (do_myruby_privmsg(line2) == 0)
#endif /* USE_RUBY */
        privmsgparse(1, 1, line2);
      mydelete(line2);
    } else {
#endif /* WITHOUT_BLOWFISH */
#ifdef USE_RUBY
      if (do_myruby_privmsg(ipl->line) == 0)
#endif /* USE_RUBY */
        privmsgparse(1, 0, ipl->line);
#ifndef WITHOUT_BLOWFISH
    }
#endif /* WITHOUT_BLOWFISH */
  }

  /* :server 001  xxxx :welcome.... */
  if ( !strcmp(ipl->part[1], "001") ) {
    irc_001(ipl);
    return;
  }

  /* :server 005 xxxx aaa bbb=x ccc=y :are supported... */
  if ( !strcmp(ipl->part[1], "005") ) {
    irc_005(ipl);
    return;
  }

  /* :server 401 botnick usernick :No such nick/channel */
  if ( !strcmp(ipl->part[1], "401") ) {
    if (ipl->part[2] && ipl->part[3]) {
      if (!strcmp(ipl->part[2], "*")) {
        lost_nick(ipl->part[3]);
      }
    }
    return;
  }

  /* :server 433 old new :Nickname is already in use. */
  if ( !strcmp(ipl->part[1], "433") ) {
    if (ipl->part[2] && ipl->part[3]) {
      if (!strcmp(ipl->part[2], "*")) {
        ioutput(OUT_S|OUT_L, COLOR_NO_COLOR,
                "Nickname %s already in use on %s, trying %s%u",
                ipl->part[3],
                gnetwork->name,
                get_config_nick(),
                gnetwork->nick_number);

        /* generate new nick and retry */
        writeserver(WRITESERVER_NORMAL, "NICK %s%u",
                    get_config_nick(),
                    (gnetwork->nick_number)++);
      }
    }
    return;
  }

  /* :server 470 botnick #channel :(you are banned) transfering you to #newchannel */
  if ( !strcmp(ipl->part[1], "470") ) {
    if (ipl->part[2] && ipl->part[3]) {
      outerror(OUTERROR_TYPE_WARN_LOUD,
               "channel on %s: %s", gnetwork->name, strstr(ipl->line, "470"));
      for (ch = irlist_get_head(&(gnetwork->channels));
           ch;
           ch = irlist_get_next(ch)) {
        if (strcmp(caps(ipl->part[2]), gnetwork->caps_nick))
             continue;
        if (strcmp(caps(ipl->part[3]), ch->name))
             continue;
        ch->flags |= CHAN_KICKED;
      }
    }
    return;
  }

  /* names list for a channel */
  /* :server 353 our_nick = #channel :nick @nick +nick nick */
  if ( !strcmp(ipl->part[1], "353") ) {
    if (ipl->part[2] && ipl->part[3] && ipl->part[5]) {
      caps(ipl->part[4]);

      for (ch = irlist_get_head(&(gnetwork->channels));
           ch;
           ch = irlist_get_next(ch)) {
        if (strcmp(ipl->part[4], ch->name) != 0)
          continue;
        for (i=0; (t = getpart(ipl->line, 6+i)); ++i) {
          addtomemberlist(ch, i == 0 ? t+1 : t);
          mydelete(t);
        }
        return;
      }
      ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR,
              "Got name data for %s which is not a known channel on %s!",
              ipl->part[4], gnetwork->name);
      writeserver(WRITESERVER_NORMAL, "PART %s", ipl->part[4]);
    }
    return;
  }

  if (gnetwork->lastping != 0) {
    if (strcmp(ipl->part[1], "PONG") == 0) {
      lag_message();
      return;
    }
  }

#if PING_SRVR
  /* server ping */
  if (strncmp(ipl->line, "PING :", 6) == 0) {
    if (gdata.debug > 0)
      ioutput(OUT_S, COLOR_NO_COLOR,
              "Server Ping on %s: %s",
              gnetwork->name, ipl->line);
    writeserver(WRITESERVER_NOW, "PO%s", ipl->line+2);
    return;
  }
#endif

  /* QUIT */
  if (strcmp(ipl->part[1], "QUIT") == 0) {
    if (gnetwork->caps_nick) {
      nick = ir_get_nickarg(ipl->line);
      if (!strcmp(caps(nick), gnetwork->caps_nick)) {
        /* we quit? */
        outerror(OUTERROR_TYPE_WARN_LOUD,
                 "Forced quit on %s: %s", gnetwork->name, ipl->line);
        close_server();
        clean_send_buffers();
        /* do not reconnect */
        gnetwork->serverstatus = SERVERSTATUS_EXIT;
      } else {
        /* someone else quit */
        for (ch = irlist_get_head(&(gnetwork->channels));
             ch;
             ch = irlist_get_next(ch)) {
          removefrommemberlist(ch, nick);
        }
        reverify_restrictsend();
      }
      mydelete(nick);
    }
    return;
  }

  /* MODE #channel +x ... */
  if (strcmp(ipl->part[1], "MODE") == 0) {
    if (ipl->part[2] && ipl->part[3]) {
      /* find channel */
      for (ch = irlist_get_head(&(gnetwork->channels)); ch; ch = irlist_get_next(ch)) {
        char *ptr;
        unsigned int plus;
        unsigned int part;
        unsigned int ii;

        if (strcasecmp(ch->name, ipl->part[2]) != 0)
          continue;

        plus = 0;
        part = 4;
        for (ptr = ipl->part[3]; *ptr; ++ptr) {
          if (*ptr == '+') {
            plus = 1;
          } else if (*ptr == '-') {
            plus = 0;
          } else {
            for (ii = 0; (ii < MAX_PREFIX && gnetwork->prefixes[ii].p_mode); ++ii) {
              if (*ptr == gnetwork->prefixes[ii].p_mode) {
                /* found a nick mode */
                nick = getpart(ipl->line, ++part);
                if (nick) {
                  if (nick[strlen(nick)-1] == IRCCTCP) {
                    nick[strlen(nick)-1] = '\0';
                  }
                  if (plus == 0) {
                    if (strcasecmp(nick, get_config_nick()) == 0) {
                      identify_needed(0);
                    }
                  }
                  changeinmemberlist_mode(ch, nick,
                                          gnetwork->prefixes[ii].p_symbol,
                                          plus);
                  mydelete(nick);
                }
                break;
              }
            }
            for (ii = 0; (ii < MAX_CHANMODES && gnetwork->chanmodes[ii]); ++ii) {
              if (*ptr == gnetwork->chanmodes[ii]) {
                /* found a channel mode that has an argument */
                ++part;
                break;
              }
            }
          }
        }
        return;
      }
      if (strcasecmp(ipl->part[2], get_config_nick()) == 0) {
        if (ipl->part[3][0] == '-') {
          identify_needed(0);
        }
      }
    }
    return;
  }

  if (ipl->part[2] && ipl->part[2][0] == ':') {
    part3a = ipl->part[2] + 1;
  } else {
    part3a = ipl->part[2];
  }

  /* JOIN */
  if (strcmp(ipl->part[1], "JOIN") == 0) {
    if (gnetwork->caps_nick && part3a) {
      caps(part3a);
      nick = ir_get_nickarg(ipl->line);
      if (!strcmp(caps(nick), gnetwork->caps_nick)) {
        /* we joined */
        /* clear now, we have succesfully logged in */
        gnetwork->serverconnectbackoff = 0;
        ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR,
                "Joined %s on %s", part3a, gnetwork->name);

        for (ch = irlist_get_head(&(gnetwork->channels));
             ch;
             ch = irlist_get_next(ch)) {
          if (strcmp(part3a, ch->name) != 0)
            continue;

          ch->flags |= CHAN_ONCHAN;
          ch->lastjoin = gdata.curtime;
          ch->nextann = gdata.curtime + gdata.waitafterjoin;
          ch->nextmsg = gdata.curtime + gdata.waitafterjoin;
          if (ch->joinmsg) {
            writeserver(WRITESERVER_NOW, "PRIVMSG %s :%s", ch->name, ch->joinmsg);
          }
          gnetwork->botstatus = BOTSTATUS_JOINED;
          start_sends();
          mydelete(nick);
          return;
        }
      } else {
        /* someone else joined */
        for (ch = irlist_get_head(&(gnetwork->channels));
             ch;
             ch = irlist_get_next(ch)) {
          if (strcmp(part3a, ch->name) != 0)
            continue;

          addtomemberlist(ch, nick);
          mydelete(nick);
          return;
        }
      }
      ir_unknown_channel(part3a);
      mydelete(nick);
    }
    return;
  }

  /* PART */
  if (strcmp(ipl->part[1], "PART") == 0) {
    if (gnetwork->caps_nick && part3a) {
      nick = ir_get_nickarg(ipl->line);
      if (!strcmp(caps(nick), gnetwork->caps_nick)) {
        /* we left? */
        mydelete(nick);
        return;
      } else {
        /* someone else left */
        caps(part3a);
        for (ch = irlist_get_head(&(gnetwork->channels));
             ch;
             ch = irlist_get_next(ch)) {
          if (strcmp(part3a, ch->name) != 0)
            continue;

          removefrommemberlist(ch, nick);
          mydelete(nick);
          reverify_restrictsend();
          return;
        }
      }
      ir_unknown_channel(part3a);
      mydelete(nick);
    }
    return;
  }

  /* NICK */
  if (strcmp(ipl->part[1], "NICK") == 0) {
    if (gnetwork->caps_nick && part3a) {
      nick = ir_get_nickarg(ipl->line);
      if (!strcmp(caps(nick), gnetwork->caps_nick)) {
        /* we changed, update nick */
        mydelete(gnetwork->user_nick);
        mydelete(gnetwork->caps_nick);
        gnetwork->user_nick = mystrdup(part3a);
        gnetwork->caps_nick = mystrdup(part3a);
        caps(gnetwork->caps_nick);
        gnetwork->nick_number = 0;
        ++(gdata.needsclear);
        identify_needed(0);
      } else {
        /* someone else has a new nick */
        for (ch = irlist_get_head(&(gnetwork->channels));
             ch;
             ch = irlist_get_next(ch)) {
          changeinmemberlist_nick(ch, nick, part3a);
        }
        user_changed_nick(nick, part3a);
      }
      mydelete(nick);
    }
    return;
  }

  /* KICK */
  if (strcmp(ipl->part[1], "KICK") == 0) {
    if (gnetwork->caps_nick && part3a && ipl->part[3]) {
      caps(part3a);
      if (!strcmp(caps(ipl->part[3]), gnetwork->caps_nick)) {
        /* we were kicked */
        for (ch = irlist_get_head(&(gnetwork->channels));
             ch;
             ch = irlist_get_next(ch)) {
          if (strcmp(part3a, ch->name) != 0)
            continue;

          if (gdata.noautorejoin) {
            outerror(OUTERROR_TYPE_WARN_LOUD,
                     "Kicked on %s: %s", gnetwork->name, ipl->line);
            ch->flags |= CHAN_KICKED;
            clearmemberlist(ch);
            reverify_restrictsend();
          } else {
            outerror(OUTERROR_TYPE_WARN_LOUD,
                     "Kicked on %s, Rejoining: %s", gnetwork->name, ipl->line);
            ch->flags &= ~CHAN_ONCHAN;
            joinchannel(ch);
          }
        }
      } else {
        /* someone else was kicked */
        for (ch = irlist_get_head(&(gnetwork->channels));
             ch;
             ch = irlist_get_next(ch)) {
          if (strcmp(part3a, ch->name) != 0)
            continue;

          removefrommemberlist(ch, ipl->part[3]);
          reverify_restrictsend();
          return;
        }
      }
    }
    return;
  }

  /* ERROR :Closing Link */
  if (strncmp(ipl->line, "ERROR :Closing Link", strlen("ERROR :Closing Link")) == 0) {
    if (gdata.exiting) {
      gnetwork->recentsent = 0;
      return;
    }
    ioutput(OUT_S|OUT_L|OUT_D, COLOR_RED,
            "Server Closed Connection on %s: %s",
            gnetwork->name, ipl->line);
    close_server();
    return;
  }
}
Пример #5
0
Файл: rtspd.c Проект: 2px/curl
/* returns -1 on failure */
static int send_doc(curl_socket_t sock, struct httprequest *req)
{
  ssize_t written;
  size_t count;
  const char *buffer;
  char *ptr=NULL;
  FILE *stream;
  char *cmd=NULL;
  size_t cmdsize=0;
  FILE *dump;
  bool persistant = TRUE;
  bool sendfailure = FALSE;
  size_t responsesize;
  int error = 0;
  int res;

  static char weare[256];

  char partbuf[80]="data";

  logmsg("Send response number %ld part %ld", req->testno, req->partno);

  switch(req->rcmd) {
  default:
  case RCMD_NORMALREQ:
    break; /* continue with business as usual */
  case RCMD_STREAM:
#define STREAMTHIS "a string to stream 01234567890\n"
    count = strlen(STREAMTHIS);
    for(;;) {
      written = swrite(sock, STREAMTHIS, count);
      if(got_exit_signal)
        return -1;
      if(written != (ssize_t)count) {
        logmsg("Stopped streaming");
        break;
      }
    }
    return -1;
  case RCMD_IDLE:
    /* Do nothing. Sit idle. Pretend it rains. */
    return 0;
  }

  req->open = FALSE;

  if(req->testno < 0) {
    size_t msglen;
    char msgbuf[64];

    switch(req->testno) {
    case DOCNUMBER_QUIT:
      logmsg("Replying to QUIT");
      buffer = docquit;
      break;
    case DOCNUMBER_WERULEZ:
      /* we got a "friends?" question, reply back that we sure are */
      logmsg("Identifying ourselves as friends");
      snprintf(msgbuf, sizeof(msgbuf), "RTSP_SERVER WE ROOLZ: %ld\r\n",
               (long)getpid());
      msglen = strlen(msgbuf);
      snprintf(weare, sizeof(weare),
               "HTTP/1.1 200 OK\r\nContent-Length: %zu\r\n\r\n%s",
               msglen, msgbuf);
      buffer = weare;
      break;
    case DOCNUMBER_INTERNAL:
      logmsg("Bailing out due to internal error");
      return -1;
    case DOCNUMBER_CONNECT:
      logmsg("Replying to CONNECT");
      buffer = docconnect;
      break;
    case DOCNUMBER_BADCONNECT:
      logmsg("Replying to a bad CONNECT");
      buffer = docbadconnect;
      break;
    case DOCNUMBER_404:
    default:
      logmsg("Replying to with a 404");
      if(req->protocol == RPROT_HTTP) {
        buffer = doc404_HTTP;
      }
      else {
        buffer = doc404_RTSP;
      }
      break;
    }

    count = strlen(buffer);
  }
  else {
    char *filename = test2file(req->testno);

    if(0 != req->partno)
      snprintf(partbuf, sizeof(partbuf), "data%ld", req->partno);

    stream=fopen(filename, "rb");
    if(!stream) {
      error = errno;
      logmsg("fopen() failed with error: %d %s", error, strerror(error));
      logmsg("Error opening file: %s", filename);
      logmsg("Couldn't open test file");
      return 0;
    }
    else {
      error = getpart(&ptr, &count, "reply", partbuf, stream);
      fclose(stream);
      if(error) {
        logmsg("getpart() failed with error: %d", error);
        return 0;
      }
      buffer = ptr;
    }

    if(got_exit_signal) {
      free(ptr);
      return -1;
    }

    /* re-open the same file again */
    stream=fopen(filename, "rb");
    if(!stream) {
      error = errno;
      logmsg("fopen() failed with error: %d %s", error, strerror(error));
      logmsg("Error opening file: %s", filename);
      logmsg("Couldn't open test file");
      free(ptr);
      return 0;
    }
    else {
      /* get the custom server control "commands" */
      error = getpart(&cmd, &cmdsize, "reply", "postcmd", stream);
      fclose(stream);
      if(error) {
        logmsg("getpart() failed with error: %d", error);
        free(ptr);
        return 0;
      }
    }
  }

  if(got_exit_signal) {
    free(ptr);
    free(cmd);
    return -1;
  }

  /* If the word 'swsclose' is present anywhere in the reply chunk, the
     connection will be closed after the data has been sent to the requesting
     client... */
  if(strstr(buffer, "swsclose") || !count) {
    persistant = FALSE;
    logmsg("connection close instruction \"swsclose\" found in response");
  }
  if(strstr(buffer, "swsbounce")) {
    prevbounce = TRUE;
    logmsg("enable \"swsbounce\" in the next request");
  }
  else
    prevbounce = FALSE;

  dump = fopen(RESPONSE_DUMP, "ab");
  if(!dump) {
    error = errno;
    logmsg("fopen() failed with error: %d %s", error, strerror(error));
    logmsg("Error opening file: %s", RESPONSE_DUMP);
    logmsg("couldn't create logfile: " RESPONSE_DUMP);
    free(ptr);
    free(cmd);
    return -1;
  }

  responsesize = count;
  do {
    /* Ok, we send no more than 200 bytes at a time, just to make sure that
       larger chunks are split up so that the client will need to do multiple
       recv() calls to get it and thus we exercise that code better */
    size_t num = count;
    if(num > 200)
      num = 200;
    written = swrite(sock, buffer, num);
    if(written < 0) {
      sendfailure = TRUE;
      break;
    }
    else {
      logmsg("Sent off %zd bytes", written);
    }
    /* write to file as well */
    fwrite(buffer, 1, (size_t)written, dump);
    if(got_exit_signal)
      break;

    count -= written;
    buffer += written;
  } while(count>0);

  /* Send out any RTP data */
  if(req->rtp_buffer) {
    logmsg("About to write %zu RTP bytes", req->rtp_buffersize);
    count = req->rtp_buffersize;
    do {
      size_t num = count;
      if(num > 200)
        num = 200;
      written = swrite(sock, req->rtp_buffer + (req->rtp_buffersize - count),
                       num);
      if(written < 0) {
        sendfailure = TRUE;
        break;
      }
      count -= written;
    } while(count > 0);

    free(req->rtp_buffer);
    req->rtp_buffersize = 0;
  }

  do {
    res = fclose(dump);
  } while(res && ((error = errno) == EINTR));
  if(res)
    logmsg("Error closing file %s error: %d %s",
           RESPONSE_DUMP, error, strerror(error));

  if(got_exit_signal) {
    free(ptr);
    free(cmd);
    return -1;
  }

  if(sendfailure) {
    logmsg("Sending response failed. Only (%zu bytes) of "
           "(%zu bytes) were sent",
           responsesize-count, responsesize);
    free(ptr);
    free(cmd);
    return -1;
  }

  logmsg("Response sent (%zu bytes) and written to " RESPONSE_DUMP,
         responsesize);
  free(ptr);

  if(cmdsize > 0) {
    char command[32];
    int quarters;
    int num;
    ptr=cmd;
    do {
      if(2 == sscanf(ptr, "%31s %d", command, &num)) {
        if(!strcmp("wait", command)) {
          logmsg("Told to sleep for %d seconds", num);
          quarters = num * 4;
          while(quarters > 0) {
            quarters--;
            res = wait_ms(250);
            if(got_exit_signal)
              break;
            if(res) {
              /* should not happen */
              error = errno;
              logmsg("wait_ms() failed with error: (%d) %s",
                     error, strerror(error));
              break;
            }
          }
          if(!quarters)
            logmsg("Continuing after sleeping %d seconds", num);
        }
        else
          logmsg("Unknown command in reply command section");
      }
      ptr = strchr(ptr, '\n');
      if(ptr)
        ptr++;
      else
        ptr = NULL;
    } while(ptr && *ptr);
  }
  free(cmd);
  req->open = persistant;

  prevtestno = req->testno;
  prevpartno = req->partno;

  return 0;
}
Пример #6
0
Файл: rtspd.c Проект: 2px/curl
static int ProcessRequest(struct httprequest *req)
{
  char *line=&req->reqbuf[req->checkindex];
  bool chunked = FALSE;
  static char request[REQUEST_KEYWORD_SIZE];
  static char doc[MAXDOCNAMELEN];
  static char prot_str[5];
  char logbuf[256];
  int prot_major, prot_minor;
  char *end;
  int error;
  end = strstr(line, END_OF_HEADERS);

  logmsg("ProcessRequest() called with testno %ld and line [%s]",
         req->testno, line);

  /* try to figure out the request characteristics as soon as possible, but
     only once! */
  if((req->testno == DOCNUMBER_NOTHING) &&
     sscanf(line,
            "%" REQUEST_KEYWORD_SIZE_TXT"s %" MAXDOCNAMELEN_TXT "s %4s/%d.%d",
            request,
            doc,
            prot_str,
            &prot_major,
            &prot_minor) == 5) {
    char *ptr;

    if(!strcmp(prot_str, "HTTP")) {
      req->protocol = RPROT_HTTP;
    }
    else if(!strcmp(prot_str, "RTSP")) {
      req->protocol = RPROT_RTSP;
    }
    else {
      req->protocol = RPROT_NONE;
      logmsg("got unknown protocol %s", prot_str);
      return 1;
    }

    req->prot_version = prot_major*10 + prot_minor;

    /* find the last slash */
    ptr = strrchr(doc, '/');

    /* get the number after it */
    if(ptr) {
      FILE *stream;
      char *filename;

      if((strlen(doc) + strlen(request)) < 200)
        snprintf(logbuf, sizeof(logbuf), "Got request: %s %s %s/%d.%d",
                 request, doc, prot_str, prot_major, prot_minor);
      else
        snprintf(logbuf, sizeof(logbuf), "Got a *HUGE* request %s/%d.%d",
                prot_str, prot_major, prot_minor);
      logmsg("%s", logbuf);

      if(!strncmp("/verifiedserver", ptr, 15)) {
        logmsg("Are-we-friendly question received");
        req->testno = DOCNUMBER_WERULEZ;
        return 1; /* done */
      }

      if(!strncmp("/quit", ptr, 5)) {
        logmsg("Request-to-quit received");
        req->testno = DOCNUMBER_QUIT;
        return 1; /* done */
      }

      ptr++; /* skip the slash */

      /* skip all non-numericals following the slash */
      while(*ptr && !ISDIGIT(*ptr))
        ptr++;

      req->testno = strtol(ptr, &ptr, 10);

      if(req->testno > 10000) {
        req->partno = req->testno % 10000;
        req->testno /= 10000;
      }
      else
        req->partno = 0;

      snprintf(logbuf, sizeof(logbuf), "Requested test number %ld part %ld",
               req->testno, req->partno);
      logmsg("%s", logbuf);

      filename = test2file(req->testno);

      stream=fopen(filename, "rb");
      if(!stream) {
        error = errno;
        logmsg("fopen() failed with error: %d %s", error, strerror(error));
        logmsg("Error opening file: %s", filename);
        logmsg("Couldn't open test file %ld", req->testno);
        req->open = FALSE; /* closes connection */
        return 1; /* done */
      }
      else {
        char *cmd = NULL;
        size_t cmdsize = 0;
        int num=0;

        int rtp_channel = 0;
        int rtp_size = 0;
        int rtp_partno = -1;
        int i = 0;
        char *rtp_scratch = NULL;

        /* get the custom server control "commands" */
        error = getpart(&cmd, &cmdsize, "reply", "servercmd", stream);
        fclose(stream);
        if(error) {
          logmsg("getpart() failed with error: %d", error);
          req->open = FALSE; /* closes connection */
          return 1; /* done */
        }
        ptr = cmd;

        if(cmdsize) {
          logmsg("Found a reply-servercmd section!");
          do {
            if(!strncmp(CMD_AUTH_REQUIRED, ptr, strlen(CMD_AUTH_REQUIRED))) {
              logmsg("instructed to require authorization header");
              req->auth_req = TRUE;
            }
            else if(!strncmp(CMD_IDLE, ptr, strlen(CMD_IDLE))) {
              logmsg("instructed to idle");
              req->rcmd = RCMD_IDLE;
              req->open = TRUE;
            }
            else if(!strncmp(CMD_STREAM, ptr, strlen(CMD_STREAM))) {
              logmsg("instructed to stream");
              req->rcmd = RCMD_STREAM;
            }
            else if(1 == sscanf(ptr, "pipe: %d", &num)) {
              logmsg("instructed to allow a pipe size of %d", num);
              if(num < 0)
                logmsg("negative pipe size ignored");
              else if(num > 0)
                req->pipe = num-1; /* decrease by one since we don't count the
                                      first request in this number */
            }
            else if(1 == sscanf(ptr, "skip: %d", &num)) {
              logmsg("instructed to skip this number of bytes %d", num);
              req->skip = num;
            }
            else if(3 == sscanf(ptr, "rtp: part %d channel %d size %d",
                                &rtp_partno, &rtp_channel, &rtp_size)) {

              if(rtp_partno == req->partno) {
                logmsg("RTP: part %d channel %d size %d",
                       rtp_partno, rtp_channel, rtp_size);

                /* Make our scratch buffer enough to fit all the
                 * desired data and one for padding */
                rtp_scratch = malloc(rtp_size + 4 + RTP_DATA_SIZE);

                /* RTP is signalled with a $ */
                rtp_scratch[0] = '$';

                /* The channel follows and is one byte */
                SET_RTP_PKT_CHN(rtp_scratch, rtp_channel);

                /* Length follows and is a two byte short in network order */
                SET_RTP_PKT_LEN(rtp_scratch, rtp_size);

                /* Fill it with junk data */
                for(i = 0; i < rtp_size; i+= RTP_DATA_SIZE) {
                  memcpy(rtp_scratch + 4 + i, RTP_DATA, RTP_DATA_SIZE);
                }

                if(req->rtp_buffer == NULL) {
                  req->rtp_buffer = rtp_scratch;
                  req->rtp_buffersize = rtp_size + 4;
                }
                else {
                  req->rtp_buffer = realloc(req->rtp_buffer,
                                            req->rtp_buffersize +
                                            rtp_size + 4);
                  memcpy(req->rtp_buffer + req->rtp_buffersize, rtp_scratch,
                         rtp_size + 4);
                  req->rtp_buffersize += rtp_size + 4;
                  free(rtp_scratch);
                }
                logmsg("rtp_buffersize is %zu, rtp_size is %d.",
                       req->rtp_buffersize, rtp_size);
              }
            }
            else {
              logmsg("funny instruction found: %s", ptr);
            }

            ptr = strchr(ptr, '\n');
            if(ptr)
              ptr++;
            else
              ptr = NULL;
          } while(ptr && *ptr);
          logmsg("Done parsing server commands");
        }
        free(cmd);
      }
    }
    else {
      if(sscanf(req->reqbuf, "CONNECT %" MAXDOCNAMELEN_TXT "s HTTP/%d.%d",
                doc, &prot_major, &prot_minor) == 3) {
        snprintf(logbuf, sizeof(logbuf),
                 "Received a CONNECT %s HTTP/%d.%d request",
                 doc, prot_major, prot_minor);
        logmsg("%s", logbuf);

        if(req->prot_version == 10)
          req->open = FALSE; /* HTTP 1.0 closes connection by default */

        if(!strncmp(doc, "bad", 3))
          /* if the host name starts with bad, we fake an error here */
          req->testno = DOCNUMBER_BADCONNECT;
        else if(!strncmp(doc, "test", 4)) {
          /* if the host name starts with test, the port number used in the
             CONNECT line will be used as test number! */
          char *portp = strchr(doc, ':');
          if(portp && (*(portp+1) != '\0') && ISDIGIT(*(portp+1)))
            req->testno = strtol(portp+1, NULL, 10);
          else
            req->testno = DOCNUMBER_CONNECT;
        }
        else
          req->testno = DOCNUMBER_CONNECT;
      }
      else {
        logmsg("Did not find test number in PATH");
        req->testno = DOCNUMBER_404;
      }
    }
  }

  if(!end) {
    /* we don't have a complete request yet! */
    logmsg("ProcessRequest returned without a complete request");
    return 0; /* not complete yet */
  }
  logmsg("ProcessRequest found a complete request");

  if(req->pipe)
    /* we do have a full set, advance the checkindex to after the end of the
       headers, for the pipelining case mostly */
    req->checkindex += (end - line) + strlen(END_OF_HEADERS);

  /* **** Persistence ****
   *
   * If the request is a HTTP/1.0 one, we close the connection unconditionally
   * when we're done.
   *
   * If the request is a HTTP/1.1 one, we MUST check for a "Connection:"
   * header that might say "close". If it does, we close a connection when
   * this request is processed. Otherwise, we keep the connection alive for X
   * seconds.
   */

  do {
    if(got_exit_signal)
      return 1; /* done */

    if((req->cl==0) && curlx_strnequal("Content-Length:", line, 15)) {
      /* If we don't ignore content-length, we read it and we read the whole
         request including the body before we return. If we've been told to
         ignore the content-length, we will return as soon as all headers
         have been received */
      char *endptr;
      char *ptr = line + 15;
      unsigned long clen = 0;
      while(*ptr && ISSPACE(*ptr))
        ptr++;
      endptr = ptr;
      errno = 0;
      clen = strtoul(ptr, &endptr, 10);
      if((ptr == endptr) || !ISSPACE(*endptr) || (ERANGE == errno)) {
        /* this assumes that a zero Content-Length is valid */
        logmsg("Found invalid Content-Length: (%s) in the request", ptr);
        req->open = FALSE; /* closes connection */
        return 1; /* done */
      }
      req->cl = clen - req->skip;

      logmsg("Found Content-Length: %lu in the request", clen);
      if(req->skip)
        logmsg("... but will abort after %zu bytes", req->cl);
      break;
    }
    else if(curlx_strnequal("Transfer-Encoding: chunked", line,
                            strlen("Transfer-Encoding: chunked"))) {
      /* chunked data coming in */
      chunked = TRUE;
    }

    if(chunked) {
      if(strstr(req->reqbuf, "\r\n0\r\n\r\n"))
        /* end of chunks reached */
        return 1; /* done */
      else
        return 0; /* not done */
    }

    line = strchr(line, '\n');
    if(line)
      line++;

  } while(line);

  if(!req->auth && strstr(req->reqbuf, "Authorization:")) {
    req->auth = TRUE; /* Authorization: header present! */
    if(req->auth_req)
      logmsg("Authorization header found, as required");
  }

  if(!req->digest && strstr(req->reqbuf, "Authorization: Digest")) {
    /* If the client is passing this Digest-header, we set the part number
       to 1000. Not only to spice up the complexity of this, but to make
       Digest stuff to work in the test suite. */
    req->partno += 1000;
    req->digest = TRUE; /* header found */
    logmsg("Received Digest request, sending back data %ld", req->partno);
  }
  else if(!req->ntlm &&
          strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAD")) {
    /* If the client is passing this type-3 NTLM header */
    req->partno += 1002;
    req->ntlm = TRUE; /* NTLM found */
    logmsg("Received NTLM type-3, sending back data %ld", req->partno);
    if(req->cl) {
      logmsg("  Expecting %zu POSTed bytes", req->cl);
    }
  }
  else if(!req->ntlm &&
          strstr(req->reqbuf, "Authorization: NTLM TlRMTVNTUAAB")) {
    /* If the client is passing this type-1 NTLM header */
    req->partno += 1001;
    req->ntlm = TRUE; /* NTLM found */
    logmsg("Received NTLM type-1, sending back data %ld", req->partno);
  }
  else if((req->partno >= 1000) &&
          strstr(req->reqbuf, "Authorization: Basic")) {
    /* If the client is passing this Basic-header and the part number is
       already >=1000, we add 1 to the part number.  This allows simple Basic
       authentication negotiation to work in the test suite. */
    req->partno += 1;
    logmsg("Received Basic request, sending back data %ld", req->partno);
  }
  if(strstr(req->reqbuf, "Connection: close"))
    req->open = FALSE; /* close connection after this request */

  if(!req->pipe &&
     req->open &&
     req->prot_version >= 11 &&
     end &&
     req->reqbuf + req->offset > end + strlen(END_OF_HEADERS) &&
     (!strncmp(req->reqbuf, "GET", strlen("GET")) ||
      !strncmp(req->reqbuf, "HEAD", strlen("HEAD")))) {
    /* If we have a persistent connection, HTTP version >= 1.1
       and GET/HEAD request, enable pipelining. */
    req->checkindex = (end - req->reqbuf) + strlen(END_OF_HEADERS);
    req->pipelining = TRUE;
  }

  while(req->pipe) {
    if(got_exit_signal)
      return 1; /* done */
    /* scan for more header ends within this chunk */
    line = &req->reqbuf[req->checkindex];
    end = strstr(line, END_OF_HEADERS);
    if(!end)
      break;
    req->checkindex += (end - line) + strlen(END_OF_HEADERS);
    req->pipe--;
  }

  /* If authentication is required and no auth was provided, end now. This
     makes the server NOT wait for PUT/POST data and you can then make the
     test case send a rejection before any such data has been sent. Test case
     154 uses this.*/
  if(req->auth_req && !req->auth)
    return 1; /* done */

  if(req->cl > 0) {
    if(req->cl <= req->offset - (end - req->reqbuf) - strlen(END_OF_HEADERS))
      return 1; /* done */
    else
      return 0; /* not complete yet */
  }

  return 1; /* done */
}