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

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