Newer
Older
after5 / a5.c
@HIROSE Yuuji HIROSE Yuuji on 1 Apr 2012 1 KB Add required files.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <string.h>

#define AFTER5PATH	"./after5.cgi"

int main(int argc, char *argv[])
{
  char *path=malloc(strlen(argv[0]));
  char *p;
  uid_t euid = geteuid();
  struct stat st;
  if (0 == euid) {
    fputs("Do not call this program with suid 0\n", stderr);
    exit(0);
  }
  strcpy(path, argv[0]);
  p = strrchr(path, '/');
  if (p) {
    *p = '\0';
  } else {
    fputs("Cannot detect the directory where this program located.\n", stderr);
    exit(1);
  }
  
  if (-1 == chdir(path)) {
    fprintf(stderr, "Cannot chdir to %s\n", path);
    exit(2);
  }
  if (0 > stat(AFTER5PATH, &st)) {
    fprintf(stderr, "Cannot access %s\n", AFTER5PATH);
    exit(3);
  }
  if (st.st_uid != euid) {
    fputs("UID of cgi program mismatch\n", stderr);
    fputs("Do chown so that wrapper and cgi files' uid matches.\n", stderr);
    exit(4);
  }
  if (0 > stat(".", &st)) {
    fputs("Cannot stat current directory\n", stderr);
    fputs("Please ensure installed directory is readable.\n", stderr);
    exit(5);
  }
  if (st.st_uid != euid) {
    fputs("UID of directory mismatch\n", stderr);
    fputs("Do chown so that wrapper and directorys' uid matches.\n", stderr);
    exit(6);
  }
  if (st.st_mode & (S_IWGRP | S_IWOTH)) {
    fputs("This directory is writable for group/others.\n", stderr);
    fputs("Do chmod og-w for installed directory.\n", stderr);
    exit(7);
  }
  execv("./after5.cgi", argv);
}