]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/import-util.c
shared: include what we use
[thirdparty/systemd.git] / src / shared / import-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2015 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <string.h>
24
25 #include "alloc-util.h"
26 #include "btrfs-util.h"
27 #include "import-util.h"
28 #include "log.h"
29 #include "macro.h"
30 #include "path-util.h"
31 #include "string-table.h"
32 #include "string-util.h"
33 #include "util.h"
34
35 int import_url_last_component(const char *url, char **ret) {
36 const char *e, *p;
37 char *s;
38
39 e = strchrnul(url, '?');
40
41 while (e > url && e[-1] == '/')
42 e--;
43
44 p = e;
45 while (p > url && p[-1] != '/')
46 p--;
47
48 if (e <= p)
49 return -EINVAL;
50
51 s = strndup(p, e - p);
52 if (!s)
53 return -ENOMEM;
54
55 *ret = s;
56 return 0;
57 }
58
59
60 int import_url_change_last_component(const char *url, const char *suffix, char **ret) {
61 const char *e;
62 char *s;
63
64 assert(url);
65 assert(ret);
66
67 e = strchrnul(url, '?');
68
69 while (e > url && e[-1] == '/')
70 e--;
71
72 while (e > url && e[-1] != '/')
73 e--;
74
75 if (e <= url)
76 return -EINVAL;
77
78 s = new(char, (e - url) + strlen(suffix) + 1);
79 if (!s)
80 return -ENOMEM;
81
82 strcpy(mempcpy(s, url, e - url), suffix);
83 *ret = s;
84 return 0;
85 }
86
87 static const char* const import_verify_table[_IMPORT_VERIFY_MAX] = {
88 [IMPORT_VERIFY_NO] = "no",
89 [IMPORT_VERIFY_CHECKSUM] = "checksum",
90 [IMPORT_VERIFY_SIGNATURE] = "signature",
91 };
92
93 DEFINE_STRING_TABLE_LOOKUP(import_verify, ImportVerify);
94
95 int tar_strip_suffixes(const char *name, char **ret) {
96 const char *e;
97 char *s;
98
99 e = endswith(name, ".tar");
100 if (!e)
101 e = endswith(name, ".tar.xz");
102 if (!e)
103 e = endswith(name, ".tar.gz");
104 if (!e)
105 e = endswith(name, ".tar.bz2");
106 if (!e)
107 e = endswith(name, ".tgz");
108 if (!e)
109 e = strchr(name, 0);
110
111 if (e <= name)
112 return -EINVAL;
113
114 s = strndup(name, e - name);
115 if (!s)
116 return -ENOMEM;
117
118 *ret = s;
119 return 0;
120 }
121
122 int raw_strip_suffixes(const char *p, char **ret) {
123
124 static const char suffixes[] =
125 ".xz\0"
126 ".gz\0"
127 ".bz2\0"
128 ".raw\0"
129 ".qcow2\0"
130 ".img\0"
131 ".bin\0";
132
133 _cleanup_free_ char *q = NULL;
134
135 q = strdup(p);
136 if (!q)
137 return -ENOMEM;
138
139 for (;;) {
140 const char *sfx;
141 bool changed = false;
142
143 NULSTR_FOREACH(sfx, suffixes) {
144 char *e;
145
146 e = endswith(q, sfx);
147 if (e) {
148 *e = 0;
149 changed = true;
150 }
151 }
152
153 if (!changed)
154 break;
155 }
156
157 *ret = q;
158 q = NULL;
159
160 return 0;
161 }
162
163 bool dkr_digest_is_valid(const char *digest) {
164 /* 7 chars for prefix, 64 chars for the digest itself */
165 if (strlen(digest) != 71)
166 return false;
167
168 return startswith(digest, "sha256:") && in_charset(digest + 7, "0123456789abcdef");
169 }
170
171 bool dkr_ref_is_valid(const char *ref) {
172 const char *colon;
173
174 if (isempty(ref))
175 return false;
176
177 colon = strchr(ref, ':');
178 if (!colon)
179 return filename_is_valid(ref);
180
181 return dkr_digest_is_valid(ref);
182 }
183
184 bool dkr_name_is_valid(const char *name) {
185 const char *slash, *p;
186
187 if (isempty(name))
188 return false;
189
190 slash = strchr(name, '/');
191 if (!slash)
192 return false;
193
194 if (!filename_is_valid(slash + 1))
195 return false;
196
197 p = strndupa(name, slash - name);
198 if (!filename_is_valid(p))
199 return false;
200
201 return true;
202 }
203
204 bool dkr_id_is_valid(const char *id) {
205
206 if (!filename_is_valid(id))
207 return false;
208
209 if (!in_charset(id, "0123456789abcdef"))
210 return false;
211
212 return true;
213 }
214
215 int import_assign_pool_quota_and_warn(const char *path) {
216 int r;
217
218 r = btrfs_subvol_auto_qgroup("/var/lib/machines", 0, true);
219 if (r == -ENOTTY) {
220 log_debug_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines, as directory is not on btrfs or not a subvolume. Ignoring.");
221 return 0;
222 }
223 if (r < 0)
224 return log_error_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines: %m");
225 if (r > 0)
226 log_info("Set up default quota hierarchy for /var/lib/machines.");
227
228 r = btrfs_subvol_auto_qgroup(path, 0, true);
229 if (r == -ENOTTY) {
230 log_debug_errno(r, "Failed to set up quota hierarchy for %s, as directory is not on btrfs or not a subvolume. Ignoring.", path);
231 return 0;
232 }
233 if (r < 0)
234 return log_error_errno(r, "Failed to set up default quota hierarchy for %s: %m", path);
235 if (r > 0)
236 log_info("Set up default quota hierarchy for %s.", path);
237
238 return 0;
239 }