summaryrefslogtreecommitdiffhomepage
path: root/src/logic/tpax_init_ustar_header.c
blob: a3ab1205a4f28e15eb697ba1ea5f0b6edb7c53ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/******************************************************/
/*  tpax: a topological pax implementation            */
/*  Copyright (C) 2020  Z. Gilboa                     */
/*  Released under GPLv2 and GPLv3; see COPYING.TPAX. */
/******************************************************/

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <sys/stat.h>

#include <tpax/tpax.h>
#include <tpax/tpax_specs.h>
#include "tpax_driver_impl.h"

#ifndef ssizeof
#define ssizeof(x) (ssize_t)(sizeof(x))
#endif

static void tpax_octal_write(char * ch, ssize_t len, uint64_t val)
{
	for (--len,--len; len>=0 ; len--) {
		ch[len] = val % 8 + '0';
		val /= 8;
	}
}

int tpax_init_ustar_header(
	const struct tpax_driver_ctx *  dctx,
	const char *                    path,
	const struct stat *             st,
	const char *                    linkname,
	struct tpax_ustar_header *      uhdr)
{
	size_t		len;
	const char *	cap;
	const char *	mark;
	unsigned char *	uch;
	unsigned char *	uchcap;
	size_t		lnklen;
	size_t		stsize;
	int64_t		stmtim;
	uint32_t	chksum;
	char		typeflag;
	struct group	grp;
	struct group *	grpres;
	struct passwd	pwd;
	struct passwd *	pwdres;
	char		pwdbuf[2048];

	/* size & mtime validation */
	stsize = S_ISREG(st->st_mode) ? st->st_size : 0;
	stmtim = st->st_mtim.tv_sec;

	if (stsize > 077777777777)
		return -1;

	if ((stmtim < 0) || (stmtim > 077777777777))
		return -1;

	/* linkname validation */
	if (S_ISLNK(st->st_mode) && !linkname)
		return -1;

	lnklen = S_ISLNK(st->st_mode)
		? strnlen(linkname,sizeof(uhdr->u_linkname) + 1)
		: 0;

	if (lnklen > sizeof(uhdr->u_linkname))
		return -1;

	/* typeflag validation */
	if (S_ISREG(st->st_mode))
		typeflag = TPAX_USTAR_TYPEFLAG_REGFILE;
	else if (S_ISLNK(st->st_mode))
		typeflag = TPAX_USTAR_TYPEFLAG_SYMLINK;
	else if (S_ISCHR(st->st_mode))
		typeflag = TPAX_USTAR_TYPEFLAG_CHARDEV;
	else if (S_ISBLK(st->st_mode))
		typeflag = TPAX_USTAR_TYPEFLAG_BLKDEV;
	else if (S_ISFIFO(st->st_mode))
		typeflag = TPAX_USTAR_TYPEFLAG_FIFODEV;
	else
		return -1;

	/* cap (without the trailing slash) */
	mark = &path[strlen(path)];
	cap  = mark;

	for (--cap; (*cap == '/') && (cap > path); cap--)
		(void)0;

	cap = ((cap == path) && (path[0] == '/'))
		? mark : &cap[1];

	/* path solely consists of slash symbols? */
	if ((cap == mark) && (path[0] =='/')) {
		if ((cap - path) > (ssizeof(uhdr->u_prefix) + 1 + ssizeof(uhdr->u_name)))
			return -1;

		else if ((cap - path) <= ssizeof(uhdr->u_name))
			mark = 0;

		else
			mark -= sizeof(uhdr->u_name);

	/* path entirely fits in u_name? */
	} else if ((cap - path) <= ssizeof(uhdr->u_name)) {
		mark = 0;

	/* split between u_prefix and u_name as needed */
	} else {
		mark = cap;

		do {
			for (--mark; (mark > path) && (*mark != '/'); mark--)
				(void)0;
		} while ((mark - path) > ssizeof(uhdr->u_prefix));
	}

	/* one shot */
	memset(uhdr,0,sizeof(*uhdr));

	/* u_name, u_prefix */
	if (mark) {
		memcpy(uhdr->u_prefix,path,mark-path);
		memcpy(uhdr->u_name,&mark[1],cap - mark);
	} else {
		memcpy(uhdr->u_name,path,cap - path);
	}

	/* u_mode */
	tpax_octal_write(uhdr->u_mode,ssizeof(uhdr->u_mode),st->st_mode & TPAX_USTAR_MODE_MASK);

	/* u_uid, u_gid, u_uname, u_gname */
	if (dctx->cctx->drvflags & TPAX_DRIVER_WRITE_FORMAT_RUSTAR) {
		tpax_octal_write(uhdr->u_uid,ssizeof(uhdr->u_uid),0);
		tpax_octal_write(uhdr->u_gid,ssizeof(uhdr->u_gid),0);
	} else {
		if ((uint64_t)st->st_uid <= 07777777)
			tpax_octal_write(uhdr->u_uid,ssizeof(uhdr->u_uid),st->st_uid);
		else
			tpax_octal_write(uhdr->u_uid,ssizeof(uhdr->u_uid),0);

		if ((uint64_t)st->st_gid <= 07777777)
			tpax_octal_write(uhdr->u_gid,ssizeof(uhdr->u_gid),st->st_gid);
		else
			tpax_octal_write(uhdr->u_gid,ssizeof(uhdr->u_gid),0);

		pwdres = 0;
		grpres = 0;

		getpwuid_r(
			st->st_uid,&pwd,
			pwdbuf,sizeof(pwdbuf),
			&pwdres);

		if (pwdres && pwd.pw_name)
			if ((len = strlen(pwd.pw_name)) < sizeof(uhdr->u_uname))
				memcpy(uhdr->u_uname,pwd.pw_name,len);

		getgrgid_r(
			st->st_gid,&grp,
			pwdbuf,sizeof(pwdbuf),
			&grpres);

		if (grpres && grp.gr_name)
			if ((len = strlen(grp.gr_name)) < sizeof(uhdr->u_gname))
				memcpy(uhdr->u_gname,grp.gr_name,len);
	}

	/* u_size, u_mtime */
	tpax_octal_write(uhdr->u_size,ssizeof(uhdr->u_size),stsize);
	tpax_octal_write(uhdr->u_mtime,ssizeof(uhdr->u_mtime),stmtim);

	/* u_typeflag */
	uhdr->u_typeflag[0] = typeflag;

	/* u_linkname */
	if (lnklen)
		memcpy(uhdr->u_linkname,linkname,lnklen);

	/* u_magic */
	uhdr->u_magic[0] = 'u';
	uhdr->u_magic[1] = 's';
	uhdr->u_magic[2] = 't';
	uhdr->u_magic[3] = 'a';
	uhdr->u_magic[4] = 'r';

	/* u_version */
	uhdr->u_version[0] = '0';
	uhdr->u_version[1] = '0';

	/* u_devmajor, u_devminor */
	tpax_octal_write(uhdr->u_devmajor,ssizeof(uhdr->u_devmajor),0);
	tpax_octal_write(uhdr->u_devminor,ssizeof(uhdr->u_devminor),0);

	/* u_chksum */
	uch    = (unsigned char *)uhdr->u_name;
	uchcap = (unsigned char *)uhdr->u_pad;

	for (chksum=0; uch<uchcap; uch++)
		chksum += *uch;

	chksum += sizeof(uhdr->u_chksum) * ' ';

	tpax_octal_write(uhdr->u_chksum,ssizeof(uhdr->u_chksum),chksum);

	/* all done; caller may now change REGFILE to HARDLINK */
	return 0;
}