summaryrefslogtreecommitdiff
path: root/plugins/ntpd.c
blob: 19e9828fd481e07c5f4173bbf86af9d7124e87cc (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#include <gdbus.h>

#define CONNMAN_API_SUBJECT_TO_CHANGE
#include <connman/types.h>
#include <connman/plugin.h>
#include <connman/task.h>
#include <connman/timeserver.h>
#include <connman/log.h>

/*
 * The peers list are the peers currently added to a running ntpd,
 * while pending_peers are the one appended but not used by ntpd yet.
 */
static GList *peers = NULL;
static GList *pending_peers = NULL;

#define NTPD_PORT 123

struct ntpd_peer {
	char *server;
	int refcount;
};

struct ntpdate_task {
	struct connman_task *task;
	gint conf_fd;
	char *conf_path;
};

static struct ntpd_peer *find_peer(GList *peer_list, const char* server)
{
	GList *list;
	struct ntpd_peer *peer;

	for (list = peer_list; list; list = list->next) {
		peer = list->data;

		if (g_str_equal(peer->server, server))
			return peer;
	}

	return NULL;
}

static void remove_peer(GList *peer_list, struct ntpd_peer *peer)
{
	if (__sync_fetch_and_sub(&peer->refcount, 1) != 1)
		return;

	g_free(peer->server);
	g_free(peer);
	peer_list = g_list_remove(peer_list, peer);
}

static connman_bool_t ntpd_running(void)
{
	int sock;
	connman_bool_t ret;
	struct sockaddr_in server_addr;

	if ((sock = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) == -1)
		return FALSE;

	server_addr.sin_family = AF_INET;
        server_addr.sin_port = htons(NTPD_PORT);
        server_addr.sin_addr.s_addr = INADDR_ANY;
        memset(&(server_addr.sin_zero), 0, 8);

	if (bind(sock, (struct sockaddr *)&server_addr,
			sizeof(struct sockaddr)) == -1) {
		if (errno == EADDRINUSE)
			ret = TRUE;
		else
			ret = FALSE;
        } else
		ret = FALSE;

	close(sock);

	return ret;
}

static void ntpdate_died(struct connman_task *task,
				int exit_code, void *user_data)
{
	struct ntpdate_task *ntpdate = user_data;

	DBG("");

	unlink(ntpdate->conf_path);
	g_free(ntpdate->conf_path);
	connman_task_destroy(ntpdate->task);
}

static void ntpdate_add_peer(struct ntpdate_task *ntpdate, char *peer)
{
	FILE *conf_file;

	DBG("%s", peer);

	conf_file = fdopen(ntpdate->conf_fd, "a+");
	if (conf_file == NULL) {
		connman_error("fdopen failed");
		return;
	}

	fprintf(conf_file, "server %s iburst\n", peer);

	fclose(conf_file);
}

static int ntpdate(void)
{
	int err;
	GError *g_err;
	GList *list;
	struct ntpd_peer *peer;
	struct ntpdate_task *ntpdate;

	DBG("");

	ntpdate = g_try_new0(struct ntpdate_task, 1);
	if (ntpdate == NULL)
		return -ENOMEM;

	/* ntpdate is deprecated, we use ntpd -q instead */
	ntpdate->task = connman_task_create(NTPD);
	if (ntpdate->task == NULL) {
		err = -ENOMEM;
		goto error_task;
	}

	connman_task_add_argument(ntpdate->task, "-g", NULL);
	connman_task_add_argument(ntpdate->task, "-q", NULL);

	/* The servers are added through a temp configuration file */
	ntpdate->conf_fd = g_file_open_tmp("connman.ntp.conf_XXXXXX",
						&ntpdate->conf_path, &g_err);
	if  (ntpdate->conf_fd == -1) {
		err = g_err->code;
		g_free(g_err);
		goto error_open;
	}

	connman_task_add_argument(ntpdate->task, "-c", ntpdate->conf_path);

	DBG("conf path %s", ntpdate->conf_path);

	for (list = pending_peers; list; list = list->next) {
		peer = list->data;

		ntpdate_add_peer(ntpdate, peer->server);
	}

	for (list = peers; list; list = list->next) {
		peer = list->data;

		ntpdate_add_peer(ntpdate, peer->server);
	}

	close(ntpdate->conf_fd);

	return connman_task_run(ntpdate->task, ntpdate_died, ntpdate,
						NULL, NULL, NULL);
error_open:
	connman_task_destroy(ntpdate->task);

error_task:
	g_free(ntpdate);

	return err;
}

static int ntpd_add_peer(char *peer)
{
	DBG("%s", peer);

	return 0;
}

static void ntpd_sync(void)
{
	int err;
	GList *list;

	DBG("");

	if (g_list_length(pending_peers) == 0 &&
			g_list_length(peers) == 0)
		return;

	if (!ntpd_running()) {
		ntpdate();
		return;
	}

	/* TODO Grab ntp keys path */

	list = g_list_first(pending_peers);
	while(list) {
		struct ntpd_peer *peer = list->data;

		err = ntpd_add_peer(peer->server);
		if (err)
			continue;

		peers = g_list_prepend(peers, peer);

		list = g_list_next(list);

		pending_peers = g_list_remove(pending_peers, peer);
	};
}

static int ntpd_append(const char *server)
{
	struct ntpd_peer *peer;

	DBG("");

	if (server == NULL)
		return 0;

	if ((peer = find_peer(pending_peers, server)) ||
			(peer = find_peer(peers, server))) {
		__sync_fetch_and_add(&peer->refcount, 1);
		return 0;
	}

	peer = g_try_new0(struct ntpd_peer, 1);
	if (peer == NULL)
		return -ENOMEM;

	peer->server = g_strdup(server);
	if (peer->server == NULL) {
		g_free(peer);
		return -ENOMEM;
	}

	peer->refcount = 1;

	pending_peers = g_list_prepend(pending_peers, peer);

	return 0;
}

static int ntpd_remove(const char *server)
{
	struct ntpd_peer *peer;

	DBG("");

	if (server == NULL)
		return 0;

	peer = find_peer(peers, server);
	if (peer == NULL)
		goto remove;

	remove_peer(peers, peer);

remove:
	/* TODO: send ntpd remove command */

	peer = find_peer(pending_peers, server);
	if (peer == NULL)
		return 0;

	remove_peer(pending_peers, peer);

	return 0;
}

static struct connman_timeserver_driver ntpd_driver = {
	.name		= "ntpd",
	.priority	= CONNMAN_TIMESERVER_PRIORITY_DEFAULT,
	.append		= ntpd_append,
	.remove		= ntpd_remove,
	.sync		= ntpd_sync,
};

static int ntpd_init(void)
{
	return connman_timeserver_driver_register(&ntpd_driver);
}

static void ntpd_exit(void)
{
	connman_timeserver_driver_unregister(&ntpd_driver);
}

CONNMAN_PLUGIN_DEFINE(ntpd, "ntpd plugin", VERSION,
		CONNMAN_PLUGIN_PRIORITY_DEFAULT, ntpd_init, ntpd_exit)