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
|
/*
* convert-exec-param.c
*
* TOMOYO Linux's utilities.
*
* Copyright (C) 2005-2009 NTT DATA CORPORATION
*
* Version: 1.7.1 2009/11/11
*
*/
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char buffer[3][65536];
int line = 0;
memset(buffer, 0, sizeof(buffer));
if (argc > 1) {
fprintf(stderr, "Usage: %s < /proc/ccs/grant_log or "
"/proc/ccs/reject_log\n", argv[0]);
return 0;
}
while (1) {
int i;
char *cp;
char *exe;
char *args;
int envc;
char *envs;
/* Find header line. */
i = getc(stdin);
if (i)
ungetc(i, stdin);
if (!fgets(buffer[0], sizeof(buffer[0]) - 1, stdin))
break;
line++;
if (!strchr(buffer[0], '\n'))
goto out;
if (buffer[0][0] != '#')
continue;
/* Check for " argc=" part. */
cp = strstr(buffer[0], " argc=");
if (!cp)
continue;
/* Get argc value. */
if (sscanf(cp + 1, "argc=%d", &argc) != 1)
goto out;
/* Check for " envc=" part. */
cp = strstr(buffer[0], " envc=");
if (!cp)
continue;
/* Get envc value. */
if (sscanf(cp + 1, "envc=%d", &envc) != 1)
goto out;
/* Get realpath part. */
exe = strstr(buffer[0], " realpath=\"");
if (!exe)
continue;
exe++;
/* Get argv[]= part. */
cp = strstr(buffer[0], " argv[]={ ");
if (!cp)
goto out;
args = cp + 10;
/* Get envp[]= part. */
cp = strstr(buffer[0], " envp[]={ ");
if (!cp)
goto out;
envs = cp + 10;
/* Terminate realpath part. */
cp = strchr(exe, ' ');
if (!cp)
goto out;
*cp = '\0';
/* Terminate argv[] part. */
cp = strstr(args - 1, " } ");
if (!cp)
goto out;
*cp = '\0';
/* Terminate envp[] part. */
cp = strstr(envs - 1, " } ");
if (!cp)
goto out;
*cp = '\0';
/* Get domainname. */
line++;
i = getc(stdin);
if (i)
ungetc(i, stdin);
if (!fgets(buffer[1], sizeof(buffer[1]) - 1, stdin) ||
!strchr(buffer[1], '\n'))
goto out;
/* Get "allow_execute " line. */
line++;
i = getc(stdin);
if (i)
ungetc(i, stdin);
if (!fgets(buffer[2], sizeof(buffer[2]) - 1, stdin))
goto out;
cp = strchr(buffer[2], '\n');
if (!cp)
goto out;
*cp-- = '\0';
while (*cp == ' ')
*cp-- = '\0';
if (strncmp(buffer[2], "allow_execute ", 14))
continue;
/* Print domainname. */
printf("%s", buffer[1]);
/* Print permission and exec.realpath part. */
printf("%s if exec.%s", buffer[2], exe);
/* Print exec.argc part. */
printf(" exec.argc=%d", argc);
/* Print exec.argv[] part. */
if (argc) {
i = 0;
cp = strtok(args, " ");
while (cp && *cp == '"') {
printf(" exec.argv[%d]=%s", i++, cp);
cp = strtok(NULL, " ");
}
}
/* Print exec.envc part. */
printf(" exec.envc=%d", envc);
/* Print exec.envp[] part. */
if (envc) {
cp = strtok(envs, " ");
while (cp && *cp == '"') {
char c = *(cp + 1);
char *cp2 = cp + 1;
if (!c || c == '"' || c == '=')
goto bad_env;
while (1) {
c = *cp2++;
if (c == '=')
break;
if (!c || c == '"')
goto bad_env;
}
if (!*cp2 || *cp2 == '"')
goto bad_env;
printf(" exec.envp[");
while (1) {
c = *cp++;
if (c == '=')
break;
putchar(c);
}
printf("\"]=\"%s", cp);
bad_env:
cp = strtok(NULL, " ");
}
}
printf("\n\n");
}
return 0;
out:
fprintf(stderr, "%d: Broken log entry. Aborted.\n", line);
return 1;
}
|