Exemple #1
0
void
th_print_long_ls(TAR *t)
{
	char modestring[12];
	struct passwd *pw;
	struct group *gr;
	uid_t uid;
	gid_t gid;
	char username[_POSIX_LOGIN_NAME_MAX];
	char groupname[_POSIX_LOGIN_NAME_MAX];
	time_t mtime;
	struct tm *mtm;

#ifdef HAVE_STRFTIME
	char timebuf[18];
#else
	const char *months[] = {
		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
	};
#endif

	uid = th_get_uid(t);
	pw = getpwuid(uid);
	if (pw == NULL)
		snprintf(username, sizeof(username), "%d", uid);
	else
		strlcpy(username, pw->pw_name, sizeof(username));

	gid = th_get_gid(t);
	gr = getgrgid(gid);
	if (gr == NULL)
		snprintf(groupname, sizeof(groupname), "%d", gid);
	else
		strlcpy(groupname, gr->gr_name, sizeof(groupname));

	strmode(th_get_mode(t), modestring);
	printf("%.10s %-8.8s %-8.8s ", modestring, username, groupname);

	if (TH_ISCHR(t) || TH_ISBLK(t))
		printf(" %3d, %3d ", th_get_devmajor(t), th_get_devminor(t));
	else
		printf("%9ld ", (long)th_get_size(t));

	mtime = th_get_mtime(t);
	mtm = localtime(&mtime);
#ifdef HAVE_STRFTIME
	strftime(timebuf, sizeof(timebuf), "%h %e %H:%M %Y", mtm);
	printf("%s", timebuf);
#else
	printf("%.3s %2d %2d:%02d %4d",
	       months[mtm->tm_mon],
	       mtm->tm_mday, mtm->tm_hour, mtm->tm_min, mtm->tm_year + 1900);
#endif

	printf(" %s", th_get_pathname(t));

	if (TH_ISSYM(t) || TH_ISLNK(t))
	{
		if (TH_ISSYM(t))
			printf(" -> ");
		else
			printf(" link to ");
		if ((t->options & TAR_GNU) && t->th_buf.gnu_longlink != NULL)
			printf("%s", t->th_buf.gnu_longlink);
		else
			printf("%.100s", t->th_buf.linkname);
	}

	putchar('\n');
}
Exemple #2
0
static int sandbox_attr(void *fpctx,void *handle,const char *attr,glite_jp_attrval_t **attrval)
{
	glite_jp_error_t	err;
	glite_jp_context_t	ctx = fpctx;
	glite_jp_attrval_t	*out = NULL;
	int			i,nout = 0, count = 0;
	sb_handle 	*h = handle;


	printf("sandbox_attr() called\n");

	memset(&err,0,sizeof err);
	err.source = __FUNCTION__;
	glite_jp_clear_error(ctx);

	*attrval = NULL;

	if (!strcmp(attr, GLITE_JP_ATTR_ISB_FILENAME)) {
		while ((i = th_read(h->t)) == 0)
		{
			printf("-- %s\n", th_get_pathname(h->t));

			if ( !(count % ALLOC_CHUNK) ) {
				*attrval = realloc(*attrval, (count + ALLOC_CHUNK + 1) * sizeof(**attrval) );
				memset( (*attrval) + count, 0, (ALLOC_CHUNK + 1) * sizeof(**attrval));
			}
			(*attrval)[count].name = strdup(GLITE_JP_ATTR_ISB_FILENAME);
			(*attrval)[count].value = strdup(th_get_pathname(h->t));
			(*attrval)[count].origin = GLITE_JP_ATTR_ORIG_FILE;
			(*attrval)[count].timestamp = th_get_mtime(h->t);

			count++;

			if (TH_ISREG(h->t) && tar_skip_regfile(h->t) != 0)
			{
				err.code = EIO;
				err.desc = "tar_skip_regfile";
				return glite_jp_stack_error(ctx,&err);
			}
		}
	}
	else if (!strcmp(attr, GLITE_JP_ATTR_OSB_FILENAME)) {
		printf("Namespace %s not implemented yet\n", GLITE_JP_ATTR_OSB_FILENAME);
	}
	else if (strstr(attr,GLITE_JP_OSB_CONTENT_NS)) {
		printf("Namespace %s not implemented yet\n", GLITE_JP_OSB_CONTENT_NS);
	}
	else if (strstr(attr,GLITE_JP_ISB_CONTENT_NS)) {
		char *fileName = (char *) attr + sizeof(GLITE_JP_ISB_CONTENT_NS);
	
		printf("untaring file: %s\n", fileName);

		while (th_read(h->t) == 0)
		{ 
			if ( !strcmp(fileName, th_get_pathname(h->t)) ) {
			/* extract the file */
				int	k;
				size_t	size;
				char	buf[T_BLOCKSIZE];
				char	*value;


				if (!TH_ISREG(h->t)) assert(0);	// not a regular file

				size = th_get_size(h->t);
				value = (char *) malloc(size * sizeof(char) + 1);
				memset( value, 0, size * sizeof(char) + 1);

				for (i = 0; i < size; i += T_BLOCKSIZE)
				{
					k = tar_block_read(h->t, buf);
					if (k == -1)
					{
						err.code = errno;
						err.desc = "tar_block_read";
						return glite_jp_stack_error(ctx,&err);
					}

					// tar_block_read calls glite_jppsbe_pread, which usually
					// returns whole block (read from the middle of uploaded
					// tar file
					// so cut k in order to the last chunk had correct size 
					if (i + T_BLOCKSIZE > size) { 
						k = size - i;
					}

					strncpy(value + i, buf, k);
				}
				*attrval = malloc(2 * sizeof(**attrval) );
				memset( (*attrval), 0, 2 * sizeof(**attrval));

				(*attrval)[0].name = strdup(attr);
				(*attrval)[0].value = value;
				(*attrval)[0].origin = GLITE_JP_ATTR_ORIG_FILE;
				(*attrval)[0].timestamp = th_get_mtime(h->t);
			}
			else if (TH_ISREG(h->t) && tar_skip_regfile(h->t) != 0)
			{
				err.code = EIO;
				err.desc = "tar_skip_regfile";
				return glite_jp_stack_error(ctx,&err);
			}
		}
	}

	return glite_jp_stack_error(ctx,&err);
}
Exemple #3
0
static int
tar_set_file_perms(TAR *t, char *realname)
{
	mode_t mode;
	uid_t uid;
	gid_t gid;
	struct utimbuf ut;
	char *filename;

	filename = (realname ? realname : th_get_pathname(t));
	mode = th_get_mode(t);
	uid = th_get_uid(t);
	gid = th_get_gid(t);
	ut.modtime = ut.actime = th_get_mtime(t);

	/* change owner/group */
	if (geteuid() == 0)
#ifdef HAVE_LCHOWN
		if (lchown(filename, uid, gid) == -1)
		{
# ifdef DEBUG
			fprintf(stderr, "lchown(\"%s\", %d, %d): %s\n",
				filename, uid, gid, strerror(errno));
# endif
#else /* ! HAVE_LCHOWN */
		if (!TH_ISSYM(t) && chown(filename, uid, gid) == -1)
		{
# ifdef DEBUG
			fprintf(stderr, "chown(\"%s\", %d, %d): %s\n",
				filename, uid, gid, strerror(errno));
# endif
#endif /* HAVE_LCHOWN */
			return -1;
		}

	/* change access/modification time */
	if (!TH_ISSYM(t) && utime(filename, &ut) == -1)
	{
#ifdef DEBUG
		perror("utime()");
#endif
		return -1;
	}

	/* change permissions */
	if (!TH_ISSYM(t) && chmod(filename, mode) == -1)
	{
#ifdef DEBUG
		perror("chmod()");
#endif
		return -1;
	}

	return 0;
}


/* switchboard */
int
tar_extract_file(TAR *t, char *realname)
{
	int i;
	char *lnp;
	int pathname_len;
	int realname_len;

	if (t->options & TAR_NOOVERWRITE)
	{
		struct stat s;

		if (lstat(realname, &s) == 0 || errno != ENOENT)
		{
			errno = EEXIST;
			return -1;
		}
	}

	if (TH_ISDIR(t))
	{
		i = tar_extract_dir(t, realname);
		if (i == 1)
			i = 0;
	}
	else if (TH_ISLNK(t))
		i = tar_extract_hardlink(t, realname);
	else if (TH_ISSYM(t))
		i = tar_extract_symlink(t, realname);
	else if (TH_ISCHR(t))
		i = tar_extract_chardev(t, realname);
	else if (TH_ISBLK(t))
		i = tar_extract_blockdev(t, realname);
	else if (TH_ISFIFO(t))
		i = tar_extract_fifo(t, realname);
	else /* if (TH_ISREG(t)) */
		i = tar_extract_regfile(t, realname);

	if (i != 0)
		return i;

	i = tar_set_file_perms(t, realname);
	if (i != 0)
		return i;

	pathname_len = strlen(th_get_pathname(t)) + 1;
	realname_len = strlen(realname) + 1;
	lnp = (char *)calloc(1, pathname_len + realname_len);
	if (lnp == NULL)
		return -1;
	strcpy(&lnp[0], th_get_pathname(t));
	strcpy(&lnp[pathname_len], realname);
#ifdef DEBUG
	printf("tar_extract_file(): calling libtar_hash_add(): key=\"%s\", "
	       "value=\"%s\"\n", th_get_pathname(t), realname);
#endif
	if (libtar_hash_add(t->h, lnp) != 0)
		return -1;

	return 0;
}
static int
tar_set_file_perms(TAR *t, char *realname)
{
	mode_t mode;
	uid_t uid;
	gid_t gid;
	struct utimbuf ut;
	char *filename;

	filename = (realname ? realname : th_get_pathname(t));
	mode = th_get_mode(t);
	uid = th_get_uid(t);
	gid = th_get_gid(t);
	ut.modtime = ut.actime = th_get_mtime(t);

#ifdef DEBUG
	printf("   ==> setting perms: %s (mode %04o, uid %d, gid %d)\n",
	       filename, mode, uid, gid);
#endif

	/* change owner/group */
	if (geteuid() == 0)
#ifdef HAVE_LCHOWN
		if (lchown(filename, uid, gid) == -1)
		{
# ifdef DEBUG
			fprintf(stderr, "lchown(\"%s\", %d, %d): %s\n",
				filename, uid, gid, strerror(errno));
# endif
#else /* ! HAVE_LCHOWN */
		if (!TH_ISSYM(t) && chown(filename, uid, gid) == -1)
		{
# ifdef DEBUG
			fprintf(stderr, "chown(\"%s\", %d, %d): %s\n",
				filename, uid, gid, strerror(errno));
# endif
#endif /* HAVE_LCHOWN */
			return -1;
		}

	/* change access/modification time */
	if (!TH_ISSYM(t) && utime(filename, &ut) == -1)
	{
#ifdef DEBUG
		perror("utime()");
#endif
		return -1;
	}

	/* change permissions */
	if (!TH_ISSYM(t) && chmod(filename, mode) == -1)
	{
#ifdef DEBUG
		perror("chmod()");
#endif
		return -1;
	}

	return 0;
}


/* switchboard */
int
tar_extract_file(TAR *t, char *realname, char *prefix, const int *progress_fd)
{
	int i;
	char *lnp;
	int pathname_len;
	int realname_len;

	if (t->options & TAR_NOOVERWRITE)
	{
		struct stat s;

		if (lstat(realname, &s) == 0 || errno != ENOENT)
		{
			errno = EEXIST;
			return -1;
		}
	}

	if (TH_ISDIR(t))
	{
		printf("dir\n");
		i = tar_extract_dir(t, realname);
		if (i == 1)
			i = 0;
	}
	else if (TH_ISLNK(t)) {
		printf("link\n");
		i = tar_extract_hardlink(t, realname, prefix);
	}
	else if (TH_ISSYM(t)) {
		printf("sym\n");
		i = tar_extract_symlink(t, realname);
	}
	else if (TH_ISCHR(t)) {
		printf("chr\n");
		i = tar_extract_chardev(t, realname);
	}
	else if (TH_ISBLK(t)) {
		printf("blk\n");
		i = tar_extract_blockdev(t, realname);
	}
	else if (TH_ISFIFO(t)) {
		printf("fifo\n");
		i = tar_extract_fifo(t, realname);
	}
	else /* if (TH_ISREG(t)) */ {
		printf("reg\n");
		i = tar_extract_regfile(t, realname, progress_fd);
	}

	if (i != 0) {
		printf("FAILED RESTORE OF FILE i: %s\n", realname);
		return i;
	}

	i = tar_set_file_perms(t, realname);
	if (i != 0) {
		printf("FAILED SETTING PERMS: %d\n", i);
		return i;
	}

#ifdef HAVE_SELINUX
	if((t->options & TAR_STORE_SELINUX) && t->th_buf.selinux_context != NULL)
	{
#ifdef DEBUG
		printf("   Restoring SELinux context %s to file %s\n", t->th_buf.selinux_context, realname);
#endif
		if (lsetfilecon(realname, t->th_buf.selinux_context) < 0) {
			fprintf(stderr, "Failed to restore SELinux context %s!\n", strerror(errno));
		}
	}
#endif

/*
	pathname_len = strlen(th_get_pathname(t)) + 1;
	realname_len = strlen(realname) + 1;
	lnp = (char *)calloc(1, pathname_len + realname_len);
	if (lnp == NULL)
		return -1;
	strcpy(&lnp[0], th_get_pathname(t));
	strcpy(&lnp[pathname_len], realname);
#ifdef DEBUG
	printf("tar_extract_file(): calling libtar_hash_add(): key=\"%s\", "
	       "value=\"%s\"\n", th_get_pathname(t), realname);
#endif
	if (libtar_hash_add(t->h, lnp) != 0)
		return -1;
	free(lnp);
*/
	return 0;
}