Usermode/init,login - Updated to new PTY code
[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         
66         return 0;
67 }
68
69 char *_GetString(int bEcho)
70 {
71         char    ret[BUFLEN];
72          int    pos = 0;
73         char    ch;
74         
75         struct ptymode  mode;
76         const int       is_pty = (_SysIOCtl(0, DRV_IOCTL_TYPE, NULL) == DRV_TYPE_TERMINAL);
77
78         // Clear PTY echo
79         if( !bEcho && is_pty ) {
80                 _SysIOCtl(0, PTY_IOCTL_GETMODE, &mode);
81                 mode.InputMode &= ~PTYIMODE_ECHO;
82                 _SysIOCtl(0, PTY_IOCTL_SETMODE, &mode);
83         }
84         
85         // Read in text
86         while( (ch = fgetc(stdin)) != -1 && ch != '\n' )
87         {
88                 // Handle backspace
89                 if(ch == '\b') {
90                         if( pos <= 0 )  continue;
91                         pos --;
92                         ret[pos] = '\0';
93                 }
94                 // Ctrl-C : Cancel
95                 else if( ch == 'c'-'a'+1)
96                         pos = 0;
97                 // Ctrl-U : Clear
98                 else if( ch == 'u'-'a'+1)
99                         pos = 0;
100                 // Ignore \r
101                 else if( ch == '\r' )
102                         continue;
103                 else
104                         ret[pos++] = ch;
105                 
106                 if(pos == BUFLEN-1)     break;
107         }
108         
109         ret[pos] = '\0';
110
111         // Re-set echo
112         if( !bEcho && is_pty ) {
113                 mode.InputMode |= PTYIMODE_ECHO;
114                 _SysIOCtl(0, PTY_IOCTL_SETMODE, &mode);
115         }
116         
117         return strdup(ret);
118 }
119
120 /**
121  * \fn char *GetUsername()
122  */
123 char *GetUsername()
124 {
125         char    ret[BUFLEN] = {0};
126          int    pos = 0;
127         char    ch;
128         
129         // Prompt the user
130         printf("Username: ");
131         fflush(stdout);
132         
133         return _GetString(1);
134 }
135
136 /**
137  * \fn char *GetPassword()
138  */
139 char *GetPassword()
140 {
141         // Prompt the user
142         printf("Password: ");
143         fflush(stdout);
144         
145         return _GetString(0);
146 }

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