Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Reinstate example_1.c #22

Open
langston-barrett opened this issue Aug 23, 2022 · 1 comment
Open

Tests: Reinstate example_1.c #22

langston-barrett opened this issue Aug 23, 2022 · 1 comment

Comments

@langston-barrett
Copy link
Collaborator

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 langston-barrett changed the title Tests: Reinstance example_1.c Tests: Reinstate example_1.c Aug 29, 2022
@langston-barrett
Copy link
Collaborator Author

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;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant