Exemplo n.º 1
0
int
t1 (void *arg)
{
    thread_setattr (current_thread, MY_NAME, arg);

    logme ("Started");
    for (;;)
    {
        logme ("sleep 2 secs");
        thread_sleep (2000);
        logme ("signal event");
        logme ("wakeup retd %d", thread_signal_cond (&ev));
    }
    return 0;
}
Exemplo n.º 2
0
int command_forward(json_object *json, BIO *bio_src)
{
   int r = -1, len = 0;
   json_object *params = NULL, *body = NULL;
   char *address = NULL, *cookie = NULL, *data = NULL, buf[100 * 1024];
   BIO *bio_conn = NULL;

   do {
      if(!(params = json_object_object_get(json, "params"))) break;
      if(!(address = (char *)json_object_get_string(json_object_object_get(params, "address")))) break;
      logme(LOGMSG_DEBUG, "FORWARD -> address: %s", address);
      if(!(cookie = (char *)json_object_get_string(json_object_object_get(params, "cookie")))) break;
      logme(LOGMSG_DEBUG, "FORWARD -> cookie: %s", cookie);

      if(!(body = json_object_object_get(json, "body"))) break;
      if(!(data = (char *)json_object_get_string(body))) break;
      if(!(len = json_object_get_string_len(body))) break;
      logme(LOGMSG_DEBUG, "FORWARD -> data: %d bytes", len);

      if(!(bio_conn = BIO_new_connect(address))) break;
      if(BIO_do_connect(bio_conn) <= 0) { logme(LOGMSG_ERROR, "Unable to connect to %s", address); break; }
      if(BIO_printf(bio_conn, "POST / HTTP/1.0\r\n" \
                              "Host: %s\r\n" \
                              "Accept: */" "*\r\n" \
                              "Cookie: %s\r\n" \
                              "Content-Length: %d\r\n" \
                              "Content-Type: application/octet-stream\r\n" \
                              "Connection: close\r\n" \
                              "\r\n",
                              address, cookie, len) <= 0) break;
      if(BIO_write(bio_conn, data, len) != len) break;
      (void)BIO_flush(bio_conn);

      while((len = BIO_read(bio_conn, buf, sizeof(buf))) > 0) if(BIO_write(bio_src, buf, len) != len) break;
      if(len != 0) break;

      r = 0;
   } while(0);
   if(bio_conn) BIO_free(bio_conn);

   return r;
}
Exemplo n.º 3
0
int
main ()
{
    thread_initial (0);

    thread_setattr (current_thread, MY_NAME, "Main");

    logme ("Started");
    thread_create (t1, 0, "t1");

    logme ("calling infinite thread_select");
    thread_select (0, NULL, NULL, &ev, TV_INFINITE);
    logme ("Woke up!");

    logme ("calling infinite thread_wait_cond");
    thread_wait_cond (&ev, NULL, TV_INFINITE);
    logme ("Woke up!");

    return 0;
}
Exemplo n.º 4
0
int command_config(json_object *json, BIO *bio_src)
{
   int r = -1;
   json_object *body = NULL;
   char *val;
   BIO *bio_file = NULL;
   char *resultok = "{\"command\":\"CONFIG\",\"result\":{\"status\":\"OK\",\"msg\":\"New configuration applied\"}}";
   char *resultko = "{\"command\":\"CONFIG\",\"result\":{\"status\":\"ERROR\",\"msg\":\"Error applying new configuration\"}}";

   do {
      if(!(body = json_object_object_get(json, "body"))) break;

      json_object_object_foreach(body, k, v) {
         val = (char *)json_object_get_string(v);
         if(!strcmp(k, "nexthop")) {
            if(!(bio_file = BIO_new_file(NEXTHOP_FILE, "w"))) break;
            BIO_puts(bio_file, val);
            BIO_free(bio_file);
            logme(LOGMSG_DEBUG, "CONFIG -> nexthop: %s", val);
         } else if(!strcmp(k, "cookie")) {
            if(!(bio_file = BIO_new_file(COOKIE_FILE, "w"))) break;
            BIO_puts(bio_file, val);
            BIO_free(bio_file);
            logme(LOGMSG_DEBUG, "CONFIG -> cookie: %s", val);
         } else if(!strcmp(k, "key")) {
            if(!(bio_file = BIO_new_file(KEY_FILE, "w"))) break;
            BIO_puts(bio_file, val);
            BIO_free(bio_file);
            logme(LOGMSG_DEBUG, "CONFIG -> key: %s", val);
         } else if(!strcmp(k, "version")) {
            if(!(bio_file = BIO_new_file(VERSION_FILE, "w"))) break;
            BIO_puts(bio_file, val);
            BIO_free(bio_file);
            logme(LOGMSG_DEBUG, "CONFIG -> version: %s", val);
         }
      }

      if(kill(getppid(), SIGHUP)) break;

      r = 0;
   } while(0);
Exemplo n.º 5
0
void ExitOn(const char *errmsg)
{
    if (!errmsg) {
#ifndef NO_STRERROR
	logme(LOG_ERROR, "%s", strerror(errno));
#else
	logme(LOG_ERROR, "Fatal error (errno %d)", errno);
#endif
    } else {
	logme(LOG_ERROR, "%s", errmsg);
    }

    if (runData.computerActive) {
	KillComputer();
    }
    close(runData.icsFd);

    StopLogging();

    exit(runData.exitValue);
}  
Exemplo n.º 6
0
int proxyhandler(BIO *cbio)
{
   BIO *mbio = NULL, *sbio = NULL;
   char *mptr = NULL;
   long mlen;
   int cfd, sfd, len = 0, found = 0;
   fd_set rfds;
   char buf[1024];
   struct sockaddr_in caddr;
   char auth[1024] = {0};
   int cl = 0;

   mbio = BIO_new(BIO_s_mem());

   for(len = 0; ; len = 0) {
      while(len < sizeof(buf)) {
         if(BIO_read(cbio, buf + len, 1) != 1) return -1;
         if(buf[len++] == '\n') break;
      }
      buf[--len] = '\0';
      if(len && (buf[len - 1] == '\r')) buf[len - 1] = '\0';
      if(!buf[0]) break;

      if(!strncasecmp(buf, "X-Forwarded-For: ", strlen("X-Forwarded-For: "))) found |= FOUND_XFF;
      if(!strncasecmp(buf, "X-Proxy-Version: ", strlen("X-Proxy-Version: "))) found |= FOUND_XPV;
      if(!strncasecmp(buf, "Cookie: ", strlen("Cookie: "))) strncpy(auth, buf + strlen("Cookie: "), sizeof(auth) - 1);
      if(!strncasecmp(buf, "Content-Length: ", strlen("Content-Length: "))) cl = atoi(buf + strlen("Content-Length: "));
      if(BIO_printf(mbio, "%s\r\n", buf) <= 0) return -1;
   }

   logme(LOGMSG_DEBUG, "Cookie: %s", auth);

   if(!strcmp(auth, conf.cookie)) return commandhandler(cbio, cl);

   sbio = BIO_new_connect(conf.nexthop);

   if(BIO_do_connect(sbio) != 1) {
      logme(LOGMSG_STATUSERROR, "Unable to connect to %s", conf.nexthop);

      return -1;
   }
   logme(LOGMSG_STATUSOK, "Running");
   logme(LOGMSG_DEBUG, "Connected to %s", conf.nexthop);
   sfd = BIO_get_fd(sbio, NULL);

   cfd = BIO_get_fd(cbio, NULL);
   len = sizeof(caddr);
   getpeername(cfd, (struct sockaddr *)&caddr, (socklen_t *)&len);

   if(!(found & FOUND_COOKIE)) logme(LOGMSG_DEBUG, "New session forwarded for %s", inet_ntoa(caddr.sin_addr));

   if((mlen = BIO_get_mem_data(mbio, &mptr)) > 0) BIO_write(sbio, mptr, mlen);
   if(!(found & FOUND_XFF)) if(BIO_printf(sbio, "X-Forwarded-For: %s\r\n", inet_ntoa(caddr.sin_addr)) <= 0) return -1;
   if(!(found & FOUND_XPV)) if(BIO_printf(sbio, "X-Proxy-Version: %s\r\n", conf.version) <= 0) return -1;
   if(BIO_puts(sbio, "\r\n") <= 0) return -1;

   do {
      FD_ZERO(&rfds);
      FD_SET(sfd, &rfds);
      FD_SET(cfd, &rfds);
      if(select(((sfd > cfd) ? sfd : cfd) + 1, &rfds, NULL, NULL, NULL) == -1) return -1;

      if(FD_ISSET(sfd, &rfds)) {
         if((len = BIO_read(sbio, buf, sizeof(buf))) > 0) if(BIO_write(cbio, buf, len) <= 0) return -1;
      } else if(FD_ISSET(cfd, &rfds)) {
         if((len = BIO_read(cbio, buf, sizeof(buf))) > 0) if(BIO_write(sbio, buf, len) <= 0) return -1;
      }
   } while(len > 0);

   return 0;
}
Exemplo n.º 7
0
int commandhandler(BIO *cbio, int cl)
{
   BIO *bbody = NULL, *bbase64 = NULL, *bcrypt = NULL;
   int ret = -1;
   char buf[100 * 1024];
   json_object *config = NULL;
   unsigned char iv[16];
   BIO *bmem = NULL;
   char *bptr = NULL, *c = NULL;
   long blen = 0;
   char *command = NULL;

   logme(LOGMSG_DEBUG, "commandhandler (cl=%d)", cl);

   do {
      if(!(bmem = BIO_new(BIO_s_mem()))) break;
      if(!(bbody = BIO_new(BIO_s_mem()))) break;
      if(!(bbase64 = BIO_new(BIO_f_base64()))) break;
      BIO_set_flags(bbase64, BIO_FLAGS_BASE64_NO_NL);
      if(!(bcrypt = BIO_new(BIO_f_cipher()))) break;
      memset(iv, 0x00, sizeof(iv));
      BIO_set_cipher(bcrypt, EVP_get_cipherbyname("aes-128-cbc"), (unsigned char *)conf.key, iv, 0);
      BIO_push(bbase64, bbody);
      BIO_push(bcrypt, bmem);

      while(blen < cl) {
         if((ret = BIO_read(cbio, buf, ((cl - blen) > sizeof(buf)) ? sizeof(buf) : (cl - blen))) <= 0) break;
         blen += ret;

         while((c = memchr(buf, '\n', ret)) || (c = memchr(buf, '\r', ret))) memmove(c, c + 1, --ret - (c - buf));

         if(BIO_write(bbody, buf, ret) != ret) {
            logme(LOGMSG_DEBUG, "BIO_write error");
            break;
         }
      }

      do {
         blen = BIO_read(bbase64, buf, sizeof(buf));
         if(blen > 0) {
            BIO_write(bcrypt, buf, blen);
         }
      } while(blen > 0);
      (void)BIO_flush(bcrypt);
      blen = BIO_get_mem_data(bmem, &bptr);

      if(!(config = json_tokener_parse(bptr))) break;
      if(!(command = (char *)json_object_get_string(json_object_object_get(config, "command")))) break;

      logme(LOGMSG_DEBUG, "command: %s", command);
      if(!strcasecmp(command, "FORWARD")) {
         ret = command_forward(config, cbio);
      } else if(!strcasecmp(command, "CONFIG")) {
         ret = command_config(config, cbio);
      } else if(!strcasecmp(command, "UPGRADE")) {
         ret = command_upgrade(config, cbio);
      } else if(!strcasecmp(command, "CHECK")) {
         ret = command_check(config, cbio);
      }
   } while(0);
   if(bbody) BIO_free(bbody);
   if(bbase64) BIO_free(bbase64);
   if(bcrypt) BIO_free(bcrypt);
   if(bmem) BIO_free(bmem);
   if(config) json_object_put(config);

   return ret;
}
Exemplo n.º 8
0
BOOL LogServer::CreateLogPipe()
{
    logme("LogServer::CreateLogPipe: cmd=%s", GetExternalCmd());
    m_fpLog = _popen(GetExternalCmd(), "wb");
    if (m_fpLog == NULL)
    {
        fprintf(stderr, "LogServer: failed to open pipe (%s). err=%d\n", GetExternalCmd(), GetLastError());
        return FALSE;
    }
    return TRUE;

#if 0
    // Create the program
    HANDLE hProgStdinRd, hProgStdinWr;
    SECURITY_ATTRIBUTES saAttr;

    // Set the bInheritHandle flag so pipe handles are inherited.
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
    saAttr.bInheritHandle = TRUE;
    saAttr.lpSecurityDescriptor = NULL;

    // Create a pipe for the child process's STDIN.
    if (! CreatePipe(&hProgStdinRd, &hProgStdinWr, &saAttr, 0))
    {
        printf("JaxerLogger: Create Stdin pipe failed.\n");
        return FALSE;
    }

    // Ensure that the write handle to the child process's pipe for STDIN is not inherited.
    SetHandleInformation( hProgStdinWr, HANDLE_FLAG_INHERIT, 0);

    PROCESS_INFORMATION pi;
    ZeroMemory(&pi, sizeof(PROCESS_INFORMATION) );

    STARTUPINFO si;
    ZeroMemory(&si, sizeof(STARTUPINFO) );
    si.cb = sizeof(STARTUPINFO);
    si.hStdInput = hProgStdinRd;
    si.dwFlags |= STARTF_USESTDHANDLES;

    logme("JaxerLog: pipe cmd=%s", GetExternalCmd());
    BOOL rc = CreateProcess(NULL,
                            (TCHAR*)GetExternalCmd(),
                            NULL,  /* default process security descriptor */
                            NULL,  /* default thread security descriptor */
                            TRUE,  /* inherit handles */
                            CREATE_NO_WINDOW | CREATE_BREAKAWAY_FROM_JOB,
                            NULL,  /* inherit environment */
                            NULL,  /* inherit current directory */
                            &si,
                            &pi);

    if (!rc)
    {
        char s[256];
        sprintf(s,"JaxerLogger: Create process (%s) failed. err=%d\n", GetExternalCmd(),
                GetLastError());
        char *t = s;
        logme(s);
        return FALSE;
    }

    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    m_fdLog = hProgStdinWr;
}
Exemplo n.º 9
0
int main(int argc, char *argv[]) 
{
    FILE *fp;
    char buf[256];

    if (argc == 1 || ParseArgs(argc, argv) == ERROR)
	Usage();

    StartLogging();

    /* auto-config */
    if (appData.autoConfigure) {
	if (strstr(appData.program, "crafty")) {
	    printf("Auto-configuring for Crafty\n");
	    appData.killEngine = FALSE;
	    appData.haveCmdNew = TRUE;
	    appData.haveCmdResult = TRUE;
	    appData.haveCmdType = FALSE;
	    appData.craftyMode = TRUE;
	    appData.longAlgMoves = FALSE;
	    appData.haveCmdPing = TRUE;
	} else if (strstr(appData.program, "gnuchessx")) {
	    printf("Auto-configuring for GNUChess 4.x\n");
	    appData.killEngine = TRUE;
	    appData.haveCmdNew = FALSE;
	    appData.haveCmdResult = FALSE;
	    appData.haveCmdType = FALSE;
	    appData.craftyMode = FALSE;
	    appData.longAlgMoves = FALSE;
	    appData.haveCmdPing = FALSE;
	} else {
	    printf("Cannot auto-configure as I don't recognize \"%s\"\n", appData.program);
	}
    }

    fp = fopen(".icsdronerc", "r");

    if (!(runData.handle = getenv("FICSHANDLE"))) {
	if (!fp || !fgets(buf, sizeof(buf), fp)) {
	    ExitOn("You must specify the handle.\nSee README file for help.\n");
	} else {
	    buf[ strlen(buf)-1 ] = '\0';
	    runData.handle = strdup(buf);
	}
    }

    if (!(runData.passwd = getenv("FICSPASSWD"))) {
	if (!fp || !fgets(buf, sizeof(buf), fp)) {
	    ExitOn("You must specify the password.\nSee README file for help.\n");
	} else {
	    buf[ strlen(buf)-1 ] = '\0';
	    runData.passwd = strdup(buf);
	}
    }
  
    signal(SIGINT, TerminateProc);
    signal(SIGTERM, TerminateProc);
    signal(SIGPIPE, BrokenPipe);

    if ((runData.icsFd = OpenTCP(appData.icsHost, appData.icsPort)) <= 0) 
	ExitOn("Failed to connect to server.\n");

    SendToIcs("%s\n%s\n", runData.handle, runData.passwd);

    /*
     *  If we are in craftymode and don't kill engine after each game, 
     *  we might aswell start the engine now.  This way the engine can
     *  init TBs or whatever it needs before it's supposed to start
     *  playing                                                       
     */
    if (appData.haveCmdNew && !appData.killEngine)
	StartComputer();
  
    SendToIcs("set style 12\n"
	      "set shout 0\n"
	      "set cshout 0\n"
	      "set open 1\n"
	      "set highlight 0\n"
	      "set prompt fics%%\n"
	      "set bell 0\n" 
	      "set seek 0\n"
	      "set width 240\n"
	      "set pin 0\n"
	      "set gin 0\n"
	      "set interface %s %s, (c) 1996-2001 by Henrik Gram.\n"
	      "iset movecase 1\n"
	      "iset nohighlight 1\n"
	      "iset lock 1\n",
	      PRODUCTNAME, PRODUCTVERSION);

    /* Send the rest of the lines in .icsdronerc as commands to the server */
    if (fp) {
	while(fgets(buf, sizeof(buf), fp))
	    SendToIcs("%s", buf);
	fclose(fp);
    }  

    MainLoop();

    logme(LOG_INFO, "Program terminated normally.");

    StopLogging();

    return 0;
}