Merge branch 'master' of [email protected]:acess2
[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                         if( pos <= 0 )  continue;
91                         pos --;
92                         ret[pos] = '\0';
93                 }
94                 else
95                         ret[pos++] = ch;
96                 
97                 // Echo out to the screen
98                 fputc(ch, stdout);
99                 
100                 if(pos == BUFLEN-1)     break;
101         }
102         
103         // Finish String
104         ret[pos] = '\0';
105         
106         printf("\n");
107         return strdup(ret);
108 }
109
110 /**
111  * \fn char *GetPassword()
112  */
113 char *GetPassword()
114 {
115         char    ret[BUFLEN];
116          int    pos = 0;
117         char    ch;
118         
119         // Prompt the user
120         printf("Password: ");
121         
122         // Read in text
123         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
124         {
125                 if(ch == '\b') {
126                         if( pos <= 0 )  continue;
127                         pos --;
128                         ret[pos] = '\0';
129                 }
130                 else
131                         ret[pos++] = ch;
132                 
133                 // Don't echo out to the screen
134                 //fputc(stdout, ch);
135                 
136                 if(pos == BUFLEN-1)     break;
137         }
138         
139         ret[pos] = '\0';
140         
141         printf("\n");
142         return strdup(ret);
143 }

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