summaryrefslogtreecommitdiff
path: root/lib/gibber/gibber-unix-transport.c
blob: cb18787c2ae3abaf56f89ff7d77d831aa2fcaf4f (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
 * gibber-linklocal-transport.c - Source for GibberLLTransport
 * Copyright (C) 2006, 2008 Collabora Ltd.
 *   @author: Sjoerd Simons <sjoerd@luon.net>
 *   @author: Alban Crequy <alban.crequy@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/* needed for struct ucred */
#define _GNU_SOURCE

#include "config.h"
#include "gibber-unix-transport.h"

#include <glib.h>

#ifdef G_OS_UNIX

/* If you claim to be Unix but you don't have these headers, you may have
 * already lost. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>

#include "gibber-util.h"

#define DEBUG_FLAG DEBUG_NET
#include "gibber-debug.h"

G_DEFINE_TYPE(GibberUnixTransport, gibber_unix_transport, \
    GIBBER_TYPE_FD_TRANSPORT)

GQuark
gibber_unix_transport_error_quark (void)
{
  static GQuark quark = 0;

  if (!quark)
    quark = g_quark_from_static_string ("gibber_unix_transport_error");

  return quark;
}

/* private structure */
typedef struct _GibberUnixTransportPrivate GibberUnixTransportPrivate;

struct _GibberUnixTransportPrivate
{
  gboolean incoming;

  GibberUnixTransportRecvCredentialsCb recv_creds_cb;
  gpointer recv_creds_data;

  gboolean dispose_has_run;
};

#define GIBBER_UNIX_TRANSPORT_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GIBBER_TYPE_UNIX_TRANSPORT, \
     GibberUnixTransportPrivate))

static void gibber_unix_transport_finalize (GObject *object);

static void
gibber_unix_transport_init (GibberUnixTransport *self)
{
  GibberUnixTransportPrivate *priv = GIBBER_UNIX_TRANSPORT_GET_PRIVATE (self);
  priv->incoming = FALSE;
}

static void gibber_unix_transport_dispose (GObject *object);
static GibberFdIOResult gibber_unix_transport_read (
    GibberFdTransport *transport,
    GIOChannel *channel,
    GError **error);

static void
gibber_unix_transport_class_init (
    GibberUnixTransportClass *gibber_unix_transport_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (gibber_unix_transport_class);
  GibberFdTransportClass *fd_class = GIBBER_FD_TRANSPORT_CLASS (
      gibber_unix_transport_class);

  g_type_class_add_private (gibber_unix_transport_class,
                            sizeof (GibberUnixTransportPrivate));

  object_class->dispose = gibber_unix_transport_dispose;
  object_class->finalize = gibber_unix_transport_finalize;

  /* override GibberFdTransport's read */
  fd_class->read = gibber_unix_transport_read;
}

void
gibber_unix_transport_dispose (GObject *object)
{
  GibberUnixTransport *self = GIBBER_UNIX_TRANSPORT (object);
  GibberUnixTransportPrivate *priv = GIBBER_UNIX_TRANSPORT_GET_PRIVATE (self);

  if (priv->dispose_has_run)
    return;

  priv->dispose_has_run = TRUE;

  priv->recv_creds_cb = NULL;
  priv->recv_creds_data = NULL;

  if (G_OBJECT_CLASS (gibber_unix_transport_parent_class)->dispose)
    G_OBJECT_CLASS (gibber_unix_transport_parent_class)->dispose (object);
}

void
gibber_unix_transport_finalize (GObject *object)
{
  G_OBJECT_CLASS (gibber_unix_transport_parent_class)->finalize (object);
}

GibberUnixTransport *
gibber_unix_transport_new (void)
{
  return g_object_new (GIBBER_TYPE_UNIX_TRANSPORT, NULL);
}

gboolean
gibber_unix_transport_connect (GibberUnixTransport *transport,
                               const gchar *path,
                               GError **error)
{
  union {
      struct sockaddr_un un;
      struct sockaddr addr;
  } addr;
  int fd;

  gibber_transport_set_state (GIBBER_TRANSPORT (transport),
      GIBBER_TRANSPORT_CONNECTING);

  memset (&addr, 0, sizeof (addr));

  fd = socket (PF_UNIX, SOCK_STREAM, 0);
  if (fd == -1)
    {
      DEBUG ("Error creating socket: %s", g_strerror (errno));
      g_set_error (error, GIBBER_UNIX_TRANSPORT_ERROR,
          GIBBER_UNIX_TRANSPORT_ERROR_CONNECT_FAILED,
          "Error creating socket: %s", g_strerror (errno));
      goto failed;
    }

  addr.un.sun_family = PF_UNIX;
  g_strlcpy (addr.un.sun_path, path, sizeof (addr.un.sun_path));

  if (connect (fd, &addr.addr, sizeof (addr.un)) == -1)
    {
      g_set_error (error, GIBBER_UNIX_TRANSPORT_ERROR,
          GIBBER_UNIX_TRANSPORT_ERROR_CONNECT_FAILED,
          "Error connecting socket: %s", g_strerror (errno));
      DEBUG ("Error connecting socket: %s", g_strerror (errno));
      goto failed;
    }
  DEBUG ("Connected to socket");

  gibber_fd_transport_set_fd (GIBBER_FD_TRANSPORT (transport), fd, TRUE);

  return TRUE;

failed:
  g_assert (error != NULL);
  gibber_transport_emit_error (GIBBER_TRANSPORT(transport), *error);

  gibber_transport_set_state (GIBBER_TRANSPORT (transport),
      GIBBER_TRANSPORT_DISCONNECTED);
  return FALSE;
}

GibberUnixTransport *
gibber_unix_transport_new_from_fd (int fd)
{
  GibberUnixTransport *transport;

  transport = gibber_unix_transport_new ();
  gibber_fd_transport_set_fd (GIBBER_FD_TRANSPORT (transport), fd, TRUE);
  return transport;
}

/* Patches that reimplement these functions for non-Linux would be welcome
 * (please file a bug) */

#if defined(__linux__)

gboolean
gibber_unix_transport_supports_credentials (void)
{
  return TRUE;
}

gboolean
gibber_unix_transport_send_credentials (GibberUnixTransport *transport,
    const guint8 *data,
    gsize size)
{
  int fd, ret;
  struct ucred *cred;
  struct msghdr msg;
  struct cmsghdr *ch;
  struct iovec iov;
  char buffer[CMSG_SPACE (sizeof (struct ucred))];

  DEBUG ("send credentials");
  fd = GIBBER_FD_TRANSPORT (transport)->fd;

  /* Set the message payload */
  memset (&iov, 0, sizeof (iov));
  iov.iov_base = (void *) data;
  iov.iov_len = size;

  memset (&msg, 0, sizeof (msg));
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = buffer;
  msg.msg_controllen = sizeof (buffer);
  memset (buffer, 0, sizeof (buffer));

  /* Set the credentials */
  ch = CMSG_FIRSTHDR (&msg);
  ch->cmsg_len = CMSG_LEN (sizeof (struct ucred));
  ch->cmsg_level = SOL_SOCKET;
  ch->cmsg_type = SCM_CREDENTIALS;

  cred = (struct ucred *) CMSG_DATA (ch);
  cred->pid = getpid ();
  cred->uid = getuid ();
  cred->gid = getgid ();

  ret = sendmsg (fd, &msg, 0);
  if (ret == -1)
    {
      DEBUG ("sendmsg failed: %s", g_strerror (errno));
      return FALSE;
    }

  return TRUE;
}

#define BUFSIZE 1024

static GibberFdIOResult
gibber_unix_transport_read (GibberFdTransport *transport,
    GIOChannel *channel,
    GError **error)
{
  GibberUnixTransport *self = GIBBER_UNIX_TRANSPORT (transport);
  GibberUnixTransportPrivate *priv = GIBBER_UNIX_TRANSPORT_GET_PRIVATE (self);
  int fd;
  guint8 buffer[BUFSIZE];
  ssize_t bytes_read;
  GibberBuffer buf;
  struct iovec iov;
  struct msghdr msg;
  char control[CMSG_SPACE (sizeof (struct ucred))];
  struct cmsghdr *ch;
  struct ucred *cred;
  int opt;

  if (priv->recv_creds_cb == NULL)
    return gibber_fd_transport_read (transport, channel, error);

  /* We are waiting for credentials */
  fd = transport->fd;

  /* set SO_PASSCRED flag */
  opt = 1;
  setsockopt (fd, SOL_SOCKET, SO_PASSCRED, &opt, sizeof (opt));

  memset (buffer, 0, sizeof (buffer));
  memset (&iov, 0, sizeof (iov));
  iov.iov_base = buffer;
  iov.iov_len = sizeof (buffer);

  memset (&msg, 0, sizeof (msg));
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = control;
  msg.msg_controllen = sizeof (control);

  bytes_read = recvmsg (fd, &msg, 0);

  if (bytes_read == -1)
    {
      GError *err = NULL;

      g_set_error_literal (&err, G_IO_CHANNEL_ERROR,
          g_io_channel_error_from_errno (errno), "recvmsg failed");

      priv->recv_creds_cb (self, NULL, NULL, err, priv->recv_creds_data);
      g_propagate_error (error, err);

      priv->recv_creds_cb = NULL;
      priv->recv_creds_data = NULL;
      return GIBBER_FD_IO_RESULT_ERROR;
    }

  /* unset SO_PASSCRED flag */
  opt = 0;
  setsockopt (fd, SOL_SOCKET, SO_PASSCRED, &opt, sizeof (opt));

  buf.data = buffer;
  buf.length = bytes_read;

  /* extract the credentials */
  ch = CMSG_FIRSTHDR (&msg);
  if (ch == NULL)
    {
      GError *err = NULL;

      DEBUG ("Message doesn't contain credentials");

      g_set_error_literal (&err, GIBBER_UNIX_TRANSPORT_ERROR,
          GIBBER_UNIX_TRANSPORT_ERROR_NO_CREDENTIALS,
          "no credentials received");

      priv->recv_creds_cb (self, &buf, NULL, err, priv->recv_creds_data);
      g_error_free (err);
    }
  else
    {
      GibberCredentials credentials;

      cred = (struct ucred *) CMSG_DATA (ch);
      credentials.pid = cred->pid;
      credentials.uid = cred->uid;
      credentials.gid = cred->gid;

      priv->recv_creds_cb (self, &buf, &credentials, NULL,
          priv->recv_creds_data);
    }

  priv->recv_creds_cb = NULL;
  priv->recv_creds_data = NULL;
  return GIBBER_FD_IO_RESULT_SUCCESS;
}

gboolean
gibber_unix_transport_recv_credentials (GibberUnixTransport *self,
    GibberUnixTransportRecvCredentialsCb callback,
    gpointer user_data)
{
  GibberUnixTransportPrivate *priv = GIBBER_UNIX_TRANSPORT_GET_PRIVATE (self);

  if (priv->recv_creds_cb != NULL)
    {
      DEBUG ("already waiting for credentials");
      return FALSE;
    }

  priv->recv_creds_cb = callback;
  priv->recv_creds_data = user_data;
  return TRUE;
}

#else /* OSs where we have no implementation */

gboolean
gibber_unix_transport_supports_credentials (void)
{
  return FALSE;
}

gboolean
gibber_unix_transport_recv_credentials (GibberUnixTransport *self,
    GibberUnixTransportRecvCredentialsCb callback,
    gpointer user_data)
{
  DEBUG ("stub implementation, failing");
  return FALSE;
}

gboolean
gibber_unix_transport_send_credentials (GibberUnixTransport *transport,
    const guint8 *data,
    gsize size)
{
  DEBUG ("stub implementation, failing");
  return FALSE;
}

static GibberFdIOResult
gibber_unix_transport_read (GibberFdTransport *transport,
    GIOChannel *channel,
    GError **error)
{
  return gibber_fd_transport_read (transport, channel, error);
}

#endif /* OSs where we have no implementation of credentials */

#endif /* G_OS_UNIX */