chiark / gitweb /
sd-bus: avoid a null dereference
[elogind.git] / src / timesync / timesyncd-conf.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2014 Kay Sievers, 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 "in-addr-util.h"
23
24 #include "timesyncd-manager.h"
25 #include "timesyncd-server.h"
26 #include "timesyncd-conf.h"
27
28 int manager_parse_server_string(Manager *m, ServerType type, const char *string) {
29         const char *word, *state;
30         size_t length;
31         ServerName *first;
32         int r;
33
34         assert(m);
35         assert(string);
36
37         first = type == SERVER_FALLBACK ? m->fallback_servers : m->system_servers;
38
39         FOREACH_WORD_QUOTED(word, length, string, state) {
40                 char buffer[length+1];
41                 bool found = false;
42                 ServerName *n;
43
44                 memcpy(buffer, word, length);
45                 buffer[length] = 0;
46
47                 /* Filter out duplicates */
48                 LIST_FOREACH(names, n, first)
49                         if (streq_ptr(n->string, buffer)) {
50                                 found = true;
51                                 break;
52                         }
53
54                 if (found)
55                         continue;
56
57                 r = server_name_new(m, NULL, type, buffer);
58                 if (r < 0)
59                         return r;
60         }
61
62         return 0;
63 }
64
65 int config_parse_servers(
66                 const char *unit,
67                 const char *filename,
68                 unsigned line,
69                 const char *section,
70                 unsigned section_line,
71                 const char *lvalue,
72                 int ltype,
73                 const char *rvalue,
74                 void *data,
75                 void *userdata) {
76
77         Manager *m = userdata;
78         int r;
79
80         assert(filename);
81         assert(lvalue);
82         assert(rvalue);
83
84         if (isempty(rvalue))
85                 manager_flush_server_names(m, ltype);
86         else {
87                 r = manager_parse_server_string(m, ltype, rvalue);
88                 if (r < 0) {
89                         log_syntax(unit, LOG_ERR, filename, line, -r, "Failed to parse NTP server string '%s'. Ignoring.", rvalue);
90                         return 0;
91                 }
92         }
93
94         return 0;
95 }
96
97 int manager_parse_config_file(Manager *m) {
98         assert(m);
99
100         return config_parse_many("/etc/systemd/timesyncd.conf",
101                                  CONF_DIRS_NULSTR("systemd/timesyncd.conf"),
102                                  "Time\0",
103                                  config_item_perf_lookup, timesyncd_gperf_lookup,
104                                  false, m);
105 }