Reorganised the modules directory, started serious work on GUI support
[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                         setuid(uinfo->UID);
62                         
63                         execve(uinfo->Shell, child_argv, child_envp);
64                         exit(-1);
65                 }
66                 
67                 // Wait for child to terminate
68                 waittid(pid, &status);
69         }
70         
71         return 0;
72 }
73
74 /**
75  * \fn char *GetUsername()
76  */
77 char *GetUsername()
78 {
79         char    ret[BUFLEN] = {0};
80          int    pos = 0;
81         char    ch;
82         
83         // Prompt the user
84         printf("Username: ");
85         
86         // Read in text
87         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
88         {
89                 if(ch == '\b') {
90                         pos --;
91                         ret[pos] = '\0';
92                 }
93                 else
94                         ret[pos++] = ch;
95                 
96                 // Echo out to the screen
97                 fputc(ch, stdout);
98                 
99                 if(pos == BUFLEN-1)     break;
100         }
101         
102         // Finish String
103         ret[pos] = '\0';
104         
105         printf("\n");
106         return strdup(ret);
107 }
108
109 /**
110  * \fn char *GetPassword()
111  */
112 char *GetPassword()
113 {
114         char    ret[BUFLEN];
115          int    pos = 0;
116         char    ch;
117         
118         // Prompt the user
119         printf("Password: ");
120         
121         // Read in text
122         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
123         {
124                 if(ch == '\b') {
125                         pos --;
126                         ret[pos] = '\0';
127                 }
128                 else
129                         ret[pos++] = ch;
130                 
131                 // Don't echo out to the screen
132                 //fputc(stdout, ch);
133                 
134                 if(pos == BUFLEN-1)     break;
135         }
136         
137         ret[pos] = '\0';
138         
139         printf("\n");
140         return strdup(ret);
141 }

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