aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/daemon/daemon.c
blob: 304a6cbb130de7d3e6043056a3d1a872bb7eb68e (plain) (blame)
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
     This file is part of GNUnet.
     (C) 2005, 2006, 2007 Christian Grothoff (and other contributing authors)

     GNUnet 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.

     GNUnet 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 GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file src/plugins/daemon/daemon.c
 * @brief code for gnunet-gtk gnunetd interaction
 * @author Christian Grothoff
 */

#include "platform.h"
#include "gnunetgtk_common.h"
#include <GNUnet/gnunet_directories.h>
#include <GNUnet/gnunet_getoption_lib.h>
#include <GNUnet/gnunet_stats_lib.h>
#include <GNUnet/gnunet_util.h>
#include <gtk/gtk.h>
#include <glib/gerror.h>

#ifdef WITH_LIBGKSU2
/* Not used because libgksu2 headers have broken depends in Debian
And this is not really needed
#include <libgksu/libgksu.h> */
gboolean
gksu_run (gchar *command_line,
          GError **error);
#endif

static struct GNUNET_CronManager *cron;

static struct GNUNET_GE_Context *ectx;

static struct GNUNET_GC_Configuration *cfg;

/**
 * cron job that periodically updates the model for the
 * application list.
 */
static void *
updateAppModelSafe (void *unused)
{
  struct GNUNET_ClientServerConnection *sock;
  GtkWidget *w;
  GtkListStore *model;
  GtkTreeIter iter;
  char *apps;
  char *next;
  char *pos;
  char *desc;

  model = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
  apps = NULL;
  sock = GNUNET_client_connection_create (ectx, cfg);
  if (sock != NULL)
    apps =
      GNUNET_get_daemon_configuration_value (sock, "GNUNETD", "APPLICATIONS");
  if (apps != NULL)
    {
      next = apps;
      do
        {
          while (*next == ' ')
            next++;
          pos = next;
          while ((*next != '\0') && (*next != ' '))
            next++;
          if (*next == '\0')
            {
              next = NULL;      /* terminate! */
            }
          else
            {
              *next = '\0';     /* add 0-termination for pos */
              next++;
            }
          if (strlen (pos) > 0)
            {
              desc =
                GNUNET_get_daemon_configuration_value (sock, "ABOUT", pos);

              gtk_list_store_append (model, &iter);
              gtk_list_store_set (model,
                                  &iter,
                                  0, pos, 1, dgettext ("GNUnet", desc), -1);
              GNUNET_free_non_null (desc);
            }
        }
      while (next != NULL);
      GNUNET_free (apps);
    }
  w =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                          "applicationList");
  gtk_tree_view_set_model (GTK_TREE_VIEW (w), GTK_TREE_MODEL (model));
  gtk_tree_selection_set_mode (gtk_tree_view_get_selection
                               (GTK_TREE_VIEW (w)), GTK_SELECTION_NONE);
  g_object_unref (model);
  GNUNET_client_connection_destroy (sock);
  return NULL;
}

static void
updateAppModel (void *dummy)
{
  GNUNET_GTK_save_call (&updateAppModelSafe, NULL);
}


static void *
doUpdateMenus (void *arg)
{
  int ret = *(int *) arg;
  static GtkWidget *killEntry = NULL;
  static GtkWidget *launchEntry = NULL;
  static GtkWidget *statsEntryYes = NULL;
  static GtkWidget *statsEntryNo = NULL;
  static GtkWidget *statsEntryError = NULL;
  static GtkWidget *chooser = NULL;
  static int once = 1;
  static int isLocal;
  char *host;
  int canStart;
  struct GNUNET_GC_Configuration *dcfg;
  char *fn;
  char *user;

  if (once)
    {
      once = 0;
      killEntry =
        glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "stopDaemon");
      launchEntry =
        glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                              "startDaemon");
      statsEntryYes =
        glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                              "statusPixmapYes");
      statsEntryNo =
        glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                              "statusPixmapNo");
      statsEntryError =
        glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                              "statusPixmapError");
      chooser =
        glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                              "gnunetdconfigFileChooserButton");
      GNUNET_GC_get_configuration_value_string (cfg, "NETWORK", "HOST",
                                                "localhost", &host);
      if (strncmp (host, "localhost:", 10) == 0)
        isLocal = TRUE;
      else
        isLocal = FALSE;
      GNUNET_free (host);
    }
  updateAppModelSafe (NULL);
  if (ret == 0)
    {
      canStart = 0;
	  fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
	  if (NULL == fn)
	    GNUNET_GC_get_configuration_value_filename (cfg, "DAEMON",
												    "CONFIGFILE",
												    GNUNET_DEFAULT_DAEMON_CONFIG_FILE,
												    &fn);
      if (isLocal && fn)
        {
          if (GNUNET_disk_file_test (ectx, fn) == GNUNET_YES)
            {
              dcfg = GNUNET_GC_create ();
              canStart = 1;
              if (0 != GNUNET_GC_parse_configuration (dcfg, fn))
                canStart = 0;
              user = NULL;
              GNUNET_GC_get_configuration_value_string (dcfg,
                                                        "GNUNETD",
                                                        "USER", "", &user);
              if (strlen (user) > 0)
                {
#if defined(WINDOWS) || defined(WITH_LIBGKSU2)
                  canStart = 1;
#else
				  struct passwd *pws;
                  if (NULL == (pws = getpwnam (user)))
                    {
                      canStart = 0;
                    }
                  else
                    {
                      if (pws->pw_uid != getuid ())
                        canStart = (geteuid () == 0);
                      else
                        canStart = 1;
                    }
#endif
                }
              GNUNET_free (user);
              GNUNET_GC_free (dcfg);
            }
          g_free (fn);
        }
      gtk_widget_hide (statsEntryYes);
      gtk_widget_set_sensitive (killEntry, FALSE);
      if (canStart && isLocal)
        {
          gtk_widget_set_sensitive (launchEntry, TRUE);
          gtk_widget_show_all (statsEntryNo);
          gtk_widget_hide (statsEntryError);
        }
      else
        {
          gtk_widget_set_sensitive (launchEntry, FALSE);
          gtk_widget_show_all (statsEntryError);
          gtk_widget_hide (statsEntryNo);
        }
    }
  else
    {
      gtk_widget_hide (statsEntryNo);
      gtk_widget_hide (statsEntryError);
      gtk_widget_show_all (statsEntryYes);
      gtk_widget_set_sensitive (killEntry, TRUE);
      gtk_widget_set_sensitive (launchEntry, FALSE);
    }
  return NULL;
}

static void
cronCheckDaemon (void *dummy)
{
  int ret;

  if (GNUNET_OK == GNUNET_test_daemon_running (ectx, cfg))
      ret = 1;
  else
      ret = 0;
  GNUNET_GTK_save_call (&doUpdateMenus, &ret);
}


/**
 * Launch gnunetd w/ checks
 */
void
on_startDaemon_clicked_daemon (GtkWidget * widget, gpointer data)
{
  GtkWidget *launchEntry;
  GtkWidget *chooser;
  char *fn;
  char *user;
  struct GNUNET_GC_Configuration *dcfg;
  int code = 0;

  launchEntry =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "startDaemon");
  gtk_widget_set_sensitive (launchEntry, FALSE);
  if (GNUNET_OK == GNUNET_test_daemon_running (ectx, cfg))
    {
      cronCheckDaemon (NULL);
      return;
    }
  else
    {
      GNUNET_GTK_add_log_entry (_("Launching gnunetd...\n"));
      chooser
        = glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                                "gnunetdconfigFileChooserButton");
      fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (chooser));
      GNUNET_GE_BREAK (ectx, fn != NULL);

      dcfg = GNUNET_GC_create ();
      if (0 != GNUNET_GC_parse_configuration (dcfg, fn))
        user = NULL;
      GNUNET_GC_get_configuration_value_string (dcfg,
                                                "GNUNETD",
                                                "USER", "", &user);
#ifdef WITH_LIBGKSU2
	  char *command;
	  GError *gerror = NULL;
	  struct passwd *pws;
	  if (strlen (user) > 0)
	    {
          pws = getpwnam (user);
          if (pws->pw_uid != getuid ())
			{
              command = g_strconcat ("gnunetd -c ", fn, NULL);
			  code = gksu_run (command, &gerror);
			  GNUNET_free (command);
			  if (gerror)
                {
				  GNUNET_GTK_add_log_entry (_("Launching gnunetd failed\n"));

				  g_error_free (gerror);
                }
              else
                {
                  GNUNET_GTK_add_log_entry (_("Launched gnunetd\n"));
                }
             g_free (fn);
			 cronCheckDaemon (NULL);
			 return;
			}
           else
             code = GNUNET_daemon_start (ectx, cfg, fn, GNUNET_YES); 
		}
#else
     code = GNUNET_daemon_start (ectx, cfg, fn, GNUNET_YES);
#endif
						
	 if (GNUNET_SYSERR != code)
        {
          GNUNET_GTK_add_log_entry (_("Launched gnunetd\n"));
        }
      else
        {
          GNUNET_GTK_add_log_entry (_("Launching gnunetd failed\n"));
        }
      if (fn != NULL)
      g_free (fn);
	  cronCheckDaemon (NULL);
    }
}

/**
 * Kill gnunetd
 */
void
on_stopDaemon_clicked_daemon (GtkWidget * widget, gpointer data)
{
  GtkWidget *killEntry = NULL;
  struct GNUNET_ClientServerConnection *sock;

  killEntry =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "stopDaemon");
  gtk_widget_set_sensitive (killEntry, FALSE);

  if (GNUNET_OK == GNUNET_test_daemon_running (ectx, cfg))
    {
      sock = GNUNET_client_connection_create (ectx, cfg);
      if (GNUNET_OK !=
          GNUNET_client_connection_request_daemon_shutdown (sock))
        {
          GtkWidget *dialog;

          dialog = gtk_message_dialog_new
            (NULL,
             GTK_DIALOG_MODAL,
             GTK_MESSAGE_ERROR,
             GTK_BUTTONS_CLOSE, _("Error requesting shutdown of gnunetd.\n"));
          gtk_dialog_run (GTK_DIALOG (dialog));
          gtk_widget_destroy (dialog);
        }
      else
        {
          GNUNET_GTK_add_log_entry (_("Terminating gnunetd...\n"));
        }
      GNUNET_client_connection_destroy (sock);
    }
  cronCheckDaemon (NULL);
}

void
init_daemon (struct GNUNET_GE_Context *e, struct GNUNET_GC_Configuration *c)
{
  GtkWidget *tab;
  GtkWidget *apps;
  GtkListStore *model;
  GtkCellRenderer *renderer;
  int col;
  char *daemon_config;

  ectx = e;
  cfg = c;
  apps =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                          "applicationList");
  model = gtk_list_store_new (1, G_TYPE_STRING);
  gtk_tree_view_set_model (GTK_TREE_VIEW (apps), GTK_TREE_MODEL (model));
  renderer = gtk_cell_renderer_text_new ();
  col = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (apps),
                                                     -1,
                                                     _("Application"),
                                                     renderer,
                                                     "text", 0, NULL);
  gtk_tree_view_column_set_resizable (gtk_tree_view_get_column
                                      (GTK_TREE_VIEW (apps), col - 1), TRUE);
  renderer = gtk_cell_renderer_text_new ();
  col = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (apps),
                                                     -1,
                                                     _("Description"),
                                                     renderer,
                                                     "text", 1, NULL);
  gtk_tree_view_column_set_resizable (gtk_tree_view_get_column
                                      (GTK_TREE_VIEW (apps), col - 1), TRUE);

  tab =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                          "daemonScrolledWindow");
  gtk_widget_show (tab);
  daemon_config = NULL;
  GNUNET_GC_get_configuration_value_filename (cfg,
                                              "DAEMON",
                                              "CONFIGFILE",
                                              GNUNET_DEFAULT_DAEMON_CONFIG_FILE,
                                              &daemon_config);
  if (GNUNET_YES == GNUNET_disk_file_test (NULL, daemon_config))
    {
      gtk_file_chooser_set_filename (GTK_FILE_CHOOSER
                                     (glade_xml_get_widget
                                      (GNUNET_GTK_get_main_glade_XML (),
                                       "gnunetdconfigFileChooserButton")),
                                     daemon_config);
    }
  else
    {
      GNUNET_GTK_add_log_entry (_
                                ("Configuration file for GNUnet daemon `%s' does not exist! Run `gnunet-setup -d'.\n"),
                                daemon_config);
    }
  
#ifndef WITH_LIBGKSU2
  GtkWidget *startDaemonConfWizard;
  GtkWidget *startDaemonConfTool;

  startDaemonConfWizard
        = glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                                "startDaemonConfWizard");
  startDaemonConfTool
        = glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                                "startDaemonConfTool");
  if (0 != ACCESS (daemon_config, W_OK))
    {
      gtk_widget_set_sensitive (startDaemonConfWizard, FALSE);
      gtk_widget_set_sensitive (startDaemonConfTool, FALSE);
    }
#endif
  GNUNET_free (daemon_config);

  cron = GNUNET_GTK_get_cron_manager ();
  GNUNET_cron_add_job (cron, &cronCheckDaemon, 0, 15 * GNUNET_CRON_SECONDS,
                       NULL);
  GNUNET_cron_add_job (cron, &updateAppModel, 5 * GNUNET_CRON_MINUTES,
                       5 * GNUNET_CRON_MINUTES, NULL);
}

void
done_daemon ()
{
  GtkWidget *w;

  GNUNET_cron_del_job (cron, &cronCheckDaemon, 15 * GNUNET_CRON_SECONDS,
                       NULL);
  GNUNET_cron_del_job (cron, &updateAppModel, 5 * GNUNET_CRON_MINUTES, NULL);
  w =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                          "applicationList");
  gtk_tree_view_set_model (GTK_TREE_VIEW (w), NULL);

  w = glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "applicationList");
  gtk_tree_view_set_model (GTK_TREE_VIEW (w), NULL);

}

/**
* Launch 'gnunet-setup -d wizard-gtk' with needed rights
*/
void
on_startDaemonConfWizard_clicked_daemon (GtkWidget *widget, gpointer data)
{
  GtkWidget *filechooserbutton;
  char *conffile;

  filechooserbutton
        = glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                                "gnunetdconfigFileChooserButton");
  conffile = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(filechooserbutton));
  GNUNET_GTK_run_gnunet_setup (conffile, TRUE);
  GNUNET_free (conffile);
}

/**
* Launch 'gnunet-setup -d gconfig' with needed rights
*/
void
on_startDaemonConfTool_clicked_daemon (GtkWidget *widget, gpointer data)
{
  GtkWidget *filechooserbutton;
  char *conffile;

  filechooserbutton
        = glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                                "gnunetdconfigFileChooserButton");
  conffile = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(filechooserbutton));
  GNUNET_GTK_run_gnunet_setup (conffile, FALSE);
  GNUNET_free (conffile);
}

/**
* Update sensitivity of daemon buttons
*/
void
on_gnunetdconfigfile_set_daemon (GtkWidget *filechooserbutton,
                                 gpointer data)
{
#ifndef WITH_LIBGKSU2
  GtkWidget *startDaemonConfWizard;
  GtkWidget *startDaemonConfTool;
  char *conffile;
  int ret;

  startDaemonConfWizard
        = glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                                "startDaemonConfWizard");
  startDaemonConfTool
        = glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                                "startDaemonConfTool");
  conffile = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(filechooserbutton));
  if (0 == ACCESS (conffile, W_OK))
    {
      gtk_widget_set_sensitive (startDaemonConfWizard, TRUE);
      gtk_widget_set_sensitive (startDaemonConfTool, TRUE);
    }
   else
    {
      gtk_widget_set_sensitive (startDaemonConfWizard, FALSE);
      gtk_widget_set_sensitive (startDaemonConfTool, FALSE);
    }
  ret = 0;
  GNUNET_GTK_save_call (&doUpdateMenus, &ret);
  GNUNET_free_non_null (conffile);
#endif
  return;
}

/* end of daemon.c */