Fixed bug where system would lock if a user task segfaulted (forgot to sti)
[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                 } while( (uid = ValidateUser(sUsername, sPassword)) == -1 );
30                 printf("\n");
31                 
32                 // Create child process
33                 pid = clone(CLONE_VM, 0);
34                 // Error check
35                 if(pid == -1) {
36                         fprintf(stderr, "Unable to fork the login process!\n");
37                         return -1;
38                 }
39                 
40                 // Spawn shell in a child process
41                 if(pid == 0)
42                 {
43                         char    *child_argv[2] = {NULL, 0};
44                         char    **child_envp = NULL;
45                         
46                         // Get user information
47                         uinfo = GetUserInfo(uid);
48                         
49                         child_argv[0] = uinfo->Shell;
50                         // Set Environment
51                         setgid(uinfo->GID);
52                         setuid(uid);
53                         
54                         execve(uinfo->Shell, child_argv, child_envp);
55                         exit(-1);
56                 }
57                 
58                 // Wait for child to terminate
59                 waittid(pid, &status);
60         }
61         
62         return 0;
63 }
64
65 /**
66  * \fn char *GetUsername()
67  */
68 char *GetUsername()
69 {
70         char    ret[BUFLEN];
71          int    pos = 0;
72         char    ch;
73         
74         // Prompt the user
75         printf("Username: ");
76         
77         // Read in text
78         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
79         {
80                 if(ch == '\b') {
81                         pos --;
82                         ret[pos] = '\0';
83                 }
84                 else
85                         ret[pos++] = ch;
86                 
87                 // Echo out to the screen
88                 fputc(ch, stdout);
89                 
90                 if(pos == BUFLEN-1)     break;
91         }
92         
93         // Finish String
94         ret[pos] = '\0';
95         
96         printf("\n");
97         return strdup(ret);
98 }
99
100 /**
101  * \fn char *GetPassword()
102  */
103 char *GetPassword()
104 {
105         char    ret[BUFLEN];
106          int    pos = 0;
107         char    ch;
108         
109         // Prompt the user
110         printf("Password: ");
111         
112         // Read in text
113         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
114         {
115                 if(ch == '\b') {
116                         pos --;
117                         ret[pos] = '\0';
118                 }
119                 else
120                         ret[pos++] = ch;
121                 
122                 // Don't echo out to the screen
123                 //fputc(stdout, ch);
124                 
125                 if(pos == BUFLEN-1)     break;
126         }
127         
128         ret[pos] = '\0';
129         
130         printf("\n");
131         return strdup(ret);
132 }

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