|
24 | 24 | int database_check(const struct su_context *ctx)
|
25 | 25 | {
|
26 | 26 | FILE *fp;
|
27 |
| - char allow = '-'; |
28 |
| - char *filename = malloc(snprintf(NULL, 0, "%s/%u-%u", REQUESTOR_STORED_PATH, ctx->from.uid, ctx->to.uid) + 1); |
29 |
| - sprintf(filename, "%s/%u-%u", REQUESTOR_STORED_PATH, ctx->from.uid, ctx->to.uid); |
| 27 | + int allow = '-'; |
| 28 | + char filename[PATH_MAX]; |
| 29 | + |
| 30 | + snprintf(filename, sizeof(filename), |
| 31 | + REQUESTOR_STORED_PATH "/%u-%u", ctx->from.uid, ctx->to.uid); |
30 | 32 | if ((fp = fopen(filename, "r"))) {
|
31 |
| - LOGD("Found file"); |
32 |
| - char cmd[PATH_MAX]; |
| 33 | + LOGD("Found file %s", filename); |
| 34 | + char cmd[ARG_MAX]; |
33 | 35 | fgets(cmd, sizeof(cmd), fp);
|
| 36 | + /* skip trailing '\n' */ |
34 | 37 | int last = strlen(cmd) - 1;
|
35 |
| - LOGD("this is the last character %u of the string", cmd[5]); |
36 |
| - if (cmd[last] == '\n') { |
37 |
| - cmd[last] = '\0'; |
38 |
| - } |
39 |
| - LOGD("Comparing %c %s, %u to %s", cmd[last - 2], cmd, last, get_command(&ctx->to)); |
| 38 | + if (last >= 0) |
| 39 | + cmd[last] = 0; |
| 40 | + |
| 41 | + LOGD("Comparing '%s' to '%s'", cmd, get_command(&ctx->to)); |
40 | 42 | if (strcmp(cmd, get_command(&ctx->to)) == 0) {
|
41 | 43 | allow = fgetc(fp);
|
42 | 44 | }
|
43 | 45 | fclose(fp);
|
44 | 46 | } else if ((fp = fopen(REQUESTOR_STORED_DEFAULT, "r"))) {
|
45 |
| - LOGD("Using default"); |
| 47 | + LOGD("Using default file %s", REQUESTOR_STORED_DEFAULT); |
46 | 48 | allow = fgetc(fp);
|
47 | 49 | fclose(fp);
|
48 | 50 | }
|
49 |
| - free(filename); |
50 | 51 |
|
51 | 52 | if (allow == '1') {
|
52 | 53 | return DB_ALLOW;
|
|
0 commit comments