summaryrefslogtreecommitdiff
path: root/xfce4-session/ice-layer.c
blob: 9ae74738c66678d10011c466fd4f39d6241896ca (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
/* $Id$ */
/*-
 * Copyright (c) 2003-2004 Benedikt Meurer <benny@xfce.org>
 * Copyright (c) 2008 Brian Tarricone <bjt23@cornell.edu>
 * 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 as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * 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 Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 */

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

#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <X11/ICE/ICElib.h>
#include <X11/ICE/ICEutil.h>
#include <X11/SM/SMlib.h>

#include <libxfce4util/libxfce4util.h>

#include <xfce4-session/ice-layer.h>
#include <xfce4-session/xfsm-global.h>
#include <xfce4-session/xfsm-manager.h>

typedef struct
{
  XfsmManager *manager;
  IceConn ice_conn;
} XfsmIceConnData;


/* prototypes */
static void ice_error_handler         (IceConn);
static gboolean ice_process_messages  (GIOChannel  *channel,
                                       GIOCondition condition,
                                       gpointer     user_data);
static gboolean ice_connection_accept (GIOChannel  *channel,
                                       GIOCondition condition,
                                       gpointer     watch_data);
static FILE *ice_tmpfile              (char **name);
static void ice_auth_add              (FILE *,
                                       FILE *,
                                       char *,
                                       IceListenObj);

static char *auth_cleanup_file;


Bool
ice_auth_proc (char *hostname)
{
  return False;
}


static void
ice_error_handler (IceConn ice_conn)
{
  /*
   * The I/O error handlers does whatever is necessary to respond
   * to the I/O error and then returns, but it does not call
   * IceCloseConnection. The ICE connection is given a "bad IO"
   * status, and all future reads and writes to the connection
   * are ignored. The next time IceProcessMessages is called it
   * will return a status of IceProcessMessagesIOError. At that
   * time, the application should call IceCloseConnection.
   */
  xfsm_verbose ("ICE connection fd = %d, ICE I/O error on connection\n",
                IceConnectionNumber (ice_conn));
}


static gboolean
ice_process_messages (GIOChannel  *channel,
                      GIOCondition condition,
                      gpointer     user_data)
{
  IceProcessMessagesStatus status;
  XfsmIceConnData         *icdata = user_data;

  status = IceProcessMessages (icdata->ice_conn, NULL, NULL);

  if (status == IceProcessMessagesIOError)
    {
      xfsm_manager_close_connection_by_ice_conn (icdata->manager,
                                                 icdata->ice_conn);

      /* remove the I/O watch */
      return FALSE;
    }

  /* keep the I/O watch running */
  return TRUE;
}


static void
ice_connection_watch (IceConn     ice_conn,
                      IcePointer  client_data,
                      Bool        opening,
                      IcePointer *watch_data)
{
  XfsmManager *manager = XFSM_MANAGER (client_data);
  GIOChannel  *channel;
  guint        watchid;
  gint         fd;
  gint         ret;

  if (opening)
    {
      XfsmIceConnData *icdata = g_new(XfsmIceConnData, 1);
      icdata->manager = manager;
      icdata->ice_conn = ice_conn;

      fd = IceConnectionNumber (ice_conn);

      /* Make sure we don't pass on these file descriptors to an
       * exec'd child process.
       */
      ret = fcntl (fd, F_SETFD, fcntl (fd, F_GETFD, 0) | FD_CLOEXEC);
      if (ret == -1)
        {
          perror ("ice_connection_watch: fcntl (fd, F_SETFD, fcntl (fd, F_GETFD, 0) | FD_CLOEXEC) failed");
        }

      channel = g_io_channel_unix_new (fd);
      watchid = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT,
                                     G_IO_ERR | G_IO_HUP | G_IO_IN,
                                     ice_process_messages,
                                     icdata, (GDestroyNotify) g_free);
      g_io_channel_unref (channel);

      *watch_data = (IcePointer) GUINT_TO_POINTER (watchid);
    }
  else
    {
      watchid = GPOINTER_TO_UINT (*watch_data);
      g_source_remove (watchid);
    }
}


static gboolean
ice_connection_accept (GIOChannel  *channel,
                       GIOCondition condition,
                       gpointer     watch_data)
{
  IceConnectStatus cstatus;
  IceAcceptStatus  astatus;
  IceListenObj     ice_listener = (IceListenObj) watch_data;
  IceConn          ice_conn;

  ice_conn = IceAcceptConnection (ice_listener, &astatus);

  if (astatus != IceAcceptSuccess)
    {
      g_warning ("Failed to accept ICE connection on listener %p",
                 (gpointer) ice_listener);
    }
  else
    {
      /* Wait for the connection to leave pending state */
      do
        {
          IceProcessMessages (ice_conn, NULL, NULL);
        }
      while ((cstatus = IceConnectionStatus (ice_conn)) == IceConnectPending);

      if (cstatus != IceConnectAccepted)
        {
          if (cstatus == IceConnectIOError)
            {
              g_warning ("I/O error opening ICE connection %p", (gpointer) ice_conn);
            }
          else
            {
              g_warning ("ICE connection %p rejected", (gpointer) ice_conn);
            }

          IceSetShutdownNegotiation (ice_conn, False);
          IceCloseConnection (ice_conn);
        }
    }

  return TRUE;
}


static FILE*
ice_tmpfile (char **name)
{
  GError *error = NULL;
  mode_t  mode;
  FILE   *fp = NULL;
  int     fd;

  mode = umask (0077);

  fd = g_file_open_tmp(".xfsm-ICE-XXXXXX", name, &error);
  if (fd < 0)
    {
      g_warning ("Unable to open temporary file: %s", error->message);
      g_error_free (error);
    }
  else
    {
      fp = fdopen (fd, "wb");
    }

  umask (mode);

  return fp;
}


static void
fprintfhex (FILE *fp, int len, char *cp)
{
  static char hexchars[] = "0123456789abcdef";

  for (; len > 0; len--, cp++)
    {
      unsigned char s = *cp;
      putc (hexchars[s >> 4], fp);
      putc (hexchars[s & 0x0f], fp);
    }
}


static void
ice_auth_add (FILE        *setup_fp,
              FILE        *cleanup_fp,
              char        *protocol,
              IceListenObj ice_listener)
{
  IceAuthDataEntry entry;

  entry.protocol_name = protocol;
  entry.network_id = IceGetListenConnectionString (ice_listener);
  entry.auth_name = "MIT-MAGIC-COOKIE-1";
  entry.auth_data = IceGenerateMagicCookie (16);
  entry.auth_data_length = 16;

  IceSetPaAuthData (1, &entry);

  fprintf (setup_fp,
           "add %s \"\" %s MIT-MAGIC-COOKIE-1 ",
           protocol,
           entry.network_id);
  fprintfhex (setup_fp, 16, entry.auth_data);
  fprintf (setup_fp, "\n");

  fprintf (cleanup_fp,
           "remove protoname=%s protodata=\"\" netid=%s authname=MIT-MAGIC-COOKIE-1\n",
           protocol,
           entry.network_id);

  free (entry.network_id);
  free (entry.auth_data);
}


gboolean
ice_setup_listeners (int           num_listeners,
                     IceListenObj *listen_objs,
                     XfsmManager  *manager)
{
  GIOChannel *channel;
  char       *auth_setup_file;
  gchar      *command;
  FILE       *cleanup_fp;
  FILE       *setup_fp;
  int         fd;
  int         n;
  int         ret;

  IceSetIOErrorHandler (ice_error_handler);
  IceAddConnectionWatch (ice_connection_watch, manager);

  cleanup_fp = ice_tmpfile(&auth_cleanup_file);
  if (cleanup_fp == NULL)
    return FALSE;

  setup_fp = ice_tmpfile(&auth_setup_file);
  if (setup_fp == NULL)
    {
      fclose (cleanup_fp);
      unlink (auth_cleanup_file);
      g_free (auth_cleanup_file);
      return FALSE;
    }

  for (n = 0; n < num_listeners; n++)
    {
      fd = IceGetListenConnectionNumber (listen_objs[n]);

      /* Make sure we don't pass on these file descriptors to an
       * exec'd child process.
       */
      ret = fcntl (fd, F_SETFD, fcntl (fd, F_GETFD, 0) | FD_CLOEXEC);
      if (ret == -1)
        {
          perror ("ice_setup_listeners: fcntl (fd, F_SETFD, fcntl (fd, F_GETFD, 0) | FD_CLOEXEC) failed");
        }

      channel = g_io_channel_unix_new (fd);
      g_io_add_watch (channel, G_IO_ERR | G_IO_HUP | G_IO_IN,
                      ice_connection_accept,
                      listen_objs[n]);
      g_io_channel_unref (channel);

      /* setup auth for this listener */
      ice_auth_add (setup_fp, cleanup_fp, "ICE", listen_objs[n]);
      ice_auth_add (setup_fp, cleanup_fp, "XSMP", listen_objs[n]);
      IceSetHostBasedAuthProc (listen_objs[n], ice_auth_proc);
    }

  fclose (setup_fp);
  fclose (cleanup_fp);

  /* setup ICE authority and remove setup file */
  command = g_strdup_printf ("%s source %s", ICEAUTH_CMD, auth_setup_file);
  if (system (command) != 0)
    {
      g_warning ("Failed to setup the ICE authentication data, session "
                 "management might not work properly.");
    }
  g_free (command);
  unlink (auth_setup_file);
  g_free (auth_setup_file);

  return TRUE;
}

void
ice_cleanup (void)
{
  gchar *command;

  g_return_if_fail (auth_cleanup_file != NULL);

  /* remove newly added ICE authority entries */
  command = g_strdup_printf ("%s source %s", ICEAUTH_CMD, auth_cleanup_file);
  if (system (command))
    g_warning ("Failed to execute \"%s\"", command);
  g_free (command);

  /* remove the cleanup file, no longer needed */
  unlink (auth_cleanup_file);
  g_free (auth_cleanup_file);
  auth_cleanup_file = NULL;
}