… | |
… | |
40 | #include "ptytty.h" |
40 | #include "ptytty.h" |
41 | |
41 | |
42 | #if UTMP_SUPPORT |
42 | #if UTMP_SUPPORT |
43 | |
43 | |
44 | #include <cstdio> |
44 | #include <cstdio> |
|
|
45 | #include <cstring> |
45 | |
46 | |
46 | #include <sys/types.h> |
47 | #include <sys/types.h> |
47 | #include <sys/stat.h> |
48 | #include <sys/stat.h> |
48 | #include <sys/fcntl.h> |
49 | #include <fcntl.h> |
49 | #include <unistd.h> |
50 | #include <unistd.h> |
50 | #include <time.h> |
51 | #include <time.h> |
|
|
52 | #include <errno.h> |
51 | |
53 | |
52 | /* |
54 | /* |
53 | * BSD style utmp entry |
55 | * BSD style utmp entry |
54 | * ut_line, ut_name, ut_host, ut_time |
56 | * ut_line, ut_name, ut_host, ut_time |
55 | * SYSV style utmp (and utmpx) entry |
57 | * SYSV style utmp (and utmpx) entry |
… | |
… | |
104 | break; |
106 | break; |
105 | } |
107 | } |
106 | else if (errno != EAGAIN && errno != EACCES) |
108 | else if (errno != EAGAIN && errno != EACCES) |
107 | break; |
109 | break; |
108 | if (!gotlock) |
110 | if (!gotlock) |
|
|
111 | { |
109 | { /* give it up */ |
112 | /* give it up */ |
110 | close (fd); |
113 | close (fd); |
111 | return; |
114 | return; |
112 | } |
115 | } |
113 | if (fstat (fd, &sbuf) == 0) |
116 | if (fstat (fd, &sbuf) == 0) |
114 | if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp)) |
117 | if (write (fd, putmp, sizeof (struct utmp)) != sizeof (struct utmp)) |
… | |
… | |
160 | if (stat (fname, &st) != 0) |
163 | if (stat (fname, &st) != 0) |
161 | return; |
164 | return; |
162 | if (S_ISDIR (st.st_mode)) |
165 | if (S_ISDIR (st.st_mode)) |
163 | { |
166 | { |
164 | sprintf (lastlogfile, "%.*s/%.*s", |
167 | sprintf (lastlogfile, "%.*s/%.*s", |
165 | sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2, fname, |
168 | (int)(sizeof (lastlogfile) - sizeof (pwent->pw_name) - 2), fname, |
166 | sizeof (pwent->pw_name), |
169 | (int)sizeof (pwent->pw_name), |
167 | (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown" |
170 | (!pwent->pw_name || pwent->pw_name[0] == '\0') ? "unknown" |
168 | : pwent->pw_name); |
171 | : pwent->pw_name); |
169 | if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0) |
172 | if ((fd = open (lastlogfile, O_WRONLY | O_CREAT, 0644)) >= 0) |
170 | { |
173 | { |
171 | write (fd, &ll, sizeof (ll)); |
174 | write (fd, &ll, sizeof (ll)); |
… | |
… | |
178 | if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)), |
181 | if (lseek (fd, (off_t) ((long)pwent->pw_uid * sizeof (ll)), |
179 | SEEK_SET) != -1) |
182 | SEEK_SET) != -1) |
180 | write (fd, &ll, sizeof (ll)); |
183 | write (fd, &ll, sizeof (ll)); |
181 | close (fd); |
184 | close (fd); |
182 | } |
185 | } |
183 | # endif /* HAVE_STRUCT_LASTLOG */ |
186 | # endif /* HAVE_STRUCT_LASTLOG */ |
184 | } |
187 | } |
185 | #endif /* LASTLOG_SUPPORT */ |
188 | #endif /* LASTLOG_SUPPORT */ |
186 | |
189 | |
187 | /* ------------------------------------------------------------------------- */ |
190 | /* ------------------------------------------------------------------------- */ |
188 | |
191 | |
189 | /* |
192 | /* |
190 | * make and write utmp and wtmp entries |
193 | * make and write utmp and wtmp entries |
… | |
… | |
206 | #ifdef HAVE_STRUCT_UTMPX |
209 | #ifdef HAVE_STRUCT_UTMPX |
207 | struct utmpx *utx = &this->utx; |
210 | struct utmpx *utx = &this->utx; |
208 | #endif |
211 | #endif |
209 | int i; |
212 | int i; |
210 | struct passwd *pwent = getpwuid (getuid ()); |
213 | struct passwd *pwent = getpwuid (getuid ()); |
|
|
214 | const char *name = (pwent && pwent->pw_name) ? pwent->pw_name : "?"; |
211 | |
215 | |
212 | if (!strncmp (pty, "/dev/", 5)) |
216 | if (!strncmp (pty, "/dev/", 5)) |
213 | pty += 5; /* skip /dev/ prefix */ |
217 | pty += 5; /* skip /dev/ prefix */ |
214 | |
218 | |
215 | #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX) |
219 | #if defined(HAVE_UTMP_PID) || defined(HAVE_STRUCT_UTMPX) |
216 | if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3)) |
220 | if (!strncmp (pty, "pty", 3) || !strncmp (pty, "tty", 3)) |
217 | strncpy (ut_id, pty + 3, sizeof (ut_id)); |
221 | strncpy (ut_id, pty + 3, sizeof (ut_id)); |
218 | else if (sscanf (pty, "pts/%d", &i) == 1) |
222 | else if (sscanf (pty, "pts/%d", &i) == 1) |
219 | sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ |
223 | sprintf (ut_id, "vt%02x", (i & 0xff)); /* sysv naming */ |
220 | else if (strncmp (pty, "pty", 3) && strncmp (pty, "tty", 3)) |
224 | else |
221 | { |
225 | { |
222 | ptytty_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty); |
226 | ptytty_warn ("can't parse tty name \"%s\", not adding utmp entry.\n", pty); |
223 | return; |
227 | return; |
224 | } |
228 | } |
225 | #endif |
229 | #endif |
… | |
… | |
229 | # ifdef HAVE_UTMP_PID |
233 | # ifdef HAVE_UTMP_PID |
230 | setutent (); |
234 | setutent (); |
231 | strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); |
235 | strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); |
232 | ut->ut_type = DEAD_PROCESS; |
236 | ut->ut_type = DEAD_PROCESS; |
233 | getutid (ut); /* position to entry in utmp file */ |
237 | getutid (ut); /* position to entry in utmp file */ |
234 | strncpy (this->ut_id, ut_id, sizeof (this->ut_id)); |
|
|
235 | # endif |
238 | # endif |
236 | #endif |
239 | #endif |
237 | |
240 | |
238 | #ifdef HAVE_STRUCT_UTMPX |
241 | #ifdef HAVE_STRUCT_UTMPX |
239 | memset (utx, 0, sizeof (struct utmpx)); |
242 | memset (utx, 0, sizeof (struct utmpx)); |
240 | setutxent (); |
243 | setutxent (); |
241 | strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); |
244 | strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); |
242 | utx->ut_type = DEAD_PROCESS; |
245 | utx->ut_type = DEAD_PROCESS; |
243 | getutxid (utx); /* position to entry in utmp file */ |
246 | getutxid (utx); /* position to entry in utmp file */ |
244 | strncpy (this->ut_id, ut_id, sizeof (this->ut_id)); |
|
|
245 | #endif |
247 | #endif |
246 | |
248 | |
247 | #ifdef HAVE_STRUCT_UTMP |
249 | #ifdef HAVE_STRUCT_UTMP |
248 | strncpy (ut->ut_line, pty, sizeof (ut->ut_line)); |
250 | strncpy (ut->ut_line, pty, sizeof (ut->ut_line)); |
|
|
251 | # ifdef HAVE_UTMP_HOST |
|
|
252 | strncpy (ut->ut_host, hostname, sizeof (ut->ut_host)); |
|
|
253 | # endif |
249 | ut->ut_time = time (NULL); |
254 | ut->ut_time = time (NULL); |
250 | # ifdef HAVE_UTMP_PID |
255 | # ifdef HAVE_UTMP_PID |
251 | strncpy (ut->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", |
256 | strncpy (ut->ut_user, name, sizeof (ut->ut_user)); |
252 | sizeof (ut->ut_user)); |
|
|
253 | strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); |
257 | strncpy (ut->ut_id, ut_id, sizeof (ut->ut_id)); |
254 | ut->ut_time = time (NULL); |
|
|
255 | ut->ut_pid = cmd_pid; |
258 | ut->ut_pid = cmd_pid; |
256 | # ifdef HAVE_UTMP_HOST |
|
|
257 | strncpy (ut->ut_host, hostname, sizeof (ut->ut_host)); |
|
|
258 | # endif |
|
|
259 | ut->ut_type = USER_PROCESS; |
259 | ut->ut_type = USER_PROCESS; |
260 | pututline (ut); |
260 | pututline (ut); |
261 | endutent (); /* close the file */ |
261 | endutent (); /* close the file */ |
262 | utmp_pos = 0; |
262 | utmp_pos = 0; |
263 | # else |
263 | # else |
264 | strncpy (ut->ut_name, (pwent && pwent->pw_name) ? pwent->pw_name : "?", |
|
|
265 | sizeof (ut->ut_name)); |
|
|
266 | # ifdef HAVE_UTMP_HOST |
|
|
267 | strncpy (ut->ut_host, hostname, sizeof (ut->ut_host)); |
264 | strncpy (ut->ut_name, name, sizeof (ut->ut_name)); |
268 | # endif |
|
|
269 | # endif |
265 | # endif |
270 | #endif |
266 | #endif |
271 | |
267 | |
272 | #ifdef HAVE_STRUCT_UTMPX |
268 | #ifdef HAVE_STRUCT_UTMPX |
273 | strncpy (utx->ut_line, pty, sizeof (utx->ut_line)); |
269 | strncpy (utx->ut_line, pty, sizeof (utx->ut_line)); |
274 | strncpy (utx->ut_user, (pwent && pwent->pw_name) ? pwent->pw_name : "?", |
270 | strncpy (utx->ut_user, name, sizeof (utx->ut_user)); |
275 | sizeof (utx->ut_user)); |
|
|
276 | strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); |
271 | strncpy (utx->ut_id, ut_id, sizeof (utx->ut_id)); |
277 | # if HAVE_UTMPX_SESSION |
272 | # if HAVE_UTMPX_SESSION |
278 | utx->ut_session = getsid (0); |
273 | utx->ut_session = getsid (0); |
279 | # endif |
274 | # endif |
280 | utx->ut_tv.tv_sec = time (NULL); |
275 | utx->ut_tv.tv_sec = time (NULL); |