session_linux.c (3325B)
1 /* see LICENSE file for copyright and license details */ 2 #ifndef _DEFAULT_SOURCE 3 #define _DEFAULT_SOURCE 4 #endif 5 #include "../config.h" 6 #include "../session.h" 7 #include "../util.h" 8 9 #if defined(__linux__) && !defined(ARUU_FORCE_POSIX) 10 #include <stdio.h> 11 #include <string.h> 12 #include <time.h> 13 #include <unistd.h> 14 #include <utmp.h> 15 16 /* getty already cleared any stale entry for this line before exec-ing 17 * login. ut_name is ut_user and ut_time is ut_tv.tv_sec under musl */ 18 void 19 session_record(const char *user) 20 { 21 struct utmp usr; 22 char *line; 23 FILE *fp; 24 25 memset(&usr, 0, sizeof(usr)); 26 usr.ut_type = USER_PROCESS; 27 usr.ut_pid = getpid(); 28 line = ttyname(STDIN_FILENO); 29 if (line) { 30 if (!strncmp(line, "/dev/", 5)) 31 line += 5; 32 strlcpy(usr.ut_line, line, sizeof(usr.ut_line)); 33 } 34 strlcpy(usr.ut_name, user, sizeof(usr.ut_name)); 35 usr.ut_time = time(NULL); 36 37 /* utmp: a live table, so this overwrites/appends the one entry for 38 * this session */ 39 fp = fopen(UTMP_PATH, "r+"); 40 if (fp) { 41 fseek(fp, 0, SEEK_END); 42 fwrite(&usr, sizeof(usr), 1, fp); 43 fclose(fp); 44 } 45 46 /* wtmp: an ever-growing append-only log of the same record, just 47 * opened differently */ 48 fp = fopen(WTMP_PATH, "a"); 49 if (fp) { 50 fwrite(&usr, sizeof(usr), 1, fp); 51 fclose(fp); 52 } 53 } 54 55 static void 56 entry(struct SessionEntry *e, const struct utmp *u) 57 { 58 memset(e, 0, sizeof(*e)); 59 memcpy(e->user, u->ut_name, MIN(sizeof(e->user) - 1, sizeof(u->ut_name))); 60 memcpy(e->line, u->ut_line, MIN(sizeof(e->line) - 1, sizeof(u->ut_line))); 61 memcpy(e->host, u->ut_host, MIN(sizeof(e->host) - 1, sizeof(u->ut_host))); 62 e->time = u->ut_time; 63 e->login = u->ut_type == USER_PROCESS; 64 } 65 66 int 67 session_each(const char *path, session_fn *fn, void *arg) 68 { 69 struct SessionEntry e; 70 struct utmp u; 71 FILE *fp; 72 int r = 0; 73 74 if (!(fp = fopen(path, "r"))) 75 return -1; 76 while (fread(&u, sizeof(u), 1, fp) == 1) { 77 entry(&e, &u); 78 if (fn(&e, arg)) 79 break; 80 } 81 if (ferror(fp)) 82 r = -1; 83 if (fclose(fp)) 84 r = -1; 85 return r; 86 } 87 88 void 89 session_clear(const char *line) 90 { 91 struct utmp usr; 92 char ut_line_buf[UT_LINESIZE + 1]; 93 FILE *fp; 94 long pos; 95 96 fp = fopen(UTMP_PATH, "r+"); 97 if (!fp) 98 return; 99 do { 100 pos = ftell(fp); 101 if (fread(&usr, sizeof(usr), 1, fp) != 1) 102 break; 103 if (usr.ut_line[0] == '\0') 104 continue; 105 memcpy(ut_line_buf, usr.ut_line, UT_LINESIZE); 106 ut_line_buf[UT_LINESIZE] = '\0'; 107 if (strcmp(ut_line_buf, line) != 0) 108 continue; 109 memset(&usr, 0, sizeof(usr)); 110 fseek(fp, pos, SEEK_SET); 111 if (fwrite(&usr, sizeof(usr), 1, fp) != 1) 112 break; 113 } while (1); 114 if (ferror(fp)) 115 weprintf("%s: I/O error:", UTMP_PATH); 116 fclose(fp); 117 } 118 119 int 120 session_lastlog(FILE *fp, uid_t uid, struct SessionEntry *e) 121 { 122 struct lastlog ll; 123 124 memset(&ll, 0, sizeof(ll)); 125 fseek(fp, uid * sizeof(struct lastlog), 0); 126 fread(&ll, sizeof(struct lastlog), 1, fp); 127 if (ferror(fp)) 128 return -1; 129 memset(e, 0, sizeof(*e)); 130 memcpy(e->line, ll.ll_line, MIN(sizeof(e->line) - 1, sizeof(ll.ll_line))); 131 memcpy(e->host, ll.ll_host, MIN(sizeof(e->host) - 1, sizeof(ll.ll_host))); 132 /* some libcs make ll_time an int32_t with compat32 */ 133 e->time = ll.ll_time; 134 return 0; 135 } 136 #endif