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

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