File: lcap.c

package info (click to toggle)
lcap 0.0.6-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 68 kB
  • ctags: 24
  • sloc: ansic: 359; makefile: 59
file content (431 lines) | stat: -rw-r--r-- 10,985 bytes parent folder | download
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
/*
  LCAP:  Linux Kernel Capability Remover
  Copyright (C) 1999  spoon@ix.netcom.com
  Copyright (C) 2000  spoon@ix.netcom.com
  
  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
  of the License, 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
*/

/* $Id: lcap.c,v 1.13 2001/03/05 22:53:27 spoon Exp $ */

#include <stdio.h>
#include <linux/capability.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <stdlib.h>
#include <ctype.h>

#define PROC_CAP "/proc/sys/kernel/cap-bound"

#ifdef LIDS
  #define PROC_LIDS "/proc/sys/lids"
#endif

typedef __u32 kernel_cap_t;

#define CAP_FULL_SET (~0)
#define CAP_TO_MASK(flag) (1 << (flag))
#define cap_raise(cap, flag)  (cap |= CAP_TO_MASK(flag))
#define cap_lower(cap, flag)  (cap &= ~CAP_TO_MASK(flag))
#define cap_raised(cap, flag) (cap & CAP_TO_MASK(flag) & CAP_FULL_SET)

#ifdef LIDS
  #define lids_raise(lidsnum) lids_set(lidsnum, 1)
  #define lids_lower(lidsnum) lids_set(lidsnum, 0)
#endif

  static char *thecaps[] =
    {
    "CAP_CHOWN",            "chown(2)/chgrp(2)",
    "CAP_DAC_OVERRIDE",     "DAC access",
    "CAP_DAC_READ_SEARCH",  "DAC read",
    "CAP_FOWNER",           "owner ID not equal user ID",
    "CAP_FSETID",           "effective user ID not equal owner ID",
    "CAP_KILL",             "real/effective ID not equal process ID",
    "CAP_SETGID",           "setgid(2)",
    "CAP_SETUID",           "set*uid(2)",
    "CAP_SETPCAP",          "transfer capability",
    "CAP_LINUX_IMMUTABLE",  "immutable and append file attributes",
    "CAP_NET_BIND_SERVICE", "binding to ports below 1024",
    "CAP_NET_BROADCAST",    "broadcasting/listening to multicast",
    "CAP_NET_ADMIN",        "interface/firewall/routing changes",
    "CAP_NET_RAW",          "raw sockets",
    "CAP_IPC_LOCK",         "locking of shared memory segments",
    "CAP_IPC_OWNER",        "IPC ownership checks",
    "CAP_SYS_MODULE",       "insertion and removal of kernel modules",
    "CAP_SYS_RAWIO",        "ioperm(2)/iopl(2) access",
    "CAP_SYS_CHROOT",       "chroot(2)",
    "CAP_SYS_PTRACE",       "ptrace(2)",
    "CAP_SYS_PACCT",        "configuration of process accounting",
    "CAP_SYS_ADMIN",        "tons of admin stuff",
    "CAP_SYS_BOOT",         "reboot(2)",
    "CAP_SYS_NICE",         "nice(2)",
    "CAP_SYS_RESOURCE",     "setting resource limits",
    "CAP_SYS_TIME",         "setting system time",
    "CAP_SYS_TTY_CONFIG",   "tty configuration",
    NULL, NULL,
    };

  #ifdef LIDS
    static char *lidscaps[] =
      {
      "LIDS_INIT",     "lock_init_children",     "protect init children",
      "LIDS_FIREWALL", "lock_ip_firewall_rules", "lock IP firewall rules",
      "LIDS_MODULE",   "lock_modules",           "disallow insetion and "
                                                 "removal of kernel modules",
      "LIDS_MOUNT",    "lock_mount",             "disallow mount/umount "
                                                 "filesystems",
/*      "LIDS_RELOAD",   "reload_conf",            "reload configuration",*/
      NULL, NULL,
      };
  #endif /* LIDS */

  static void usage(const char *argv0)
    {
    fprintf(stderr, "Usage: %s [-h]\n", argv0);
    fprintf(stderr, "       -h = you're looking at it\n");
    fprintf(stderr, "Usage: %s [-v[v]] -c cap\n", argv0);
    fprintf(stderr, "       -v = verbose mode\n");
    fprintf(stderr, "       -c = return 1 if capability is set, 0 if not\n");
    fprintf(stderr, "       cap = capability to check\n");
    fprintf(stderr, "Usage: %s [-v[v]] [-z] cap ...\n", argv0);
    fprintf(stderr, "       -v = verbose mode\n");
    fprintf(stderr, "       -z = zero out capability set\n");
    fprintf(stderr, "       cap = with -z, capabilities to not zero out\n");
    fprintf(stderr, "       cap = without -z, capabilities to zero out\n");
    };

  static void listcaps(const kernel_cap_t caps)
    {
    int index=0;

    printf("Current capabilities: 0x%08X\n", caps);
    while (thecaps[index*2] != NULL)
      {
      printf("  %2d) %c%-25s", index, cap_raised(caps, index) ? '*' : ' ',
             thecaps[index*2]);
      if ((index) % 2)
        printf("\n");
      index++;
      };
    printf("\n");
    printf("    * = Capabilities currently allowed\n");
    };

  #ifdef LIDS
    void lids_set(int lidsnum, int value)
      {
      FILE *fptr;
      char file[PATH_MAX];

      snprintf(file, PATH_MAX, "%s/%s", PROC_LIDS, lidscaps[lidsnum*3+1]);
      if ((fptr=fopen(file, "w")) == NULL)
        {
        perror(file);
        exit(errno);
        };
      fprintf(fptr, "%d", value);
      fclose(fptr);
      };

    int lids_raised(int lidsnum)
      {
      FILE *fptr;
      int lids;
      char file[PATH_MAX];

      snprintf(file, PATH_MAX, "%s/%s", PROC_LIDS, lidscaps[lidsnum*3+1]);
      if ((fptr=fopen(file, "r")) == NULL)
        {
        perror(file);
        exit(errno);
        };
      fscanf(fptr, "%d", &lids);
      fclose(fptr);
      
      return(lids);
      };

    static void listlids()
      {
      int index=0;

      while (lidscaps[index*3] != NULL)
        {
        printf("      %c%-25s", 
               lids_raised(index) ? '*' : ' ', lidscaps[index*3]);
        if ((index) % 2)
          printf("\n");
        index++;
        };
      printf("    * = Functionality currently set\n");
      };
  #endif /* LIDS */

int main(int argc, char *argv[])
  {
  kernel_cap_t caps=0;
  FILE *fptr;
  int index;
  int option;
  short checkflag=0, errorflag=0, verboseflag=0, zeroflag=0;

  /* open the /proc file */
  if ((fptr=fopen(PROC_CAP, "r")) == NULL)
    {
    perror(PROC_CAP);
    exit(errno);
    };
  /* snag the current setting */
  fscanf(fptr, "%d", &caps);
  fclose(fptr);

  while ((option=getopt(argc, argv, "chvz")) != -1)
    {
    switch (option)
      {
      case 'c':
        checkflag = 1;
        break;
      case 'v':
        verboseflag++;
        break;
      case 'z':
        zeroflag = 1;
        break;
      case 'h':
      default:
        errorflag = 1;
        break;
      };
    };

  if (((checkflag) && (optind+1 != argc)) ||
      ((argc == optind) && (argc > 1)) ||
      (errorflag))
    {
    usage(argv[0]);
    exit(1);
    };

  if (argc == 1)
    {
    listcaps(caps);
    #ifdef LIDS
      printf("\n");
      listlids();
    #endif
    exit(0);
    };

  if (verboseflag >= 1)
    {
    printf("Current capabilities: 0x%08X\n", caps);
    };

  if (checkflag)
    {
    int capnum=-1;
    #ifdef LIDS
      short islids=0;
    #endif
    char *capstr=NULL, *capdescstr=NULL;

    if (isdigit(argv[optind][0]))
      {
      capnum = (int)strtol(argv[optind], (char **)NULL, 10);
      capstr = thecaps[capnum*2];
      capdescstr = thecaps[capnum*2+1];
      }
    else
      {
      int jndex=0;

      while (thecaps[jndex] != NULL)
        {
        if (!strcmp(argv[optind], thecaps[jndex]))
          {
          capnum = jndex/2;
          capstr = thecaps[jndex];
          capdescstr = thecaps[jndex+1];
          };
        jndex += 2;
        };

      #ifdef LIDS
        jndex = 0;

        while (lidscaps[jndex] != NULL)
          {
          if (!strcmp(argv[optind], lidscaps[jndex]))
            {
            capnum = jndex/3;
            capstr = lidscaps[jndex];
            capdescstr = lidscaps[jndex+2];
            islids = 1;
            };
          jndex += 3;
          };
      #endif
      };

    if (capnum == -1)
      {
      fprintf(stderr, "    %s: invalid capability\n", argv[2]);
      exit(1);
      };

    #ifdef LIDS
      if (!islids)
        {
    #endif
        if (verboseflag >= 1)
          {
          printf("  %2d) %c%-25s\n", capnum,
                 cap_raised(caps, capnum) ? '*' : ' ', thecaps[capnum*2]);
          printf("    * = Capability currently allowed\n");
          };
        exit(cap_raised(caps,capnum) ? 1 : 0);
    #ifdef LIDS
        }
      else
        {
        short raised;

        raised = lids_raised(capnum) ? 1 : 0;
        if (verboseflag >= 1)
          {
          printf("       %c%-25s\n", raised ? 1 : 0, lidscaps[capnum*3]);
          printf("    * = Capability currently allowed\n");
          };
        exit(raised);
        };
    #endif
    };

  if (verboseflag >= 1)
    {
    if (zeroflag)
      printf("  Setting");
    else
      printf("  Removing");
    printf(" capabilities:\n");
    };

  if (zeroflag)
    {
    caps = 0;
    };

  for (index=optind; index < argc; index++)
    {
    int capnum=-1;
    #ifdef LIDS
      short islids=0;
    #endif
    char *capstr=NULL, *capdescstr=NULL;

    if (isdigit(argv[index][0]))
      {
      capnum = (int)strtol(argv[index], (char **)NULL, 10);
      capstr = thecaps[capnum*2];
      capdescstr = thecaps[capnum*2+1];
      }
    else
      {
      int jndex=0;

      while (thecaps[jndex] != NULL)
        {
        if (!strcmp(argv[index], thecaps[jndex]))
          {
          capnum = jndex/2;
          capstr = thecaps[jndex];
          capdescstr = thecaps[jndex+1];
          };
        jndex += 2;
        };

      #ifdef LIDS
        jndex = 0;

        while (lidscaps[jndex] != NULL)
          {
          if (!strcmp(argv[optind], lidscaps[jndex]))
            {
            capnum = jndex/3;
            capstr = lidscaps[jndex];
            capdescstr = lidscaps[jndex+2];
            islids = 1;
            };
          jndex += 3;
          };
      #endif
      };

    if (capnum == -1)
      {
      fprintf(stderr, "    %s: invalid capability\n", argv[index]);
      exit(1);
      };

    #ifdef LIDS
      if (!islids)
        {
    #endif
        if (verboseflag >= 1)
          printf("    %2d) %-22s %s\n", capnum, capstr, capdescstr);

        if (zeroflag)
          cap_raise(caps, capnum);
        else
          cap_lower(caps, capnum);
    #ifdef LIDS
        } 
      else
        {
        if (verboseflag >= 1)
          printf("        %-22s %s\n", capstr, capdescstr);
        if (zeroflag)
          lids_lower(capnum);
        else
          lids_raise(capnum);
        };
    #endif
    };

  if (verboseflag >= 2)
    {
    listcaps(caps);
    #ifdef LIDS
      printf("\n");
      listlids();
    #endif
    };

  /* open the /proc file */
  if ((fptr=fopen(PROC_CAP, "w")) == NULL)
    {
    perror(PROC_CAP);
    exit(errno);
    };
  /* write the new setting */
  fprintf(fptr, "0x%x", caps);
  fclose(fptr);

  return(0);
  };