tuilogin.c (2703B)
1 /* form and string handling */ 2 #include <form.h> 3 #include <string.h> 4 5 /* for writing to file descriptors */ 6 #include <unistd.h> 7 8 /* for getting errors from library functions */ 9 #include <errno.h> 10 11 /* for setting environment variables */ 12 #include <stdlib.h> 13 14 /* user & password handling */ 15 #include <sys/types.h> 16 #include <pwd.h> 17 18 /* for initgroups() */ 19 #include <grp.h> 20 21 int main(void) 22 { 23 FIELD *field[3]; 24 FORM *login_form; 25 int row, col; 26 char user[32]; 27 char password[255]; 28 struct passwd *userinfo; 29 30 initscr(); /* initialize */ 31 cbreak(); /* fix tty from 'cooked' mode */ 32 getmaxyx(stdscr, row, col); /* getting the max rows & columns */ 33 noecho(); /* disable general echoing */ 34 keypad(stdscr, TRUE); /* setup keyboard */ 35 36 // TODO: display forms, fields & input on subwindow 37 //WINDOW *formwin = subwin() 38 39 int prow = row / 2; /* centered prompt row y */ 40 int pcol = col / 2; /* centered prompt col x */ 41 42 /* Initialize the fields */ 43 /* new_field(height, width, topleft row, topleft column, off-screen rows, additional buffers) */ 44 field[0] = new_field(1, 10, prow, pcol, 0, 0); 45 field[1] = new_field(1, 10, prow + 2, pcol, 0, 0); 46 field[2] = NULL; // *field[] is NULL terminated 47 48 /* Set field options */ 49 set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ 50 field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when filled */ 51 set_field_back(field[1], A_UNDERLINE); 52 field_opts_off(field[1], O_AUTOSKIP); 53 54 /* Create the form and post it */ 55 login_form = new_form(field); 56 post_form(login_form); 57 mvprintw(prow, pcol - 9, "Username:"); 58 mvprintw(prow + 2, pcol - 9, "Password:"); 59 box(stdscr, ACS_VLINE, ACS_HLINE); 60 refresh(); 61 echo(); 62 63 /* readiness notification for process supervision */ 64 write(3, "\n", 2); 65 close(3); 66 67 /* since we utilize readiness notifs we are just going to error out if there's a bad password/user and rely on the supervision to restart us */ 68 // TODO: error handling if password is wrong 69 mvgetnstr(prow, pcol, user, 32); 70 noecho(); 71 mvgetnstr(prow + 2, pcol, password, 255); 72 73 74 /* free our forms and fields */ 75 unpost_form(login_form); 76 free_form(login_form); 77 free_field(field[0]); 78 free_field(field[1]); 79 endwin(); /* safely close ncurses */ 80 81 /* getting our password info from the user */ 82 errno = 0; 83 userinfo = getpwnam(user); 84 if (userinfo == NULL) { 85 if (errno) 86 printf("getpwnam %s:\n", user); 87 else 88 printf("wrong username.\n"); 89 } 90 initgroups(userinfo->pw_name, userinfo->pw_gid); 91 setgid(userinfo->pw_gid); 92 setuid(userinfo->pw_uid); 93 94 setenv("HOME", userinfo->pw_dir, 1); 95 setenv("SHELL", userinfo->pw_shell, 1); 96 chdir(userinfo->pw_dir); 97 execl(userinfo->pw_shell, userinfo->pw_shell, NULL); 98 return errno; 99 }