2
3
4
5
6
7
8
12#include <security/pam_appl.h>
13#include <security/pam_misc.h>
22#include <sys/resource.h>
29#define HOST "localhost"
41static struct pam_conv conv = {
47
48
49
50
51
52
53void ABEND(
int e,
char const *);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77int main(
int argc,
char **argv) {
79 char exec_cmd[
MAXLEN] =
"/usr/bin/bash";
82 bool ssh_login =
false;
87 pam_handle_t *pamh = NULL;
89 char *username = getenv(
"USER");
90 if (username == NULL) {
91 syslog(LOG_ERR,
"USER environment variable not set");
92 fprintf(stderr,
"Error: USER environment variable not set\n");
95 retval = pam_start(
"rsh-auth", username, &conv, &pamh);
96 if (retval != PAM_SUCCESS) {
97 syslog(LOG_ERR,
"PAM start failed: %s", pam_strerror(pamh, retval));
98 fprintf(stderr,
"PAM start failed: %s\n", pam_strerror(pamh, retval));
111 retval = pam_authenticate(pamh, 0);
112 if (retval == PAM_SUCCESS) {
113 syslog(LOG_AUTH,
"Authentication succeeded for user '%s': %s", username, pam_strerror(pamh, retval));
115 syslog(LOG_AUTH,
"Authentication failed for user '%s': %s", username, pam_strerror(pamh, retval));
116 fprintf(stderr,
"Failure: Authentication failed: %s\n", pam_strerror(pamh, retval));
120 pam_end(pamh, retval);
122 if (retval != PAM_SUCCESS) {
123 fprintf(stderr,
"PAM authentication failed: %s\n", pam_strerror(pamh, retval));
128 if ((p = getenv(
"SHELL")))
129 strncpy(exec_cmd, p,
MAXLEN - 1);
130 cargv[0] = strdup(exec_cmd);
133 for (
int i = 1; i < argc; i++) {
134 if (strcmp(argv[i],
"-i") == 0) {
135 cargv[c++] = strdup(
"-i");
136 }
else if (strcmp(argv[i],
"-D1") == 0 && ssh_login) {
137 fprintf(stderr,
"SSH authentication succeeded\n");
139 cargv[c++] = strdup(argv[i]);
147 ABEND(EXIT_FAILURE
, "fork() fatal error");
150 if (argv[0] && strstr(argv[0],
"rsh")) {
151 if (setuid(0) || setgid(0))
152 ABEND(EXIT_FAILURE
, "setuid(0) fatal error");
154 getrlimit(RLIMIT_FSIZE, &rl);
155 rl.rlim_cur = RLIM_INFINITY;
156 rl.rlim_max = RLIM_INFINITY;
157 setrlimit(RLIMIT_FSIZE, &rl);
159 execvp(exec_cmd, cargv);
160 ABEND(EXIT_FAILURE
, "execvp() fatal error");
163 waitpid(pid, &status, 0);
164 for (
int i = 0; i < c; i++)
167 if (WIFEXITED(status)) {
168 rc = WEXITSTATUS(status);
170 ABEND(rc,
"Child process exited");
172 if (WIFSIGNALED(status)) {
173 rc = WTERMSIG(status);
174 ABEND(rc,
"Child process terminated by signal");
176 ABEND(EXIT_FAILURE,
"Child process terminated abnormally");
185 fprintf(stderr,
"%s: %d %s\n", s, e, strerror(e));
void ABEND(int)
The ABEND function is a signal handler that is called when the program receives certain signals (e....
int main(int argc, char **argv)