Usermode/login - Made the login process re-enable the cursor
[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         printf("\x1B[?25h");    // Re-enable the cursor 
22         printf("\x1B[2J");      // Clear Screen
23
24         for(;;)
25         {
26                 // Validate User
27                 for(;;)
28                 {
29                         sUsername = GetUsername();
30                         sPassword = GetPassword();
31                         if( (uid = ValidateUser(sUsername, sPassword)) == -1 )
32                         {
33                                 printf("\nInvalid username or password\n");
34                                 free(sUsername);
35                                 free(sPassword);
36                         }
37                         else
38                                 break;
39                 }
40                 printf("\n");
41                 
42                 // Create child process
43                 pid = clone(CLONE_VM, 0);
44                 // Error check
45                 if(pid == -1) {
46                         fprintf(stderr, "Unable to fork the login process!\n");
47                         return -1;
48                 }
49                 
50                 printf("pid = %i\n", pid);
51                 
52                 // Spawn shell in a child process
53                 if(pid == 0)
54                 {
55                         char    *child_argv[2] = {NULL, 0};
56                         char    **child_envp = NULL;
57                         
58                         // Get user information
59                         uinfo = GetUserInfo(uid);
60                         
61                         child_argv[0] = uinfo->Shell;
62                         // Set Environment
63                         setgid(uinfo->GID);
64                         //setuid(uid);
65                         setuid(uinfo->UID);
66                         
67                         execve(uinfo->Shell, child_argv, child_envp);
68                         exit(-1);
69                 }
70                 
71                 // Wait for child to terminate
72                 waittid(pid, &status);
73         }
74         
75         return 0;
76 }
77
78 /**
79  * \fn char *GetUsername()
80  */
81 char *GetUsername()
82 {
83         char    ret[BUFLEN] = {0};
84          int    pos = 0;
85         char    ch;
86         
87         // Prompt the user
88         printf("Username: ");
89         
90         // Read in text
91         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
92         {
93                 if(ch == '\b') {
94                         if( pos <= 0 )  continue;
95                         pos --;
96                         ret[pos] = '\0';
97                 }
98                 else
99                         ret[pos++] = ch;
100                 
101                 // Echo out to the screen
102                 fputc(ch, stdout);
103                 
104                 if(pos == BUFLEN-1)     break;
105         }
106         
107         // Finish String
108         ret[pos] = '\0';
109         
110         printf("\n");
111         return strdup(ret);
112 }
113
114 /**
115  * \fn char *GetPassword()
116  */
117 char *GetPassword()
118 {
119         char    ret[BUFLEN];
120          int    pos = 0;
121         char    ch;
122         
123         // Prompt the user
124         printf("Password: ");
125         
126         // Read in text
127         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
128         {
129                 if(ch == '\b') {
130                         if( pos <= 0 )  continue;
131                         pos --;
132                         ret[pos] = '\0';
133                 }
134                 else
135                         ret[pos++] = ch;
136                 
137                 // Don't echo out to the screen
138                 //fputc(stdout, ch);
139                 
140                 if(pos == BUFLEN-1)     break;
141         }
142         
143         ret[pos] = '\0';
144         
145         printf("\n");
146         return strdup(ret);
147 }

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