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

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