MATE icon indicating copy to clipboard operation
MATE copied to clipboard

Tests: Reinstate `example_1.c`

Open langston-barrett opened this issue 1 year ago • 1 comments

This is a test program developed by Apogee Research that we used extensively in MATE's tests. They intend to go through the necessary steps to get approval for us to publish it, but haven't yet. Many of the MATE tests had to be disabled. We should reinstate this test whenever we can.

langston-barrett avatar Aug 23 '22 21:08 langston-barrett

This program has been approved for public release. Here it is:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef CHESS_LOCAL
#include <netinet/in.h>
#include <signal.h>
#endif
#include <string.h>
//#include <testbed.h>

#define PORT 8081

#define FALSE 0
#define TRUE 1

#define SERVER_HELLO "Enter Information:\n"
#define R_FAILED "ACCESS_DENIED\n"

#define BUFFER_SIZE 64

#ifndef CHESS_LOCAL
int server_fd, new_client;
struct sockaddr_in address;
#endif
char pswd[21];
char FLAG[21];

int authenticatedFunction(char *input_str)
{
    FILE *file_p;
    file_p = fopen("/tmp/Log.txt", "a");
    fputs(input_str, file_p);
    fclose(file_p);
    return TRUE;
}
#ifndef CHESS_LOCAL
int setupServer()
{
    int opt = 1; // reuse address

    // create socket file descriptor, attach to 8081
    server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
        return FALSE;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(PORT);
    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)))
        return FALSE;

    printf("Listening on port %i...\n", PORT);
    return TRUE;
}
#endif
int runServer()
{
    // Create a structure to store the request
    struct request
    {
        char buffer[BUFFER_SIZE];
        int authenticated;
    } req;

#ifndef CHESS_LOCAL
    int addrlen = sizeof(address);
#endif

    while (TRUE)
    {
        //Reset request structure contents
        memset(req.buffer, 0, sizeof(req.buffer));
        req.authenticated = FALSE;

#ifndef CHESS_LOCAL
        listen(server_fd, 10);
        new_client = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen);
        send(new_client, SERVER_HELLO, strlen(SERVER_HELLO), 0);
#else
        write(STDOUT_FILENO, SERVER_HELLO, strlen(SERVER_HELLO));
#endif

        // Read and process input
        // ** Suggested patch change to recv(new_client, req.buffer, BUFFER_SIZE, 0)
#ifndef CHESS_LOCAL
        recv(new_client, req.buffer, 1024, 0);
#else
        read(STDIN_FILENO, req.buffer, 1024);
#endif
        printf("Processing Input %s\n", req.buffer);

        // Authenticate request
        if (!strncmp(req.buffer, pswd, strlen(pswd)))
            req.authenticated = TRUE;

        // Request Authenticated
        if (req.authenticated != FALSE)
        {
            if (strlen(req.buffer) > strlen(pswd) + 2)
            {
                authenticatedFunction(req.buffer + strlen(pswd));
            }
#ifndef CHESS_LOCAL
            send(new_client, FLAG, strlen(FLAG), 0);
#else
            write(STDOUT_FILENO, FLAG, strlen(FLAG));
#endif
        }

        // Request Not Authenticated
        else
        {
#ifndef CHESS_LOCAL
            send(new_client, R_FAILED, strlen(R_FAILED), 0);
#else
            write(STDOUT_FILENO, R_FAILED, strlen(R_FAILED));
#endif
        }
#ifndef CHESS_LOCAL
        close(new_client);
#endif
        printf("Response Sent\n");
    }
    return TRUE;
}
int main(int argc, char **argv)
{
    // Assert that we are running on the testbed
    // assert_execution_on_testbed();

    if (argc != 3)
    {
        printf("Usage: ./example_1.bin <server passcode (1-20 characters)> <flag (1-20 characters)>\n");
        printf("%d",argc);
        return -1;
    }
    if (strlen(argv[1]) > 20 || strlen(argv[2]) > 20)
    {
        printf("Usage: ./example_1.bin <server passcode (1-20 characters)> <flag (1-20 characters)>\n");
        return -1;
    }

    strcpy(pswd, argv[1]);
    strcpy(FLAG, argv[2]);
    strcpy(FLAG + strlen(FLAG), "\n");

#ifndef CHESS_LOCAL
    if (setupServer() != 1)
    {
        printf("Server not started\n");
        return -1;
    }
#endif
    runServer();
    return 0;
}

langston-barrett avatar Aug 29 '22 18:08 langston-barrett