Beispiel #1
0
static u64 calculate_dir_inode_size(char *dirname)
{
	int count, i;
	struct direct **files, *cur_file;
	u64 dir_inode_size = 0;

	count = scandir(dirname, &files, directory_select, NULL);

	for (i = 0; i < count; i++) {
		cur_file = files[i];
		dir_inode_size += strlen(cur_file->d_name);
	}

	free_namelist(files, count);

	dir_inode_size *= 2;
	return dir_inode_size;
}
Beispiel #2
0
static isc_result_t
process_dhtkey(dns_message_t *msg, dns_name_t *signer, dns_name_t *name,
	       dns_rdata_tkey_t *tkeyin, dns_tkeyctx_t *tctx,
	       dns_rdata_tkey_t *tkeyout,
	       dns_tsig_keyring_t *ring, dns_namelist_t *namelist)
{
	isc_result_t result = ISC_R_SUCCESS;
	dns_name_t *keyname, ourname;
	dns_rdataset_t *keyset = NULL;
	dns_rdata_t keyrdata = DNS_RDATA_INIT, ourkeyrdata = DNS_RDATA_INIT;
	isc_boolean_t found_key = ISC_FALSE, found_incompatible = ISC_FALSE;
	dst_key_t *pubkey = NULL;
	isc_buffer_t ourkeybuf, *shared = NULL;
	isc_region_t r, r2, ourkeyr;
	unsigned char keydata[DST_KEY_MAXSIZE];
	unsigned int sharedsize;
	isc_buffer_t secret;
	unsigned char *randomdata = NULL, secretdata[256];
	dns_ttl_t ttl = 0;

	if (tctx->dhkey == NULL) {
		tkey_log("process_dhtkey: tkey-dhkey not defined");
		tkeyout->error = dns_tsigerror_badalg;
		return (DNS_R_REFUSED);
	}

	if (!dns_name_equal(&tkeyin->algorithm, DNS_TSIG_HMACMD5_NAME)) {
		tkey_log("process_dhtkey: algorithms other than "
			 "hmac-md5 are not supported");
		tkeyout->error = dns_tsigerror_badalg;
		return (ISC_R_SUCCESS);
	}

	/*
	 * Look for a DH KEY record that will work with ours.
	 */
	for (result = dns_message_firstname(msg, DNS_SECTION_ADDITIONAL);
	     result == ISC_R_SUCCESS && !found_key;
	     result = dns_message_nextname(msg, DNS_SECTION_ADDITIONAL)) {
		keyname = NULL;
		dns_message_currentname(msg, DNS_SECTION_ADDITIONAL, &keyname);
		keyset = NULL;
		result = dns_message_findtype(keyname, dns_rdatatype_key, 0,
					      &keyset);
		if (result != ISC_R_SUCCESS)
			continue;

		for (result = dns_rdataset_first(keyset);
		     result == ISC_R_SUCCESS && !found_key;
		     result = dns_rdataset_next(keyset)) {
			dns_rdataset_current(keyset, &keyrdata);
			pubkey = NULL;
			result = dns_dnssec_keyfromrdata(keyname, &keyrdata,
							 msg->mctx, &pubkey);
			if (result != ISC_R_SUCCESS) {
				dns_rdata_reset(&keyrdata);
				continue;
			}
			if (dst_key_alg(pubkey) == DNS_KEYALG_DH) {
				if (dst_key_paramcompare(pubkey, tctx->dhkey))
				{
					found_key = ISC_TRUE;
					ttl = keyset->ttl;
					break;
				} else
					found_incompatible = ISC_TRUE;
			}
			dst_key_free(&pubkey);
			dns_rdata_reset(&keyrdata);
		}
	}

	if (!found_key) {
		if (found_incompatible) {
			tkey_log("process_dhtkey: found an incompatible key");
			tkeyout->error = dns_tsigerror_badkey;
			return (ISC_R_SUCCESS);
		} else {
			tkey_log("process_dhtkey: failed to find a key");
			return (DNS_R_FORMERR);
		}
	}

	RETERR(add_rdata_to_list(msg, keyname, &keyrdata, ttl, namelist));

	isc_buffer_init(&ourkeybuf, keydata, sizeof(keydata));
	RETERR(dst_key_todns(tctx->dhkey, &ourkeybuf));
	isc_buffer_usedregion(&ourkeybuf, &ourkeyr);
	dns_rdata_fromregion(&ourkeyrdata, dns_rdataclass_any,
			     dns_rdatatype_key, &ourkeyr);

	dns_name_init(&ourname, NULL);
	dns_name_clone(dst_key_name(tctx->dhkey), &ourname);

	/*
	 * XXXBEW The TTL should be obtained from the database, if it exists.
	 */
	RETERR(add_rdata_to_list(msg, &ourname, &ourkeyrdata, 0, namelist));

	RETERR(dst_key_secretsize(tctx->dhkey, &sharedsize));
	RETERR(isc_buffer_allocate(msg->mctx, &shared, sharedsize));

	result = dst_key_computesecret(pubkey, tctx->dhkey, shared);
	if (result != ISC_R_SUCCESS) {
		tkey_log("process_dhtkey: failed to compute shared secret: %s",
			 isc_result_totext(result));
		goto failure;
	}
	dst_key_free(&pubkey);

	isc_buffer_init(&secret, secretdata, sizeof(secretdata));

	randomdata = isc_mem_get(tkeyout->mctx, TKEY_RANDOM_AMOUNT);
	if (randomdata == NULL)
		goto failure;

	result = dst__entropy_getdata(randomdata, TKEY_RANDOM_AMOUNT,
				      ISC_FALSE);
	if (result != ISC_R_SUCCESS) {
		tkey_log("process_dhtkey: failed to obtain entropy: %s",
			 isc_result_totext(result));
		goto failure;
	}

	r.base = randomdata;
	r.length = TKEY_RANDOM_AMOUNT;
	r2.base = tkeyin->key;
	r2.length = tkeyin->keylen;
	RETERR(compute_secret(shared, &r2, &r, &secret));
	isc_buffer_free(&shared);

	RETERR(dns_tsigkey_create(name, &tkeyin->algorithm,
				  isc_buffer_base(&secret),
				  isc_buffer_usedlength(&secret),
				  ISC_TRUE, signer, tkeyin->inception,
				  tkeyin->expire, ring->mctx, ring, NULL));

	/* This key is good for a long time */
	tkeyout->inception = tkeyin->inception;
	tkeyout->expire = tkeyin->expire;

	tkeyout->key = randomdata;
	tkeyout->keylen = TKEY_RANDOM_AMOUNT;

	return (ISC_R_SUCCESS);

 failure:
	if (!ISC_LIST_EMPTY(*namelist))
		free_namelist(msg, namelist);
	if (shared != NULL)
		isc_buffer_free(&shared);
	if (pubkey != NULL)
		dst_key_free(&pubkey);
	if (randomdata != NULL)
		isc_mem_put(tkeyout->mctx, randomdata, TKEY_RANDOM_AMOUNT);
	return (result);
}
Beispiel #3
0
static int traverse_directory(struct btrfs_trans_handle *trans,
			      struct btrfs_root *root, char *dir_name,
			      struct directory_name_entry *dir_head, int out_fd)
{
	int ret = 0;

	struct btrfs_inode_item cur_inode;
	struct btrfs_inode_item *inode_item;
	int count, i, dir_index_cnt;
	struct direct **files;
	struct stat st;
	struct directory_name_entry *dir_entry, *parent_dir_entry;
	struct direct *cur_file;
	ino_t parent_inum, cur_inum;
	ino_t highest_inum = 0;
	char *parent_dir_name;
	struct btrfs_path path;
	struct extent_buffer *leaf;
	struct btrfs_key root_dir_key;
	u64 root_dir_inode_size = 0;

	/* Add list for source directory */
	dir_entry = malloc(sizeof(struct directory_name_entry));
	dir_entry->dir_name = dir_name;
	dir_entry->path = strdup(dir_name);

	parent_inum = highest_inum + BTRFS_FIRST_FREE_OBJECTID;
	dir_entry->inum = parent_inum;
	list_add_tail(&dir_entry->list, &dir_head->list);

	btrfs_init_path(&path);

	root_dir_key.objectid = btrfs_root_dirid(&root->root_item);
	root_dir_key.offset = 0;
	btrfs_set_key_type(&root_dir_key, BTRFS_INODE_ITEM_KEY);
	ret = btrfs_lookup_inode(trans, root, &path, &root_dir_key, 1);
	if (ret) {
		fprintf(stderr, "root dir lookup error\n");
		return -1;
	}

	leaf = path.nodes[0];
	inode_item = btrfs_item_ptr(leaf, path.slots[0],
				    struct btrfs_inode_item);

	root_dir_inode_size = calculate_dir_inode_size(dir_name);
	btrfs_set_inode_size(leaf, inode_item, root_dir_inode_size);
	btrfs_mark_buffer_dirty(leaf);

	btrfs_release_path(&path);

	do {
		parent_dir_entry = list_entry(dir_head->list.next,
					      struct directory_name_entry,
					      list);
		list_del(&parent_dir_entry->list);

		parent_inum = parent_dir_entry->inum;
		parent_dir_name = parent_dir_entry->dir_name;
		if (chdir(parent_dir_entry->path)) {
			fprintf(stderr, "chdir error for %s\n",
				parent_dir_name);
			goto fail_no_files;
		}

		count = scandir(parent_dir_entry->path, &files,
				directory_select, NULL);
		if (count == -1)
		{
			fprintf(stderr, "scandir for %s failed: %s\n",
				parent_dir_name, strerror (errno));
			goto fail;
		}

		for (i = 0; i < count; i++) {
			cur_file = files[i];

			if (lstat(cur_file->d_name, &st) == -1) {
				fprintf(stderr, "lstat failed for file %s\n",
					cur_file->d_name);
				goto fail;
			}

			cur_inum = ++highest_inum + BTRFS_FIRST_FREE_OBJECTID;
			ret = add_directory_items(trans, root,
						  cur_inum, parent_inum,
						  cur_file->d_name,
						  &st, &dir_index_cnt);
			if (ret) {
				fprintf(stderr, "add_directory_items failed\n");
				goto fail;
			}

			ret = add_inode_items(trans, root, &st,
					      cur_file->d_name, cur_inum,
					      parent_inum, dir_index_cnt,
					      &cur_inode);
			if (ret) {
				fprintf(stderr, "add_inode_items failed\n");
				goto fail;
			}

			ret = add_xattr_item(trans, root,
					     cur_inum, cur_file->d_name);
			if (ret) {
				fprintf(stderr, "add_xattr_item failed\n");
				if(ret != -ENOTSUP)
					goto fail;
			}

			if (S_ISDIR(st.st_mode)) {
				dir_entry = malloc(sizeof(struct directory_name_entry));
				dir_entry->dir_name = cur_file->d_name;
				dir_entry->path = make_path(parent_dir_entry->path,
							    cur_file->d_name);
				dir_entry->inum = cur_inum;
				list_add_tail(&dir_entry->list,	&dir_head->list);
			} else if (S_ISREG(st.st_mode)) {
				ret = add_file_items(trans, root, &cur_inode,
						     cur_inum, parent_inum, &st,
						     cur_file->d_name, out_fd);
				if (ret) {
					fprintf(stderr, "add_file_items failed\n");
					goto fail;
				}
			} else if (S_ISLNK(st.st_mode)) {
				ret = add_symbolic_link(trans, root,
						        cur_inum, cur_file->d_name);
				if (ret) {
					fprintf(stderr, "add_symbolic_link failed\n");
					goto fail;
				}
			}
		}

		free_namelist(files, count);
		free(parent_dir_entry->path);
		free(parent_dir_entry);

		index_cnt = 2;

	} while (!list_empty(&dir_head->list));

	return 0;
fail:
	free_namelist(files, count);
fail_no_files:
	free(parent_dir_entry->path);
	free(parent_dir_entry);
	return -1;
}
Beispiel #4
0
main( int argc, char *argv[] )
{
    char *bbshome = NULL, *prog;
    char copybuf[256];
    int c, rc;
    int in_header = 1;
    FILE *fp;
    NAMELIST userid_list;
    LONG failmask;

    /* Prefix sender with INTERNET: */
    strcpy(sender, MAILER_PREFIX);
    strcat(sender, ":");
    sptr = sender + strlen(sender);
    ssize = sizeof(sender) - strlen(sender);
 
    /* Find out if we are bbsmail or bbsmaild */
    prog = strrchr(argv[0], '/');
    if (prog == NULL) prog = argv[0];
    else prog++;
    if (!strcmp(prog, "bbsmail")) {
        /* We do not expect headers. Get sender info from passwd file. */
        struct passwd *pw = getpwuid(getuid());
        if (pw != NULL) {
            char *comma;
            strncpy(sptr, pw->pw_name, ssize);
            strncpy(sendername, pw->pw_gecos, sizeof(sendername)+1);
            if ((comma = strchr(sendername, ',')) != NULL) *comma = '\0';
	}
        daemon = in_header = 0;     
    }

    /* Scan argv for option flags */
    while ((c = getopt(argc, argv, "d:s:?")) != -1)
      {
	switch (c)
	  {
	  case 'd':
	    bbshome = optarg;
	    break;
          case 's':
	    if (daemon) {
	      usage(prog);
	      return 2;
            }
	    strncpy(subject, optarg, sizeof(subject)-1);
            break;
	  case '?':
	    usage(prog);
	    return 2;
	  }
      }

    /* Must have at least one recipient */
    if (optind >= argc) {
        usage(prog);
        return 2;
    }

    /* Form recipients into namelist */
    userid_list = NULL;
    while (optind < argc) {
        if (!is_in_namelist(userid_list, argv[optind]))
            add_namelist(&userid_list, argv[optind], NULL);
        optind++;
    }
    
    /* Home and initialize bbs library */
    if (home_bbs(bbshome) == -1) {
        fprintf(stderr, "%s: Cannot chdir to %s\n", prog, bbshome);
        return 1;
    }

    if (local_bbs_initialize(NULL) != S_OK) {
        fprintf(stderr, "%s: local_bbs_initialize failed\n", prog);
        return 1;
    }

    /* Identify ourself for the log file */
    strcpy(user_params.u.userid, "[bbsmail]");
    user_params.perms = ~0;

    /* Copy message to temporary file, eating headers first */
    fp = fopen(server.tempfile, "w");
    if (fp == NULL) {
        free_namelist(&userid_list);
        fprintf(stderr, "%s: ", prog);
        perror("spool file open failed");
        return 1;
    }
    while (fgets(copybuf, sizeof copybuf, stdin) != NULL) {
        if (in_header) {
            in_header = eat_header(copybuf);
        }
        if (!in_header) fputs(copybuf, fp);
    }
    fclose(fp);            

    /* Make sure we know who the sender is */
    if (*sptr == '\0') {
        free_namelist(&userid_list);
        remove(server.tempfile);
        fprintf(stderr, "%s: cannot determine sender\n", prog);
        return 1;
    }

    /* Now mail it and let our caller know what happened */
    rc = local_bbs_mail(sender, sendername, userid_list, subject,
			server.tempfile, &failmask);

    if (rc != S_OK) {
        apply_namelist(userid_list, show_failures, &failmask);
        if (rc == S_NOSUCHUSER)
	  fprintf(stderr, "bbs_mail: No such user\n");
        else if (rc == S_CANNOTMAIL)
	  fprintf(stderr, "bbs_mail: User cannot receive Internet mail\n");
    }

    /* That's all, folks */
    free_namelist(&userid_list);
    remove(server.tempfile);
    local_bbs_disconnect();

    return (rc == S_OK ? 0 : 1);
}    
Beispiel #5
0
int main (int argc, char *argv[])
{
    char * dname;
    struct arcfilt filt;
    struct dataset ds;
    int r;

    if (argc < 2)
    {
        printf ("readarc takes at least one argument.");
        return -1;
    }

    dname = argv[1];
    if (dname == 0)
    {
         printf ("could not get file name.\n");
         return 1;
    }

    if (argc < 4)
      filt.use_utc = 0;
    else
    {
      r = txt2utc (argv[2], filt.t1);
      if (r != 0)
      {
        printf ("could not parse UTC time 1!");
        return -1;
      }
      r = txt2utc (argv[3], filt.t2);
      if (r != 0)
      {
        printf ("could not parse UTC time 2!");
        return -1;
      }
      DEBUG ("Selecting on time range (%lu,%lu) - (%lu,%lu)\n",
        filt.t1[0], filt.t1[1], filt.t2[0], filt.t2[1]);
      filt.use_utc = 1;
    }

    if (argc < 5)
    {
      filt.nl.n = 0;
      filt.nl.s = 0;
    }
    else
    {
      int nn, in;
      char ** nlist;
      nn = argc - 4;
      nlist = malloc (nn * sizeof (char *));
      for (in=0; in<nn; in++)
        nlist[in] = argv[in+4];
      create_namelist (nn, nlist, &(filt.nl));
      free (nlist);
    }
    filt.fname = dname;
    DEBUG ("Number of register name specifications = %d.\n", filt.nl.n);

    DEBUG ("Calling readarc.\n");

    r = readarc (&filt, &ds); 
    DEBUG ("Returned %d.\n", r);
    free_namelist (&(filt.nl));
    if (r != 0)
      return r;

    DEBUG ("Dumping timestreams.\n");    
    r = output_txt (&ds);

    free_dataset (&ds);

    return 0;
}
Beispiel #6
0
int main (int argc, char* argv[])
{
  int next_option;

  struct arcfilt filt;
  struct dataset ds;
  int r;
  int verbose = 0;
  char ** nlist;
  int nn;
  int format, do_tar, do_gzip;

  /* A string listing valid short options letters.  */
  const char* const short_options = "ho:r:s:e:f:tzv";
  /* An array describing valid long options.  */
  const struct option long_options[] = {
    { "help",     0, NULL, 'h' },
    { "output",   1, NULL, 'o' },
    { "register", 1, NULL, 'r' },
    { "start",    1, NULL, 's' },
    { "end",      1, NULL, 'e' },
    { "format",   1, NULL, 'f' },
    { "tar",      0, NULL, 't' },
    { "gzip",     0, NULL, 'z' },
    { "verbose",  0, NULL, 'v' },
    { NULL,       0, NULL, 0   }   /* Required at end of array.  */
  };

  /* The name of the file to receive program output, or NULL for
     standard output.  */
  const char* output_filename = NULL;

  /* Remember the name of the program, to incorporate in messages.
     The name is stored in argv[0].  */
  program_name = argv[0];

  filt.use_utc = 0;
  filt.t1[0] = 0;
  filt.t1[1] = 0;
  filt.t2[0] = 0 - 1;    /* Max uint32_t */
  filt.t2[1] = 0;
  if (argc > 0)
    nlist = malloc (argc * sizeof (char *));
  else
    nlist = NULL;
  nn = 0;
  format = OUTFORMAT_DIRFILE;
  do_tar = 1;
  do_gzip = 1;

  do {
    next_option = getopt_long (argc, argv, short_options,
                               long_options, NULL);
    switch (next_option)
    {
    case 'h':   /* -h or --help */
      /* User has requested usage information.  Print it to standard
         output, and exit with exit code zero (normal termination).  */
      print_usage (stdout, 0);

    case 'o':   /* -o or --output */
      /* This option takes an argument, the name of the output file.  */
      output_filename = optarg;
      break;

    case 's':   /* -s or --start */
      /* This option takes an argument, the starting UTC time. */
      r = txt2utc (optarg, filt.t1);
      filt.use_utc = 1;
      if (r != 0)
      {
        printf ("could not parse UTC starting time!");
        return -1;
      }
      break;

    case 'e':   /* -e or --end */
      /* This option takes an argument, the ending UTC time. */
      r = txt2utc (optarg, filt.t2);
      filt.use_utc = 1;
      if (r != 0)
      {
        printf ("could not parse UTC ending time!");
        return -1;
      }
      break;

    case 'r':   /* -r or --register */
      /* This option takes an argument, the register specification. */
      /* It can be used multiple times. */
      nlist[nn] = optarg;
      nn++;
      break;

    case 't':   /* -t or --tar */
      do_tar = 1;
      break;

    case 'z':   /* -z or --gzip */
      do_gzip = 1;
      break;

    case 'f':   /* -f or --format */
      if (!strcasecmp (optarg, "dir") || !strcasecmp (optarg, "dirfile"))
        format = OUTFORMAT_DIRFILE;
      else if (!strcasecmp (optarg, "txt") || !strcasecmp (optarg, "text"))
        format = OUTFORMAT_TXT;
      else if (!strcasecmp (optarg, "hex") || !strcasecmp (optarg, "dump"))
        format = OUTFORMAT_HEX;
      else
      {
        printf ("Unrecognized format type %s.\n", optarg);
        return -1;
      }
      break;

    case 'v':   /* -v or --verbose */
      verbose = 1;
      break;

    case '?':   /* The user specified an invalid option.  */
      /* Print usage information to standard error, and exit with exit
         code one (indicating abnormal termination).  */
      print_usage (stderr, 1);

    case -1:    /* Done with options.  */
      break;

    default:    /* Something else: unexpected.  */
      abort ();
    }
  }
  while (next_option != -1);

  /* Done with options.  OPTIND points to first non-option argument. */
  if (nn>0)
    create_namelist (nn, nlist, &(filt.nl));
  else
  {
    filt.nl.n = 0;
    filt.nl.s = 0;
  }
  if (argc > 0)
    free (nlist);

  for (int i = optind; i < argc; ++i)
  {
    char * use_output_fname;
    filt.fname = argv[i];

    DEBUG ("Number of register name specifications = %d.\n", filt.nl.n);
    DEBUG ("Calling readarc.\n");

    r = readarc (&filt, &ds); 
    if (r != 0)
    {
      free_namelist (&(filt.nl));
      return r;
    }
    DEBUG ("Returned %d.\n", r);

    use_output_fname = output_filename;
    if (use_output_fname == NULL)
      guess_output_filename (filt.fname, &use_output_fname, format, do_tar, do_gzip);

    switch (format)
    {
      case OUTFORMAT_TXT:
        r = output_txt (&ds);
        break;
      case OUTFORMAT_HEX:
        r = output_hex (&ds);
        break;
      case OUTFORMAT_DIRFILE:
        if (!do_tar)
          r = output_dirball (use_output_fname, &ds, 0);
        else if (!do_gzip)
          r = output_dirball (use_output_fname, &ds, 1);
        else
          r = output_dirball (use_output_fname, &ds, 2);
        break;
      default:
        printf ("Unknown output type code %d.\n", format);
    }

    if ((use_output_fname != NULL) && (use_output_fname != output_filename))
      free (use_output_fname);

    free_dataset (&ds);
  }
  free_namelist (&(filt.nl));

  return 0;
}
Beispiel #7
0
void free_namelist( name_decl_t *namelist ){
	if ( namelist ){
		free_namelist( namelist->next );
		free( namelist );
	}
}