Usermode/login - Added PTY mode reset before showing prompt
[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)  continue;
33                         sPassword = GetPassword();
34                         if(!sPassword)  continue;
35                         if( (uid = ValidateUser(sUsername, sPassword)) == -1 )
36                         {
37                                 printf("\nInvalid username or password\n");
38                                 _SysDebug("Auth failure: '%s':'%s'", sUsername, sPassword);
39                                 free(sUsername);
40                                 free(sPassword);
41                         }
42                         else
43                                 break;
44                 }
45                 printf("\n");
46
47                 uinfo = GetUserInfo(uid);
48                 struct s_sys_spawninfo  spawninfo;
49                 spawninfo.flags = 0;
50                 spawninfo.gid = uinfo->GID;
51                 spawninfo.uid = uinfo->UID;
52                 const char      *child_argv[2] = {"-", 0};
53                 const char      **child_envp = NULL;
54                  int    fds[] = {0, 1, 2};
55                 pid = _SysSpawn(uinfo->Shell, child_argv, child_envp, 3, fds, &spawninfo);
56                 // Error check
57                 if(pid == -1) {
58                         fprintf(stderr, "Unable to fork the login process!\n");
59                         return -1;
60                 }
61                 
62                 // Wait for child to terminate
63                 _SysWaitTID(pid, &status);
64         
65                 // Clear graphics mode  
66                 struct ptymode  mode = {.InputMode = PTYIMODE_ECHO|PTYIMODE_CANON,.OutputMode=0};
67                 _SysIOCtl(0, PTY_IOCTL_SETMODE, &mode);
68         }
69         
70         return 0;
71 }
72
73 char *_GetString(int bEcho)
74 {
75         char    ret[BUFLEN];
76          int    pos = 0;
77         char    ch;
78         
79         struct ptymode  mode;
80         const int       is_pty = (_SysIOCtl(0, DRV_IOCTL_TYPE, NULL) == DRV_TYPE_TERMINAL);
81
82         // Clear PTY echo
83         if( !bEcho && is_pty ) {
84                 _SysIOCtl(0, PTY_IOCTL_GETMODE, &mode);
85                 mode.InputMode &= ~PTYIMODE_ECHO;
86                 _SysIOCtl(0, PTY_IOCTL_SETMODE, &mode);
87         }
88         
89         // Read in text
90         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
91         {
92                 // Handle backspace
93                 if(ch == '\b') {
94                         if( pos <= 0 )  continue;
95                         pos --;
96                         ret[pos] = '\0';
97                 }
98                 // Ctrl-C : Cancel
99                 else if( ch == 'c'-'a'+1)
100                         pos = 0;
101                 // Ctrl-U : Clear
102                 else if( ch == 'u'-'a'+1)
103                         pos = 0;
104                 // Ignore \r
105                 else if( ch == '\r' )
106                         continue;
107                 else
108                         ret[pos++] = ch;
109                 
110                 if(pos == BUFLEN-1)     break;
111         }
112         
113         ret[pos] = '\0';
114
115         // Re-set echo
116         if( !bEcho && is_pty ) {
117                 mode.InputMode |= PTYIMODE_ECHO;
118                 _SysIOCtl(0, PTY_IOCTL_SETMODE, &mode);
119         }
120         
121         return strdup(ret);
122 }
123
124 /**
125  * \fn char *GetUsername()
126  */
127 char *GetUsername()
128 {
129         char    ret[BUFLEN] = {0};
130          int    pos = 0;
131         char    ch;
132         
133         // Prompt the user
134         printf("Username: ");
135         fflush(stdout);
136         
137         return _GetString(1);
138 }
139
140 /**
141  * \fn char *GetPassword()
142  */
143 char *GetPassword()
144 {
145         // Prompt the user
146         printf("Password: ");
147         fflush(stdout);
148         
149         return _GetString(0);
150 }

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