chiark / gitweb /
logind: fix some potentially uninitialized accesses
[elogind.git] / src / unit-name.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU 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 #include <assert.h>
25
26 #include "util.h"
27 #include "unit-name.h"
28
29 #define VALID_CHARS                             \
30         "0123456789"                            \
31         "abcdefghijklmnopqrstuvwxyz"            \
32         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"            \
33         ":-_.\\"
34
35 bool unit_name_is_valid_no_type(const char *n, bool template_ok) {
36         const char *e, *i, *at;
37
38         /* Valid formats:
39          *
40          *         string@instance.suffix
41          *         string.suffix
42          */
43
44         assert(n);
45
46         if (strlen(n) >= UNIT_NAME_MAX)
47                 return false;
48
49         e = strrchr(n, '.');
50         if (!e || e == n)
51                 return false;
52
53         for (i = n, at = NULL; i < e; i++) {
54
55                 if (*i == '@' && !at)
56                         at = i;
57
58                 if (!strchr("@" VALID_CHARS, *i))
59                         return false;
60         }
61
62         if (at) {
63                 if (at == n)
64                         return false;
65
66                 if (!template_ok && at+1 == e)
67                         return false;
68         }
69
70         return true;
71 }
72
73 bool unit_instance_is_valid(const char *i) {
74         assert(i);
75
76         /* The max length depends on the length of the string, so we
77          * don't really check this here. */
78
79         if (i[0] == 0)
80                 return false;
81
82         /* We allow additional @ in the instance string, we do not
83          * allow them in the prefix! */
84
85         for (; *i; i++)
86                 if (!strchr("@" VALID_CHARS, *i))
87                         return false;
88
89         return true;
90 }
91
92 bool unit_prefix_is_valid(const char *p) {
93
94         /* We don't allow additional @ in the instance string */
95
96         if (p[0] == 0)
97                 return false;
98
99         for (; *p; p++)
100                 if (!strchr(VALID_CHARS, *p))
101                         return false;
102
103         return true;
104 }
105
106 int unit_name_to_instance(const char *n, char **instance) {
107         const char *p, *d;
108         char *i;
109
110         assert(n);
111         assert(instance);
112
113         /* Everything past the first @ and before the last . is the instance */
114         if (!(p = strchr(n, '@'))) {
115                 *instance = NULL;
116                 return 0;
117         }
118
119         assert_se(d = strrchr(n, '.'));
120         assert(p < d);
121
122         if (!(i = strndup(p+1, d-p-1)))
123                 return -ENOMEM;
124
125         *instance = i;
126         return 0;
127 }
128
129 char *unit_name_to_prefix_and_instance(const char *n) {
130         const char *d;
131
132         assert(n);
133
134         assert_se(d = strrchr(n, '.'));
135
136         return strndup(n, d - n);
137 }
138
139 char *unit_name_to_prefix(const char *n) {
140         const char *p;
141
142         if ((p = strchr(n, '@')))
143                 return strndup(n, p - n);
144
145         return unit_name_to_prefix_and_instance(n);
146 }
147
148 char *unit_name_change_suffix(const char *n, const char *suffix) {
149         char *e, *r;
150         size_t a, b;
151
152         assert(n);
153         assert(unit_name_is_valid_no_type(n, true));
154         assert(suffix);
155
156         assert_se(e = strrchr(n, '.'));
157         a = e - n;
158         b = strlen(suffix);
159
160         if (!(r = new(char, a + b + 1)))
161                 return NULL;
162
163         memcpy(r, n, a);
164         memcpy(r+a, suffix, b+1);
165
166         return r;
167 }
168
169 char *unit_name_build(const char *prefix, const char *instance, const char *suffix) {
170         char *r;
171
172         assert(prefix);
173         assert(unit_prefix_is_valid(prefix));
174         assert(!instance || unit_instance_is_valid(instance));
175         assert(suffix);
176
177         if (!instance)
178                 return strappend(prefix, suffix);
179
180         if (asprintf(&r, "%s@%s%s", prefix, instance, suffix) < 0)
181                 return NULL;
182
183         return r;
184 }
185
186 static char* do_escape(const char *f, char *t) {
187         assert(f);
188         assert(t);
189
190         for (; *f; f++) {
191                 if (*f == '/')
192                         *(t++) = '-';
193                 else if (*f == '-' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
194                         *(t++) = '\\';
195                         *(t++) = 'x';
196                         *(t++) = hexchar(*f >> 4);
197                         *(t++) = hexchar(*f);
198                 } else
199                         *(t++) = *f;
200         }
201
202         return t;
203 }
204
205 char *unit_name_build_escape(const char *prefix, const char *instance, const char *suffix) {
206         char *r, *t;
207         size_t a, b, c;
208
209         assert(prefix);
210         assert(suffix);
211
212         /* Takes a arbitrary string for prefix and instance plus a
213          * suffix and makes a nice string suitable as unit name of it,
214          * escaping all weird chars on the way.
215          *
216          * / becomes ., and all chars not allowed in a unit name get
217          * escaped as \xFF, including \ and ., of course. This
218          * escaping is hence reversible.
219          *
220          * This is primarily useful to make nice unit names from
221          * strings, but is actually useful for any kind of string.
222          */
223
224         a = strlen(prefix);
225         c = strlen(suffix);
226
227         if (instance) {
228                 b = strlen(instance);
229
230                 if (!(r = new(char, a*4 + 1 + b*4 + c + 1)))
231                         return NULL;
232
233                 t = do_escape(prefix, r);
234                 *(t++) = '@';
235                 t = do_escape(instance, t);
236         } else {
237
238                 if (!(r = new(char, a*4 + c + 1)))
239                         return NULL;
240
241                 t = do_escape(prefix, r);
242         }
243
244         strcpy(t, suffix);
245         return r;
246 }
247
248 char *unit_name_escape(const char *f) {
249         char *r, *t;
250
251         if (!(r = new(char, strlen(f)*4+1)))
252                 return NULL;
253
254         t = do_escape(f, r);
255         *t = 0;
256
257         return r;
258
259 }
260
261 char *unit_name_unescape(const char *f) {
262         char *r, *t;
263
264         assert(f);
265
266         if (!(r = strdup(f)))
267                 return NULL;
268
269         for (t = r; *f; f++) {
270                 if (*f == '-')
271                         *(t++) = '/';
272                 else if (*f == '\\') {
273                         int a, b;
274
275                         if (f[1] != 'x' ||
276                             (a = unhexchar(f[2])) < 0 ||
277                             (b = unhexchar(f[3])) < 0) {
278                                 /* Invalid escape code, let's take it literal then */
279                                 *(t++) = '\\';
280                         } else {
281                                 *(t++) = (char) ((a << 4) | b);
282                                 f += 3;
283                         }
284                 } else
285                         *(t++) = *f;
286         }
287
288         *t = 0;
289
290         return r;
291 }
292
293 bool unit_name_is_template(const char *n) {
294         const char *p;
295
296         assert(n);
297
298         if (!(p = strchr(n, '@')))
299                 return false;
300
301         return p[1] == '.';
302 }
303
304 char *unit_name_replace_instance(const char *f, const char *i) {
305         const char *p, *e;
306         char *r, *k;
307         size_t a;
308
309         assert(f);
310
311         p = strchr(f, '@');
312         assert_se(e = strrchr(f, '.'));
313
314         a = p - f;
315
316         if (p) {
317                 size_t b;
318
319                 b = strlen(i);
320
321                 if (!(r = new(char, a + 1 + b + strlen(e) + 1)))
322                         return NULL;
323
324                 k = mempcpy(r, f, a + 1);
325                 k = mempcpy(k, i, b);
326         } else {
327
328                 if (!(r = new(char, a + strlen(e) + 1)))
329                         return NULL;
330
331                 k = mempcpy(r, f, a);
332         }
333
334         strcpy(k, e);
335         return r;
336 }
337
338 char *unit_name_template(const char *f) {
339         const char *p, *e;
340         char *r;
341         size_t a;
342
343         if (!(p = strchr(f, '@')))
344                 return strdup(f);
345
346         assert_se(e = strrchr(f, '.'));
347         a = p - f + 1;
348
349         if (!(r = new(char, a + strlen(e) + 1)))
350                 return NULL;
351
352         strcpy(mempcpy(r, f, a), e);
353         return r;
354
355 }
356
357 char *unit_name_from_path(const char *path, const char *suffix) {
358         char *p, *r;
359
360         assert(path);
361         assert(suffix);
362
363         if (!(p = strdup(path)))
364                 return NULL;
365
366         path_kill_slashes(p);
367
368         path = p[0] == '/' ? p + 1 : p;
369
370         if (path[0] == 0) {
371                 free(p);
372                 return strappend("-", suffix);
373         }
374
375         r = unit_name_build_escape(path, NULL, suffix);
376         free(p);
377
378         return r;
379 }
380
381 char *unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix) {
382         char *p, *r;
383
384         assert(path);
385         assert(suffix);
386
387         if (!(p = strdup(path)))
388                 return NULL;
389
390         path_kill_slashes(p);
391
392         path = p[0] == '/' ? p + 1 : p;
393
394         if (path[0] == 0) {
395                 free(p);
396                 return unit_name_build_escape(prefix, "-", suffix);
397         }
398
399         r = unit_name_build_escape(prefix, path, suffix);
400         free(p);
401
402         return r;
403 }
404
405 char *unit_name_to_path(const char *name) {
406         char *w, *e;
407
408         assert(name);
409
410         if (!(w = unit_name_to_prefix(name)))
411                 return NULL;
412
413         e = unit_name_unescape(w);
414         free(w);
415
416         if (!e)
417                 return NULL;
418
419         if (e[0] != '/') {
420                 w = strappend("/", e);
421                 free(e);
422
423                 if (!w)
424                         return NULL;
425
426                 e = w;
427         }
428
429         return e;
430 }
431
432 char *unit_name_path_unescape(const char *f) {
433         char *e;
434
435         assert(f);
436
437         if (!(e = unit_name_unescape(f)))
438                 return NULL;
439
440         if (e[0] != '/') {
441                 char *w;
442
443                 w = strappend("/", e);
444                 free(e);
445
446                 if (!w)
447                         return NULL;
448
449                 e = w;
450         }
451
452         return e;
453 }