Zurück zur SP2 Übersicht
format-server.c
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
static const char SHELL[] = "/bin/sh";
static void die(char *msg) {
perror(msg);
exit(EXIT_FAILURE);
}
static void executeShell(void) {
execl(SHELL, SHELL, NULL);
perror(SHELL);
exit(EXIT_FAILURE);
}
static int checkPassword(char *pw, char *geheim) {
strtok(pw, "\r\n");
strtok(geheim, "\r\n");
return !strcmp(pw, geheim);
}
int main(int argc, char *argv[]) {
int user_authenticated = 0;
FILE *fp;
char geheim[31 + 1];
char buffer[63 + 1];
fp = fopen("secret.txt", "r");
if (!fp) {
die("secret.txt");
}
fgets(geheim, sizeof(geheim), fp);
fclose(fp);
printf("Enter your token (up to %d chars):\n", sizeof(buffer) - 1);
fflush(stdout);
if (fgets(buffer, sizeof(buffer), stdin) == NULL) {
return EXIT_FAILURE;
}
printf(buffer);
if (checkPassword(buffer, geheim)) {
user_authenticated = 1;
}
if (user_authenticated) {
executeShell();
}
}