Example #1
0
int main(int argc, char* argv[]) {

    // Assemble the key file name
    char* homedir = getenv("HOME");
    int len = strlen(homedir) + 128;
    keyfile = malloc(len);
    snprintf(keyfile, len, "%s/.scache_%d", homedir, getuid());

    timeout = (argc>1) ? atoi(argv[1]) : 300;
    if (timeout<=0) return delete_keyfile();
    if (timeout>300) timeout = 300;

    if (geteuid()) bail("This program must be run as root.");
    if (!isatty(0)) bail("ERROR: STDIN is not a TTY");
    if (isatty(1)) bail("ERROR: stdout is a TTY");

    struct stat statbuf;

loop:

    if (stat(keyfile, &statbuf)<0) {
        init_keyfile();
        if (stat(keyfile, &statbuf)<0) bail("Failed to create keyfile");
    }
    if (statbuf.st_uid) bail("ERROR: Key file is not owned by root");
    if (statbuf.st_mode != 0100600) bail("ERROR: Key file has incorrect mode");
    FILE* f = fopen(keyfile, "r");
    if (!f) bail("Could not open keyfile.");

    int cnt;
    char* line = 0;
    size_t linecap;

    // Check the timeout
    if ((cnt = getline(&line, &linecap, f)) <= 0) bail("Error reading keyfile");
    if (now() > atoi(line)) {
        fclose(f);
        delete_keyfile();
        fprintf(stderr, "Cached pass phrase has expired.\n");
        goto loop;
    }

    // Check the PPPID
    if ((cnt = getline(&line, &linecap, f)) <= 0) bail("Error reading keyfile");
    if (getpppid() != atoi(line)) bail("PPPID mismatch");

    // Check the TTY name
    if ((cnt = getline(&line, &linecap, f)) <= 0) bail("Error reading keyfile");
    line[strlen(line)-1]=0;
    char* tty = ttyname(0);
    if (strcmp(tty, line)) bail("TTY mismatch");

    // Check the UID
    if ((cnt = getline(&line, &linecap, f)) <= 0) bail("Error reading keyfile");
    if (getuid() != atoi(line)) bail("UID mismatch");

    // Everthing checks out, get the secret
    if ((cnt = getline(&line, &linecap, f)) <= 0) bail("Error reading keyfile");
    fclose(f);
    line[cnt-1] = 0;
    // Update the time stamp
    update_keyfile(line);
    printf("%s\n", line);
}
Example #2
0
int main (int argc, char **argv)
{
  int res = 0;
  struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
  sqlprofile = g_try_new0(struct sqlprofile, 1);
  g_mutex_init(&cache.m);
  
  cache.open_table = g_hash_table_new_full(g_int64_hash, g_int64_equal,
					   g_free, free_sqlfs_file);

  if (fuse_opt_parse(&args, sqlprofile, sqlfs_opts, sqlfs_opt_proc) == -1)
    res = 1;

  if (!res && !sqlprofile->profile)
    res = 1;

  if (!res) {
    GError *terr = NULL;
    
    init_keyfile(sqlprofile->profile, &terr);
    if (terr != NULL)
      res = 1;
    
    if (!res) {
      init_cache(&terr);
      
      if (terr != NULL)
	res = 2;
    }

    if (!res) {
      res = sqlfs_fuse_main(&args);
      fuse_opt_free_args(&args);

      if (terr != NULL)
	res = 3;
    }

    if (!res || res > 2)
      destroy_cache(&terr);

    if (!res || res > 1)
      close_keyfile();

    if (sqlprofile != NULL) {
      if (sqlprofile->profile != NULL)
	g_free(sqlprofile->profile);

      g_free(sqlprofile);
    }

    if (terr != NULL) {
      g_error("Position %d - #%d: %s",
		res, terr->code, terr->message);
      g_error_free(terr);
    }
  }
  else {
    g_error("Position %d - #%d: %s",
	    res, 0, "Invalid arguments");
  }

  g_hash_table_destroy(cache.open_table);
  
  g_mutex_clear(&cache.m);
  
  return res;
}