File: setcap.c

package info (click to toggle)
libcap2 0.cvs.20010529-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 496 kB
  • ctags: 240
  • sloc: ansic: 1,606; makefile: 147
file content (138 lines) | stat: -rw-r--r-- 2,466 bytes parent folder | download | duplicates (2)
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
/*
 * $Id: setcap.c,v 1.1.2.1 2000/07/11 05:07:05 agmorgan Exp $
 *
 * Copyright (c) 1997 Andrew G. Morgan  <morgan@parc.power.net>
 *
 * This sets the capabilities of a given file.
 */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/capability.h>
#include <unistd.h>

static void usage(void)
{
    fprintf(stderr,
	    "usage: setcap [-q] (-|<caps>) <filename> "
	    "[ ... (-|<capsN>) <filenameN> ]\n"
	);
    exit(1);
}

#define MAXCAP  2048

static int read_caps(int quiet, const char *filename, char *buffer)
{
    int i=MAXCAP;

    if (!quiet) {
	fprintf(stderr,	"Please enter caps for file [empty line to end]:\n");
    }
    while (i > 0) {
	int j = read(STDIN_FILENO, buffer, i);

	if (j < 0) {
	    fprintf(stderr, "\n[Error - aborting]\n");
	    exit(1);
	}

	if (j==0 || buffer[0] == '\n') {
	    /* we're done */
	    break;
	}

	/* move on... */

	i -= j;
	buffer += j;
    }

    /* <NUL> terminate */
    buffer[0] = '\0';

    return (i < MAXCAP ? 0:-1);
}

int main(int argc, char **argv)
{
    char buffer[MAXCAP+1];
    int retval, quiet=0;
    cap_t cap_d;

    if (argc < 3) {
	usage();
    }

    while (--argc > 0) {
	const char *text;

	if (!strcmp(*++argv,"-q")) {
	    quiet = 1;
	    continue;
	}
	if (!strcmp(*argv,"-")) {
	    retval = read_caps(quiet, *argv, buffer);
	    if (retval)
		usage();
	    text = buffer;
	} else
	    text = *argv;

	cap_d = cap_from_text(text);
	if (cap_d == NULL) {
	    perror("fatal error");
	    usage();
	}
#ifdef DEBUG
	{
	    ssize_t length;
	    const char *result;

	    result = cap_to_text(cap_d, &length);
	    fprintf(stderr, "[caps set to:\n%s\n]\n", result);
	}
#endif

	if (--argc <= 0)
	    usage();

	retval = cap_set_file(*++argv, cap_d);

	if (retval != 0) {
	    fprintf(stderr,
		    "Failed to set capabilities on file `%s'\n"
		    " (%s)\n", argv[0], strerror(errno));
	    usage();
	}
    }

    return 0;
}

/*
 * $Log: setcap.c,v $
 * Revision 1.1.2.1  2000/07/11 05:07:05  agmorgan
 *
 * Relevant BUGIDs: none
 *
 * Purpose of commit: adding filesystem support
 *
 * Commit summary:
 * ---------------
 * reviving old code to use filesystem support.
 *
 * Revision 1.1.1.1  2000/07/01 22:25:08  agmorgan
 * Linux libcap-1.10 sources
 *
 * Revision 1.1.1.1  1999/04/17 22:16:31  morgan
 * release 1.0 of libcap
 *
 * Revision 1.2  1997/05/04 05:34:32  morgan
 * non void main
 *
 * Revision 1.1  1997/04/28 01:01:20  morgan
 * Initial revision
 *
 */