s4

annotate pwrap.c @ 534:ce6d0f04f520

Do not read s4-funcs.sh twice
author HIROSE Yuuji <yuuji@gentei.org>
date Sat, 06 Apr 2019 18:46:10 +0900
parents
children
rev   line source
yuuji@519 1 #include <stdio.h>
yuuji@519 2 #include <stdlib.h>
yuuji@519 3 #include <sys/types.h>
yuuji@519 4 #include <unistd.h>
yuuji@519 5 #include <sys/stat.h>
yuuji@519 6 #include <string.h>
yuuji@519 7
yuuji@519 8 #ifndef CGISCRIPT_PATH
yuuji@519 9 #define CGISCRIPT_PATH "./s4.cgi"
yuuji@519 10 #endif
yuuji@519 11
yuuji@519 12 int main(int argc, char *argv[])
yuuji@519 13 {
yuuji@519 14 char *path=malloc(strlen(argv[0]));
yuuji@519 15 char *p;
yuuji@519 16 uid_t euid = geteuid();
yuuji@519 17 struct stat st;
yuuji@519 18 if (0 == euid) {
yuuji@519 19 fputs("Do not call this program with suid 0\n", stderr);
yuuji@519 20 exit(0);
yuuji@519 21 }
yuuji@519 22 strcpy(path, argv[0]);
yuuji@519 23 p = strrchr(path, '/');
yuuji@519 24 if (p) {
yuuji@519 25 *p = '\0';
yuuji@519 26 } else {
yuuji@519 27 fputs("Cannot detect the directory where this program located.\n", stderr);
yuuji@519 28 exit(1);
yuuji@519 29 }
yuuji@519 30
yuuji@519 31 if (-1 == chdir(path)) {
yuuji@519 32 fprintf(stderr, "Cannot chdir to %s\n", path);
yuuji@519 33 exit(2);
yuuji@519 34 }
yuuji@519 35 if (0 > stat(CGISCRIPT_PATH, &st)) {
yuuji@519 36 fprintf(stderr, "Cannot access %s\n", CGISCRIPT_PATH);
yuuji@519 37 exit(3);
yuuji@519 38 }
yuuji@519 39 if (st.st_uid != euid) {
yuuji@519 40 fputs("UID of cgi program mismatch\n", stderr);
yuuji@519 41 fputs("Do chown so that wrapper and cgi files' uid matches.\n", stderr);
yuuji@519 42 exit(4);
yuuji@519 43 }
yuuji@519 44 if (st.st_mode & (S_IWGRP | S_IWOTH)) {
yuuji@519 45 fputs("This program is writable for group/others.\n", stderr);
yuuji@519 46 fputs("Do chmod og-w for installed cgi-program.\n", stderr);
yuuji@519 47 exit(5);
yuuji@519 48 }
yuuji@519 49 if (0 > stat(".", &st)) {
yuuji@519 50 fputs("Cannot stat current directory\n", stderr);
yuuji@519 51 fputs("Please ensure installed directory is readable.\n", stderr);
yuuji@519 52 exit(6);
yuuji@519 53 }
yuuji@519 54 /*
yuuji@519 55 if (st.st_uid != euid) {
yuuji@519 56 fputs("UID of directory mismatch\n", stderr);
yuuji@519 57 fputs("Do chown so that wrapper and directorys' uid matches.\n", stderr);
yuuji@519 58 exit(7);
yuuji@519 59 }
yuuji@519 60 */
yuuji@519 61 if (st.st_mode & (S_IWGRP | S_IWOTH)) {
yuuji@519 62 fputs("This directory is writable for group/others.\n", stderr);
yuuji@519 63 fputs("Do chmod og-w for installed directory.\n", stderr);
yuuji@519 64 exit(8);
yuuji@519 65 }
yuuji@519 66 argv[0] = CGISCRIPT_PATH;
yuuji@519 67 execv(CGISCRIPT_PATH, argv);
yuuji@519 68 }