Applications/IRC - Cleaning up command code
[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[?25h");    // Re-enable the cursor 
22 //      printf("\x1B[2J");      // Clear Screen
23
24         for(;;)
25         {
26                 // Validate User
27                 for(;;)
28                 {
29                         sUsername = GetUsername();
30                         if(!sUsername)  continue;
31                         sPassword = GetPassword();
32                         if(!sPassword)  continue;
33                         if( (uid = ValidateUser(sUsername, sPassword)) == -1 )
34                         {
35                                 printf("\nInvalid username or password\n");
36                                 _SysDebug("Auth failure: '%s':'%s'", sUsername, sPassword);
37                                 free(sUsername);
38                                 free(sPassword);
39                         }
40                         else
41                                 break;
42                 }
43                 printf("\n");
44
45                 uinfo = GetUserInfo(uid);
46                 struct s_sys_spawninfo  spawninfo;
47                 spawninfo.flags = 0;
48                 spawninfo.gid = uinfo->GID;
49                 spawninfo.uid = uinfo->UID;
50                 const char      *child_argv[2] = {"-", 0};
51                 const char      **child_envp = NULL;
52                  int    fds[] = {0, 1, 2};
53                 pid = _SysSpawn(uinfo->Shell, child_argv, child_envp, 3, fds, &spawninfo);
54                 // Error check
55                 if(pid == -1) {
56                         fprintf(stderr, "Unable to fork the login process!\n");
57                         return -1;
58                 }
59                 
60                 // Wait for child to terminate
61                 _SysWaitTID(pid, &status);
62         }
63         
64         return 0;
65 }
66
67 char *_GetString(int bEcho)
68 {
69         char    ret[BUFLEN];
70          int    pos = 0;
71         char    ch;
72         
73         // Read in text
74         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
75         {
76                 // Handle backspace
77                 if(ch == '\b') {
78                         if( pos <= 0 )  continue;
79                         pos --;
80                         ret[pos] = '\0';
81                 }
82                 // Ctrl-C : Cancel
83                 else if( ch == 'c'-'a'+1)
84                         pos = 0;
85                 // Ctrl-U : Clear
86                 else if( ch == 'u'-'a'+1)
87                         pos = 0;
88                 // Ignore \r
89                 else if( ch == '\r' )
90                         continue;
91                 else
92                         ret[pos++] = ch;
93                 
94                 // Don't echo out to the screen
95                 if( bEcho ) {
96                         fputc(ch, stdout);
97                         fflush(stdout);
98                 }
99                 
100                 if(pos == BUFLEN-1)     break;
101         }
102         
103         ret[pos] = '\0';
104         
105         printf("\n");
106         return strdup(ret);
107 }
108
109 /**
110  * \fn char *GetUsername()
111  */
112 char *GetUsername()
113 {
114         char    ret[BUFLEN] = {0};
115          int    pos = 0;
116         char    ch;
117         
118         // Prompt the user
119         printf("Username: ");
120         fflush(stdout);
121         
122         return _GetString(1);
123 }
124
125 /**
126  * \fn char *GetPassword()
127  */
128 char *GetPassword()
129 {
130         // Prompt the user
131         printf("Password: ");
132         fflush(stdout);
133         
134         return _GetString(0);
135 }

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