1 /*
2  * Copyright (C) 2003 Bastien Nocera <hadess@hadess.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  */
19 
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <errno.h>
30 #include "bacon-message-connection.h"
31 
32 #ifndef UNIX_PATH_MAX
33 #define UNIX_PATH_MAX 108
34 #endif
35 
36 struct BaconMessageConnection {
37 	/* A server accepts connections */
38 	gboolean is_server;
39 
40 	/* The socket path itself */
41 	char *path;
42 
43 	/* File descriptor of the socket */
44 	int fd;
45 	/* Channel to watch */
46 	GIOChannel *chan;
47 	/* Event id returned by g_io_add_watch() */
48 	int conn_id;
49 
50 	/* Connections accepted by this connection */
51 	GSList *accepted_connections;
52 
53 	/* callback */
54 	void (*func) (const char *message, gpointer user_data);
55 	gpointer data;
56 };
57 
58 static gboolean
test_is_socket(const char * path)59 test_is_socket (const char *path)
60 {
61 	struct stat s;
62 
63 	if (stat (path, &s) == -1)
64 		return FALSE;
65 
66 	if (S_ISSOCK (s.st_mode))
67 		return TRUE;
68 
69 	return FALSE;
70 }
71 
72 static gboolean
is_owned_by_user_and_socket(const char * path)73 is_owned_by_user_and_socket (const char *path)
74 {
75 	struct stat s;
76 
77 	if (stat (path, &s) == -1)
78 		return FALSE;
79 
80 	if (s.st_uid != geteuid ())
81 		return FALSE;
82 
83 	if ((s.st_mode & S_IFSOCK) != S_IFSOCK)
84 		return FALSE;
85 
86 	return TRUE;
87 }
88 
89 static gboolean server_cb (GIOChannel *source,
90 			   GIOCondition condition, gpointer data);
91 
92 static gboolean
setup_connection(BaconMessageConnection * conn)93 setup_connection (BaconMessageConnection *conn)
94 {
95 	g_return_val_if_fail (conn->chan == NULL, FALSE);
96 
97 	conn->chan = g_io_channel_unix_new (conn->fd);
98 	if (!conn->chan) {
99 		return FALSE;
100 	}
101 	g_io_channel_set_line_term (conn->chan, "\n", 1);
102 	conn->conn_id = g_io_add_watch (conn->chan, G_IO_IN, server_cb, conn);
103 
104 	return TRUE;
105 }
106 
107 static void
accept_new_connection(BaconMessageConnection * server_conn)108 accept_new_connection (BaconMessageConnection *server_conn)
109 {
110 	BaconMessageConnection *conn;
111 	int alen;
112 
113 	g_return_if_fail (server_conn->is_server);
114 
115 	conn = g_new0 (BaconMessageConnection, 1);
116 	conn->is_server = FALSE;
117 	conn->func = server_conn->func;
118 	conn->data = server_conn->data;
119 
120 	conn->fd = accept (server_conn->fd, NULL, (guint *)&alen);
121 
122 	server_conn->accepted_connections =
123 		g_slist_prepend (server_conn->accepted_connections, conn);
124 
125 	setup_connection (conn);
126 }
127 
128 static gboolean
server_cb(GIOChannel * source,GIOCondition condition,gpointer data)129 server_cb (GIOChannel *source, GIOCondition condition, gpointer data)
130 {
131 	BaconMessageConnection *conn = (BaconMessageConnection *)data;
132 	char *message, *subs, buf;
133 	int cd, rc, offset;
134 	gboolean finished;
135 
136 	offset = 0;
137 	if (conn->is_server && conn->fd == g_io_channel_unix_get_fd (source)) {
138 		accept_new_connection (conn);
139 		return TRUE;
140 	}
141 	message = g_malloc (1);
142 	cd = conn->fd;
143 	rc = read (cd, &buf, 1);
144 	while (rc > 0 && buf != '\n')
145 	{
146 		message = g_realloc (message, rc + offset + 1);
147 		message[offset] = buf;
148 		offset = offset + rc;
149 		rc = read (cd, &buf, 1);
150 	}
151 	if (rc <= 0) {
152 		g_io_channel_shutdown (conn->chan, FALSE, NULL);
153 		g_io_channel_unref (conn->chan);
154 		conn->chan = NULL;
155 		close (conn->fd);
156 		conn->fd = -1;
157 		g_free (message);
158 		conn->conn_id = 0;
159 
160 		return FALSE;
161 	}
162 	message[offset] = '\0';
163 
164 	subs = message;
165 	finished = FALSE;
166 
167 	while (finished == FALSE && *subs != '\0')
168 	{
169 		if (conn->func)
170 			(*conn->func) (subs, conn->data);
171 
172 		subs += strlen (subs) + 1;
173 		if (subs - message >= offset)
174 			finished = TRUE;
175 	}
176 
177 	g_free (message);
178 
179 	return TRUE;
180 }
181 
182 static char *
find_file_with_pattern(const char * dir,const char * pattern)183 find_file_with_pattern (const char *dir, const char *pattern)
184 {
185 	GDir *filedir;
186 	char *found_filename;
187 	const char *filename;
188 	GPatternSpec *pat;
189 
190 	filedir = g_dir_open (dir, 0, NULL);
191 	if (!filedir)
192 		return NULL;
193 
194 	pat = g_pattern_spec_new (pattern);
195 	if (!pat)
196 	{
197 		g_dir_close (filedir);
198 		return NULL;
199 	}
200 
201 	found_filename = NULL;
202 
203 	while ((filename = g_dir_read_name (filedir)))
204 	{
205 		if (g_pattern_match_string (pat, filename))
206 		{
207 			char *tmp = g_build_filename (dir, filename, NULL);
208 			if (is_owned_by_user_and_socket (tmp))
209 				found_filename = g_strdup (filename);
210 			g_free (tmp);
211 		}
212 
213 		if (found_filename)
214 			break;
215 	}
216 
217 	g_pattern_spec_free (pat);
218 	g_dir_close (filedir);
219 
220 	return found_filename;
221 }
222 
223 static char *
socket_filename(const char * prefix)224 socket_filename (const char *prefix)
225 {
226 	char *pattern, *newfile, *path, *filename;
227 	const char *tmpdir;
228 
229 	pattern = g_strdup_printf ("%s.%s.*", prefix, g_get_user_name ());
230 	tmpdir = g_get_tmp_dir ();
231 	filename = find_file_with_pattern (tmpdir, pattern);
232 	if (!filename)
233 	{
234 		newfile = g_strdup_printf ("%s.%s.%u", prefix,
235 				g_get_user_name (), g_random_int ());
236 		path = g_build_filename (tmpdir, newfile, NULL);
237 		g_free (newfile);
238 	} else {
239 		path = g_build_filename (tmpdir, filename, NULL);
240 		g_free (filename);
241 	}
242 
243 	g_free (pattern);
244 	return path;
245 }
246 
247 static gboolean
try_server(BaconMessageConnection * conn)248 try_server (BaconMessageConnection *conn)
249 {
250 	struct sockaddr_un uaddr;
251 
252 	uaddr.sun_family = AF_UNIX;
253 	strncpy (uaddr.sun_path, conn->path,
254 			MIN (strlen(conn->path)+1, UNIX_PATH_MAX));
255 	conn->fd = socket (PF_UNIX, SOCK_STREAM, 0);
256 	if (bind (conn->fd, (struct sockaddr *) &uaddr, sizeof (uaddr)) == -1)
257 	{
258 		conn->fd = -1;
259 		return FALSE;
260 	}
261 	listen (conn->fd, 5);
262 
263 	if (!setup_connection (conn))
264 		return FALSE;
265 	return TRUE;
266 }
267 
268 static gboolean
try_client(BaconMessageConnection * conn)269 try_client (BaconMessageConnection *conn)
270 {
271 	struct sockaddr_un uaddr;
272 
273 	uaddr.sun_family = AF_UNIX;
274 	strncpy (uaddr.sun_path, conn->path,
275 			MIN(strlen(conn->path)+1, UNIX_PATH_MAX));
276 	conn->fd = socket (PF_UNIX, SOCK_STREAM, 0);
277 	if (connect (conn->fd, (struct sockaddr *) &uaddr,
278 				sizeof (uaddr)) == -1)
279 	{
280 		conn->fd = -1;
281 		return FALSE;
282 	}
283 
284 	return setup_connection (conn);
285 }
286 
287 BaconMessageConnection *
bacon_message_connection_new(const char * prefix)288 bacon_message_connection_new (const char *prefix)
289 {
290 	BaconMessageConnection *conn;
291 
292 	g_return_val_if_fail (prefix != NULL, NULL);
293 
294 	conn = g_new0 (BaconMessageConnection, 1);
295 	conn->path = socket_filename (prefix);
296 
297 	if (test_is_socket (conn->path) == FALSE)
298 	{
299 		if (!try_server (conn))
300 		{
301 			bacon_message_connection_free (conn);
302 			return NULL;
303 		}
304 
305 		conn->is_server = TRUE;
306 		return conn;
307 	}
308 
309 	if (try_client (conn) == FALSE)
310 	{
311 		unlink (conn->path);
312 		try_server (conn);
313 		if (conn->fd == -1)
314 		{
315 			bacon_message_connection_free (conn);
316 			return NULL;
317 		}
318 
319 		conn->is_server = TRUE;
320 		return conn;
321 	}
322 
323 	conn->is_server = FALSE;
324 	return conn;
325 }
326 
327 void
bacon_message_connection_free(BaconMessageConnection * conn)328 bacon_message_connection_free (BaconMessageConnection *conn)
329 {
330 	GSList *child_conn;
331 
332 	g_return_if_fail (conn != NULL);
333 	/* Only servers can accept other connections */
334 	g_return_if_fail (conn->is_server != FALSE || !conn->accepted_connections);
335 
336 	child_conn = conn->accepted_connections;
337 	while (child_conn) {
338 		bacon_message_connection_free (child_conn->data);
339 		child_conn = g_slist_next (child_conn);
340 	}
341 	g_slist_free (conn->accepted_connections);
342 
343 	if (conn->conn_id) {
344 		g_source_remove (conn->conn_id);
345 		conn->conn_id = 0;
346 	}
347 	if (conn->chan) {
348 		g_io_channel_shutdown (conn->chan, FALSE, NULL);
349 		g_io_channel_unref (conn->chan);
350 	}
351 
352 	if (conn->is_server != FALSE) {
353 		unlink (conn->path);
354 	}
355 	if (conn->fd != -1) {
356 		close (conn->fd);
357 	}
358 
359 	g_free (conn->path);
360 	g_free (conn);
361 }
362 
363 void
bacon_message_connection_set_callback(BaconMessageConnection * conn,BaconMessageReceivedFunc func,gpointer user_data)364 bacon_message_connection_set_callback (BaconMessageConnection *conn,
365 				       BaconMessageReceivedFunc func,
366 				       gpointer user_data)
367 {
368 	g_return_if_fail (conn != NULL);
369 
370 	conn->func = func;
371 	conn->data = user_data;
372 }
373 
374 void
bacon_message_connection_send(BaconMessageConnection * conn,const char * message)375 bacon_message_connection_send (BaconMessageConnection *conn,
376 			       const char *message)
377 {
378 	g_return_if_fail (conn != NULL);
379 	g_return_if_fail (message != NULL);
380 
381 	g_io_channel_write_chars (conn->chan, message, strlen (message),
382 				  NULL, NULL);
383 	g_io_channel_write_chars (conn->chan, "\n", 1, NULL, NULL);
384 	g_io_channel_flush (conn->chan, NULL);
385 }
386 
387 gboolean
bacon_message_connection_get_is_server(BaconMessageConnection * conn)388 bacon_message_connection_get_is_server (BaconMessageConnection *conn)
389 {
390 	g_return_val_if_fail (conn != NULL, FALSE);
391 
392 	return conn->is_server;
393 }
394 
395