-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.c
More file actions
125 lines (104 loc) · 2.94 KB
/
Copy pathauth.c
File metadata and controls
125 lines (104 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "auth.h"
#include "tsh.h"
#include "helper.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
// if name exists in file passwd, set variable password to be the corresponding password
int exist_user(char * name, char password[]) {
char line[MAXLINE * 2];
char * line_name, * line_password;
FILE * fp = fopen("./etc/passwd", "r");
if(fp == NULL){
unix_error("fopen");
}
while(fgets(line, MAXLINE * 2, fp)!=NULL){
line_name = strtok(line, ":");
if(strcmp(name, line_name) == 0){
line_password = strtok(NULL, ":");
strcpy(password, line_password);
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
int check_auth(char * name, char * passwd) {
char line_password[MAXLINE];
if(exist_user(name, line_password)){
if(strcmp(line_password, passwd) == 0){
return 1;
}
}
return 0;
}
/*
* login - Performs user authentication for the shell
*
* See specificaiton for how this function should act
*
* This function returns a string of the username that is logged in
*/
char * login() {
char * name = (char*)malloc(sizeof(char) * MAXLINE);
char passwd[MAXLINE];
while(1){
printf("username: ");
fflush(stdout);
fgets(name, MAXLINE, stdin); // use fgets, instead of scanf, in case there are spaces in the input
name[strlen(name) - 1] = '\0'; // deal with '\n' at the end
if(strcmp(name, "quit")==0){
free(name);
exit(0);
}
printf("password: ");
fflush(stdout);
fgets(passwd, MAXLINE, stdin);
passwd[strlen(passwd) - 1] = '\0';
if(strcmp(passwd, "quit")==0){
free(name);
exit(0);
}
if(check_auth(name, passwd)){
return name;
}else{
print_error("User Authentication failed. Please try again.\n");
}
}
}
void add_user(char ** argv) {
if(strcmp(username, "root")!=0){
print_error("root privileges required to run adduser.\n");
return;
}
if(argv[1] == NULL || argv[2]==NULL){
print_error("need more arguments\n");
return;
} else if(argv[3] != NULL){
print_error("too many arguments\n");
return;
}
char password[MAXLINE];
if(exist_user(argv[1], password)){
print_error("User already exists\n");
return;
}
FILE * fp = fopen("./etc/passwd", "a");
if(fp == NULL){
unix_error("fopen");
}
fprintf(fp, "%s:%s:/home/%s\n", argv[1], argv[2], argv[1]);
fclose(fp);
char path[MAXLINE+20];
sprintf(path, "./home/%s", argv[1]);
if(mkdir(path, 0777)!=0)
unix_error("mkdir error");
strcat(path, "/.tsh_history");
FILE * historyp = fopen(path, "w");
if(historyp == NULL){
unix_error("fopen");
}
fclose(historyp);
}