Ejemplo n.º 1
0
int
fmtmsg(long classification, const char *label, int severity,
	const char *text, const char *action, const char *tag)
{
	FILE *console;
	const char *p, *sevstr;
	int result;

	/* Validate label constraints, if not null. */
	if (label != MM_NULLLBL) {
		/*
		 * Two fields, separated by a colon.  The first field is up to
		 * 10 bytes, the second is up to 14 bytes.
		 */
		p = strchr(label, ':');
		if (p ==  NULL || p - label > 10 || strlen(p + 1) > 14)
			return (MM_NOTOK);
	}
	/* Validate severity argument. */
	if ((sevstr = severity2str(severity)) == NULL)
		return (MM_NOTOK);

	/*
	 * Fact in search for a better place: XSH5 does not define any
	 * functionality for `classification' bits other than the display
	 * subclassification.
	 */

	result = 0;

	if (classification & MM_PRINT) {
		if (writeit(stderr, msgverb(getenv("MSGVERB")),
		    label, sevstr, text, action, tag) < 0)
			result |= MM_NOMSG;
	}
	/* Similar to MM_PRINT but ignoring $MSGVERB. */
	if (classification & MM_CONSOLE) {
		if ((console = fopen(_PATH_CONSOLE, "w")) != NULL) {
			if (writeit(console, MM_VERBALL,
			    label, sevstr, text, action, tag) < 0)
				result |= MM_NOCON;
			/*
			 * Ignore result: does not constitute ``generate a
			 * console message.''
			 */
			(void)fclose(console);
		} else {
			result |= MM_NOCON;
		}
	}

	if (result == (MM_NOMSG | MM_NOCON))
		result = MM_NOTOK;

	return (result == 0 ? MM_OK : result);
}
Ejemplo n.º 2
0
int send_char_to_palacios_as_scancodes(int fd, unsigned char c)
{
    unsigned char sc;

    if (debug_enable) {
	fprintf(stderr,"key '%c'\n",c);
    }

    if (c<0x80) { 
	struct key_code k = ascii_to_key_code[c];
	
	if (k.scan_code==0 && k.capital==0) { 
	    if (debug_enable) { 
		fprintf(stderr,"Cannot send key '%c' to palacios as it maps to no scancode\n",c);
	    }
	} else {
	    if (k.capital) { 
		//shift down
		sc = 0x2a ; // left shift down
		writeit(fd,sc);
	    }
	    
	    
	    sc = k.scan_code;
	    
	    writeit(fd,sc);  // key down
	    
	    sc |= 0x80;      // key up
	    
	    writeit(fd,sc);
	    
	    if (k.capital) { 
		sc = 0x2a | 0x80;
		writeit(fd,sc);
	    }
	}
	    
    } else {

	
	if (debug_enable) { 
	    fprintf(stderr,"Cannot send key '%c' to palacios because it is >=0x80\n",c);
	}

	
    }
    return 0;
}
Ejemplo n.º 3
0
/* client_write() sends data to the write callback(s)

   The bit pattern defines to what "streams" to write to. Body and/or header.
   The defines are in sendf.h of course.
 */
CURLcode Curl_client_write(struct SessionHandle *data,
                           int type,
                           char *ptr,
                           size_t len)
{
    size_t wrote;

    if(0 == len)
        len = strlen(ptr);

    if(type & CLIENTWRITE_BODY) {
        wrote = data->set.fwrite(ptr, 1, len, data->set.out);
        if(wrote != len) {
            failf (data, "Failed writing body");
            return CURLE_WRITE_ERROR;
        }
    }
    if((type & CLIENTWRITE_HEADER) &&
            (data->set.fwrite_header || data->set.writeheader) ) {
        /*
         * Write headers to the same callback or to the especially setup
         * header callback function (added after version 7.7.1).
         */
        curl_write_callback writeit=
            data->set.fwrite_header?data->set.fwrite_header:data->set.fwrite;

        wrote = writeit(ptr, 1, len, data->set.writeheader);
        if(wrote != len) {
            failf (data, "Failed writing header");
            return CURLE_WRITE_ERROR;
        }
    }

    return CURLE_OK;
}
Ejemplo n.º 4
0
Archivo: rtsp.c Proyecto: AndyUI/curl
static
CURLcode rtp_client_write(struct connectdata *conn, char *ptr, size_t len)
{
  struct SessionHandle *data = conn->data;
  size_t wrote;
  curl_write_callback writeit;

  if(len == 0) {
    failf (data, "Cannot write a 0 size RTP packet.");
    return CURLE_WRITE_ERROR;
  }

  writeit = data->set.fwrite_rtp?data->set.fwrite_rtp:data->set.fwrite_func;
  wrote = writeit(ptr, 1, len, data->set.rtp_out);

  if(CURL_WRITEFUNC_PAUSE == wrote) {
    failf (data, "Cannot pause RTP");
    return CURLE_WRITE_ERROR;
  }

  if(wrote != len) {
    failf (data, "Failed writing RTP data");
    return CURLE_WRITE_ERROR;
  }

  return CURLE_OK;
}
Ejemplo n.º 5
0
int main(void)
{
    someglobal = 0x68;
    writeit();

    return 0;
}
Ejemplo n.º 6
0
void readwrite(int fdin, int fdout)
{
	int n;
	int bufsize=1024;
	char buf[bufsize+1];	
	while((n=read(fdin,buf,bufsize))>0)
	{
		writeit(fdout,buf,n);
	}
	if(n==-1)
	{
		perror("read error");
	}
};
Ejemplo n.º 7
0
static
CURLcode rtp_client_write(struct connectdata *conn, char *ptr, size_t len)
{
  struct Curl_easy *data = conn->data;
  size_t wrote;
  curl_write_callback writeit;
  void *user_ptr;

  if(len == 0) {
    failf(data, "Cannot write a 0 size RTP packet.");
    return CURLE_WRITE_ERROR;
  }

  /* If the user has configured CURLOPT_INTERLEAVEFUNCTION then use that
     function and any configured CURLOPT_INTERLEAVEDATA to write out the RTP
     data. Otherwise, use the CURLOPT_WRITEFUNCTION with the CURLOPT_WRITEDATA
     pointer to write out the RTP data. */
  if(data->set.fwrite_rtp) {
    writeit = data->set.fwrite_rtp;
    user_ptr = data->set.rtp_out;
  }
  else {
    writeit = data->set.fwrite_func;
    user_ptr = data->set.out;
  }

  Curl_set_in_callback(data, true);
  wrote = writeit(ptr, 1, len, user_ptr);
  Curl_set_in_callback(data, false);

  if(CURL_WRITEFUNC_PAUSE == wrote) {
    failf(data, "Cannot pause RTP");
    return CURLE_WRITE_ERROR;
  }

  if(wrote != len) {
    failf(data, "Failed writing RTP data");
    return CURLE_WRITE_ERROR;
  }

  return CURLE_OK;
}
Ejemplo n.º 8
0
Archivo: sendf.c Proyecto: ahqmhjk/curl
/* Curl_client_write() sends data to the write callback(s)

   The bit pattern defines to what "streams" to write to. Body and/or header.
   The defines are in sendf.h of course.

   If CURL_DO_LINEEND_CONV is enabled, data is converted IN PLACE to the
   local character encoding.  This is a problem and should be changed in
   the future to leave the original data alone.
 */
CURLcode Curl_client_write(struct connectdata *conn,
                           int type,
                           char *ptr,
                           size_t len)
{
  struct SessionHandle *data = conn->data;
  size_t wrote;

  if(0 == len)
    len = strlen(ptr);

  /* If reading is actually paused, we're forced to append this chunk of data
     to the already held data, but only if it is the same type as otherwise it
     can't work and it'll return error instead. */
  if(data->req.keepon & KEEP_RECV_PAUSE) {
    size_t newlen;
    char *newptr;
    if(type != data->state.tempwritetype)
      /* major internal confusion */
      return CURLE_RECV_ERROR;

    DEBUGASSERT(data->state.tempwrite);

    /* figure out the new size of the data to save */
    newlen = len + data->state.tempwritesize;
    /* allocate the new memory area */
    newptr = realloc(data->state.tempwrite, newlen);
    if(!newptr)
      return CURLE_OUT_OF_MEMORY;
    /* copy the new data to the end of the new area */
    memcpy(newptr + data->state.tempwritesize, ptr, len);
    /* update the pointer and the size */
    data->state.tempwrite = newptr;
    data->state.tempwritesize = newlen;

    return CURLE_OK;
  }

  if(type & CLIENTWRITE_BODY) {
    if((conn->handler->protocol&CURLPROTO_FTP) &&
       conn->proto.ftpc.transfertype == 'A') {
      /* convert from the network encoding */
      size_t rc = Curl_convert_from_network(data, ptr, len);
      /* Curl_convert_from_network calls failf if unsuccessful */
      if(rc)
        return rc;

#ifdef CURL_DO_LINEEND_CONV
      /* convert end-of-line markers */
      len = convert_lineends(data, ptr, len);
#endif /* CURL_DO_LINEEND_CONV */
    }
    /* If the previous block of data ended with CR and this block of data is
       just a NL, then the length might be zero */
    if(len) {
      wrote = data->set.fwrite_func(ptr, 1, len, data->set.out);
    }
    else {
      wrote = len;
    }

    if(CURL_WRITEFUNC_PAUSE == wrote)
      return pausewrite(data, type, ptr, len);

    if(wrote != len) {
      failf(data, "Failed writing body (%zu != %zu)", wrote, len);
      return CURLE_WRITE_ERROR;
    }
  }

  if((type & CLIENTWRITE_HEADER) &&
     (data->set.fwrite_header || data->set.writeheader) ) {
    /*
     * Write headers to the same callback or to the especially setup
     * header callback function (added after version 7.7.1).
     */
    curl_write_callback writeit=
      data->set.fwrite_header?data->set.fwrite_header:data->set.fwrite_func;

    /* Note: The header is in the host encoding
       regardless of the ftp transfer mode (ASCII/Image) */

    wrote = writeit(ptr, 1, len, data->set.writeheader);
    if(CURL_WRITEFUNC_PAUSE == wrote)
      /* here we pass in the HEADER bit only since if this was body as well
         then it was passed already and clearly that didn't trigger the pause,
         so this is saved for later with the HEADER bit only */
      return pausewrite(data, CLIENTWRITE_HEADER, ptr, len);

    if(wrote != len) {
      failf (data, "Failed writing header");
      return CURLE_WRITE_ERROR;
    }
  }

  return CURLE_OK;
}
Ejemplo n.º 9
0
/* client_write() sends data to the write callback(s)

   The bit pattern defines to what "streams" to write to. Body and/or header.
   The defines are in sendf.h of course.
 */
CURLcode Curl_client_write(struct connectdata *conn,
                           int type,
                           char *ptr,
                           size_t len)
{
  struct SessionHandle *data = conn->data;
  size_t wrote;

  if (data->state.cancelled) {
      /* We just suck everything into a black hole */
      return CURLE_OK;
  }

  if(0 == len)
    len = strlen(ptr);

  if(type & CLIENTWRITE_BODY) {
    if((conn->protocol&PROT_FTP) && conn->proto.ftpc.transfertype == 'A') {
#ifdef CURL_DOES_CONVERSIONS
      /* convert from the network encoding */
      size_t rc;
      rc = Curl_convert_from_network(data, ptr, len);
      /* Curl_convert_from_network calls failf if unsuccessful */
      if(rc != CURLE_OK)
        return rc;
#endif /* CURL_DOES_CONVERSIONS */

#ifdef CURL_DO_LINEEND_CONV
      /* convert end-of-line markers */
      len = convert_lineends(data, ptr, len);
#endif /* CURL_DO_LINEEND_CONV */
    }
    /* If the previous block of data ended with CR and this block of data is
       just a NL, then the length might be zero */
    if (len) {
      wrote = data->set.fwrite(ptr, 1, len, data->set.out);
    }
    else {
      wrote = len;
    }

    if(wrote != len) {
      failf (data, "Failed writing body");
      return CURLE_WRITE_ERROR;
    }
  }

  if((type & CLIENTWRITE_HEADER) &&
     (data->set.fwrite_header || data->set.writeheader) ) {
    /*
     * Write headers to the same callback or to the especially setup
     * header callback function (added after version 7.7.1).
     */
    curl_write_callback writeit=
      data->set.fwrite_header?data->set.fwrite_header:data->set.fwrite;

    /* Note: The header is in the host encoding
       regardless of the ftp transfer mode (ASCII/Image) */

    wrote = writeit(ptr, 1, len, data->set.writeheader);
    if(wrote != len) {
      failf (data, "Failed writing header");
      return CURLE_WRITE_ERROR;
    }
  }

  return CURLE_OK;
}
Ejemplo n.º 10
0
/*
 * Receive a file.
 */
static void recvtftp(struct testcase *test, struct formats *pf)
{
  ssize_t n, size;
  recvblock = 0;
#if defined(HAVE_ALARM) && defined(SIGALRM)
  mysignal(SIGALRM, timer);
#endif
  rdp = w_init();
  rap = (struct tftphdr *)ackbuf;
  do {
    timeout = 0;
    rap->th_opcode = htons((u_short)opcode_ACK);
    rap->th_block = htons((u_short)recvblock);
    recvblock++;
#ifdef HAVE_SIGSETJMP
    (void) sigsetjmp(timeoutbuf, 1);
#endif
send_ack:
    if (swrite(peer, ackbuf, 4) != 4) {
      logmsg("write: fail\n");
      goto abort;
    }
    write_behind(test, pf->f_convert);
    for ( ; ; ) {
#ifdef HAVE_ALARM
      alarm(rexmtval);
#endif
      n = sread(peer, rdp, PKTSIZE);
#ifdef HAVE_ALARM
      alarm(0);
#endif
      if (n < 0) {                       /* really? */
        logmsg("read: fail\n");
        goto abort;
      }
      rdp->th_opcode = ntohs((u_short)rdp->th_opcode);
      rdp->th_block = ntohs((u_short)rdp->th_block);
      if (rdp->th_opcode == opcode_ERROR)
        goto abort;
      if (rdp->th_opcode == opcode_DATA) {
        if (rdp->th_block == recvblock) {
          break;                         /* normal */
        }
        /* Re-synchronize with the other side */
        (void) synchnet(peer);
        if (rdp->th_block == (recvblock-1))
          goto send_ack;                 /* rexmit */
      }
    }

    size = writeit(test, &rdp, (int)(n - 4), pf->f_convert);
    if (size != (n-4)) {                 /* ahem */
      if (size < 0)
        nak(ERRNO + 100);
      else
        nak(ENOSPACE);
      goto abort;
    }
  } while (size == SEGSIZE);
  write_behind(test, pf->f_convert);

  rap->th_opcode = htons((u_short)opcode_ACK);  /* send the "final" ack */
  rap->th_block = htons((u_short)recvblock);
  (void) swrite(peer, ackbuf, 4);
#if defined(HAVE_ALARM) && defined(SIGALRM)
  mysignal(SIGALRM, justtimeout);        /* just abort read on timeout */
  alarm(rexmtval);
#endif
  n = sread(peer, buf, sizeof(buf));     /* normally times out and quits */
#ifdef HAVE_ALARM
  alarm(0);
#endif
  if (n >= 4 &&                          /* if read some data */
      rdp->th_opcode == opcode_DATA &&   /* and got a data block */
      recvblock == rdp->th_block) {      /* then my last ack was lost */
    (void) swrite(peer, ackbuf, 4);      /* resend final ack */
  }
abort:
  return;
}
Ejemplo n.º 11
0
int main(int argc, char* argv[]) {
    char * enclave_path = argv[1];
    int vm_id = atoi(argv[2]);
    int ctrl_fd;
    int cons_fd;

    use_curses = 1;

    if (argc < 3) {
	printf("usage: v3_cons_sc <enclave_device> <vm_id>\n");
	return -1;
    }

    /* Check for minimum Terminal size at start */
    if (check_terminal_size() != 0) {
        printf ("Error: terminal too small!\n");
        return -1;
    }



    ctrl_fd = pet_ioctl_path(enclave_path, PISCES_ENCLAVE_CTRL_CONNECT, NULL);



    if (ctrl_fd < 0) {
	printf("Error opening enclave control channel\n");
	return -1;
    }

    cons_fd = pet_ioctl_fd(ctrl_fd, PISCES_CMD_VM_CONS_CONNECT, (void *)(uint64_t)vm_id); 

    close(ctrl_fd);

    if (cons_fd < 0) {
	printf("Error opening VM console\n");
	close(cons_fd);
	return -1;
    }

    tcgetattr(STDIN_FILENO, &console.termios_old);
    atexit(handle_exit);

    console.x = 0;
    console.y = 0;


    if (use_curses) {
	gettmode();
	console.win = initscr();
	
	if (console.win == NULL) {
	    fprintf(stderr, "Error initialization curses screen\n");
	    exit(-1);
	}

	scrollok(console.win, 1);

	erase();
    }

    /*
    termios = console.termios_old;
    termios.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | IGNPAR);
    termios.c_iflag &= ~(INLCR | INPCK | ISTRIP | IXOFF | IXON | PARMRK); 
    //termios.c_iflag &= ~(ICRNL | INLCR );    

    //  termios.c_iflag |= SCANCODES; 
    //    termios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); 
    //termios.c_lflag &= ~(ICANON | IEXTEN | ISIG | NOFLSH);
    termios.c_lflag &= ~(ICANON | ECHO);

    termios.c_cc[VMIN] = 1;
    termios.c_cc[VTIME] = 0;

    tcflush(STDIN_FILENO, TCIFLUSH);
    tcsetattr(STDIN_FILENO, TCSANOW, &termios);
    */    

    raw();
    cbreak();
    noecho();
    keypad(console.win, TRUE);

    //ioctl(STDIN_FILENO, KDSKBMODE, K_RAW);

    while (1) {
	int ret; 
	fd_set rset;

	FD_ZERO(&rset);
	FD_SET(cons_fd, &rset);
	FD_SET(STDIN_FILENO, &rset);

	ret = select(cons_fd + 1, &rset, NULL, NULL, NULL);
	
	//	printf("Returned from select...\n");

	if (ret == 0) {
	    continue;
	} else if (ret == -1) {
	    perror("Select returned error...\n");
	    return -1;
	}

	if (FD_ISSET(cons_fd, &rset)) {
	    if (handle_console_msg(cons_fd) == -1) {
		printf("Console Error\n");
		return -1;
	    }
	}

	if (FD_ISSET(STDIN_FILENO, &rset)) {
	    int key = getch();



	    if (key == 27) { // ESC
		int key2 = getch();

		if (key2 == '`') {
		    break;
		} else if (key2 == 27) {
		    unsigned char sc = 0;
		    sc = 0x01;
		    writeit(cons_fd, sc);
		    sc |= 0x80;
		    writeit(cons_fd, sc);
		} else {
		    unsigned char sc = 0;

		    sc = 0x1d;  // left ctrl down
		    writeit(cons_fd,sc);

		    if (send_char_to_palacios_as_scancodes(cons_fd, key2)) {
			printf("Error sending key to console\n");
			return -1;
		    }

		    sc = 0x1d | 0x80;   // left ctrl up
		    writeit(cons_fd,sc); 
		}
		


	  } else if (key == KEY_LEFT) { // LEFT ARROW
		unsigned char sc = 0;
		//	sc = 0xe0;
		//writeit(cons_fd, sc);
		sc = 0x4B;
		writeit(cons_fd, sc);
		sc = 0x4B | 0x80;;
		writeit(cons_fd, sc);
		//sc = 0xe0 | 0x80;
		//writeit(cons_fd, sc);
	    } else if (key == KEY_RIGHT) { // RIGHT ARROW
		unsigned char sc = 0;
		//sc = 0xe0;
		//writeit(cons_fd, sc);
		sc = 0x4D;
		writeit(cons_fd, sc);
		sc = 0x4d | 0x80;
		writeit(cons_fd, sc);
		//sc = 0xe0 | 0x80;
		//writeit(cons_fd, sc);
	    } else if (key == KEY_UP) { // UP ARROW
		unsigned char sc = 0;
		//sc = 0xe0;
		//writeit(cons_fd, sc);
		sc = 0x48;
		writeit(cons_fd, sc);
		sc = 0x48 | 0x80;
		writeit(cons_fd, sc);
		//sc = 0xe0 | 0x80;
		//writeit(cons_fd, sc);
	    } else if (key == KEY_DOWN) { // DOWN ARROW
		unsigned char sc = 0;
		//	sc = 0xe0;
		//writeit(cons_fd, sc);
		sc = 0x50;
		writeit(cons_fd, sc);
		sc = 0x50 | 0x80;
		writeit(cons_fd, sc);
		//sc = 0xe0 | 0x80;
		//writeit(cons_fd, sc);


            } else {
		if (send_char_to_palacios_as_scancodes(cons_fd,key)) {
		    printf("Error sending key to console\n");
		    return -1;
		}
	    }
	    
	}
    } 

    erase();

    printf("Console terminated\n");

    close(cons_fd);

    return 0; 
}
Ejemplo n.º 12
0
/*
 * Receive a file.
 */
static void recvtftp(struct testcase *test, struct formats *pf)
{
  struct tftphdr *dp;
  struct tftphdr *ap;    /* ack buffer */
  unsigned short block = 0;
  int n, size;
#if defined(HAVE_ALARM) && defined(SIGALRM)
  mysignal(SIGALRM, timer);
#endif
  dp = w_init();
  ap = (struct tftphdr *)ackbuf;
  do {
    timeout = 0;
    ap->th_opcode = htons((u_short)ACK);
    ap->th_block = htons((u_short)block);
    block++;
#ifdef HAVE_SIGSETJMP
    (void) sigsetjmp(timeoutbuf, 1);
#endif
send_ack:
    if (send(peer, ackbuf, 4, 0) != 4) {
      logmsg("write: fail\n");
      goto abort;
    }
    write_behind(test, pf->f_convert);
    for ( ; ; ) {
#ifdef HAVE_ALARM
      alarm(rexmtval);
#endif
      n = recv(peer, dp, PKTSIZE, 0);
#ifdef HAVE_ALARM
      alarm(0);
#endif
      if (n < 0) {                       /* really? */
        logmsg("read: fail\n");
        goto abort;
      }
      dp->th_opcode = ntohs((u_short)dp->th_opcode);
      dp->th_block = ntohs((u_short)dp->th_block);
      if (dp->th_opcode == ERROR)
        goto abort;
      if (dp->th_opcode == DATA) {
        if (dp->th_block == block) {
          break;                         /* normal */
        }
        /* Re-synchronize with the other side */
        (void) synchnet(peer);
        if (dp->th_block == (block-1))
          goto send_ack;                 /* rexmit */
      }
    }

    size = writeit(test, &dp, n - 4, pf->f_convert);
    if (size != (n-4)) {                 /* ahem */
      if (size < 0)
        nak(errno + 100);
      else
        nak(ENOSPACE);
      goto abort;
    }
  } while (size == SEGSIZE);
  write_behind(test, pf->f_convert);

  ap->th_opcode = htons((u_short)ACK);   /* send the "final" ack */
  ap->th_block = htons((u_short)(block));
  (void) send(peer, ackbuf, 4, 0);
#if defined(HAVE_ALARM) && defined(SIGALRM)
  mysignal(SIGALRM, justquit);           /* just quit on timeout */
  alarm(rexmtval);
#endif
  n = recv(peer, buf, sizeof (buf), 0);  /* normally times out and quits */
#ifdef HAVE_ALARM
  alarm(0);
#endif
  if (n >= 4 &&                          /* if read some data */
      dp->th_opcode == DATA &&           /* and got a data block */
      block == dp->th_block) {           /* then my last ack was lost */
    (void) send(peer, ackbuf, 4, 0);     /* resend final ack */
  }
abort:
  return;
}
Ejemplo n.º 13
0
int main(int argc, char *argv[])
{

  extern char *description;
  extern char *proxyaddr;
  extern char *google_server;
  extern char pretty_query[1024];
  extern char mid_query[1024];
  extern char out_query[1024];
  extern char *site;
  extern char *xtra_stuff;
  extern char total_string[256];
  extern int dflag,oflag,sflag,vflag,xflag;
  extern int total;
  extern int cooked;
  extern int proxyport;
  extern long results;
  
  char *in_query = NULL;
  char list[1024];
  char *proxy = NULL;
  char *in_filename = NULL;
  char *out_filename = NULL;
  char *type,*value,*build_buf;
  
  int c;
  int fflag = 0, qflag = 0;
  int indexofflag = 0;
  int in_count=0;
  
  FILE *scanlist;
  FILE *outfile;

  time_t timer;
  
  build_buf=calloc(256, sizeof(char));
  
  if (argc == 1) usage();
  
  while ((c = getopt(argc,argv,"acdhvi:o:p:q:s:t:x:")) != -1) {
     switch (c) {
     case 'd': //decode flag
	dflag=1;
	break;
     case 'p': // proxy server
  	  if ( (proxyaddr=calloc(256, sizeof(char))) == NULL)
  	  {
		  printf("Error allocating memory for proxyaddr. Aborting.\n");
		  exit(1);
  	  }
	  if (strstr(optarg,":") == NULL) {
		printf("Error: Proxy address must be like address:port.\n");
		usage();
	  }
	  pflag=1;
	  proxy=optarg;
          snprintf(proxyaddr,256,"%s",strtok(proxy,":"));
          proxyport=atoi(strtok(NULL,"\0"));
          //printf("%s|%d",proxyaddr,proxyport);
	  break;
     case 'q': // inline query
	  qflag=1;
	  in_query=optarg;
	  break;
     case 'i': // input query file
	  fflag=1;
	  in_filename=optarg;
	  break;
     case 'o': // output html file
	  oflag=1;
	  out_filename=optarg;
	  break;
     case 's': // site variable
	  sflag=1;
	  site=optarg;
	  break;
     case 't': // target_server
  	  if ( (google_server=calloc(256, sizeof(char))) == NULL)
  	  {
		  printf("Error allocating memory for google_server. Aborting.\n");
		  exit(1);
  	  }
	  google_server=optarg;
	  break;
     case 'v': // verbose
	  vflag=1;
	  break;
     case 'x': // extra stuff for appliances
	  xflag=1;
	  xtra_stuff=optarg;
	  break;
     case 'h':  //help
	  usage();
     default: 
	  usage();
     }
  }

  //clean up the server, if necessary
  if (strstr(google_server,"http://") != NULL)
	strncpy(google_server,google_server+7,256);

  // A bunch of errors for mixed-up user parameters
  if (strlen(google_server) == 0) {
	printf("You must provide the google server with the -t argument!\n");
	usage();
  }

  if (strstr(google_server,"www.google.com") != NULL) {
    printf("\n***!!! WARNING: You are querying a www.google.com server !!!***\n");
    printf("This tool was designed to query Google appliances, not the google.com website.\n");
    printf("The google.com scanning functionality is included for EDUCATIONAL PURPOSES ONLY \n");
    printf("to help webmasters determine the potential Google exposure of their sites.\n");
    printf("\nDo you acknowledge that: \n");
    printf("  - You are knowingly violating Google's terms of service found at\n");
    printf("    http://www.google.com/terms_of_service.html\n");
    printf("  - You are using this tool to assess your own web site's exposure\n");
    printf("  - The use of this tool in this way is not condoned by the author\n");
    printf("  - You will not hold the author liable in any way for the use of this tool\n\n");
    printf("Agree? (y/n) [n] ");
    if ( getchar() != 'y' ) exit(1);
  }

  if (!qflag && !fflag) {
     printf("Error: supply either -q or -f flag.\n");
     usage();
  } 
  if (qflag && fflag) {
     printf("Error: -q and -f should not be used together.\n");
     printf("-q allows for a single Google query from the command line,\n");
     printf("-f allows for multiple Google queries read from a file.\n");
     usage();
  }

  // OK, the user wants output. Open the file, write the header.
  if (oflag) {
    if (( outfile = fopen(out_filename,"a")) == NULL) {
       perror(out_filename);
       exit(1);
    }
    writeit(outfile,"","<br><CENTER><b>Gooscan Results</b></CENTER><br>",0,"raw");
    snprintf(build_buf,256,"<center>site: %s<br>input file: %s<br>",\
		sflag?site:"none" , fflag?in_filename:"none");
    writeit(outfile," ",build_buf,0,"raw");
    timer=time(NULL);
    snprintf(build_buf,256,"Executed: %s</CENTER><br>",asctime(localtime(&timer)));
    writeit(outfile," ",build_buf,0,"raw");
    writeit(outfile,NULL,NULL,0,"header");
  }
  
  // OK, user wants one query. Run it, write it, close the outfile, exit.
  if (qflag) {
    results=google_query("raw","raw",in_query);
    printf("\"%s\" returned %ld results.\n",in_query,results);
    if (oflag) writeit(outfile,out_query,pretty_query,results,"line");
  } 

  // Here's the fun stuff. Queries from an infile.
  else if (fflag) {

     if (sflag) printf("Searching google for site %s...\n",site);

      //fopen the infile
      if (fflag) 
	  if (( scanlist = fopen(in_filename,"r")) == NULL) {
		  perror(in_filename);
		  exit(1);
	  }
 

	// If we're searching a site, do a generic index.of. 
	// If that fails, never try index.of search again.
   	if (sflag) {
          results=(google_query("raw","raw","intitle:index.of"));
	  if ( results == 0) {
            indexofflag = 0;
	    printf("Generic index.of search returned no results.\n");
	    printf("Skipping index.of tests.\n");
	  }
	  else {
	    indexofflag = 1; 
	    printf("Generic index.of search returned %ld results\n",results);
	  }
	}
	else indexofflag = 1;

	//The main file reading loop
	while ( fgets(list,1024,scanlist) != NULL) {
		total =0;
		cooked=0;
		memset(total_string,'\0',256);
		memset(mid_query,'\0',256);
		memset(out_query,'\0',256);
   	
		if ((type=calloc(48, sizeof(char))) == NULL)
		{
		  printf("Error allocating memory for type. Aborting.\n");
		  exit(1);
		}
		if ((value=calloc(200, sizeof(char))) == NULL)
		{
		  printf("Error allocating memory for value. Aborting.\n");
		  exit(1);
		}
		if ((description=calloc(2048, sizeof(char))) == NULL)
		{
		  printf("Error allocating memory for description. Aborting.\n");
		  exit(1);
		}

		snprintf(type,48,"%s",strtok(list,"|"));
		snprintf(value,200,"%s",strtok(NULL,"|"));
		in_count=atoi(strtok(NULL,"|"));
		//printf("type:%s,value:%s:count:%d,description:%s\n",type,value,in_count,description);
		if ( type != NULL && value != NULL) {
		   if ( strcmp(type,"cooked") == 0) cooked=1;
		   // for "file" lines, we do inurl and indexof searches
		   if ( strcmp(type,"file") == 0 )  {
		      if (indexofflag == 1) {
			results=google_query("indexof",type,value);	
  			if (oflag) writeit(outfile,out_query,pretty_query,results,"line");
		      }
		      //inurl: test
		      results=google_query("inurl",type,value);
  	     	      if (oflag) writeit(outfile,out_query,pretty_query,results,"line");

		      if (results >0 || vflag) printf("%sTotal:%d\n",total_string,total);
		   }
		   else {
			results=google_query(type,"Results",value);
  	     	        if (results>=0) {
			   if (oflag) writeit(outfile,out_query,pretty_query,results,"line");
		        if (results >0 || vflag) printf("%s\n",total_string);
			}
			else if (vflag) printf("Unknown type in data file (%s). Skipping...\n",type);
		   }
		   free(description);
		   free(type);
		   free(value);
 		   //printf("mid_q:%s\n",mid_query);
		}
	}
	fclose(scanlist);
  }
	
  if (oflag) {
    writeit(outfile,NULL,NULL,0,"footer"); 
    fclose(outfile);
  }

//printf("out_query: %s\n",out_query);
  return 0;
}
Ejemplo n.º 14
0
/*
 * Receive a file.
 */
int tftp_recvfile(int f, union sock_addr *peeraddr,
		   int fd, const char *name, const char *mode)
{
    struct tftphdr *ap;
    struct tftphdr *dp;
    int n;
    volatile u_short block;
    volatile int size, firsttrip;
    volatile unsigned long amount;
    union sock_addr from;
    socklen_t fromlen;
    FILE *file;
    volatile int convert;       /* true if converting crlf -> lf */
    u_short dp_opcode, dp_block;

    startclock();
    dp = w_init();
    ap = (struct tftphdr *)ackbuf;
    convert = !strcmp(mode, "netascii");
    file = fdopen(fd, convert ? "wt" : "wb");
    block = 1;
    firsttrip = 1;
    amount = 0;

    bsd_signal(SIGALRM, timer);
    do {
        if (firsttrip) {
            size = makerequest(RRQ, name, ap, mode);
            firsttrip = 0;
        } else {
            ap->th_opcode = htons((u_short) ACK);
            ap->th_block = htons((u_short) block);
            size = 4;
            block++;
        }
        timeout = 0;
        (void)sigsetjmp(timeoutbuf, 1);
      send_ack:
        if (trace)
            tpacket("sent", ap, size);
        if (sendto(f, ackbuf, size, 0, &(peeraddr->sa),
                   SOCKLEN(peeraddr)) != size) {
            alarm(0);
            perror("tftp: sendto");
            goto abort;
        }
        write_behind(file, convert);
        for (;;) {
            alarm(rexmtval);
            do {
                fromlen = sizeof(from);
                n = recvfrom(f, dp, PKTSIZE, 0,
                             &from.sa, &fromlen);
            } while (n <= 0);
            alarm(0);
            if (n < 0) {
                perror("tftp: recvfrom");
                goto abort;
            }
            sa_set_port(peeraddr, SOCKPORT(&from));  /* added */
            if (trace)
                tpacket("received", dp, n);
            /* should verify client address */
            dp_opcode = ntohs((u_short) dp->th_opcode);
            dp_block = ntohs((u_short) dp->th_block);
            if (dp_opcode == ERROR) {
                printf("Error code %d: %s\n", dp_block, dp->th_msg);
                goto abort;
            }
            if (dp_opcode == DATA) {
                int j;

                if (dp_block == block) {
                    break;      /* have next packet */
                }
                /* On an error, try to synchronize
                 * both sides.
                 */
                j = synchnet(f);
                if (j && trace) {
                    printf("discarded %d packets\n", j);
                }
                if (dp_block == (block - 1)) {
                    goto send_ack;      /* resend ack */
                }
            }
        }
        /*      size = write(fd, dp->th_data, n - 4); */
        size = writeit(file, &dp, n - 4, convert);
        if (size < 0) {
            nak(f, peeraddr, errno + 100, NULL);
            break;
        }
        amount += size;
    } while (size == SEGSIZE);
  abort:                       /* ok to ack, since user */
    ap->th_opcode = htons((u_short) ACK);       /* has seen err msg */
    ap->th_block = htons((u_short) block);
    (void)sendto(f, ackbuf, 4, 0, (struct sockaddr *)peeraddr,
                 SOCKLEN(peeraddr));
    write_behind(file, convert);        /* flush last buffer */
    fclose(file);
    stopclock();
    //if (amount > 0)
    //    printstats("Received", amount);
    return amount;
}
Ejemplo n.º 15
0
/*
 * Receive a file.
 */
void
recvfile(struct formats *pf)
{
	struct tftphdr *dp;
	struct tftphdr *ap;    /* ack buffer */
	int n, size;
	volatile unsigned short block;

	signal(SIGALRM, timer);
	dp = w_init();
	ap = (struct tftphdr *)ackbuf;
	block = 0;
	do {
		timeouts = 0;
		ap->th_opcode = htons((u_short)ACK);
		ap->th_block = htons((u_short)block);
		block++;
		(void) setjmp(timeoutbuf);
send_ack:
		if (send(peer, ackbuf, 4, 0) != 4) {
			syslog(LOG_ERR, "write: %m");
			goto abort;
		}
		write_behind(file, pf->f_convert);
		for ( ; ; ) {
			alarm(rexmtval);
			n = recv(peer, dp, PKTSIZE, 0);
			alarm(0);
			if (n < 0) {            /* really? */
				syslog(LOG_ERR, "read: %m");
				goto abort;
			}
			dp->th_opcode = ntohs((u_short)dp->th_opcode);
			dp->th_block = ntohs((u_short)dp->th_block);
			if (dp->th_opcode == ERROR)
				goto abort;
			if (dp->th_opcode == DATA) {
				if (dp->th_block == block) {
					break;   /* normal */
				}
				/* Re-synchronize with the other side */
				(void) synchnet(peer);
				if (dp->th_block == (block-1))
					goto send_ack;          /* rexmit */
			}
		}
		/*  size = write(file, dp->th_data, n - 4); */
		size = writeit(file, &dp, n - 4, pf->f_convert);
		if (size != (n-4)) {                    /* ahem */
			if (size < 0) nak(errno + 100);
			else nak(ENOSPACE);
			goto abort;
		}
	} while (size == SEGSIZE);
	write_behind(file, pf->f_convert);
	(void) fclose(file);            /* close data file */

	ap->th_opcode = htons((u_short)ACK);    /* send the "final" ack */
	ap->th_block = htons((u_short)(block));
	(void) send(peer, ackbuf, 4, 0);

	signal(SIGALRM, justquit);      /* just quit on timeout */
	alarm(rexmtval);
	n = recv(peer, buf, sizeof (buf), 0); /* normally times out and quits */
	alarm(0);
	if (n >= 4 &&                   /* if read some data */
	    dp->th_opcode == DATA &&    /* and got a data block */
	    block == dp->th_block) {	/* then my last ack was lost */
		(void) send(peer, ackbuf, 4, 0);     /* resend final ack */
	}
abort:
	return;
}