s4

changeset 519:8d2a121cbc59

Add wrapper creation process
author HIROSE Yuuji <yuuji@gentei.org>
date Sat, 06 Apr 2019 17:22:34 +0900
parents f330dcb80a7f
children 0d8785102531
files pwrap.c s4-start.sh
diffstat 2 files changed, 78 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/pwrap.c	Sat Apr 06 17:22:34 2019 +0900
     1.3 @@ -0,0 +1,68 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <sys/types.h>
     1.7 +#include <unistd.h>
     1.8 +#include <sys/stat.h>
     1.9 +#include <string.h>
    1.10 +
    1.11 +#ifndef CGISCRIPT_PATH
    1.12 +#define CGISCRIPT_PATH	"./s4.cgi"
    1.13 +#endif
    1.14 +
    1.15 +int main(int argc, char *argv[])
    1.16 +{
    1.17 +  char *path=malloc(strlen(argv[0]));
    1.18 +  char *p;
    1.19 +  uid_t euid = geteuid();
    1.20 +  struct stat st;
    1.21 +  if (0 == euid) {
    1.22 +    fputs("Do not call this program with suid 0\n", stderr);
    1.23 +    exit(0);
    1.24 +  }
    1.25 +  strcpy(path, argv[0]);
    1.26 +  p = strrchr(path, '/');
    1.27 +  if (p) {
    1.28 +    *p = '\0';
    1.29 +  } else {
    1.30 +    fputs("Cannot detect the directory where this program located.\n", stderr);
    1.31 +    exit(1);
    1.32 +  }
    1.33 +  
    1.34 +  if (-1 == chdir(path)) {
    1.35 +    fprintf(stderr, "Cannot chdir to %s\n", path);
    1.36 +    exit(2);
    1.37 +  }
    1.38 +  if (0 > stat(CGISCRIPT_PATH, &st)) {
    1.39 +    fprintf(stderr, "Cannot access %s\n", CGISCRIPT_PATH);
    1.40 +    exit(3);
    1.41 +  }
    1.42 +  if (st.st_uid != euid) {
    1.43 +    fputs("UID of cgi program mismatch\n", stderr);
    1.44 +    fputs("Do chown so that wrapper and cgi files' uid matches.\n", stderr);
    1.45 +    exit(4);
    1.46 +  }
    1.47 +  if (st.st_mode & (S_IWGRP | S_IWOTH)) {
    1.48 +    fputs("This program is writable for group/others.\n", stderr);
    1.49 +    fputs("Do chmod og-w for installed cgi-program.\n", stderr);
    1.50 +    exit(5);
    1.51 +  }
    1.52 +  if (0 > stat(".", &st)) {
    1.53 +    fputs("Cannot stat current directory\n", stderr);
    1.54 +    fputs("Please ensure installed directory is readable.\n", stderr);
    1.55 +    exit(6);
    1.56 +  }
    1.57 +/*
    1.58 +  if (st.st_uid != euid) {
    1.59 +    fputs("UID of directory mismatch\n", stderr);
    1.60 +    fputs("Do chown so that wrapper and directorys' uid matches.\n", stderr);
    1.61 +    exit(7);
    1.62 +  }
    1.63 +*/
    1.64 +  if (st.st_mode & (S_IWGRP | S_IWOTH)) {
    1.65 +    fputs("This directory is writable for group/others.\n", stderr);
    1.66 +    fputs("Do chmod og-w for installed directory.\n", stderr);
    1.67 +    exit(8);
    1.68 +  }
    1.69 +  argv[0] = CGISCRIPT_PATH;
    1.70 +  execv(CGISCRIPT_PATH, argv);
    1.71 +}
     2.1 --- a/s4-start.sh	Sat Apr 06 16:19:27 2019 +0900
     2.2 +++ b/s4-start.sh	Sat Apr 06 17:22:34 2019 +0900
     2.3 @@ -89,7 +89,16 @@
     2.4         -D_CASE_=$casefile s4.cgi.m4 > index.cgi && chmod +x index.cgi
     2.5      (echo "ADMIN=$ADMIN"
     2.6       [ "$guest" ] && echo "guestonlymode=1") >> s4-config.sh
     2.7 -    rc=0
     2.8 +    sq $db "REPLACE INTO user VALUES('$ADMIN');"
     2.9 +    cat > .htaccess <<-EOF
    2.10 +	DirectoryIndex	index.cgi
    2.11 +	AddHandler cgi-script .cgi
    2.12 +	EOF
    2.13 +    if [ pwrap.c -nt index.cgi ]; then
    2.14 +      ${CC:-gcc} -o index.cgi pwrap.c \
    2.15 +	&& chmod u+s index.cgi \
    2.16 +	&& rc=0
    2.17 +    fi
    2.18    else
    2.19      echo Calling s4-init.sh 'FAILED!!'
    2.20    fi