~ubuntu-branches/ubuntu/jaunty/libgksu1.2/jaunty

« back to all changes in this revision

Viewing changes to libgksu/gksu-context.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2006-07-07 15:30:14 UTC
  • mfrom: (1.1.4 upstream) (2.1.1 etch)
  • Revision ID: james.westby@ubuntu.com-20060707153014-r6qp2uiv3pzfh4g2
Tags: 1.3.8-1ubuntu1
* Sync with Debian
* debian/control.in:
  - Build-Depends on libstartup-notification0-dev
  - libgksu1.2-1 Conflicts with libgksu1.2-0
  - soname change
* debian/libgksu1.2-1.install, debian/libgksu1.2-1.shlibs:
  - soname change
* debian/patches/01_startup_notification.patch:
  - add startup notification
* debian/patches/99_autoreconf.patch:
  - autoreconf

Show diffs side-by-side

added added

removed removed

Lines of Context:
111
111
 * gksu_context_get_user:
112
112
 * @context: the #GksuContext from which to grab the information
113
113
 *
114
 
 * Gets the user the command will be run as, as set 
 
114
 * Gets the user the command will be run as, as set
115
115
 * by gksu_context_set_user.
116
116
 *
117
117
 * Returns: a pointer to the string containing the username.
144
144
 * gksu_context_get_command:
145
145
 * @context: the #GksuContext from which to grab the information
146
146
 *
147
 
 * Gets the command that will be run, as set by 
 
147
 * Gets the command that will be run, as set by
148
148
 * gksu_context_set_command.
149
149
 *
150
150
 * Returns: a pointer to the string containing the command.
246
246
 * Finds out if the environment in which the program will be
247
247
 * run will be reset.
248
248
 *
249
 
 * Returns: TRUE if the environment is going to be kept, 
 
249
 * Returns: TRUE if the environment is going to be kept,
250
250
 * FALSE otherwise.
251
251
 */
252
252
gboolean
423
423
 
424
424
  f = fopen(cmdfile, "r");
425
425
  g_free (cmdfile);
426
 
  if(f != NULL) 
 
426
  if(f != NULL)
427
427
    {
428
428
      fgets(cmdline, 255, f);
429
429
      cmdline[255] = '\0';
438
438
/**
439
439
 * prepare_xauth:
440
440
 *
441
 
 * Sets up the variables with values for the $DISPLAY 
 
441
 * Sets up the variables with values for the $DISPLAY
442
442
 * environment variable and xauth-related stuff. Also
443
443
 * creates a temporary directory to hold a .Xauthority
444
444
 *
447
447
static int
448
448
prepare_xauth (GksuContext *context)
449
449
{
 
450
  gchar *xauth_bin = NULL;
 
451
 
450
452
  FILE *xauth_output;
451
453
  gchar xauth[256] = {0};
452
454
 
458
460
  display = g_strdup (getenv ("DISPLAY"));
459
461
  tmpv = g_strsplit (display, ":", 3);
460
462
  g_free (display);
461
 
  
 
463
 
462
464
  context->display = g_strdup_printf (":%s", tmpv[1]);
463
 
  
 
465
 
464
466
  g_strfreev (tmpv);
465
467
 
 
468
  /* find out where the xauth binary is located */
 
469
  if (g_file_test ("/usr/bin/xauth", G_FILE_TEST_IS_EXECUTABLE))
 
470
    xauth_bin = "/usr/bin/xauth";
 
471
  else if (g_file_test ("/usr/X11R6/bin/xauth", G_FILE_TEST_IS_EXECUTABLE))
 
472
    xauth_bin = "/usr/X11R6/bin/xauth";
 
473
  else
 
474
    {
 
475
      fprintf (stderr,
 
476
               _("Failed to obtain xauth key: xauth binary not found "
 
477
                 "at usual locations"));
 
478
 
 
479
      return 1;
 
480
    }
 
481
 
466
482
  /* get the authorization token */
467
 
  tmp = g_strdup_printf ("/usr/X11R6/bin/xauth list %s | "
 
483
  tmp = g_strdup_printf ("%s list %s | "
468
484
                         "head -1 | awk '{ print $3 }'",
 
485
                         xauth_bin,
469
486
                         context->display);
470
487
  if ((xauth_output = popen (tmp, "r")) == NULL)
471
488
    {
483
500
 
484
501
  if (context->debug)
485
502
    {
486
 
      fprintf(stderr, 
 
503
      fprintf(stderr,
487
504
              "xauth: -%s-\n"
488
505
              "display: -%s-\n",
489
506
              context->xauth, context->display);
521
538
  int in, out;
522
539
  int r;
523
540
  char *newfn;
524
 
  char buf[BUFSIZ];
 
541
  char buf[BUFSIZ] = "";
525
542
 
526
543
  newfn = g_strdup_printf("%s/.Xauthority", dir);
527
544
 
528
545
  out = open(newfn, O_WRONLY | O_CREAT | O_EXCL, 0600);
529
 
  if (out == -1) 
 
546
  if (out == -1)
530
547
    {
531
548
      if (errno == EEXIST)
532
549
        fprintf (stderr,
540
557
 
541
558
      return TRUE;
542
559
    }
543
 
  
 
560
 
544
561
  in = open(fn, O_RDONLY);
545
562
  if (in == -1)
546
563
    {
549
566
               fn, dir, strerror(errno));
550
567
      return TRUE;
551
568
    }
552
 
  
553
 
  while ((r = read(in, buf, BUFSIZ)) > 0) 
 
569
 
 
570
  while ((r = read(in, buf, BUFSIZ)) > 0)
554
571
    {
555
572
      if (full_write(out, buf, r) == -1)
556
573
        {
585
602
      fprintf (stderr, strerror(errno));
586
603
      return 1;
587
604
    }
588
 
  
 
605
 
589
606
  xauth = g_strdup(g_getenv ("XAUTHORITY"));
590
607
  if (xauth == NULL)
591
608
    xauth = g_strdup_printf ("%s/.Xauthority", g_get_home_dir());
600
617
}
601
618
 
602
619
static void
603
 
sudo_reset_xauth (GksuContext *context, gchar *xauth, 
 
620
sudo_reset_xauth (GksuContext *context, gchar *xauth,
604
621
                  gchar *xauth_env)
605
622
{
606
623
  /* reset the env var as it was before or clean it  */
608
625
    setenv ("XAUTHORITY", xauth_env, TRUE);
609
626
  else
610
627
    unsetenv ("XAUTHORITY");
611
 
  
 
628
 
612
629
  if (context->debug)
613
630
    fprintf (stderr, "xauth: %s\nxauth_env: %s\ndir: %s\n",
614
631
             xauth, xauth_env, context->dir);
615
 
  
 
632
 
616
633
  unlink (xauth);
617
634
  rmdir (context->dir);
618
635
 
653
670
      /* executes the command */
654
671
      if (execv (cmd[0], cmd) == -1)
655
672
        {
656
 
          fprintf (stderr, 
 
673
          fprintf (stderr,
657
674
                   _("Unable to run /bin/su: %s"),
658
675
                   strerror(errno));
659
676
 
660
677
          for (i = 0 ; cmd[i] != NULL ; i++)
661
678
            g_free (cmd[i]);
662
679
          g_free(cmd);
663
 
          
 
680
 
664
681
          return TRUE;
665
682
        }
666
683
    }
675
692
      struct timeval tv;
676
693
      struct termios tio;
677
694
 
678
 
      gchar buf[256];
 
695
      gchar buf[256] = {0};
679
696
      gint status;
680
697
 
681
698
      FD_ZERO(&rfds);
687
704
      if (select (fdpty+1, &rfds, NULL, NULL, &tv) < 0)
688
705
        return TRUE;
689
706
 
690
 
      read (fdpty, buf, 256);
 
707
      read (fdpty, buf, 255);
691
708
      if (context->debug)
692
709
        fprintf (stderr, "try_su_run: buf: -%s-\n", buf);
693
710
 
694
711
      usleep (100);
695
712
 
696
713
      tcgetattr (fdpty, &tio);
697
 
      
 
714
 
698
715
      if ((tio.c_lflag & ECHO))
699
716
        need_pass = FALSE;
700
717
 
749
766
 * Returns: the child's error status, 0 if all went fine, -1 if failed
750
767
 */
751
768
gboolean
752
 
gksu_context_run_full (GksuContext *context, 
753
 
                       GksuAskPasswordFunc ask_pass, gpointer user_data, 
754
 
                       GksuPasswordNotNeededFunc pass_not_needed, 
 
769
gksu_context_run_full (GksuContext *context,
 
770
                       GksuAskPasswordFunc ask_pass, gpointer user_data,
 
771
                       GksuPasswordNotNeededFunc pass_not_needed,
755
772
                       gpointer pnn_user_data, GError **error)
756
773
{
757
774
  GQuark gksu_quark;
780
797
      return -1;
781
798
    }
782
799
 
783
 
  /* 
 
800
  /*
784
801
     FIXME: need to check if we are in X and also need to
785
802
     provide GError stuff
786
803
  */
804
821
      cmd[i] = g_strdup ("-c"); i++;
805
822
 
806
823
      /* needs to get X authorization prior to running the program */
807
 
      cmd[i] = g_strdup_printf ("%s \"%s\"", auxcommand, 
 
824
      cmd[i] = g_strdup_printf ("%s \"%s\"", auxcommand,
808
825
                                context->command); i++;
809
826
 
810
827
      cmd[i] = NULL;
812
829
      /* executes the command */
813
830
      if (execv (cmd[0], cmd) == -1)
814
831
        {
815
 
          fprintf (stderr, 
 
832
          fprintf (stderr,
816
833
                   _("Unable to run /bin/su: %s"),
817
834
                   strerror(errno));
818
835
        }
858
875
              struct termios tio;
859
876
 
860
877
              usleep (100);
861
 
              read (fdpty, buf, 256);
 
878
              read (fdpty, buf, 255);
862
879
              if (context->debug)
863
880
                fprintf (stderr, "gksu_context_run: buf: -%s-\n", buf);
864
881
 
879
896
                  usleep (100);
880
897
 
881
898
                  password_needed = TRUE;
882
 
                  write (fdpty, context->password, 
 
899
                  write (fdpty, context->password,
883
900
                         strlen(context->password));
884
901
                }
885
902
            }
887
904
 
888
905
      if (!password_needed)
889
906
        pass_not_needed (context, pnn_user_data);
890
 
      
 
907
 
891
908
      if (context->debug)
892
909
        fprintf (stderr, "DEBUG (run:after-pass) buf: -%s-\n", buf);
893
910
      if (strncmp (buf, "gksu", 4) && strncmp (buf, "su", 2))
904
921
 
905
922
      if (!strncmp (buf, "su: Authentication failure", 26))
906
923
        {
907
 
          g_set_error (error, gksu_quark, 
 
924
          g_set_error (error, gksu_quark,
908
925
                       GKSU_CONTEXT_ERROR_WRONGPASS,
909
926
                       _("Wrong password."));
910
927
          if (context->debug)
913
930
      else if (!strncmp (buf, "gksu: waiting", 13))
914
931
        {
915
932
          gchar *line;
916
 
          
 
933
 
917
934
          if (context->debug)
918
935
            fprintf (stderr, "DEBUG (gksu: waiting) buf: -%s-\n", buf);
919
936
 
920
937
          line = g_strdup_printf ("gksu-run: %s\n", context->display);
921
938
          write (fdpty, line, strlen(line));
922
939
          g_free (line);
923
 
          
 
940
 
924
941
          line = g_strdup_printf ("gksu-run: %s\n", context->xauth);
925
942
          write (fdpty, line, strlen(line));
926
943
          g_free (line);
1009
1026
                }
1010
1027
 
1011
1028
              if (error == NULL)
1012
 
                g_set_error (error, gksu_quark, 
 
1029
                g_set_error (error, gksu_quark,
1013
1030
                             GKSU_CONTEXT_ERROR_CHILDFAILED,
1014
1031
                             _("su terminated with %d status"),
1015
1032
                             WEXITSTATUS(status));
1025
1042
 
1026
1043
/* DO NOT USE -- Deprecated */
1027
1044
gboolean
1028
 
gksu_context_ask_and_run (GksuContext *context, GksuAskPasswordFunc ask_pass, 
 
1045
gksu_context_ask_and_run (GksuContext *context, GksuAskPasswordFunc ask_pass,
1029
1046
                          gpointer user_data, GError **error)
1030
1047
{
1031
1048
  return gksu_context_run_full (context, ask_pass, user_data, NULL, NULL, error);
1068
1085
  int child_pipe[2];    /* For talking to the child */
1069
1086
 
1070
1087
  gboolean need_pass = TRUE;
1071
 
  
 
1088
 
1072
1089
  if ((pipe(parent_pipe)) == -1)
1073
1090
    return TRUE;
1074
1091
 
1076
1093
    return TRUE;
1077
1094
 
1078
1095
  cmd = g_new (gchar *, argcount + 1);
1079
 
  
 
1096
 
1080
1097
  argcount = 0;
1081
1098
 
1082
1099
  /* sudo binary */
1104
1121
  pid = fork();
1105
1122
  if (pid == -1)
1106
1123
    return TRUE;
1107
 
  else if (pid == 0) 
 
1124
  else if (pid == 0)
1108
1125
    {
1109
1126
      // Child
1110
1127
      close(child_pipe[1]);
1111
1128
      dup2(child_pipe[0], STDIN_FILENO);
1112
1129
      dup2(parent_pipe[1], STDERR_FILENO);
1113
 
      
 
1130
 
1114
1131
      execv(cmd[0], cmd);
1115
 
      
 
1132
 
1116
1133
      return TRUE;
1117
 
    } 
1118
 
  else 
 
1134
    }
 
1135
  else
1119
1136
    {
1120
1137
      // Parent
1121
1138
      close(parent_pipe[1]);
1122
 
      
 
1139
 
1123
1140
      infile = fdopen(parent_pipe[0], "r");
1124
1141
      if (!infile)
1125
1142
        return TRUE;
1139
1156
      if (context->debug)
1140
1157
        fprintf (stderr, "buffer: -%s-\n", buffer);
1141
1158
 
1142
 
      if (strncmp(buffer, "GNOME_SUDO_PASS", 15) == 0) 
 
1159
      if (strncmp(buffer, "GNOME_SUDO_PASS", 15) == 0)
1143
1160
        {
1144
1161
          if (context->debug)
1145
1162
            fprintf (stderr, "We'll need a password...\n");
1146
1163
 
1147
1164
          fprintf (outfile, "\n");
1148
 
        } 
1149
 
      else 
 
1165
        }
 
1166
      else
1150
1167
        {
1151
1168
          if (context->debug)
1152
1169
            fprintf (stderr, "We won't need a password, it seems!\n");
1204
1221
 * Returns: the child's error status, 0 if all went fine, -1 if failed
1205
1222
 */
1206
1223
gboolean
1207
 
gksu_context_sudo_run_full (GksuContext *context, 
1208
 
                            GksuAskPasswordFunc ask_pass, gpointer user_data, 
1209
 
                            GksuPasswordNotNeededFunc pass_not_needed, 
 
1224
gksu_context_sudo_run_full (GksuContext *context,
 
1225
                            GksuAskPasswordFunc ask_pass, gpointer user_data,
 
1226
                            GksuPasswordNotNeededFunc pass_not_needed,
1210
1227
                            gpointer pnn_user_data, GError **error)
1211
1228
{
1212
1229
  char **cmd;
1216
1233
 
1217
1234
  GQuark gksu_quark;
1218
1235
 
1219
 
  gchar *xauth = NULL, 
 
1236
  gchar *xauth = NULL,
1220
1237
    *xauth_env = NULL;
1221
1238
 
1222
1239
  pid_t pid;
1225
1242
  FILE *infile, *outfile;
1226
1243
  int parent_pipe[2];   /* For talking to the parent */
1227
1244
  int child_pipe[2];    /* For talking to the child */
1228
 
  
 
1245
 
1229
1246
  gksu_quark = g_quark_from_string (PACKAGE);
1230
1247
 
1231
1248
  if (!context->command)
1236
1253
      return -1;
1237
1254
    }
1238
1255
 
1239
 
  /* 
 
1256
  /*
1240
1257
     FIXME: need to check if we are in X
1241
1258
  */
1242
1259
  if (sudo_prepare_xauth (context) == 1)
1255
1272
    fprintf (stderr, "xauth: %s\n", xauth);
1256
1273
 
1257
1274
  cmd = g_new (gchar *, argcount + 1);
1258
 
  
 
1275
 
1259
1276
  argcount = 0;
1260
1277
 
1261
1278
  /* sudo binary */
1313
1330
            inside_quotes = !inside_quotes;
1314
1331
          }
1315
1332
 
1316
 
        if ((context->command[i] == ' ' && inside_quotes == FALSE) 
 
1333
        if ((context->command[i] == ' ' && inside_quotes == FALSE)
1317
1334
            || context->command[i] == '\0')
1318
1335
          {
1319
1336
            tmp_arg = g_realloc (tmp_arg, sizeof(gchar)*(j+1));
1378
1395
      sudo_reset_xauth (context, xauth, xauth_env);
1379
1396
      return -1;
1380
1397
    }
1381
 
  else if (pid == 0) 
 
1398
  else if (pid == 0)
1382
1399
    {
1383
1400
      // Child
1384
1401
      setsid();   // make us session leader
1385
1402
      close(child_pipe[1]);
1386
1403
      dup2(child_pipe[0], STDIN_FILENO);
1387
1404
      dup2(parent_pipe[1], STDERR_FILENO);
1388
 
      
 
1405
 
1389
1406
      execv(cmd[0], cmd);
1390
 
      
 
1407
 
1391
1408
      g_set_error (error, gksu_quark, GKSU_CONTEXT_ERROR_EXEC,
1392
1409
                   _("Failed to exec new process: %s"),
1393
1410
                   strerror(errno));
1394
1411
      sudo_reset_xauth (context, xauth, xauth_env);
1395
1412
      return -1;
1396
 
    } 
1397
 
  else 
 
1413
    }
 
1414
  else
1398
1415
    {
1399
1416
      gchar *cmdline = NULL;
1400
1417
 
1401
1418
      // Parent
1402
1419
      close(parent_pipe[1]);
1403
 
      
 
1420
 
1404
1421
      infile = fdopen(parent_pipe[0], "r");
1405
1422
      if (!infile)
1406
1423
        {
1431
1448
      if (context->debug)
1432
1449
        fprintf (stderr, "buffer: -%s-\n", buffer);
1433
1450
 
1434
 
      if (strncmp(buffer, "GNOME_SUDO_PASS", 15) == 0) 
 
1451
      if (strncmp(buffer, "GNOME_SUDO_PASS", 15) == 0)
1435
1452
        {
1436
1453
          if (context->debug)
1437
1454
            fprintf (stderr, "Yeah, we're in...\n");
1450
1467
            {
1451
1468
              if (ask_pass (context, _("Password: "), user_data, error))
1452
1469
                return -1;
1453
 
              
 
1470
 
1454
1471
              g_return_val_if_fail (context->password, -1);
1455
1472
            }
1456
 
          
 
1473
 
1457
1474
          usleep (100);
1458
1475
 
1459
1476
          fprintf (outfile, "%s", context->password);
1464
1481
          */
1465
1482
          fgets (buffer, 255, infile);
1466
1483
          fgets (buffer, 255, infile);
1467
 
        } 
1468
 
      else 
 
1484
        }
 
1485
      else
1469
1486
        {
1470
1487
          if (context->debug)
1471
1488
            fprintf (stderr, "No password prompt found; we'll assume we don't need a password.\n");
1511
1528
 
1512
1529
          bzero(buffer, 256);
1513
1530
          if(!fgets (buffer, 255, infile))
1514
 
            break;
 
1531
            continue;
1515
1532
          fprintf (stderr, "%s", buffer);
1516
1533
        }
1517
1534
 
1524
1541
          fprintf (stderr, "%s", buffer);
1525
1542
          fflush (stderr);
1526
1543
        }
1527
 
      
 
1544
 
1528
1545
      sudo_reset_xauth (context, xauth, xauth_env);
1529
1546
 
1530
1547
      if (WIFEXITED(status))
1545
1562
                }
1546
1563
 
1547
1564
              if (error == NULL)
1548
 
                g_set_error (error, gksu_quark, 
 
1565
                g_set_error (error, gksu_quark,
1549
1566
                             GKSU_CONTEXT_ERROR_CHILDFAILED,
1550
1567
                             _("sudo terminated with %d status"),
1551
1568
                             WEXITSTATUS(status));
1552
1569
            }
1553
1570
        }
1554
1571
    }
1555
 
  
 
1572
 
1556
1573
  /* if error is set we have found an error condition */
1557
1574
  if (error)
1558
1575
    return TRUE;
1562
1579
 
1563
1580
/* DO NOT USE -- Deprecated */
1564
1581
gboolean
1565
 
gksu_context_sudo_ask_and_run (GksuContext *context, GksuAskPasswordFunc ask_pass, 
 
1582
gksu_context_sudo_ask_and_run (GksuContext *context, GksuAskPasswordFunc ask_pass,
1566
1583
                               gpointer user_data, GError **error)
1567
1584
{
1568
1585
  return gksu_context_sudo_run_full (context, ask_pass, user_data, NULL, NULL, error);