]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.c
core/load-fragment: refuse units with errors in RootDirectory/RootImage/DynamicUser
[thirdparty/systemd.git] / src / shared / conf-parser.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27
28 #include "alloc-util.h"
29 #include "conf-files.h"
30 #include "conf-parser.h"
31 #include "extract-word.h"
32 #include "fd-util.h"
33 #include "fs-util.h"
34 #include "log.h"
35 #include "macro.h"
36 #include "parse-util.h"
37 #include "path-util.h"
38 #include "process-util.h"
39 #include "signal-util.h"
40 #include "socket-util.h"
41 #include "string-util.h"
42 #include "strv.h"
43 #include "syslog-util.h"
44 #include "time-util.h"
45 #include "utf8.h"
46
47 int config_item_table_lookup(
48 const void *table,
49 const char *section,
50 const char *lvalue,
51 ConfigParserCallback *func,
52 int *ltype,
53 void **data,
54 void *userdata) {
55
56 const ConfigTableItem *t;
57
58 assert(table);
59 assert(lvalue);
60 assert(func);
61 assert(ltype);
62 assert(data);
63
64 for (t = table; t->lvalue; t++) {
65
66 if (!streq(lvalue, t->lvalue))
67 continue;
68
69 if (!streq_ptr(section, t->section))
70 continue;
71
72 *func = t->parse;
73 *ltype = t->ltype;
74 *data = t->data;
75 return 1;
76 }
77
78 return 0;
79 }
80
81 int config_item_perf_lookup(
82 const void *table,
83 const char *section,
84 const char *lvalue,
85 ConfigParserCallback *func,
86 int *ltype,
87 void **data,
88 void *userdata) {
89
90 ConfigPerfItemLookup lookup = (ConfigPerfItemLookup) table;
91 const ConfigPerfItem *p;
92
93 assert(table);
94 assert(lvalue);
95 assert(func);
96 assert(ltype);
97 assert(data);
98
99 if (!section)
100 p = lookup(lvalue, strlen(lvalue));
101 else {
102 char *key;
103
104 key = strjoin(section, ".", lvalue);
105 if (!key)
106 return -ENOMEM;
107
108 p = lookup(key, strlen(key));
109 free(key);
110 }
111
112 if (!p)
113 return 0;
114
115 *func = p->parse;
116 *ltype = p->ltype;
117 *data = (uint8_t*) userdata + p->offset;
118 return 1;
119 }
120
121 /* Run the user supplied parser for an assignment */
122 static int next_assignment(const char *unit,
123 const char *filename,
124 unsigned line,
125 ConfigItemLookup lookup,
126 const void *table,
127 const char *section,
128 unsigned section_line,
129 const char *lvalue,
130 const char *rvalue,
131 bool relaxed,
132 void *userdata) {
133
134 ConfigParserCallback func = NULL;
135 int ltype = 0;
136 void *data = NULL;
137 int r;
138
139 assert(filename);
140 assert(line > 0);
141 assert(lookup);
142 assert(lvalue);
143 assert(rvalue);
144
145 r = lookup(table, section, lvalue, &func, &ltype, &data, userdata);
146 if (r < 0)
147 return r;
148
149 if (r > 0) {
150 if (func)
151 return func(unit, filename, line, section, section_line,
152 lvalue, ltype, rvalue, data, userdata);
153
154 return 0;
155 }
156
157 /* Warn about unknown non-extension fields. */
158 if (!relaxed && !startswith(lvalue, "X-"))
159 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
160
161 return 0;
162 }
163
164 /* Parse a variable assignment line */
165 static int parse_line(const char* unit,
166 const char *filename,
167 unsigned line,
168 const char *sections,
169 ConfigItemLookup lookup,
170 const void *table,
171 bool relaxed,
172 bool allow_include,
173 char **section,
174 unsigned *section_line,
175 bool *section_ignored,
176 char *l,
177 void *userdata) {
178
179 char *e;
180
181 assert(filename);
182 assert(line > 0);
183 assert(lookup);
184 assert(l);
185
186 l = strstrip(l);
187
188 if (!*l)
189 return 0;
190
191 if (strchr(COMMENTS "\n", *l))
192 return 0;
193
194 if (startswith(l, ".include ")) {
195 _cleanup_free_ char *fn = NULL;
196
197 /* .includes are a bad idea, we only support them here
198 * for historical reasons. They create cyclic include
199 * problems and make it difficult to detect
200 * configuration file changes with an easy
201 * stat(). Better approaches, such as .d/ drop-in
202 * snippets exist.
203 *
204 * Support for them should be eventually removed. */
205
206 if (!allow_include) {
207 log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
208 return 0;
209 }
210
211 fn = file_in_same_dir(filename, strstrip(l+9));
212 if (!fn)
213 return -ENOMEM;
214
215 return config_parse(unit, fn, NULL, sections, lookup, table, relaxed, false, false, userdata);
216 }
217
218 if (*l == '[') {
219 size_t k;
220 char *n;
221
222 k = strlen(l);
223 assert(k > 0);
224
225 if (l[k-1] != ']') {
226 log_syntax(unit, LOG_ERR, filename, line, 0, "Invalid section header '%s'", l);
227 return -EBADMSG;
228 }
229
230 n = strndup(l+1, k-2);
231 if (!n)
232 return -ENOMEM;
233
234 if (sections && !nulstr_contains(sections, n)) {
235
236 if (!relaxed && !startswith(n, "X-"))
237 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
238
239 free(n);
240 *section = mfree(*section);
241 *section_line = 0;
242 *section_ignored = true;
243 } else {
244 free(*section);
245 *section = n;
246 *section_line = line;
247 *section_ignored = false;
248 }
249
250 return 0;
251 }
252
253 if (sections && !*section) {
254
255 if (!relaxed && !*section_ignored)
256 log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
257
258 return 0;
259 }
260
261 e = strchr(l, '=');
262 if (!e) {
263 log_syntax(unit, LOG_WARNING, filename, line, 0, "Missing '='.");
264 return -EINVAL;
265 }
266
267 *e = 0;
268 e++;
269
270 return next_assignment(unit,
271 filename,
272 line,
273 lookup,
274 table,
275 *section,
276 *section_line,
277 strstrip(l),
278 strstrip(e),
279 relaxed,
280 userdata);
281 }
282
283 /* Go through the file and parse each line */
284 int config_parse(const char *unit,
285 const char *filename,
286 FILE *f,
287 const char *sections,
288 ConfigItemLookup lookup,
289 const void *table,
290 bool relaxed,
291 bool allow_include,
292 bool warn,
293 void *userdata) {
294
295 _cleanup_free_ char *section = NULL, *continuation = NULL;
296 _cleanup_fclose_ FILE *ours = NULL;
297 unsigned line = 0, section_line = 0;
298 bool section_ignored = false, allow_bom = true;
299 int r;
300
301 assert(filename);
302 assert(lookup);
303
304 if (!f) {
305 f = ours = fopen(filename, "re");
306 if (!f) {
307 /* Only log on request, except for ENOENT,
308 * since we return 0 to the caller. */
309 if (warn || errno == ENOENT)
310 log_full(errno == ENOENT ? LOG_DEBUG : LOG_ERR,
311 "Failed to open configuration file '%s': %m", filename);
312 return errno == ENOENT ? 0 : -errno;
313 }
314 }
315
316 fd_warn_permissions(filename, fileno(f));
317
318 for (;;) {
319 char buf[LINE_MAX], *l, *p, *c = NULL, *e;
320 bool escaped = false;
321
322 if (!fgets(buf, sizeof buf, f)) {
323 if (feof(f))
324 break;
325
326 return log_error_errno(errno, "Failed to read configuration file '%s': %m", filename);
327 }
328
329 l = buf;
330 if (allow_bom && startswith(l, UTF8_BYTE_ORDER_MARK))
331 l += strlen(UTF8_BYTE_ORDER_MARK);
332 allow_bom = false;
333
334 truncate_nl(l);
335
336 if (continuation) {
337 c = strappend(continuation, l);
338 if (!c) {
339 if (warn)
340 log_oom();
341 return -ENOMEM;
342 }
343
344 continuation = mfree(continuation);
345 p = c;
346 } else
347 p = l;
348
349 for (e = p; *e; e++) {
350 if (escaped)
351 escaped = false;
352 else if (*e == '\\')
353 escaped = true;
354 }
355
356 if (escaped) {
357 *(e-1) = ' ';
358
359 if (c)
360 continuation = c;
361 else {
362 continuation = strdup(l);
363 if (!continuation) {
364 if (warn)
365 log_oom();
366 return -ENOMEM;
367 }
368 }
369
370 continue;
371 }
372
373 r = parse_line(unit,
374 filename,
375 ++line,
376 sections,
377 lookup,
378 table,
379 relaxed,
380 allow_include,
381 &section,
382 &section_line,
383 &section_ignored,
384 p,
385 userdata);
386 free(c);
387
388 if (r < 0) {
389 if (warn)
390 log_warning_errno(r, "Failed to parse file '%s': %m",
391 filename);
392 return r;
393 }
394 }
395
396 return 0;
397 }
398
399 static int config_parse_many_files(
400 const char *conf_file,
401 char **files,
402 const char *sections,
403 ConfigItemLookup lookup,
404 const void *table,
405 bool relaxed,
406 void *userdata) {
407
408 char **fn;
409 int r;
410
411 if (conf_file) {
412 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, relaxed, false, true, userdata);
413 if (r < 0)
414 return r;
415 }
416
417 STRV_FOREACH(fn, files) {
418 r = config_parse(NULL, *fn, NULL, sections, lookup, table, relaxed, false, true, userdata);
419 if (r < 0)
420 return r;
421 }
422
423 return 0;
424 }
425
426 /* Parse each config file in the directories specified as nulstr. */
427 int config_parse_many_nulstr(
428 const char *conf_file,
429 const char *conf_file_dirs,
430 const char *sections,
431 ConfigItemLookup lookup,
432 const void *table,
433 bool relaxed,
434 void *userdata) {
435
436 _cleanup_strv_free_ char **files = NULL;
437 int r;
438
439 r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
440 if (r < 0)
441 return r;
442
443 return config_parse_many_files(conf_file, files,
444 sections, lookup, table, relaxed, userdata);
445 }
446
447 /* Parse each config file in the directories specified as strv. */
448 int config_parse_many(
449 const char *conf_file,
450 const char* const* conf_file_dirs,
451 const char *dropin_dirname,
452 const char *sections,
453 ConfigItemLookup lookup,
454 const void *table,
455 bool relaxed,
456 void *userdata) {
457
458 _cleanup_strv_free_ char **dropin_dirs = NULL;
459 _cleanup_strv_free_ char **files = NULL;
460 const char *suffix;
461 int r;
462
463 suffix = strjoina("/", dropin_dirname);
464 r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
465 if (r < 0)
466 return r;
467
468 r = conf_files_list_strv(&files, ".conf", NULL, (const char* const*) dropin_dirs);
469 if (r < 0)
470 return r;
471
472 return config_parse_many_files(conf_file, files,
473 sections, lookup, table, relaxed, userdata);
474 }
475
476 #define DEFINE_PARSER(type, vartype, conv_func) \
477 int config_parse_##type( \
478 const char *unit, \
479 const char *filename, \
480 unsigned line, \
481 const char *section, \
482 unsigned section_line, \
483 const char *lvalue, \
484 int ltype, \
485 const char *rvalue, \
486 void *data, \
487 void *userdata) { \
488 \
489 vartype *i = data; \
490 int r; \
491 \
492 assert(filename); \
493 assert(lvalue); \
494 assert(rvalue); \
495 assert(data); \
496 \
497 r = conv_func(rvalue, i); \
498 if (r < 0) \
499 log_syntax(unit, LOG_ERR, filename, line, r, \
500 "Failed to parse %s value, ignoring: %s", \
501 #type, rvalue); \
502 \
503 return 0; \
504 } \
505 struct __useless_struct_to_allow_trailing_semicolon__
506
507 DEFINE_PARSER(int, int, safe_atoi);
508 DEFINE_PARSER(long, long, safe_atoli);
509 DEFINE_PARSER(uint8, uint8_t, safe_atou8);
510 DEFINE_PARSER(uint16, uint16_t, safe_atou16);
511 DEFINE_PARSER(uint32, uint32_t, safe_atou32);
512 DEFINE_PARSER(uint64, uint64_t, safe_atou64);
513 DEFINE_PARSER(unsigned, unsigned, safe_atou);
514 DEFINE_PARSER(double, double, safe_atod);
515 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
516 DEFINE_PARSER(sec, usec_t, parse_sec);
517 DEFINE_PARSER(mode, mode_t, parse_mode);
518
519 int config_parse_iec_size(const char* unit,
520 const char *filename,
521 unsigned line,
522 const char *section,
523 unsigned section_line,
524 const char *lvalue,
525 int ltype,
526 const char *rvalue,
527 void *data,
528 void *userdata) {
529
530 size_t *sz = data;
531 uint64_t v;
532 int r;
533
534 assert(filename);
535 assert(lvalue);
536 assert(rvalue);
537 assert(data);
538
539 r = parse_size(rvalue, 1024, &v);
540 if (r < 0 || (uint64_t) (size_t) v != v) {
541 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
542 return 0;
543 }
544
545 *sz = (size_t) v;
546 return 0;
547 }
548
549 int config_parse_si_size(const char* unit,
550 const char *filename,
551 unsigned line,
552 const char *section,
553 unsigned section_line,
554 const char *lvalue,
555 int ltype,
556 const char *rvalue,
557 void *data,
558 void *userdata) {
559
560 size_t *sz = data;
561 uint64_t v;
562 int r;
563
564 assert(filename);
565 assert(lvalue);
566 assert(rvalue);
567 assert(data);
568
569 r = parse_size(rvalue, 1000, &v);
570 if (r < 0 || (uint64_t) (size_t) v != v) {
571 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
572 return 0;
573 }
574
575 *sz = (size_t) v;
576 return 0;
577 }
578
579 int config_parse_iec_uint64(const char* unit,
580 const char *filename,
581 unsigned line,
582 const char *section,
583 unsigned section_line,
584 const char *lvalue,
585 int ltype,
586 const char *rvalue,
587 void *data,
588 void *userdata) {
589
590 uint64_t *bytes = data;
591 int r;
592
593 assert(filename);
594 assert(lvalue);
595 assert(rvalue);
596 assert(data);
597
598 r = parse_size(rvalue, 1024, bytes);
599 if (r < 0)
600 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse size value, ignoring: %s", rvalue);
601
602 return 0;
603 }
604
605 int config_parse_bool(const char* unit,
606 const char *filename,
607 unsigned line,
608 const char *section,
609 unsigned section_line,
610 const char *lvalue,
611 int ltype,
612 const char *rvalue,
613 void *data,
614 void *userdata) {
615
616 int k;
617 bool *b = data;
618 bool fatal = ltype;
619
620 assert(filename);
621 assert(lvalue);
622 assert(rvalue);
623 assert(data);
624
625 k = parse_boolean(rvalue);
626 if (k < 0) {
627 log_syntax(unit, LOG_ERR, filename, line, k,
628 "Failed to parse boolean value%s: %s",
629 fatal ? "" : ", ignoring", rvalue);
630 return fatal ? -ENOEXEC : 0;
631 }
632
633 *b = !!k;
634 return 0;
635 }
636
637 int config_parse_tristate(
638 const char* unit,
639 const char *filename,
640 unsigned line,
641 const char *section,
642 unsigned section_line,
643 const char *lvalue,
644 int ltype,
645 const char *rvalue,
646 void *data,
647 void *userdata) {
648
649 int k, *t = data;
650
651 assert(filename);
652 assert(lvalue);
653 assert(rvalue);
654 assert(data);
655
656 /* A tristate is pretty much a boolean, except that it can
657 * also take the special value -1, indicating "uninitialized",
658 * much like NULL is for a pointer type. */
659
660 k = parse_boolean(rvalue);
661 if (k < 0) {
662 log_syntax(unit, LOG_ERR, filename, line, k, "Failed to parse boolean value, ignoring: %s", rvalue);
663 return 0;
664 }
665
666 *t = !!k;
667 return 0;
668 }
669
670 int config_parse_string(
671 const char *unit,
672 const char *filename,
673 unsigned line,
674 const char *section,
675 unsigned section_line,
676 const char *lvalue,
677 int ltype,
678 const char *rvalue,
679 void *data,
680 void *userdata) {
681
682 char **s = data, *n;
683
684 assert(filename);
685 assert(lvalue);
686 assert(rvalue);
687 assert(data);
688
689 if (!utf8_is_valid(rvalue)) {
690 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
691 return 0;
692 }
693
694 if (isempty(rvalue))
695 n = NULL;
696 else {
697 n = strdup(rvalue);
698 if (!n)
699 return log_oom();
700 }
701
702 free(*s);
703 *s = n;
704
705 return 0;
706 }
707
708 int config_parse_path(
709 const char *unit,
710 const char *filename,
711 unsigned line,
712 const char *section,
713 unsigned section_line,
714 const char *lvalue,
715 int ltype,
716 const char *rvalue,
717 void *data,
718 void *userdata) {
719
720 char **s = data, *n;
721 bool fatal = ltype;
722
723 assert(filename);
724 assert(lvalue);
725 assert(rvalue);
726 assert(data);
727
728 if (!utf8_is_valid(rvalue)) {
729 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, rvalue);
730 return fatal ? -ENOEXEC : 0;
731 }
732
733 if (!path_is_absolute(rvalue)) {
734 log_syntax(unit, LOG_ERR, filename, line, 0,
735 "Not an absolute path%s: %s",
736 fatal ? "" : ", ignoring", rvalue);
737 return fatal ? -ENOEXEC : 0;
738 }
739
740 n = strdup(rvalue);
741 if (!n)
742 return log_oom();
743
744 path_kill_slashes(n);
745
746 free(*s);
747 *s = n;
748
749 return 0;
750 }
751
752 int config_parse_strv(const char *unit,
753 const char *filename,
754 unsigned line,
755 const char *section,
756 unsigned section_line,
757 const char *lvalue,
758 int ltype,
759 const char *rvalue,
760 void *data,
761 void *userdata) {
762
763 char ***sv = data;
764 int r;
765
766 assert(filename);
767 assert(lvalue);
768 assert(rvalue);
769 assert(data);
770
771 if (isempty(rvalue)) {
772 char **empty;
773
774 /* Empty assignment resets the list. As a special rule
775 * we actually fill in a real empty array here rather
776 * than NULL, since some code wants to know if
777 * something was set at all... */
778 empty = new0(char*, 1);
779 if (!empty)
780 return log_oom();
781
782 strv_free(*sv);
783 *sv = empty;
784
785 return 0;
786 }
787
788 for (;;) {
789 char *word = NULL;
790
791 r = extract_first_word(&rvalue, &word, NULL, EXTRACT_QUOTES|EXTRACT_RETAIN_ESCAPE);
792 if (r == 0)
793 break;
794 if (r == -ENOMEM)
795 return log_oom();
796 if (r < 0) {
797 log_syntax(unit, LOG_ERR, filename, line, r, "Invalid syntax, ignoring: %s", rvalue);
798 break;
799 }
800
801 if (!utf8_is_valid(word)) {
802 log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, word);
803 free(word);
804 continue;
805 }
806 r = strv_consume(sv, word);
807 if (r < 0)
808 return log_oom();
809 }
810
811 return 0;
812 }
813
814 int config_parse_log_facility(
815 const char *unit,
816 const char *filename,
817 unsigned line,
818 const char *section,
819 unsigned section_line,
820 const char *lvalue,
821 int ltype,
822 const char *rvalue,
823 void *data,
824 void *userdata) {
825
826
827 int *o = data, x;
828
829 assert(filename);
830 assert(lvalue);
831 assert(rvalue);
832 assert(data);
833
834 x = log_facility_unshifted_from_string(rvalue);
835 if (x < 0) {
836 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log facility, ignoring: %s", rvalue);
837 return 0;
838 }
839
840 *o = (x << 3) | LOG_PRI(*o);
841
842 return 0;
843 }
844
845 int config_parse_log_level(
846 const char *unit,
847 const char *filename,
848 unsigned line,
849 const char *section,
850 unsigned section_line,
851 const char *lvalue,
852 int ltype,
853 const char *rvalue,
854 void *data,
855 void *userdata) {
856
857
858 int *o = data, x;
859
860 assert(filename);
861 assert(lvalue);
862 assert(rvalue);
863 assert(data);
864
865 x = log_level_from_string(rvalue);
866 if (x < 0) {
867 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse log level, ignoring: %s", rvalue);
868 return 0;
869 }
870
871 *o = (*o & LOG_FACMASK) | x;
872 return 0;
873 }
874
875 int config_parse_signal(
876 const char *unit,
877 const char *filename,
878 unsigned line,
879 const char *section,
880 unsigned section_line,
881 const char *lvalue,
882 int ltype,
883 const char *rvalue,
884 void *data,
885 void *userdata) {
886
887 int *sig = data, r;
888
889 assert(filename);
890 assert(lvalue);
891 assert(rvalue);
892 assert(sig);
893
894 r = signal_from_string_try_harder(rvalue);
895 if (r <= 0) {
896 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse signal name, ignoring: %s", rvalue);
897 return 0;
898 }
899
900 *sig = r;
901 return 0;
902 }
903
904 int config_parse_personality(
905 const char *unit,
906 const char *filename,
907 unsigned line,
908 const char *section,
909 unsigned section_line,
910 const char *lvalue,
911 int ltype,
912 const char *rvalue,
913 void *data,
914 void *userdata) {
915
916 unsigned long *personality = data, p;
917
918 assert(filename);
919 assert(lvalue);
920 assert(rvalue);
921 assert(personality);
922
923 p = personality_from_string(rvalue);
924 if (p == PERSONALITY_INVALID) {
925 log_syntax(unit, LOG_ERR, filename, line, 0, "Failed to parse personality, ignoring: %s", rvalue);
926 return 0;
927 }
928
929 *personality = p;
930 return 0;
931 }
932
933 int config_parse_ifname(
934 const char *unit,
935 const char *filename,
936 unsigned line,
937 const char *section,
938 unsigned section_line,
939 const char *lvalue,
940 int ltype,
941 const char *rvalue,
942 void *data,
943 void *userdata) {
944
945 char **s = data;
946 int r;
947
948 assert(filename);
949 assert(lvalue);
950 assert(rvalue);
951 assert(data);
952
953 if (isempty(rvalue)) {
954 *s = mfree(*s);
955 return 0;
956 }
957
958 if (!ifname_valid(rvalue)) {
959 log_syntax(unit, LOG_ERR, filename, line, 0, "Interface name is not valid or too long, ignoring assignment: %s", rvalue);
960 return 0;
961 }
962
963 r = free_and_strdup(s, rvalue);
964 if (r < 0)
965 return log_oom();
966
967 return 0;
968 }
969
970 int config_parse_ip_port(
971 const char *unit,
972 const char *filename,
973 unsigned line,
974 const char *section,
975 unsigned section_line,
976 const char *lvalue,
977 int ltype,
978 const char *rvalue,
979 void *data,
980 void *userdata) {
981
982 uint16_t *s = data;
983 uint16_t port;
984 int r;
985
986 assert(filename);
987 assert(lvalue);
988 assert(rvalue);
989 assert(data);
990
991 if (isempty(rvalue)) {
992 *s = 0;
993 return 0;
994 }
995
996 r = parse_ip_port(rvalue, &port);
997 if (r < 0) {
998 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse port '%s'.", rvalue);
999 return 0;
1000 }
1001
1002 *s = port;
1003
1004 return 0;
1005 }