Add formatter and pipe support
This commit is contained in:
parent
27156191f1
commit
253e48af77
14
.clang-format
Normal file
14
.clang-format
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
---
|
||||||
|
Language: Cpp
|
||||||
|
BasedOnStyle: WebKit
|
||||||
|
SpaceAfterTemplateKeyword: false
|
||||||
|
AlignEscapedNewlines: true
|
||||||
|
AlignTrailingComments: true
|
||||||
|
BreakBeforeInheritanceComma: true
|
||||||
|
BreakConstructorInitializers: BeforeComma
|
||||||
|
IndentPPDirectives: AfterHash
|
||||||
|
BreakBeforeBraces: Custom
|
||||||
|
BraceWrapping:
|
||||||
|
AfterFunction: true
|
||||||
|
NamespaceIndentation: None
|
||||||
|
|
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
|||||||
build:
|
build:
|
||||||
cc -lcrypt -I. sus.c -o sus
|
cc -Wall -lcrypt -I. sus.c -o sus
|
||||||
|
|
||||||
all: build install
|
all: build install
|
||||||
|
|
||||||
|
102
sus.c
102
sus.c
@ -9,18 +9,17 @@
|
|||||||
|
|
||||||
#define _XOPEN_SOURCE
|
#define _XOPEN_SOURCE
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <grp.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
#include <shadow.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pwd.h>
|
|
||||||
#include <grp.h>
|
|
||||||
#include <shadow.h>
|
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#define MAX_PASSWORD 3
|
#define MAX_PASSWORD 3
|
||||||
|
|
||||||
// TODO: Add pipe support
|
|
||||||
|
|
||||||
void get_password(char* password);
|
void get_password(char* password);
|
||||||
|
|
||||||
void cats(char** str, const char* str2);
|
void cats(char** str, const char* str2);
|
||||||
@ -28,12 +27,13 @@ void cats(char **str, const char *str2);
|
|||||||
int CheckIfInGroup(const char* string);
|
int CheckIfInGroup(const char* string);
|
||||||
|
|
||||||
int GetUID(const char* username, uid_t* uid);
|
int GetUID(const char* username, uid_t* uid);
|
||||||
void PrintHelp(); void CreatePasswordMessage();
|
void PrintHelp();
|
||||||
|
void CreatePasswordMessage();
|
||||||
|
|
||||||
int CheckPassword(char* password);
|
int CheckPassword(char* password);
|
||||||
int AskPassword();
|
int AskPassword();
|
||||||
|
|
||||||
int uid = 0;
|
uid_t uid = 0;
|
||||||
|
|
||||||
int command_start = 1;
|
int command_start = 1;
|
||||||
|
|
||||||
@ -41,23 +41,22 @@ char* command_name;
|
|||||||
|
|
||||||
struct termios old_terminal;
|
struct termios old_terminal;
|
||||||
|
|
||||||
void intHandler(int dummy) {
|
void getln(int, char*, size_t);
|
||||||
|
|
||||||
|
void intHandler(int dummy)
|
||||||
|
{
|
||||||
tcsetattr(STDIN_FILENO, TCSANOW, &old_terminal);
|
tcsetattr(STDIN_FILENO, TCSANOW, &old_terminal);
|
||||||
puts("Cancelled.");
|
puts("Cancelled.");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv)
|
||||||
struct rule *rule;
|
{
|
||||||
char **envp;
|
|
||||||
|
|
||||||
if (CheckIfInGroup("wheel") != 1) {
|
if (CheckIfInGroup("wheel") != 1) {
|
||||||
puts("ERROR: Not in the wheel group.");
|
puts("ERROR: Not in the wheel group.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
tcgetattr(STDIN_FILENO, &old_terminal);
|
|
||||||
|
|
||||||
signal(SIGINT, intHandler);
|
signal(SIGINT, intHandler);
|
||||||
|
|
||||||
command_name = malloc((1 + strlen(argv[0])) * sizeof(char));
|
command_name = malloc((1 + strlen(argv[0])) * sizeof(char));
|
||||||
@ -97,7 +96,6 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
if (username[0] == '-')
|
if (username[0] == '-')
|
||||||
memmove(username, username + 1, strlen(username));
|
memmove(username, username + 1, strlen(username));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char* command = "";
|
char* command = "";
|
||||||
@ -110,8 +108,7 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
if (username[0] == '\0')
|
if (username[0] == '\0')
|
||||||
uid = 0;
|
uid = 0;
|
||||||
else
|
else if (GetUID(username, &uid) == -1) {
|
||||||
if (GetUID(username, &uid) == -1) {
|
|
||||||
printf("ERROR: Could not find username: %s", username);
|
printf("ERROR: Could not find username: %s", username);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -124,17 +121,20 @@ int main(int argc, char** argv) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetUID(const char* username, uid_t* uid) { struct passwd *pw; pw = getpwnam(username); if (pw != NULL) { *uid = pw->pw_uid;
|
int GetUID(const char* username, uid_t* uid)
|
||||||
|
{
|
||||||
|
struct passwd* pw;
|
||||||
|
pw = getpwnam(username);
|
||||||
|
if (pw != NULL) {
|
||||||
|
*uid = pw->pw_uid;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CheckIfInGroup(const char* string) {
|
int CheckIfInGroup(const char* string)
|
||||||
int ngroups_max;
|
{
|
||||||
ngroups_max = sysconf(_SC_NGROUPS_MAX);
|
|
||||||
|
|
||||||
int groups = getgroups(0, NULL);
|
int groups = getgroups(0, NULL);
|
||||||
|
|
||||||
gid_t list[groups];
|
gid_t list[groups];
|
||||||
@ -151,19 +151,16 @@ int CheckIfInGroup(const char* string) {
|
|||||||
return -1;
|
return -1;
|
||||||
if (strcmp(grp->gr_name, "wheel") == 0)
|
if (strcmp(grp->gr_name, "wheel") == 0)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintHelp() {
|
void PrintHelp() { printf("Usage: %s [- user] command\n", command_name); }
|
||||||
printf("Usage: %s [- user] command\n", command_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
int AskPassword() {
|
int AskPassword()
|
||||||
|
{
|
||||||
for (int i = 0; i < MAX_PASSWORD; i++) {
|
for (int i = 0; i < MAX_PASSWORD; i++) {
|
||||||
printf("Password: ");
|
|
||||||
char password[BUFSIZ];
|
char password[BUFSIZ];
|
||||||
get_password(password);
|
get_password(password);
|
||||||
|
|
||||||
@ -172,10 +169,14 @@ int AskPassword() {
|
|||||||
return 1;
|
return 1;
|
||||||
else
|
else
|
||||||
CreatePasswordMessage();
|
CreatePasswordMessage();
|
||||||
}
|
sleep(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int CheckPassword(char* password) {
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int CheckPassword(char* password)
|
||||||
|
{
|
||||||
struct spwd* shadow_entry;
|
struct spwd* shadow_entry;
|
||||||
struct passwd* pa = getpwuid(getuid());
|
struct passwd* pa = getpwuid(getuid());
|
||||||
char *p, *correct, *supplied, *salt;
|
char *p, *correct, *supplied, *salt;
|
||||||
@ -200,11 +201,10 @@ int CheckPassword(char* password) {
|
|||||||
return !!strcmp(supplied, correct);
|
return !!strcmp(supplied, correct);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CreatePasswordMessage() {
|
void CreatePasswordMessage() { puts("Incorrect password! Try again."); }
|
||||||
puts("Incorrect password! Try again.");
|
|
||||||
}
|
|
||||||
|
|
||||||
void cats(char **str, const char *str2) {
|
void cats(char** str, const char* str2)
|
||||||
|
{
|
||||||
char* tmp = NULL;
|
char* tmp = NULL;
|
||||||
|
|
||||||
// Reset *str
|
// Reset *str
|
||||||
@ -218,8 +218,7 @@ void cats(char **str, const char *str2) {
|
|||||||
if (*str == NULL) {
|
if (*str == NULL) {
|
||||||
*str = calloc(strlen(str2) + 1, sizeof(char));
|
*str = calloc(strlen(str2) + 1, sizeof(char));
|
||||||
memcpy(*str, str2, strlen(str2));
|
memcpy(*str, str2, strlen(str2));
|
||||||
}
|
} else { // Append
|
||||||
else { // Append
|
|
||||||
tmp = calloc(strlen(*str) + 1, sizeof(char));
|
tmp = calloc(strlen(*str) + 1, sizeof(char));
|
||||||
memcpy(tmp, *str, strlen(*str));
|
memcpy(tmp, *str, strlen(*str));
|
||||||
*str = calloc(strlen(*str) + strlen(str2) + 1, sizeof(char));
|
*str = calloc(strlen(*str) + strlen(str2) + 1, sizeof(char));
|
||||||
@ -227,25 +226,40 @@ void cats(char **str, const char *str2) {
|
|||||||
memcpy(*str + strlen(*str), str2, strlen(str2));
|
memcpy(*str + strlen(*str), str2, strlen(str2));
|
||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_password(char* password)
|
void get_password(char* password)
|
||||||
{
|
{
|
||||||
static struct termios new_terminal;
|
static struct termios new_terminal;
|
||||||
|
|
||||||
|
int ttyfd = open("/dev/tty", O_RDWR);
|
||||||
|
|
||||||
|
tcgetattr(ttyfd, &old_terminal);
|
||||||
|
|
||||||
new_terminal = old_terminal;
|
new_terminal = old_terminal;
|
||||||
new_terminal.c_lflag &= ~(ECHO);
|
new_terminal.c_lflag &= ~(ECHO);
|
||||||
|
|
||||||
tcsetattr(STDIN_FILENO, TCSANOW, &new_terminal);
|
tcsetattr(ttyfd, TCSAFLUSH, &new_terminal);
|
||||||
|
|
||||||
if (fgets(password, BUFSIZ, stdin) == NULL)
|
write(ttyfd, "Password: ", 11);
|
||||||
password[0] = '\0';
|
|
||||||
else
|
getln(ttyfd, password, sizeof(password));
|
||||||
password[strlen(password)-1] = '\0';
|
|
||||||
|
tcsetattr(ttyfd, TCSAFLUSH, &old_terminal);
|
||||||
|
|
||||||
|
close(ttyfd);
|
||||||
|
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
|
||||||
tcsetattr(STDIN_FILENO, TCSANOW, &old_terminal);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void getln(int fd, char* buf, size_t bufsiz)
|
||||||
|
{
|
||||||
|
ssize_t nr = -1;
|
||||||
|
char ch;
|
||||||
|
while ((nr = read(fd, &ch, 1)) == 1 && ch != '\n' && ch != '\r') {
|
||||||
|
if (buf < buf + bufsiz - 1) {
|
||||||
|
*buf++ = ch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*buf = '\0';
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user