BarryServer : Git

All the code for all my projects
// BarryServer : Git / OrionUserland / blob / master / login / main.c

// Related

OrionUserland

Barry Using POSIX names in structs 2e11092 (3 years, 3 months ago)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <pwd.h>

#define MANUAL_LOGIN

/* Main function */
int
main(int argc, char *argv[])
{
	int fd, sz;
	char *buf, *line, *input = malloc(1024);

	Passwd *pwd;
	Termios tcold, tcnew;

	while (1) {
		printf("\nUsername: ");
		memset(input, 0, 1024);
#ifdef MANUAL_LOGIN
		sz = read(STDIN_FILENO, input, 1024);
#else
		printf("root\n");
		sz = 5;
		memcpy(input, "root", 5);
#endif
		input[--sz] = '\0';
		if (sz < 1)
			continue;

		pwd = getpwnam(input);
		if (!pwd)
			continue;

		printf("Password: ");
		memset(input, 0, 1024);
		ioctl(STDIN_FILENO, TCGETS, &tcold);
		memcpy(&tcnew, &tcold, sizeof(Termios));
		tcnew.c_lflag &= ~ECHO;
		ioctl(STDIN_FILENO, TCSETS, &tcnew);
#ifdef MANUAL_LOGIN
		sz = read(STDIN_FILENO, input, 1024);
#else
		printf("password\n");
		sz = 9;
		memcpy(input, "password", 9);
#endif
		input[--sz] = '\0';
		if (sz < 1)
			continue;
		ioctl(STDIN_FILENO, TCSETS, &tcold);

		if (!strcmp(input, pwd->pw_passwd))
			break;
		printf("Login incorrect\n");
	}
	printf("\n");

	dbgprintf("Authenticating \"%s\" (%d:%d) -> \"%s\" @ \"%s\"",
	          pwd->pw_name, pwd->pw_uid, pwd->pw_gid, pwd->pw_shell,
	          pwd->pw_dir);
	chdir(pwd->pw_dir);
	setuid(pwd->pw_uid);
	setgid(pwd->pw_gid);
	char *v[] = { "sh", NULL };
	execve(pwd->pw_shell, v, NULL);

	return 0;
}