Thread: C help
View Single Post
Old 05-09-07, 11:38 AM   #4
Registered User
Join Date: Jul 2005
Posts: 3,606
Default Re: C help

This is what I ended up with, thanks again for your help, feel free to make any additional comments:

/* evilghost
 * Thanks wnd for code snippets & help.
 * May 8, 2007
 * SETUID binary to fix permissions with uploaded images if they are invalid.
 * Permissions on compiled binary must be 4770 with uid/gid root:www-data

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>

#define wwwdata_GID 33
#define shared_GID 5140
#define BASEPATH "/www/dlf/dlf-albums/"

FILE *fout;

int main(int argc, char **argv){
        time_t curtime;
        struct tm *loctime;
        char t[80];
        t[79] = '\0';

        //Check initial input.
        if (argc != 2 || argv[1] == NULL || strncmp(argv[1], BASEPATH, strlen(BASEPATH)) != 0 || strstr(argv[1], "/..") != NULL) {
                fprintf(stderr,"%s\n","Invalid file path passed.");
                return EXIT_FAILURE;

        // Get the current time & convert to local.
        curtime = time (NULL);
        loctime = localtime (&curtime);
        strftime(t, 79, "%a %b %H:%M:%S %Y", loctime);

        //Write audit trail.
        if ((fout = fopen ("/www/dlf/fixperms.log", "a+"))  != (FILE *)0){
                fprintf(fout, "%s\t%s\n", t, argv[1]);
                fprintf(stderr,"%s\n","Error while opening audit log for writing");
                return EXIT_FAILURE;

        //Adjust permissions
        chown(argv[1], wwwdata_GID, shared_GID);
        chmod(argv[1], 0770);
evilghost is offline   Reply With Quote