Usermode/ld-acess - Cleaning out unused/unneeded headers
[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                 // Spawn shell in a child process
51                 if(pid == 0)
52                 {
53                         char    *child_argv[2] = {NULL, 0};
54                         char    **child_envp = NULL;
55                         
56                         // Get user information
57                         uinfo = GetUserInfo(uid);
58                         
59                         child_argv[0] = uinfo->Shell;
60                         // Set Environment
61                         setgid(uinfo->GID);
62                         //setuid(uid);
63                         setuid(uinfo->UID);
64                         
65                         execve(uinfo->Shell, child_argv, child_envp);
66                         exit(-1);
67                 }
68                 
69                 // Wait for child to terminate
70                 waittid(pid, &status);
71         }
72         
73         return 0;
74 }
75
76 /**
77  * \fn char *GetUsername()
78  */
79 char *GetUsername()
80 {
81         char    ret[BUFLEN] = {0};
82          int    pos = 0;
83         char    ch;
84         
85         // Prompt the user
86         printf("Username: ");
87         
88         // Read in text
89         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
90         {
91                 if(ch == '\b') {
92                         if( pos <= 0 )  continue;
93                         pos --;
94                         ret[pos] = '\0';
95                 }
96                 else
97                         ret[pos++] = ch;
98                 
99                 // Echo out to the screen
100                 fputc(ch, stdout);
101                 
102                 if(pos == BUFLEN-1)     break;
103         }
104         
105         // Finish String
106         ret[pos] = '\0';
107         
108         printf("\n");
109         return strdup(ret);
110 }
111
112 /**
113  * \fn char *GetPassword()
114  */
115 char *GetPassword()
116 {
117         char    ret[BUFLEN];
118          int    pos = 0;
119         char    ch;
120         
121         // Prompt the user
122         printf("Password: ");
123         
124         // Read in text
125         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
126         {
127                 if(ch == '\b') {
128                         if( pos <= 0 )  continue;
129                         pos --;
130                         ret[pos] = '\0';
131                 }
132                 else
133                         ret[pos++] = ch;
134                 
135                 // Don't echo out to the screen
136                 //fputc(stdout, ch);
137                 
138                 if(pos == BUFLEN-1)     break;
139         }
140         
141         ret[pos] = '\0';
142         
143         printf("\n");
144         return strdup(ret);
145 }

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