86b188420a5d27d82129b4a8d0d699c0b5696da5
[tpg/acess2.git] / Usermode / Applications / login_src / main.c
1 /*
2  * Acess 2 Login
3  * - By John Hodge (thePowersGang)
4  */
5 #include "header.h"
6 #include <acess/devices/pty.h>  // Enable/disable echo
7
8 // === CONSTANTS ===
9 #define BUFLEN  1024
10
11 // === PROTOTYPES ===
12 char    *GetUsername();
13 char    *GetPassword();
14
15 // === CODE ===
16 int main(int argc, char *argv[])
17 {
18         char    *sUsername, *sPassword;
19          int    pid, uid = 0;
20          int    status = 0;
21         tUserInfo       *uinfo;
22
23         printf("\x1B[?25h");    // Re-enable the cursor 
24 //      printf("\x1B[2J");      // Clear Screen
25
26         for(;;)
27         {
28                 // Validate User
29                 for(;;)
30                 {
31                         sUsername = GetUsername();
32                         if(!sUsername || !sUsername[0]) continue;
33                         sPassword = GetPassword();
34                         if(!sPassword) {
35                                 free(sUsername);
36                                 continue;
37                         }
38                         if( (uid = ValidateUser(sUsername, sPassword)) == -1 )
39                         {
40                                 printf("\nInvalid username or password\n");
41                                 _SysDebug("Auth failure: '%s':'%s'", sUsername, sPassword);
42                                 free(sUsername);
43                                 free(sPassword);
44                         }
45                         else
46                                 break;
47                 }
48                 printf("\n");
49
50                 uinfo = GetUserInfo(uid);
51                 struct s_sys_spawninfo  spawninfo;
52                 spawninfo.flags = 0;
53                 spawninfo.gid = uinfo->GID;
54                 spawninfo.uid = uinfo->UID;
55                 const char      *child_argv[2] = {"-", 0};
56                 const char      **child_envp = NULL;
57                  int    fds[] = {0, 1, 2};
58                 pid = _SysSpawn(uinfo->Shell, child_argv, child_envp, 3, fds, &spawninfo);
59                 // Error check
60                 if(pid == -1) {
61                         fprintf(stderr, "Unable to fork the login process!\n");
62                         return -1;
63                 }
64                 
65                 // Wait for child to terminate
66                 _SysWaitTID(pid, &status);
67         
68                 // Clear graphics mode  
69                 struct ptymode  mode = {.InputMode = PTYIMODE_ECHO|PTYIMODE_CANON,.OutputMode=0};
70                 _SysIOCtl(0, PTY_IOCTL_SETMODE, &mode);
71                 fprintf(stderr, "\x1b[R");
72         }
73         
74         return 0;
75 }
76
77 char *_GetString(int bEcho)
78 {
79         char    ret[BUFLEN];
80          int    pos = 0;
81         char    ch;
82         
83         struct ptymode  mode;
84         const int       is_pty = (_SysIOCtl(0, DRV_IOCTL_TYPE, NULL) == DRV_TYPE_TERMINAL);
85
86         // Clear PTY echo
87         if( !bEcho && is_pty ) {
88                 _SysIOCtl(0, PTY_IOCTL_GETMODE, &mode);
89                 mode.InputMode &= ~PTYIMODE_ECHO;
90                 _SysIOCtl(0, PTY_IOCTL_SETMODE, &mode);
91         }
92         
93         // Read in text
94         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
95         {
96                 // Handle backspace
97                 if(ch == '\b') {
98                         if( pos <= 0 )  continue;
99                         pos --;
100                         ret[pos] = '\0';
101                 }
102                 // Ctrl-C : Cancel
103                 else if( ch == 'c'-'a'+1)
104                         pos = 0;
105                 // Ctrl-U : Clear
106                 else if( ch == 'u'-'a'+1)
107                         pos = 0;
108                 // Ignore \r
109                 else if( ch == '\r' )
110                         continue;
111                 else
112                         ret[pos++] = ch;
113                 
114                 if(pos == BUFLEN-1)     break;
115         }
116         
117         ret[pos] = '\0';
118
119         // Re-set echo
120         if( !bEcho && is_pty ) {
121                 mode.InputMode |= PTYIMODE_ECHO;
122                 _SysIOCtl(0, PTY_IOCTL_SETMODE, &mode);
123         }
124
125         return strdup(ret);
126 }
127
128 /**
129  * \fn char *GetUsername()
130  */
131 char *GetUsername()
132 {
133         char    ret[BUFLEN] = {0};
134          int    pos = 0;
135         char    ch;
136         
137         // Prompt the user
138         printf("Username: ");
139         fflush(stdout);
140         
141         return _GetString(1);
142 }
143
144 /**
145  * \fn char *GetPassword()
146  */
147 char *GetPassword()
148 {
149         // Prompt the user
150         printf("Password: ");
151         fflush(stdout);
152         
153         return _GetString(0);
154 }

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