59b4a784d5bab9d047f3864a267d2ad20efc7aa6
[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                 uinfo = GetUserInfo(uid);
43                 struct s_sys_spawninfo  spawninfo;
44                 spawninfo.flags = 0;
45                 spawninfo.gid = uinfo->GID;
46                 spawninfo.uid = uinfo->UID;
47                 const char      *child_argv[2] = {"-", 0};
48                 const char      **child_envp = NULL;
49                  int    fds[] = {0, 1, 2};
50                 pid = _SysSpawn(uinfo->Shell, child_argv, child_envp, 3, fds, &spawninfo);
51                 // Error check
52                 if(pid == -1) {
53                         fprintf(stderr, "Unable to fork the login process!\n");
54                         return -1;
55                 }
56                 
57                 // Wait for child to terminate
58                 _SysWaitTID(pid, &status);
59         }
60         
61         return 0;
62 }
63
64 /**
65  * \fn char *GetUsername()
66  */
67 char *GetUsername()
68 {
69         char    ret[BUFLEN] = {0};
70          int    pos = 0;
71         char    ch;
72         
73         // Prompt the user
74         printf("Username: ");
75         
76         // Read in text
77         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
78         {
79                 if(ch == '\b') {
80                         if( pos <= 0 )  continue;
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                         if( pos <= 0 )  continue;
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