Example #1
0
int main(int argc, char* argv[])
{
  const char* tmp;
  const char* end;
  int log_requests;
  
  log_requests = getenv("LOGREQUESTS") != 0;
  log_responses = getenv("LOGRESPONSES") != 0;

  tmp = getenv("TIMEOUT");
  if (tmp) {
    if ((timeout = strtou(tmp, &end)) == 0 || *end != 0) {
      respond(421, 1, "Configuration error, invalid timeout value");
      return 1;
    }
  }
  else
    timeout = 900;
  inbuf.io.timeout = timeout * 1000;
  outbuf.io.timeout = timeout * 1000;

  sig_alarm_catch(handle_alrm);
  if (!startup(argc, argv)) return 1;
  for (;;) {
    int len = read_request();
    if (len < 0) break;
    parse_request(len);
    if (!dispatch_request(internal_verbs, verbs, log_requests)) break;
  }
  return 0;
}
Example #2
0
static void
glitch_items(size_t index, unsigned int name)
{
    char buf[40] = { 0 };
    unsigned int item;
    void __attribute__((regparm(1))) (*fnptr)(unsigned int);

    printf("BZZZT!\n");
    if (fread_until(buf, '\n', sizeof(buf), stdin) == EXIT_FAILURE)
        return;
    if (cgc_strlen(buf) == 0 || strtou(buf, 16, &item) == EXIT_FAILURE)
        return;

    fnptr = (void *)(name | 0xf0000000);
    fnptr(item);
}
Example #3
0
static int parse_gids(const char* gids)
{
  gid_t groups[NGROUPS_MAX];
  int count;
  const char* ptr;
  ptr = gids;
  count = 0;
  while (*ptr != 0 && count < NGROUPS_MAX) {
    groups[count] = strtou(ptr, &ptr);
    if (*ptr != 0 && *ptr != ',') return 0;
    while (*ptr == ',') ++ptr;
    ++count;
  }
  if (setgroups(count, groups) == -1) return 0;
  return 1;
}
Example #4
0
int
do_safari_zone(void)
{
    char buf[40] = { 0 };
    unsigned int move, round, name;
    size_t num_monsters = sizeof(data) / sizeof(data[0]);
    size_t index = game_state.games.safari_zone.encounter_data + 1;
    struct monster *monster;
    unsigned char capture_chance, run_chance;

    printf("Welcome to the Safari Zone!\n");

    if (!check_cookie(game_state.games.gallon_challenge.cookie))
        return EXIT_FAILURE;

    if (index > num_monsters - 1) {
        printf("Nothing happened...\n");
        return EXIT_SUCCESS;
    }

    monster = &data[index];
    game_state.games.safari_zone.encounter_data = get_flag_byte(index) % (num_monsters - 2);
    capture_chance = monster->capture_chance;
    run_chance = monster->run_chance;
    round = 0;

    printf("A wild %s has appeared!\n", monster->name);
    while (1) {
        printf("What to do?\n"
                "1. Ball\n"
                "2. Rock\n"
                "3. Bait\n"
                "4. Run\n\n");

        if (fread_until(buf, '\n', sizeof(buf), stdin) == EXIT_FAILURE)
            return EXIT_FAILURE;
        if (cgc_strlen(buf) == 0 || strtou(buf, 16, &move) == EXIT_FAILURE)
            return EXIT_FAILURE;

        if (round > 10 || (round > 0 && run_chance >= get_flag_byte(round))) {
            printf("%s got away :(\n", monster->name);
            return EXIT_SUCCESS; 
        }
        round++;

        if (move == 1) {
            if (capture_chance >= get_flag_byte(round++))
                break;
            else
                printf("Darn! Almost had it!\n");
        } else if (move == 2) {
            capture_chance *= 2;
            run_chance *= 2;
        } else if (move == 3) {
            capture_chance /= 2;
            run_chance /= 2;
        } else if (move == 4) {
            printf("Got away safely!\n");
            return EXIT_SUCCESS;
        }
    }

    printf("Congratulations, you've caught %s!\n"
            "Please enter a nickname:\n", monster->name);

    do {
        if (fread_until(buf, '\n', sizeof(buf), stdin) == EXIT_FAILURE)
            continue;
        if (cgc_strlen(buf) == 0 || strtou(buf, 16, &name) == EXIT_FAILURE)
            continue;

        break;
    } while(1);

    monster->set_nickname(index, name);

    return EXIT_SUCCESS;
}
Example #5
0
int vksprintf(char *buf,char *fmt,va_list parms)
{
    int scanned = 0,w = 0,prec = 0, l, size = 0;
    int n1 = 0;
    unsigned n2 = 0,parsing = 0,flag = 0;
    char *base = buf;
    char *sp = NULL;
    
    while (*fmt != 0) {
	if (*fmt != '%' && !parsing) {
	    /* No token detected */
	    *buf++ = *fmt++;
	    scanned++;
	}
	else {	    
	    /* We need to make a conversion */
	    if (*fmt == '%') {
		fmt++;
		parsing = 1;
		w = 10;
		prec = 4;
		size = STD_SIZE;
		flag = 0;
	    }
	    /* Parse token */
	    switch(*fmt) {
		case '%' : *buf++ = '%';
			   scanned++;
			   parsing = 0;
			   break;
		case 'c' : *buf++ = va_arg(parms, char);
			   scanned++;
			   parsing = 0;
			   break;
		case 'i' :
	 	case 'd' : switch (size) {
		    		case STD_SIZE : n1 = va_arg(parms, int);
						break;
				case LONG_SIZE : n1 = va_arg(parms, long int);
						 break;
				case SHORT_SIZE : n1 = va_arg(parms, short int);
						  break;
			   }
			   l = dcvt(n1,buf,10,w,flag);
			   scanned += l;
			   buf += l;
			   parsing = 0;
			   break;
		case 'u' : switch (size) {
		    		case STD_SIZE : n2 = va_arg(parms, unsigned);
						break;
				case LONG_SIZE : n2 = va_arg(parms, unsigned long);
						 break;
				case SHORT_SIZE : n2 = va_arg(parms, unsigned short);
						  break;
			   }
			   l = ucvt(n2,buf,10,w,flag);
			   scanned += l;
			   buf += l;
			   parsing = 0;
			   break;
		case 'p' :
		case 'x' : switch (size) {
		    		case STD_SIZE : n2 = va_arg(parms, unsigned);
						break;
				case LONG_SIZE : n2 = va_arg(parms, unsigned long);
						 break;
				case SHORT_SIZE : n2 = va_arg(parms, unsigned short);
						  break;
			   }
			   l = ucvt(n2,buf,16,w,flag);
			   scanned += l;
			   buf += l;
			   parsing = 0;
			   break;
		case 's' : sp = va_arg(parms, char *);
			   l = 0;
			   while (*sp != 0) {
			       *buf++ = *sp++;
			       l++;
			   }
			   scanned += l;
			   parsing = 0;
			   break;
		case 'l' : size = LONG_SIZE;
			   break;
		case 'n' :
		case 'h' : size = SHORT_SIZE;
			   break;
		case '+' : flag |= ADD_PLUS;
			   break;
		case '-' : flag |= LEFT_PAD;
			   break;
		case '.' : parsing = 2;
			   flag |= RESPECT_WIDTH;
			   break;
		case '1' :
		case '2' :
		case '3' :
		case '4' :
		case '5' :
		case '6' :
		case '7' :
		case '8' :
		case '9' :
		case '0' : if (parsing == 1) {
		    	      w = strtou(fmt,10,&base);
			      /* MG */
			      /* if the first numeral is zero a ZERO pad is */
			      /* required */
			      /* but not if LEFT_PAD is set*/
			      if (*fmt!='0'||flag&LEFT_PAD)
				  flag |= SPACE_PAD ;
			      else
				  flag |= ZERO_PAD ;
			      fmt = base-1;
			   } else if (parsing == 2) {
			       prec = strtou(fmt,10,&base);
			       fmt = base-1;
			       parsing = 1;
			   }
			   break;
		default :  parsing = 0;
			   break;
	    }
	    fmt++;
	}
    }
    *buf = 0;
    return(scanned);
}
Example #6
0
int startup(int argc, char* argv[])
{
  const char* tmp;
  const char* end;
  const char* cwdstr;
  char* ptr;
  unsigned long session_timeout;
  unsigned startup_code;

  if ((tmp = getenv("TCPLOCALIP")) == 0) FAIL("Missing $TCPLOCALIP.");
  if (!parse_localip(tmp)) FAIL("Could not parse $TCPLOCALIP.");
  if ((tmp = getenv("TCPREMOTEIP")) == 0) FAIL("Missing $TCPREMOTEIP.");
  if (!parse_remoteip(tmp)) FAIL("Could not parse $TCPREMOTEIP.");
  if ((tmp = getenv("UID")) == 0) FAIL("Missing $UID.");
  if (!(uid = strtou(tmp, &end)) || *end) FAIL("Invalid $UID.");
  if ((tmp = getenv("GID")) == 0) FAIL("Missing $GID.");
  if (!(gid = strtou(tmp, &end)) || *end) FAIL("Invalid $GID.");
  if ((home = getenv("HOME")) == 0) FAIL("Missing $HOME.");
  if ((tmp = getenv("GIDS")) != 0 && !parse_gids(tmp))
    FAIL("Could not parse or set supplementary group IDs.");

  /* Strip off trailing slashes in $HOME */
  ptr = (char*)home + strlen(home)-1;
  while (ptr > home && *ptr == '/') *ptr-- = 0;

  if ((user = getenv("USER")) == 0) FAIL("Missing $USER.");
  if ((group = getenv("GROUP")) == 0) group = "mygroup";
  if (chdir(home)) FAIL("Could not chdir to $HOME.");
  if (!load_tables()) FAIL("Loading startup tables failed.");
  if (getenv("CHROOT") != 0) {
    cwdstr = "/";
    if (chroot(".")) FAIL("Could not chroot.");
  }
  else if (getenv("SOFTCHROOT") != 0) {
    cwdstr = "/";
  }
  else {
    cwdstr = home;
    if (chdir("/")) FAIL("Could not chdir to '/'.");
  }
  if (!str_copys(&cwd, cwdstr)) FAIL("Could not set CWD string");
  if (setgid(gid)) FAIL("Could not set GID.");
  if (setuid(uid)) FAIL("Could not set UID.");
  if ((user_len = strlen(user)) > MAX_NAME_LEN) {
    user_len = MAX_NAME_LEN;
    ((char*)user)[MAX_NAME_LEN] = 0;
  }
  if ((group_len = strlen(group)) > MAX_NAME_LEN) {
    group_len = MAX_NAME_LEN;
    ((char*)group)[MAX_NAME_LEN] = 0;
  }

  lockhome = (getenv("LOCKHOME") != 0);
  nodotfiles = (getenv("NODOTFILES") != 0);
  list_options = (nodotfiles ? 0 : PATH_MATCH_DOTFILES);

  session_timeout = 0;
  if ((tmp = getenv("SESSION_TIMEOUT")) != 0)
    session_timeout = strtou(tmp, &tmp);
  alarm(session_timeout);
  connect_timeout = timeout;
  if ((tmp = getenv("CONNECT_TIMEOUT")) != 0)
    connect_timeout = strtou(tmp, &tmp);

  if ((tmp = getenv("TWOFTPD_BIND_PORT_FD")) != 0) {
    if ((bind_port_fd = strtou(tmp, &end)) == 0 || *end != 0)
      FAIL("Invalid $TWOFTPD_BIND_PORT_FD");
  }
  else
    bind_port_fd = -1;

  startup_code = (getenv("AUTHENTICATED") != 0) ? 230 : 220;
  if ((tmp = getenv("BANNER")) != 0) show_banner(startup_code, tmp);
  message_file = getenv("MESSAGEFILE");
  show_message_file(startup_code);
  return respond(startup_code, 1, "Ready to transfer files.");
  (void)argc;
  (void)argv;
}