Beispiel #1
0
int main () {
    GC_init();
    int sock = CHECK(socket, AF_INET, SOCK_STREAM, 0);
    sockaddr_in listen_addr = any_addr();
    printf("bind...\n");
    CHECK(bind, sock, (sockaddr*)&listen_addr, sizeof(listen_addr));
    printf("listen...\n");
    CHECK(listen, sock, SOMAXCONN);
    sockaddr_in client_addr;
    socklen_t client_addr_len;
    int new_sock;
    printf("accept...\n");
    do {
        new_sock = CHECK(accept, sock, (sockaddr*)&client_addr, &client_addr_len);
        char* message = recv_string(new_sock);
        puts(message);
        const char* response = process_message((const char*)message);
        puts("Response generated.");
        CHECK(send, new_sock, response, strlen(response) + 1, 0);
        puts("Response sent.\n");
//        char endbuf [MAX_BUFFER];
//        CHECK(recv, new_sock, endbuf, MAX_BUFFER, 0);
//        puts("Close request recieved.");
        CHECK(close, new_sock);
    } while (0);
    CHECK(close, sock);
    return 0;
}
Beispiel #2
0
int wait_fill_buffer(irc_connection *con)
{
    unsigned free = ringbuf_free(con);
    unsigned right = IRC_BUFFER_SIZE - con->wpos;
    free = MIN(free, right);

    int recvd = recv_string(con, con->buf + con->wpos, free);
    con->wpos = (con->wpos + recvd) % IRC_BUFFER_SIZE;

    con->buf[con->wpos] = '\0';

    return recvd;
}
Beispiel #3
0
int recv_message(int socket, char** buffer) {
  char size[6]; // we allow 4 hex bytes for size; this allows messages to be at
                // most 65536 bytes in length
  int res, length, sum;

  res = recv_string(socket,size,5);
  if (res!=6) {
    printf("only recv'd %d hex digits\n", res);
    return -1;
  }

  res = htoi(size, &sum); // parse the hex digits
  if (!(res==6)) {
    printf("only parsed %d hex digits in \"%s\"\n", res, size);
    return -1;
  }

  // now, noting that a hex digit is 4 bits, we can use bit ops instead of
  // string ones:
  length = sum >> 8; // drop the two characters off the right hand side
  sum = sum - (length << 8); // leave only the last two in sum

  *buffer = (char*) malloc((length+2)*sizeof(char));

  res = recv_string(socket, *buffer, length);
  if (!(length == res)) {
    printf("message had length %d, but said it was %d\n", res, length);
    return -1;
  }
  if (!((res = csum(*buffer)) == sum)) {
    printf("\"%s\" had checksum %x, but said it was %x\n", *buffer, res, sum);
    return -1;
  }

  return length;
}
Beispiel #4
0
static int get_block(int conn, int fd, int max_size, int encrypt)
{
  char *data;
  long len;
  int n_read, n, i, code;

  recv_string(conn, &data, &len);

  if (encrypt)
    {
#ifdef HAVE_KRB4
      char *unenc = malloc(len);

      if (!unenc)
	{
	  send_int(conn, ENOMEM);
	  return -1;
	}

      des_pcbc_encrypt((des_cblock *)data, (des_cblock *)unenc, len, sched, &ivec, 1);
      for (i = 0; i < 8; i++)
	ivec[i] = data[len - 8 + i] ^ unenc[len - 8 + i];
      free(data);
      data = unenc;
#endif
    }

  n_read = MIN(len, max_size);
  n = 0;
  while (n < n_read)
    {
      int n_wrote;
      n_wrote = write(fd, data + n, n_read - n);
      if (n_wrote == -1)
	{
	  code = errno;
	  com_err(whoami, errno, "writing file (get_file)");
	  send_int(conn, code);
	  free(data);
	  close(fd);
	  return -1;
	}
      n += n_wrote;
    }
  free(data);
  return n;
}
Beispiel #5
0
/*
 * Background process -- runs with privilege.
 */
static void
pam_server (int fd, const char *service, int verb, const struct name_value_list *name_value_list)
{
  struct user_pass up;
  int command;
#ifdef USE_PAM_DLOPEN
  static const char pam_so[] = "libpam.so";
#endif

  /*
   * Do initialization
   */
  if (DEBUG (verb))
    fprintf (stderr, "AUTH-PAM: BACKGROUND: INIT service='%s'\n", service);

#ifdef USE_PAM_DLOPEN
  /*
   * Load PAM shared object
   */
  if (!dlopen_pam (pam_so))
    {
      fprintf (stderr, "AUTH-PAM: BACKGROUND: could not load PAM lib %s: %s\n", pam_so, dlerror());
      send_control (fd, RESPONSE_INIT_FAILED);
      goto done;
    }
#endif

  /*
   * Tell foreground that we initialized successfully
   */
  if (send_control (fd, RESPONSE_INIT_SUCCEEDED) == -1)
    {
      fprintf (stderr, "AUTH-PAM: BACKGROUND: write error on response socket [1]\n");
      goto done;
    }

  /*
   * Event loop
   */
  while (1)
    {
      memset (&up, 0, sizeof (up));
      up.verb = verb;
      up.name_value_list = name_value_list;

      /* get a command from foreground process */
      command = recv_control (fd);

      if (DEBUG (verb))
	fprintf (stderr, "AUTH-PAM: BACKGROUND: received command code: %d\n", command);

      switch (command)
	{
	case COMMAND_VERIFY:
	  if (recv_string (fd, up.username, sizeof (up.username)) == -1
	      || recv_string (fd, up.password, sizeof (up.password)) == -1
	      || recv_string (fd, up.common_name, sizeof (up.common_name)) == -1)
	    {
	      fprintf (stderr, "AUTH-PAM: BACKGROUND: read error on command channel: code=%d, exiting\n",
		       command);
	      goto done;
	    }

	  if (DEBUG (verb))
	    {
#if 0
	      fprintf (stderr, "AUTH-PAM: BACKGROUND: USER/PASS: %s/%s\n",
		       up.username, up.password);
#else
	      fprintf (stderr, "AUTH-PAM: BACKGROUND: USER: %s\n", up.username);
#endif
	    }

	  if (pam_auth (service, &up)) /* Succeeded */
	    {
	      if (send_control (fd, RESPONSE_VERIFY_SUCCEEDED) == -1)
		{
		  fprintf (stderr, "AUTH-PAM: BACKGROUND: write error on response socket [2]\n");
		  goto done;
		}
	    }
	  else /* Failed */
	    {
	      if (send_control (fd, RESPONSE_VERIFY_FAILED) == -1)
		{
		  fprintf (stderr, "AUTH-PAM: BACKGROUND: write error on response socket [3]\n");
		  goto done;
		}
	    }
	  break;

	case COMMAND_EXIT:
	  goto done;

	case -1:
	  fprintf (stderr, "AUTH-PAM: BACKGROUND: read error on command channel\n");
	  goto done;

	default:
	  fprintf (stderr, "AUTH-PAM: BACKGROUND: unknown command code: code=%d, exiting\n",
		   command);
	  goto done;
	}
    }
 done:

#ifdef USE_PAM_DLOPEN
  dlclose_pam ();
#endif
  if (DEBUG (verb))
    fprintf (stderr, "AUTH-PAM: BACKGROUND: EXIT\n");

  return;
}
Beispiel #6
0
/* do the sasl negotiation; return -1 if it fails */
int mysasl_negotiate(FILE *in, FILE *out, sasl_conn_t *conn)
{
    char buf[8192];
    char chosenmech[128];
    const char *data;
    int len;
    int r = SASL_FAIL;
    const char *userid;
#ifdef HAVE_GSS_GET_NAME_ATTRIBUTE
    gss_name_t peer = GSS_C_NO_NAME;
#endif
    
    /* generate the capability list */
    if (mech) {
	dprintf(2, "forcing use of mechanism %s\n", mech);
	data = strdup(mech);
	len = strlen(data);
    } else {
	int count;

	dprintf(1, "generating client mechanism list... ");
	r = sasl_listmech(conn, NULL, NULL, " ", NULL,
			  &data, (unsigned int *)&len, &count);
	if (r != SASL_OK) saslfail(r, "generating mechanism list");
	dprintf(1, "%d mechanisms\n", count);
    }

    /* send capability list to client */
    send_string(out, data, len);

    dprintf(1, "waiting for client mechanism...\n");
    len = recv_string(in, chosenmech, sizeof chosenmech);
    if (len <= 0) {
	printf("client didn't choose mechanism\n");
	fputc('N', out); /* send NO to client */
	fflush(out);
	return -1;
    }

    if (mech && strcasecmp(mech, chosenmech)) {
	printf("client didn't choose mandatory mechanism\n");
	fputc('N', out); /* send NO to client */
	fflush(out);
	return -1;
    }

    len = recv_string(in, buf, sizeof(buf));
    if(len != 1) {
	saslerr(r, "didn't receive first-send parameter correctly");
	fprintf(stderr, "%s\n", sasl_errdetail(conn));
	fputc('N', out);
	fflush(out);
	return -1;
    }

    if(buf[0] == 'Y') {
        /* receive initial response (if any) */
        len = recv_string(in, buf, sizeof(buf));

        /* start libsasl negotiation */
        r = sasl_server_start(conn, chosenmech, buf, len,
			      &data, (unsigned int *)&len);
    } else {
	r = sasl_server_start(conn, chosenmech, NULL, 0,
			      &data, (unsigned int *)&len);
    }
    
    if (r != SASL_OK && r != SASL_CONTINUE) {
	saslerr(r, "starting SASL negotiation");
	fprintf(stderr, "%s\n", sasl_errdetail(conn));
	fputc('N', out); /* send NO to client */
	fflush(out);
	return -1;
    }

    while (r == SASL_CONTINUE) {
	if (data) {
	    dprintf(2, "sending response length %d...\n", len);
	    fputc('C', out); /* send CONTINUE to client */
	    send_string(out, data, len);
	} else {
	    dprintf(2, "sending null response...\n");
	    fputc('C', out); /* send CONTINUE to client */
	    send_string(out, "", 0);
	}

	dprintf(1, "waiting for client reply...\n");
	len = recv_string(in, buf, sizeof buf);
	if (len < 0) {
	    printf("client disconnected\n");
	    return -1;
	}

	r = sasl_server_step(conn, buf, len, &data, (unsigned int *)&len);
	if (r != SASL_OK && r != SASL_CONTINUE) {
	    saslerr(r, "performing SASL negotiation");
	    fprintf(stderr, "%s\n", sasl_errdetail(conn));
	    fputc('N', out); /* send NO to client */
	    fflush(out);
	    return -1;
	}
    }

    if (r != SASL_OK) {
	saslerr(r, "incorrect authentication");
	fprintf(stderr, "%s\n", sasl_errdetail(conn));
	fputc('N', out); /* send NO to client */
	fflush(out);
	return -1;
    }

    fputc('O', out); /* send OK to client */
    fflush(out);
    dprintf(1, "negotiation complete\n");

    r = sasl_getprop(conn, SASL_USERNAME, (const void **) &userid);
    printf("successful authentication '%s'\n", userid);

#ifdef HAVE_GSS_GET_NAME_ATTRIBUTE
    r = sasl_getprop(conn, SASL_GSS_PEER_NAME, (const void **) &peer);
    if (peer != GSS_C_NO_NAME) {
        OM_uint32 minor;
        enumerateAttributes(&minor, peer, 1);
    }
#endif

    return 0;
}
Beispiel #7
0
int main(int argc, char **argv)
{
  char *str, *p;
  size_t len;
  struct _dt *d;
  struct utsname name;
  int s, conn;
  struct sigaction sa;
  FILE *pid_file;
  char *pid_path = NULL;

  whoami = strrchr(argv[0], '/');
  if (whoami)
    whoami++;
  else
    whoami = argv[0];

  /* interpret arguments here */
  if (argc != 1)
    {
      fprintf(stderr, "Usage:  %s\n", whoami);
      exit(1);
    }

  if (!config_lookup("nofork"))
    {
      if (fork())
	exit(0);
      setsid();
    }

  uname(&name);
  hostname = name.nodename;

  umask(0022);
  mr_init();

  sigemptyset(&sa.sa_mask);
  sa.sa_flags = SA_RESTART;
  sa.sa_handler = child_handler;
  sigaction(SIGCHLD, &sa, NULL);

  /* If the config file contains a line "user username", the
   * daemon will run with that user's UID.
   */
  if ((p = config_lookup("user")))
    {
      struct passwd *pw;
      pw = getpwnam(p);
      if (!pw)
	{
	  com_err(whoami, errno, "Unable to find user %s\n", p);
	  exit(1);
	}
      uid = pw->pw_uid;
    }

  /* If the config file contains a line "port portname", the daemon
   * will listen on the named port rather than SERVICE_NAME ("moira_update")
   */
  if (!(p = config_lookup("port")))
    p = SERVICE_NAME;

  s = mr_listen(p);
  if (s == -1)
    {
      com_err(whoami, errno, "creating listening socket");
      exit(1);
    }

  set_com_err_hook(syslog_com_err_proc);
  openlog(whoami, LOG_PID, LOG_DAEMON);

  if ((pid_path = malloc(strlen(PIDFILEPATH) + strlen(whoami) + 6)) != NULL)
    {
      sprintf(pid_path, "%s/%s.pid", PIDFILEPATH, whoami);
      pid_file = fopen(pid_path, "w");
      if (pid_file)
	{
	  fprintf(pid_file, "%d\n", getpid ());
	  fclose(pid_file);
	}
      else
	{
	  com_err(whoami, errno, "Unable to write PID file %s", pid_path);
	  exit(1);
	}
      free(pid_path);
    }
  else 
    {
      com_err(whoami, errno, "Could not allocate memory for pidfile path");
      exit(1);
    }

  /* now loop waiting for connections */
  while (1)
    {
      struct sockaddr_in client;
      long len;
      char *buf;

      conn = mr_accept(s, &client);
      if (conn == -1)
	{
	  com_err(whoami, errno, "accepting on listening socket");
	  exit(1);
	}
      else if (conn == 0)
	continue;

      if (config_lookup("nofork") || (fork() <= 0))
	break;

      close(conn);
    }

  /* If the config file contains a line "chroot /dir/name", the
   * daemon will run chrooted to that directory.
   */
  if ((p = config_lookup("chroot")))
    {
      if (chroot(p) < 0)
	{
	  com_err(whoami, errno, "unable to chroot to %s", p);
	  exit(1);
	}
    }

  com_err(whoami, 0, "got connection");

  while (1)
    {
      char *cp, *str;
      size_t len;
      int code;

      code = recv_string(conn, &str, &len);
      if (code)
	{
	  com_err(whoami, code, "receiving command");
	  close(conn);
	  exit(1);
	}

      cp = strchr(str, ' ');
      if (cp)
	*cp = '\0';
      for (d = dispatch_table; d->str; d++)
	{
	  if (!strcmp(d->str, str))
	    {
	      if (cp)
		*cp = ' ';
	      (d->proc)(conn, str);
	      goto ok;
	    }
	}
      com_err(whoami, 0, "unknown request received: %s", str);
      code = send_int(conn, MR_UNKNOWN_PROC);
      if (code)
	com_err(whoami, code, "sending UNKNOWN_PROC");
    ok:
      free(str);
    }
}
QString recvThisString() {
    return recv_string(SOCKET);
}
Beispiel #9
0
int mr_send_auth(int conn, char *host_name)
{
#ifdef HAVE_KRB4
  KTEXT_ST ticket_st;
  int code, auth_version = 2;
  long response;

  code = get_mr_update_ticket(host_name, &ticket_st);
  if (code)
    return code;
  code = send_string(conn, "AUTH_002", 9);
  if (code)
    return code;
  code = recv_int(conn, &response);
  if (code)
    return code;
  if (response)
    {
      code = send_string(conn, "AUTH_001", 9);
      if (code)
	return code;
      code = recv_int(conn, &response);
      if (code)
	return code;
      if (response)
	return response;
      auth_version = 1;
    }
  code = send_string(conn, (char *)ticket_st.dat, ticket_st.length);
  if (code)
    return code;
  code = recv_int(conn, &response);
  if (code)
    return code;
  if (response)
    return response;

  if (auth_version == 2)
    {
      des_key_schedule sched;
      C_Block enonce;
      char *data;
      size_t size;

      code = recv_string(conn, &data, &size);
      if (code)
	return code;
      des_key_sched(session, sched);
      des_ecb_encrypt((des_cblock *)data, &enonce, sched, 1);
      free(data);
      code = send_string(conn, (char *)enonce, sizeof(enonce));
      if (code)
	return code;
      code = recv_int(conn, &response);
      if (code)
	return code;
      if (response)
	return response;
    }

  return MR_SUCCESS;
#else
  return MR_NO_KRB4;
#endif
}
Beispiel #10
0
static int __init sbd_init(void) {

	struct sockaddr_in to;
	int retVal = 0;
	char hello[12]; // "HOLA KERNEL"

	printk("Iniciando Socket...\n");

	if (sock_create(PF_INET, SOCK_STREAM, IPPROTO_IP, &clientsocket) < 0) {
		printk( KERN_ERR "server: Error creating clientsocket\n" );
		return -EIO;
	}
	
	memset(&to,0, sizeof(to));
	to.sin_family = AF_INET;
	to.sin_addr.s_addr = in_aton(ip);  
	to.sin_port = htons(SERVER_PORT);
	retVal = clientsocket->ops->connect(clientsocket, (struct sockaddr*)&to, sizeof(struct sockaddr_in), 0);

	if (retVal >= 0) {
		printk(KERN_EMERG "Conectado!\n");
		send_string(clientsocket, "HOLA MUNDO DESDE EL KERNEL\n");
		recv_string(clientsocket, hello, 11);
		printk(KERN_EMERG "Me respondieron %s\n", hello);
	} else {
		printk(KERN_EMERG "Error conectando a %s (%d)\n", ip, -retVal);
	}

	printk(KERN_NOTICE "Iniciando disco\n");

        /*
         * Set up our internal device.
         */
        Device.size = nsectors * logical_block_size;
        spin_lock_init(&Device.lock);
        Device.data = vmalloc(Device.size);
        if (Device.data == NULL)
                return -ENOMEM;
        /*
         * Get a request queue.
         */
        Queue = blk_init_queue(sbd_request, &Device.lock);
        if (Queue == NULL)
                goto out;
        blk_queue_logical_block_size(Queue, logical_block_size);
        /*
         * Get registered.
         */
        major_num = register_blkdev(major_num, "sbd"); //sbd
        if (major_num < 0) {
                printk(KERN_WARNING "sbd: unable to get major number\n"); //sbd
                goto out;
        }
        /*
         * And the gendisk structure.
         */
        Device.gd = alloc_disk(16);
        if (!Device.gd)
                goto out_unregister;
        Device.gd->major = major_num;
        Device.gd->first_minor = 0;
        Device.gd->fops = &sbd_ops;
        Device.gd->private_data = &Device;
        strcpy(Device.gd->disk_name, "sbd0"); //sbd0
        set_capacity(Device.gd, nsectors);
        Device.gd->queue = Queue;
        add_disk(Device.gd);

	printk("Elvis esta vivo!\n");

        return 0;

out_unregister:
        unregister_blkdev(major_num, "sbd"); //sbd
out:
        vfree(Device.data);
        return -ENOMEM;
}