<commit_before>#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <pwd.h>
#include "tcp.h"


/*
  httpd.c
  A tiny HTTP/1.0 server.

  See http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html
  for a reference guide.
*/


/*
  Decide if we use 'size' or 'length' or 'len' (for ALL code).
  Also for 'chars' and 'bytes'. (Be consistent.)
*/
#define LISTEN_PORT 80
#define TIME_OUT 5
#define MAX_REQUEST_LENGTH 512
#define MAX_RESPONSE_LENGTH 6225
#define PROTOCOL "HTTP/1.0"

typedef enum {
    METHOD_GET, METHOD_HEAD, METHOD_POST, METHOD_PUT,
    METHOD_DELETE, METHOD_EDIT, METHOD_OPTIONS,
    METHOD_TRACE, METHOD_UNKNOWN
} http_method;

typedef enum {
    STATUS_OK, STATUS_HTTP_VERSION_NOT_SUPPORTED,
    STATUS_NOT_IMPLEMENTED, STATUS_PAYMENT_REQUIRED
} http_status;

typedef enum {
    HEADER_CONTENT_TYPE, HEADER_SERVER, HEADER_ISLAND
} http_header;


int serve(void);
int parse_request(char *request, http_method *method, char **url, char **protocol);
int parse_url(char *url, char **filename, char **mimetype);
int read_token(char *buffer, int buffer_length, char *token);
int count_spaces(char *buffer, int buffer_length);
void write_response(char *buffer, http_method method, char *url, char *protocol);
void send_response(char *buffer);
int handle_get(char *buffer, char *url);
int file_name_character(char *c);
int write_status(char *buffer, http_status status);
int write_header(char *buffer, http_header header, char *value);
int write_body(char *buffer);


static void alarm_handler(int sig) {
    /* just return to interrupt */
}


/* 1 on failure, no return on success */

int main(int argc, char** argv) {

    char *eth, *ip1, *ip2;
    struct passwd *nobody;

    if (argc < 2) {
        printf("No www directory found.\nUsage: %s wwwdir\n", argv[0]);
        return 1;
    }

    if (chdir(argv[1]) < 0) {
        printf("Could not change dir to '%s'.\n", argv[1]);
        return 1;
    }

    /* if we are superuser, chroot and change effective uid and guid */
    if (geteuid() == 0) {
        if (chroot(argv[1]) < 0) {
            printf("Could not chroot to www directory.\n");
            return 1;
        }
        errno = 0;
        if (((nobody = getpwnam("nobody")) == NULL)) {
            printf("1\n");
            return 1;
        }
        if (setegid(nobody->pw_gid)) {
            printf("2\n");
            return 1;
        }
        if (seteuid(nobody->pw_uid)) {
            printf("Could not change to user `nobody'.\n");
            return 1;
        }
        printf("Changed root, uid, and guid.\n");
    }

    eth = getenv("ETH");
    if (!eth) {
        printf("The ETH environment variable must be set!\n");
        return 1;
    }

    ip1 = getenv("IP1");
    ip2 = getenv("IP2");
    if ((!ip1)||(!ip2)) {
        printf("The IP1 and IP2 environment variables must be set!\n");
        return 1;
    }

    if (tcp_socket() != 0) {
        printf("HTTPD: Opening socket failed\n");
        return 1;
    }

    while (serve()) { }

    printf("Listening failed.\n");

    return 1;

}

<commit_msg>Changed setuid code in httpd.<commit_after>#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <errno.h>
#include <pwd.h>
#include "tcp.h"


/*
  httpd.c
  A tiny HTTP/1.0 server.

  See http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html
  for a reference guide.
*/


/*
  Decide if we use 'size' or 'length' or 'len' (for ALL code).
  Also for 'chars' and 'bytes'. (Be consistent.)
*/
#define LISTEN_PORT 80
#define UNPRIVILIGED_UID 1000
#define TIME_OUT 5
#define MAX_REQUEST_LENGTH 512
#define MAX_RESPONSE_LENGTH 6225
#define PROTOCOL "HTTP/1.0"

typedef enum {
    METHOD_GET, METHOD_HEAD, METHOD_POST, METHOD_PUT,
    METHOD_DELETE, METHOD_EDIT, METHOD_OPTIONS,
    METHOD_TRACE, METHOD_UNKNOWN
} http_method;

typedef enum {
    STATUS_OK, STATUS_HTTP_VERSION_NOT_SUPPORTED,
    STATUS_NOT_IMPLEMENTED, STATUS_PAYMENT_REQUIRED
} http_status;

typedef enum {
    HEADER_CONTENT_TYPE, HEADER_SERVER, HEADER_ISLAND
} http_header;


int serve(void);
int parse_request(char *request, http_method *method, char **url, char **protocol);
int parse_url(char *url, char **filename, char **mimetype);
int read_token(char *buffer, int buffer_length, char *token);
int count_spaces(char *buffer, int buffer_length);
void write_response(char *buffer, http_method method, char *url, char *protocol);
void send_response(char *buffer);
int handle_get(char *buffer, char *url);
int file_name_character(char *c);
int write_status(char *buffer, http_status status);
int write_header(char *buffer, http_header header, char *value);
int write_body(char *buffer);


static void alarm_handler(int sig) {
    /* just return to interrupt */
}


/* 1 on failure, no return on success */

int main(int argc, char** argv) {

    char *eth, *ip1, *ip2;

    if (argc < 2) {
        printf("No www directory found.\nUsage: %s wwwdir\n", argv[0]);
        return 1;
    }

    if (chdir(argv[1]) < 0) {
        printf("Could not change dir to '%s'.\n", argv[1]);
        return 1;
    }

    eth = getenv("ETH");
    if (!eth) {
        printf("The ETH environment variable must be set!\n");
        return 1;
    }

    ip1 = getenv("IP1");
    ip2 = getenv("IP2");
    if ((!ip1)||(!ip2)) {
        printf("The IP1 and IP2 environment variables must be set!\n");
        return 1;
    }

    if (tcp_socket() != 0) {
        printf("HTTPD: Opening socket failed\n");
        return 1;
    }

    /* if we are superuser, chroot and change effective uid and guid */
    /* do this after opening tcp socket, because we need access to /dev/eth */
    if (geteuid() == 0) {
        if (chroot(argv[1]) < 0) {
            printf("Could not chroot to www directory.\n");
            return 1;
        }
        /* this must be seteuid() actually on linux, add a preprocessor
           conditional for this... */
        if (setuid(UNPRIVILIGED_UID)) {
            printf("Could not change to user `nobody'.\n");
            return 1;
        }
        printf("Changed root, uid, and guid.\n");
    }

    while (serve()) { }

    printf("Listening failed.\n");

    return 1;

}


{'fraction_non_alphanumeric': 0.135748031496063, 'fraction_numerical': 0.015590551181102362, 'mean_word_length': 2.94472049689441, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5058655', 'n_tokens_mistral': 2290, 'n_tokens_neox': 2147, 'n_words': 625}