Index: usr.bin/script/script.c =================================================================== --- usr.bin/script/script.c +++ usr.bin/script/script.c @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -59,6 +60,8 @@ #include #include +#include + #define DEF_BUF 65536 struct stamp { @@ -85,6 +88,9 @@ static void consume(FILE *, off_t, char *, int); static void playback(FILE *) __dead2; static void usage(void); +static char *make_id(char *); +static char *clean_ttyname(char *); +static void utmpx_update(); int main(int argc, char *argv[]) @@ -333,6 +339,8 @@ if (shell == NULL) shell = _PATH_BSHELL; + utmpx_update(); + (void)close(master); (void)fclose(fscript); free(fmfname); @@ -505,3 +513,92 @@ (void)fclose(fp); 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"); +}