~ubuntu-branches/ubuntu/quantal/keyutils/quantal

« back to all changes in this revision

Viewing changes to keyctl.c

  • Committer: Package Import Robot
  • Author(s): Daniel Baumann
  • Date: 2012-04-16 09:45:04 UTC
  • mfrom: (10.2.6 sid)
  • Revision ID: package-import@ubuntu.com-20120416094504-in50o9m1zbwvrmrs
Tags: 1.5.5-2
* Adding patch from Simon Ruderich <simon@ruderich.org> to fix FTBFS
  with hardening flags (Closes: #661393).
* Updating to standards version 3.9.3.
* Updating copyright file machine-readable format version 1.0.
* Tightening debhelper install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
        const char      *format;
28
28
};
29
29
 
 
30
static int act_keyctl___version(int argc, char *argv[]);
30
31
static int act_keyctl_show(int argc, char *argv[]);
31
32
static int act_keyctl_add(int argc, char *argv[]);
32
33
static int act_keyctl_padd(int argc, char *argv[]);
63
64
static int act_keyctl_purge(int argc, char *argv[]);
64
65
 
65
66
const struct command commands[] = {
 
67
        { act_keyctl___version, "--version",    "" },
66
68
        { act_keyctl_add,       "add",          "<type> <desc> <data> <keyring>" },
67
69
        { act_keyctl_chgrp,     "chgrp",        "<key> <gid>" },
68
70
        { act_keyctl_chown,     "chown",        "<key> <uid>" },
97
99
        { NULL,                 "session",      "- [<prog> <arg1> <arg2> ...]" },
98
100
        { NULL,                 "session",      "<name> [<prog> <arg1> <arg2> ...]" },
99
101
        { act_keyctl_setperm,   "setperm",      "<key> <mask>" },
100
 
        { act_keyctl_show,      "show",         "" },
 
102
        { act_keyctl_show,      "show",         "[-x] [<keyring>]" },
101
103
        { act_keyctl_timeout,   "timeout",      "<key> <timeout>" },
102
104
        { act_keyctl_unlink,    "unlink",       "<key> [<keyring>]" },
103
105
        { act_keyctl_update,    "update",       "<key> <data>" },
104
106
        { NULL,                 NULL,           NULL }
105
107
};
106
108
 
107
 
static int dump_key_tree(key_serial_t keyring, const char *name);
 
109
static int dump_key_tree(key_serial_t keyring, const char *name, int hex_key_IDs);
108
110
static void format(void) __attribute__((noreturn));
109
111
static void error(const char *msg) __attribute__((noreturn));
110
112
static key_serial_t get_key_id(const char *arg);
223
225
 
224
226
/*****************************************************************************/
225
227
/*
 
228
 * Display version information
 
229
 */
 
230
static int act_keyctl___version(int argc, char *argv[])
 
231
{
 
232
        printf("keyctl from %s (Built %s)\n",
 
233
               keyutils_version_string, keyutils_build_string);
 
234
        return 0;
 
235
}
 
236
 
 
237
/*****************************************************************************/
 
238
/*
226
239
 * grab data from stdin
227
240
 */
228
 
static char *grab_stdin(void)
 
241
static char *grab_stdin(size_t *_size)
229
242
{
230
243
        static char input[65536 + 1];
231
244
        int n, tmp;
249
262
        }
250
263
 
251
264
        input[n] = '\0';
 
265
        *_size = n;
252
266
 
253
267
        return input;
254
268
 
306
320
 */
307
321
static int act_keyctl_show(int argc, char *argv[])
308
322
{
309
 
        if (argc != 1)
 
323
        key_serial_t keyring = KEY_SPEC_SESSION_KEYRING;
 
324
        int hex_key_IDs = 0;
 
325
 
 
326
        if (argc >= 2 && strcmp(argv[1], "-x") == 0) {
 
327
                hex_key_IDs = 1;
 
328
                argc--;
 
329
                argv++;
 
330
        }
 
331
 
 
332
        if (argc > 2)
310
333
                format();
311
334
 
312
 
        dump_key_tree(KEY_SPEC_SESSION_KEYRING, "Session Keyring");
 
335
        if (argc == 2)
 
336
                keyring = get_key_id(argv[1]);
 
337
 
 
338
        dump_key_tree(keyring, argc == 2 ? "Keyring" : "Session Keyring", hex_key_IDs);
313
339
        return 0;
314
340
 
315
341
} /* end act_keyctl_show() */
344
370
 */
345
371
static int act_keyctl_padd(int argc, char *argv[])
346
372
{
347
 
        char *args[6];
 
373
        key_serial_t dest;
 
374
        size_t datalen;
 
375
        void *data;
 
376
        int ret;
 
377
 
348
378
 
349
379
        if (argc != 4)
350
380
                format();
351
381
 
352
 
        args[0] = argv[0];
353
 
        args[1] = argv[1];
354
 
        args[2] = argv[2];
355
 
        args[3] = grab_stdin();
356
 
        args[4] = argv[3];
357
 
        args[5] = NULL;
358
 
 
359
 
        return act_keyctl_add(5, args);
 
382
        dest = get_key_id(argv[3]);
 
383
 
 
384
        data = grab_stdin(&datalen);
 
385
 
 
386
        ret = add_key(argv[1], argv[2], data, datalen, dest);
 
387
        if (ret < 0)
 
388
                error("add_key");
 
389
 
 
390
        /* print the resulting key ID */
 
391
        printf("%d\n", ret);
 
392
        return 0;
360
393
 
361
394
} /* end act_keyctl_padd() */
362
395
 
420
453
static int act_keyctl_prequest2(int argc, char *argv[])
421
454
{
422
455
        char *args[6];
 
456
        size_t datalen;
423
457
 
424
458
        if (argc != 3 && argc != 4)
425
459
                format();
427
461
        args[0] = argv[0];
428
462
        args[1] = argv[1];
429
463
        args[2] = argv[2];
430
 
        args[3] = grab_stdin();
 
464
        args[3] = grab_stdin(&datalen);
431
465
        args[4] = argv[3];
432
466
        args[5] = NULL;
433
467
 
461
495
 */
462
496
static int act_keyctl_pupdate(int argc, char *argv[])
463
497
{
464
 
        char *args[4];
 
498
        key_serial_t key;
 
499
        size_t datalen;
 
500
        void *data;
465
501
 
466
502
        if (argc != 2)
467
503
                format();
468
504
 
469
 
        args[0] = argv[0];
470
 
        args[1] = argv[1];
471
 
        args[2] = grab_stdin();
472
 
        args[3] = NULL;
473
 
 
474
 
        return act_keyctl_update(3, args);
 
505
        key = get_key_id(argv[1]);
 
506
        data = grab_stdin(&datalen);
 
507
 
 
508
        if (keyctl_update(key, data, datalen) < 0)
 
509
                error("keyctl_update");
 
510
 
 
511
        return 0;
475
512
 
476
513
} /* end act_keyctl_pupdate() */
477
514
 
1134
1171
 */
1135
1172
static int act_keyctl_pinstantiate(int argc, char *argv[])
1136
1173
{
1137
 
        char *args[5];
 
1174
        key_serial_t key, dest;
 
1175
        size_t datalen;
 
1176
        void *data;
1138
1177
 
1139
1178
        if (argc != 3)
1140
1179
                format();
1141
1180
 
1142
 
        args[0] = argv[0];
1143
 
        args[1] = argv[1];
1144
 
        args[2] = grab_stdin();
1145
 
        args[3] = argv[2];
1146
 
        args[4] = NULL;
1147
 
 
1148
 
        return act_keyctl_instantiate(4, args);
 
1181
        key = get_key_id(argv[1]);
 
1182
        dest = get_key_id(argv[2]);
 
1183
        data = grab_stdin(&datalen);
 
1184
 
 
1185
        if (keyctl_instantiate(key, data, datalen, dest) < 0)
 
1186
                error("keyctl_instantiate");
 
1187
 
 
1188
        return 0;
1149
1189
 
1150
1190
} /* end act_keyctl_pinstantiate() */
1151
1191
 
1550
1590
/*
1551
1591
 * recursively display a key/keyring tree
1552
1592
 */
1553
 
static int dump_key_tree_aux(key_serial_t key, int depth, int more)
 
1593
static int dump_key_tree_aux(key_serial_t key, int depth, int more, int hex_key_IDs)
1554
1594
{
1555
1595
        static char dumpindent[64];
1556
1596
        key_serial_t *pk;
1604
1644
        /* and print */
1605
1645
        calc_perms(pretty_mask, perm, uid, gid);
1606
1646
 
1607
 
        printf("%9d %s  %5d %5d  %s%s%s: %s\n",
1608
 
               key,
1609
 
               pretty_mask,
1610
 
               uid, gid,
1611
 
               dumpindent,
1612
 
               depth > 0 ? "\\_ " : "",
1613
 
               type, desc + dpos);
 
1647
        if (hex_key_IDs)
 
1648
                printf("0x%08x %s  %5d %5d  %s%s%s: %s\n",
 
1649
                       key,
 
1650
                       pretty_mask,
 
1651
                       uid, gid,
 
1652
                       dumpindent,
 
1653
                       depth > 0 ? "\\_ " : "",
 
1654
                       type, desc + dpos);
 
1655
        else
 
1656
                printf("%10d %s  %5d %5d  %s%s%s: %s\n",
 
1657
                       key,
 
1658
                       pretty_mask,
 
1659
                       uid, gid,
 
1660
                       dumpindent,
 
1661
                       depth > 0 ? "\\_ " : "",
 
1662
                       type, desc + dpos);
1614
1663
 
1615
1664
        /* if it's a keyring then we're going to want to recursively
1616
1665
         * display it if we can */
1661
1710
 
1662
1711
                                kcount += dump_key_tree_aux(key,
1663
1712
                                                            rdepth,
1664
 
                                                            ringlen - 4 >= sizeof(key_serial_t));
 
1713
                                                            ringlen - 4 >= sizeof(key_serial_t),
 
1714
                                                            hex_key_IDs);
1665
1715
                        }
1666
1716
 
1667
1717
                } while (ringlen -= 4, ringlen >= sizeof(key_serial_t));
1678
1728
/*
1679
1729
 * recursively list a keyring's contents
1680
1730
 */
1681
 
static int dump_key_tree(key_serial_t keyring, const char *name)
 
1731
static int dump_key_tree(key_serial_t keyring, const char *name, int hex_key_IDs)
1682
1732
{
1683
1733
        printf("%s\n", name);
1684
 
        return dump_key_tree_aux(keyring, 0, 0);
 
1734
 
 
1735
        keyring = keyctl_get_keyring_ID(keyring, 0);
 
1736
        if (keyring == -1)
 
1737
                error("Unable to dump key");
 
1738
 
 
1739
        return dump_key_tree_aux(keyring, 0, 0, hex_key_IDs);
1685
1740
 
1686
1741
} /* end dump_key_tree() */