static void spawn_login(struct passwd *pwd, const char *tty, const char *id) { char *p, buf[50]; /* utmp */ ut.ut_type = USER_PROCESS; ut.ut_pid = getpid(); strncpy(ut.ut_line, tty, UT_LINESIZE); strncpy(ut.ut_id, id, 2); time(&ut.ut_time); strncpy(ut.ut_user, pwd->pw_name, UT_NAMESIZE); pututline(&ut); /* Don't leak utmp into the child */ endutent(); /* We don't care if initgroups fails - it only grants extra rights */ //initgroups(pwd->pw_name, pwd->pw_gid); /* But we do care if these fail! */ if (setgid(pwd->pw_gid) == -1 || setuid(pwd->pw_uid) == -1) _exit(255); signal(SIGINT, SIG_DFL); /* setup user environment variables */ envset("LOGNAME", pwd->pw_name); envset("HOME", pwd->pw_dir); envset("SHELL", pwd->pw_shell); /* home directory */ if (chdir(pwd->pw_dir)) putstr("login: unable to change to home directory, using /\n"); /* show the motd file */ if (!showfile("/etc/motd")) crlf(); /* and spawn the shell */ strcpy(buf, "-"); if ((p = strrchr(pwd->pw_shell, '/')) != NULL) strcat(buf, ++p); else strcat(buf, pwd->pw_shell); argp[0] = buf; argp[1] = NULL; execve(pwd->pw_shell, (void *) argp, (void *) env); putstr("login: can't execute shell\n"); exit(1); }
/* * Internal implementation of "getty" and "login" */ static pid_t getty(const char *ttyname, const char *id) { int fdtty, pid; struct passwd *pwd; const char *pr; char *p, buf[50], salt[3]; char hn[64]; gethostname(hn, sizeof(hn)); for (;;) { pid = fork(); if (pid == -1) { putstr("init: can't fork\n"); } else { if (pid != 0) /* parent's context: return pid of the child process */ return pid; close(0); close(1); close(2); setpgrp(); setpgid(0,0); /* Throw all the init working spacd we inherited */ brk_warn(membase); fdtty = open(ttyname, O_RDWR); if (fdtty < 0) return -1; /* here we are inside child's context of execution */ envset("PATH", "/bin:/usr/bin"); envset("CTTY", ttyname); /* make stdin, stdout and stderr point to fdtty */ dup(fdtty); dup(fdtty); ut.ut_type = INIT_PROCESS; ut.ut_pid = getpid(); ut.ut_id[0] = id[0]; ut.ut_id[1] = id[1]; pututline(&ut); /* display the /etc/issue file, if exists */ showfile("/etc/issue"); if (*hn) { putstr(hn); putstr(" "); } /* loop until a valid user name is entered * and a shell is spawned */ for (;;) { putstr("login: "******"Password: "******""; } if (strcmp(pr, pwd->pw_passwd) == 0) spawn_login(pwd, ttyname, id); } putstr("\nLogin incorrect\n\n"); signal(SIGALRM, sigalarm); alarm(2); pause(); } } } }
/** * built-in commands: exit, in, out, bg, fg, jobs, kill * returns 1 if a built-in command is executed, * 0 otherwise */ int checkBuiltInCommands() { if (strcmp("history", commandArgv[0]) == 0) { ReversePrint(); } if (strcmp("alias", commandArgv[0]) == 0) { return 0; } if(strcmp("which", commandArgv[0]) == 0) { if (commandArgv[1] == NULL) { printf("R-Shell: Missing Parameters\n"); } else { printwhich(); } return 1; } if(strcmp("where", commandArgv[0]) == 0) { if (commandArgv[1] == NULL) { printf("R-Shell: Missing Parameters\n"); } else { printwhere(); } return 1; } if(strcmp("setenv", commandArgv[0]) == 0) { envset(); return 1; } if(strcmp("printenv", commandArgv[0]) == 0) { envprint(); return 1; } if (strcmp("pid", commandArgv[0]) == 0) { printpid(); return 1; } if (strcmp("pwd", commandArgv[0]) == 0) { printpwd(); return 1; } if (strcmp("prompt", commandArgv[0]) == 0) { promptCmd(); return 1; } if (strcmp("exit", commandArgv[0]) == 0) { exit(EXIT_SUCCESS); } if (strcmp("cd", commandArgv[0]) == 0) { changeDirectory(); return 1; } if (strcmp("kill", commandArgv[0]) == 0) { if (commandArgv[1] == NULL) return 0; killJob(atoi(commandArgv[1])); return 1; } if (strcmp("ls", commandArgv[0]) == 0) { return 0; } if (strcmp("cd", commandArgv[0]) == 0) { return 0; } return 0; }