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

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