Fixed CLIShell's `dir` command, added a logout/exit command. Added a newline after...
[tpg/acess2.git] / Usermode / Applications / login_src / main.c
1 /*
2  * Acess 2 Login
3  */
4 #include "header.h"
5
6 // === CONSTANTS ===
7 #define BUFLEN  1024
8
9 // === PROTOTYPES ===
10 char    *GetUsername();
11 char    *GetPassword();
12
13 // === CODE ===
14 int main(int argc, char *argv[])
15 {
16         char    *sUsername, *sPassword;
17          int    pid, uid = 0;
18          int    status = 0;
19         tUserInfo       *uinfo;
20         
21         for(;;)
22         {
23                 printf("\x1B[2J");      // Clear Screen
24                 // Validate User
25                 do {
26                         if(uid == -1)   printf("\n");
27                         sUsername = GetUsername();
28                         sPassword = GetPassword();
29                         printf("\n");
30                 } while( (uid = ValidateUser(sUsername, sPassword)) == -1 );
31                 printf("\n");
32                 
33                 // Create child process
34                 pid = clone(CLONE_VM, 0);
35                 // Error check
36                 if(pid == -1) {
37                         fprintf(stderr, "Unable to fork the login process!\n");
38                         return -1;
39                 }
40                 
41                 // Spawn shell in a child process
42                 if(pid == 0)
43                 {
44                         char    *child_argv[2] = {NULL, 0};
45                         char    **child_envp = NULL;
46                         
47                         // Get user information
48                         uinfo = GetUserInfo(uid);
49                         
50                         child_argv[0] = uinfo->Shell;
51                         // Set Environment
52                         setgid(uinfo->GID);
53                         setuid(uid);
54                         
55                         execve(uinfo->Shell, child_argv, child_envp);
56                         exit(-1);
57                 }
58                 
59                 // Wait for child to terminate
60                 waittid(pid, &status);
61         }
62         
63         return 0;
64 }
65
66 /**
67  * \fn char *GetUsername()
68  */
69 char *GetUsername()
70 {
71         char    ret[BUFLEN];
72          int    pos = 0;
73         char    ch;
74         
75         // Prompt the user
76         printf("Username: ");
77         
78         // Read in text
79         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
80         {
81                 if(ch == '\b') {
82                         pos --;
83                         ret[pos] = '\0';
84                 }
85                 else
86                         ret[pos++] = ch;
87                 
88                 // Echo out to the screen
89                 fputc(ch, stdout);
90                 
91                 if(pos == BUFLEN-1)     break;
92         }
93         
94         // Finish String
95         ret[pos] = '\0';
96         
97         printf("\n");
98         return strdup(ret);
99 }
100
101 /**
102  * \fn char *GetPassword()
103  */
104 char *GetPassword()
105 {
106         char    ret[BUFLEN];
107          int    pos = 0;
108         char    ch;
109         
110         // Prompt the user
111         printf("Password: ");
112         
113         // Read in text
114         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
115         {
116                 if(ch == '\b') {
117                         pos --;
118                         ret[pos] = '\0';
119                 }
120                 else
121                         ret[pos++] = ch;
122                 
123                 // Don't echo out to the screen
124                 //fputc(stdout, ch);
125                 
126                 if(pos == BUFLEN-1)     break;
127         }
128         
129         ret[pos] = '\0';
130         
131         printf("\n");
132         return strdup(ret);
133 }

UCC git Repository :: git.ucc.asn.au