Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/script/script.c
Context not available. | |||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <libutil.h> | #include <libutil.h> | ||||
#include <paths.h> | #include <paths.h> | ||||
#include <pwd.h> | |||||
#include <signal.h> | #include <signal.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
Context not available. | |||||
#include <termios.h> | #include <termios.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <utmpx.h> | |||||
#define DEF_BUF 65536 | #define DEF_BUF 65536 | ||||
struct stamp { | struct stamp { | ||||
Context not available. | |||||
static void consume(FILE *, off_t, char *, int); | static void consume(FILE *, off_t, char *, int); | ||||
static void playback(FILE *) __dead2; | static void playback(FILE *) __dead2; | ||||
static void usage(void); | static void usage(void); | ||||
static char *make_id(char *); | |||||
static char *clean_ttyname(char *); | |||||
static void utmpx_update(); | |||||
int | int | ||||
main(int argc, char *argv[]) | main(int argc, char *argv[]) | ||||
Context not available. | |||||
if (shell == NULL) | if (shell == NULL) | ||||
shell = _PATH_BSHELL; | shell = _PATH_BSHELL; | ||||
utmpx_update(); | |||||
(void)close(master); | (void)close(master); | ||||
(void)fclose(fscript); | (void)fclose(fscript); | ||||
free(fmfname); | free(fmfname); | ||||
Context not available. | |||||
(void)fclose(fp); | (void)fclose(fp); | ||||
exit(0); | exit(0); | ||||
} | } | ||||
static char * | |||||
make_id(char *tty) | |||||
{ | |||||
char *res = tty; | |||||
if (strncmp(res, "pts/", 4) == 0) | |||||
res += 4; | |||||
return (res); | |||||
} | |||||
static char * | |||||
clean_ttyname(char *tty) | |||||
{ | |||||
char *res = tty; | |||||
if (strncmp(res, _PATH_DEV, strlen(_PATH_DEV)) == 0) | |||||
res += strlen(_PATH_DEV); | |||||
return (res); | |||||
} | |||||
static void | |||||
utmpx_update() | |||||
{ | |||||
struct utmpx utmpx; | |||||
struct timeval tv; | |||||
struct passwd *pw; | |||||
char *clean_tty; | |||||
char *user = NULL; | |||||
char *host = NULL; | |||||
char *line; | |||||
char *tmp; | |||||
int pid = getpid(); | |||||
/* | |||||
* Create utmp entry for child | |||||
*/ | |||||
line = ptsname(master); | |||||
clean_tty = clean_ttyname(line); | |||||
memset(&utmpx, 0, sizeof(utmpx)); | |||||
user = getenv("USER"); | |||||
if(user == NULL) | |||||
user = getenv("LOGNAME"); | |||||
if(user == NULL) | |||||
user = getenv("USERNAME"); | |||||
if (user == NULL) { | |||||
if ((pw = getpwnam(user)) && pw->pw_uid != getuid()) { | |||||
if ((pw = getpwuid(getuid()))) | |||||
user = pw->pw_name; | |||||
} | |||||
} | |||||
if (user == NULL) | |||||
strncpy(utmpx.ut_user, "root", sizeof(utmpx.ut_user)); | |||||
else | |||||
strncpy(utmpx.ut_user, user, sizeof(utmpx.ut_user)); | |||||
strncpy(utmpx.ut_line, clean_tty, sizeof(utmpx.ut_line)); | |||||
host = getenv("REMOTEHOST"); | |||||
if (host == NULL) { | |||||
host = getenv("SSH_CLIENT"); | |||||
if (host != NULL) { | |||||
/* get IP */ | |||||
tmp = host; | |||||
while (*tmp != ' ') | |||||
tmp++; | |||||
if (*tmp == ' ') | |||||
*tmp = '\0'; | |||||
} | |||||
} | |||||
if (host == NULL) | |||||
strncpy(utmpx.ut_host, "127.0.0.1", sizeof(utmpx.ut_host)); | |||||
else | |||||
strncpy(utmpx.ut_host, host, sizeof(utmpx.ut_host)); | |||||
strncpy(utmpx.ut_id, make_id(clean_tty), sizeof(utmpx.ut_id)); | |||||
utmpx.ut_pid = pid; | |||||
utmpx.ut_type = USER_PROCESS; | |||||
gettimeofday (&tv, NULL); | |||||
utmpx.ut_tv.tv_sec = tv.tv_sec; | |||||
utmpx.ut_tv.tv_usec = tv.tv_usec; | |||||
if (pututxline(&utmpx) == NULL) | |||||
fprintf(stderr, "pututxline failed"); | |||||
} | |||||
Context not available. |