aboutsummaryrefslogtreecommitdiff
path: root/src/setup/gnunet-setup.c
blob: ba82800e75b41a42b6830fd6cb5ea918972090a7 (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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
     This file is part of GNUnet.
     (C) 2010, 2011, 2012, 2013 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 3, 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/setup/gnunet-setup.c
 * @brief Main function of gnunet-setup
 * @author Christian Grothoff
 */
#include "gnunet-setup.h"
#include "gnunet-setup-transport.h"
#include "gnunet-setup-options.h"
#include <gnunet/gnunet_constants.h>
#include <regex.h>
#if ENABLE_NLS
#include <locale.h>
#endif


/**
 * Main loop handle.
 */
static struct GNUNET_GTK_MainLoop *ml;

/**
 * Name of the configuration file.
 */
static const char *cfgName;

/**
 * Our configuration.
 */
struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * Global return value (for success/failure of gnunet-setup).
 */
static int gret;

/**
 * Resolver process handle.
 */
static struct GNUNET_OS_Process *resolver;

/**
 * Namestore process handle.
 */
static struct GNUNET_OS_Process *namestore;

/**
 * Identity process handle.
 */
static struct GNUNET_OS_Process *identity;

/**
 * Run autoconfig-only and then exit immediately.
 */
static int do_autoconfig;

#ifndef MINGW
/**
 * Flag to enable privilege escalation.
 */
static int do_gksu;
#endif


/**
 * Get an object from the main window.
 *
 * @param name name of the object
 * @return NULL on error, otherwise the object
 */
GObject *
GNUNET_SETUP_get_object (const char *name)
{
  if (NULL == ml)
    return NULL;
  return GNUNET_GTK_main_loop_get_object (ml, name);
}


/**
 * User clicked on some widget, update the help button label and link.
 *
 * @param widget widget that was clicked on (unused)
 * @param event the current event
 * @param user_data context with the option specification to evaluate
 * @return FALSE (continue event handling)
 */
static gboolean
help_click_callback (GtkWidget * widget, GdkEventButton * event,
                     gpointer user_data)
{
  const struct GNUNET_SETUP_OptionSpecification *os = user_data;
  GtkLinkButton *help;

  if (GDK_BUTTON_PRESS != event->type)
    return FALSE;
  help = GTK_LINK_BUTTON (GNUNET_SETUP_get_object ("GNUNET_setup_help_text"));
  gtk_link_button_set_uri (help, os->help_url);
  gtk_button_set_label (GTK_BUTTON (help), os->help_text);
  return FALSE;
}


/**
 * The main visible page in our main notebook changed.  If the
 * GNS page is visible, hide the help text, otherwise show it.
 */
void
GNUNET_setup_notebook_switch_page_cb (GtkNotebook *notebook,
				      GtkWidget   *page,
				      guint        page_num,
				      gpointer     user_data)
{
  GtkWidget *help;
  GtkWidget *gnu;

  help = GTK_WIDGET (GNUNET_SETUP_get_object ("GNUNET_setup_help_text"));
  gnu = GTK_WIDGET (GNUNET_SETUP_get_object ("GNUNET_setup_gns_vbox"));
  if (gnu == page)
    gtk_widget_hide (help);
  else
    gtk_widget_show (help);
}


/**
 * Change the visibility of widgets according to the
 * value and visibility specification given.
 *
 * @param os option specification
 * @param value current value for the given option
 */
static void
update_visibility (const struct GNUNET_SETUP_OptionSpecification *os,
                   const char *value)
{
  unsigned int i;
  const struct GNUNET_SETUP_VisibilitySpecification *vs;
  GtkWidget *widget;
  regex_t r;

  if (NULL == os->visibility)
    return;
  i = 0;
  while (os->visibility[i].widget_name != NULL)
  {
    vs = &os->visibility[i];
    widget = GTK_WIDGET (GNUNET_SETUP_get_object (vs->widget_name));
    if (NULL == widget)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Widget `%s' not found\n"),
                  vs->widget_name);
    }
    if (NULL != vs->show_value)
    {
      if (0 !=
          regcomp (&r, vs->show_value, REG_EXTENDED | REG_ICASE | REG_NOSUB))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    _("Invalid regular expression `%s'\n"), vs->show_value);
        i++;
        continue;
      }
      if (0 == regexec (&r, value, 0, NULL, 0))
        gtk_widget_show (widget);
      else
        gtk_widget_hide (widget);
      regfree (&r);
    }
    if (NULL != vs->hide_value)
    {
      if (0 != regcomp (&r, vs->hide_value, REG_ICASE | REG_NOSUB))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                    _("Invalid regular expression `%s'\n"), vs->show_value);
        i++;
        continue;
      }
      if (0 == regexec (&r, value, 0, NULL, 0))
        gtk_widget_hide (widget);
      else
        gtk_widget_show (widget);
      regfree (&r);
    }
    i++;
  }
}


/**
 * Function called whenever a widget changes its state.
 *
 * @param os details about the option
 */
static void
widget_state_change_callback (const struct GNUNET_SETUP_OptionSpecification *os)
{
  GObject *widget;
  char *value;

  widget = GNUNET_SETUP_get_object (os->widget_name);
  GNUNET_assert (NULL != os->save_function);
  if (GNUNET_OK !=
      os->save_function (os->load_save_cls, os->section, os->option, widget,
                         cfg))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                _("Failed to obtain option value from widget `%s'\n"),
                os->widget_name);
    return;
  }
  if (NULL != os->input_validation_function)
    os->input_validation_function (os->input_validation_function_cls, widget);
  if ((os->section != NULL) && (os->option != NULL))
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CONFIGURATION_get_value_string (cfg, os->section,
                                                          os->option, &value));
  else
    return;
  update_visibility (os, value);
  GNUNET_free_non_null (value);
}


/**
 * Load options into the main dialog.
 */
static void
load_options ()
{
  const struct GNUNET_SETUP_OptionSpecification *os;
  unsigned int i;
  GObject *widget;
  char *value;

#ifndef LINUX
  gtk_widget_hide (GTK_WIDGET (GNUNET_SETUP_get_object ("GNUNET_setup_gns_hijack_checkbutton")));
#endif
  i = 0;
  while (NULL != option_specifications[i].widget_name)
  {
    os = &option_specifications[i];
    widget = GNUNET_SETUP_get_object (os->widget_name);
    if (NULL == widget)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("Widget `%s' not found\n"),
                  os->widget_name);
      i++;
      continue;
    }
    if (NULL != os->load_function)
    {
      if ((NULL == os->section) || (NULL == os->option))
      {
        if (GNUNET_OK !=
            os->load_function (os->load_save_cls, NULL, NULL, NULL, widget,
                               cfg))
        {
          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                      _("Failed to initialize widget `%s'\n"), os->widget_name);
        }
      }
      else
      {
        if (GNUNET_OK !=
            GNUNET_CONFIGURATION_get_value_string (cfg, os->section, os->option,
                                                   &value))
        {
          GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                      _
                      ("No default value known for option `%s' in section `%s'\n"),
                      os->option, os->section);
        }
        else
        {
          if (GNUNET_OK !=
              os->load_function (os->load_save_cls, os->section, os->option,
                                 value, widget, cfg))
          {
            GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                        _("Failed to initialize widget `%s' with value `%s'\n"),
                        os->widget_name, value);
          }
          else
          {
            update_visibility (os, value);
          }
          GNUNET_free (value);
        }
      }
    }
    if (NULL != os->input_validation_function)
      os->input_validation_function (os->input_validation_function_cls, widget);
    if (os->help_text != NULL)
    {
      g_signal_connect (widget, "button-press-event",
                        G_CALLBACK (&help_click_callback), (void *) os);
    }
    if (NULL != os->change_signal)
    {
      GNUNET_assert (NULL != os->save_function);
      g_signal_connect_swapped (widget, os->change_signal,
                                G_CALLBACK (&widget_state_change_callback),
                                (void *) os);
    }
    i++;
  }
}


/**
 * Write final configuration to disk.
 *
 * @return GNUNET_OK on success
 */
static int
write_configuration ()
{
  struct GNUNET_CONFIGURATION_Handle *cfgDefault;
  int ret;

  cfgDefault = GNUNET_CONFIGURATION_create ();
  (void) GNUNET_CONFIGURATION_load (cfgDefault, NULL);  /* load defaults only */
  ret = GNUNET_CONFIGURATION_write_diffs (cfgDefault, cfg, cfgName);
  GNUNET_CONFIGURATION_destroy (cfgDefault);
  return ret;
}


/**
 * Method run on shutdown.
 *
 * @param cls the main loop handle
 * @param tc scheduler context
 */
static void
cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  if (NULL == ml)
  {
    GNUNET_break (0);
    return;
  }
  GNUNET_GTK_main_loop_quit (ml);
  ml = NULL;
  if (GNUNET_OK != write_configuration ())
    gret = 1;
  cfg = NULL;
  if (NULL != resolver)
  {
    GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
    GNUNET_OS_process_destroy (resolver);
    resolver = NULL;
  }
  if (NULL != namestore)
  {
    GNUNET_break (0 == GNUNET_OS_process_kill (namestore, SIGTERM));
    GNUNET_OS_process_destroy (namestore);
    namestore = NULL;
  }
  if (NULL != identity)
  {
    GNUNET_break (0 == GNUNET_OS_process_kill (identity, SIGTERM));
    GNUNET_OS_process_destroy (identity);
    identity = NULL;
  }
}


/**
 * Write configuration to dis, (re)start the namestore process and
 * reload the namestore models.
 *
 *
 * @param cls closure (unused)
 * @param tc scheduler context (unused)
 */
void
GNUNET_SETUP_restart_namestore (void *cls,
				const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  if (GNUNET_OK != write_configuration ())
    return; /* no point in re-starting namestore ... */
  if (NULL != namestore)
  {
    GNUNET_break (0 == GNUNET_OS_process_kill (namestore, SIGTERM));
    GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (namestore));
    GNUNET_OS_process_destroy (namestore);
    namestore = NULL;
  }
  namestore =
    GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ALL,
                             NULL, NULL,
                             "gnunet-service-namestore",
			     "gnunet-service-namestore", NULL);
}


/**
 * Callback invoked if the application is supposed to exit.
 */
void
GNUNET_SETUP_quit_cb (GObject * object, gpointer user_data)
{
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * If the test failed, start the resolver process.
 *
 * @param cls closure, NULL
 * @param result #GNUNET_YES if the service is running
 */
static void
start_resolver (void *cls,
		int result)
{
  char *binary;

  if (GNUNET_YES == result)
    return;
  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
  resolver =
    GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ALL,
                             NULL,NULL,
                             binary,
			     "gnunet-service-resolver", NULL);
  GNUNET_free (binary);
}


/**
 * If the test failed, start the identity process.
 *
 * @param cls closure, NULL
 * @param result #GNUNET_YES if the service is running
 */
static void
start_identity (void *cls,
		int result)
{
  char *binary;

  if (GNUNET_YES == result)
    return;
  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-identity");
  identity =
    GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ALL,
                             NULL, NULL,
                             binary,
			     "gnunet-service-identity", NULL);
  GNUNET_free (binary);
}


/**
 * If the test failed, start the namestore process.
 *
 * @param cls closure, NULL
 * @param result #GNUNET_YES if the service is running
 */
static void
start_namestore (void *cls,
		 int result)
{
  char *binary;

  if (GNUNET_YES == result)
    return;
  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-namestore");
  namestore =
    GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_ALL,
                             NULL, NULL,
                             binary,
			     "gnunet-service-namestore", NULL);
  GNUNET_free (binary);
}



#ifndef MINGW
/**
 * Try elevating user priviledges to run as user 'gnunet' or 'root'.
 *
 * @param username user gnunet-setup should be run as
 * @return GNUNET_OK on success
 */
static int
try_gksu (const char *username)
{
  struct GNUNET_OS_Process *proc;

  proc = GNUNET_OS_start_process (GNUNET_NO,
				  GNUNET_OS_INHERIT_STD_ALL,
				  NULL, NULL,
				  "gksu",
				  "-u", username,
				  "-m", _("Enter YOUR password to run gnunet-setup as user 'gnunet' (assuming 'sudo' allows it)"),
				  "-D", _("Enter YOUR password to run gnunet-setup as user 'gnunet' (assuming 'sudo' allows it)"),
				  NULL);
  if (NULL == proc)
    return GNUNET_SYSERR;
  GNUNET_OS_process_wait (proc);
  GNUNET_OS_process_destroy (proc);
  return GNUNET_OK;
}
#endif


/**
 * User clicked on the button to edit the list of friends.
 * Launch gnunet-peerinfo-gtk.
 */
void
GNUNET_setup_launch_edit_friends_button_clicked_cb (GtkButton *button,
                                                    gpointer *user_data)
{
  struct GNUNET_OS_Process *proc;

  proc = GNUNET_OS_start_process (GNUNET_NO,
				  GNUNET_OS_INHERIT_STD_ALL,
				  NULL, NULL,
				  "gnunet-peerinfo-gtk",
				  "-c", cfgName,
                                  NULL);
  if (NULL == proc)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Failed to launch gnunet-peerinfo-gtk\n"));
    return;
  }
  /* simply yield control, let it run */
  GNUNET_free (proc);
}


/**
 * Called once the autoconfiguration is done.  Triggers shutdown.
 *
 * @param cls unused
 */
static void
autoconfig_finished_cb (void *cls)
{
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Actual main method that sets up the configuration window.
 *
 * @param cls the main loop handle
 * @param tc scheduler context
 */
static void
run (void *cls,
     const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  GtkWidget *main_window;
#ifndef MINGW
  uid_t my_uid;
  struct passwd *gnunet_pw;


  my_uid = getuid ();
  gnunet_pw = getpwnam ("gnunet");
  if ( (0 != do_gksu) &&
       (0 != my_uid) &&
       (NULL != gnunet_pw) &&
       (my_uid != gnunet_pw->pw_uid) &&
       (GNUNET_OK == try_gksu ("gnunet")))
  {
    GNUNET_GTK_main_loop_quit (cls);
    return;
  }
#endif

  ml = cls;
  if (GNUNET_OK != GNUNET_GTK_main_loop_build_window (ml, NULL))
    return;

  cfgName = GNUNET_GTK_main_loop_get_configuration_file (ml);
  cfg = GNUNET_CONFIGURATION_create ();

#ifndef MINGW
  if ( (0 != my_uid) &&
       (NULL != gnunet_pw) &&
       (my_uid != gnunet_pw->pw_uid) )
  {
    /* load system defaults, system/'gnunet' user's configuration and THEN
       our configuration file */
    if (GNUNET_YES ==
	GNUNET_DISK_file_test ("/etc/gnunet.conf"))
      (void) GNUNET_CONFIGURATION_load (cfg, "/etc/gnunet.conf");
    else
    {
#if HAVE_GETPWNAM
      struct passwd *pw;

      pw = getpwnam ("gnunet");
      if (NULL != pw)
      {
	char *cfgname;

	GNUNET_asprintf (&cfgname,
			 "%s%s%s",
			 pw->pw_dir,
			 DIR_SEPARATOR_STR,
			 ".gnunet/gnunet.conf");
	(void) GNUNET_CONFIGURATION_load (cfg, cfgname);
	GNUNET_free (cfgname);
      }
#endif
    }
    (void) GNUNET_CONFIGURATION_parse (cfg, cfgName);
  }
  else
#endif
  {
    /* only load system defaults and our configuration file */
    (void) GNUNET_CONFIGURATION_load (cfg, cfgName);
  }
  main_window = GTK_WIDGET (GNUNET_SETUP_get_object ("GNUNET_setup_dialog"));
  main_window = GNUNET_GTK_plug_me ("GNUNET_FS_GTK_PLUG",
                                    main_window);
  GNUNET_CLIENT_service_test ("resolver",
			      cfg,
			      GNUNET_CONSTANTS_SERVICE_TIMEOUT,
			      &start_resolver, NULL);
  GNUNET_CLIENT_service_test ("namestore",
			      cfg,
			      GNUNET_CONSTANTS_SERVICE_TIMEOUT,
			      &start_namestore, NULL);
  GNUNET_CLIENT_service_test ("identity",
			      cfg,
			      GNUNET_CONSTANTS_SERVICE_TIMEOUT,
			      &start_identity, NULL);
  load_options ();
  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
				&cleanup_task, NULL);
  if (do_autoconfig)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
		_("Attempting fully-automatic, non-interactive network configuration\n"));
    GNUNET_setup_transport_autoconfig_start (&autoconfig_finished_cb, NULL);
    return;
  }
  gtk_widget_show (main_window);
  gtk_window_present (GTK_WINDOW (main_window));
}


/**
 * Main function for gnunet-setup.
 *
 * @param argc number of arguments
 * @param argv arguments
 * @return 0 on success
 */
int
main (int argc, char *const *argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    { 'a', "autoconfig", NULL,
      gettext_noop ("attempt automatic configuration of the network and instantly exit"),
      0, &GNUNET_GETOPT_set_one, &do_autoconfig },
#ifndef MINGW
    { 'e', "elevate-priviledges", NULL,
      gettext_noop ("run as user 'gnunet', if necessary by executing gksu to elevate rights"),
      0, &GNUNET_GETOPT_set_one, &do_gksu },
#endif
    GNUNET_GETOPT_OPTION_END
  };
  int ret;

  if (GNUNET_OK ==
      GNUNET_GTK_main_loop_start ("gnunet-setup", "gnunet-setup", argc, argv,
                                  options, "gnunet_setup_main_window.glade",
                                  &run))
    ret = gret;
  else
    ret = 1;
  return ret;
}

/* end of gnunet-setup.c */