]>
Commit | Line | Data |
---|---|---|
1d7b9b20 | 1 | /* |
2 | * Copyright (c) 2000 Andre Lucas. All rights reserved. | |
564dd50a | 3 | * Portions copyright (c) 1998 Todd C. Miller |
4 | * Portions copyright (c) 1996 Jason Downs | |
5 | * Portions copyright (c) 1996 Theo de Raadt | |
1d7b9b20 | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
1d7b9b20 | 15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
26 | */ | |
27 | ||
b6610e8f | 28 | /* |
29 | * The btmp logging code is derived from login.c from util-linux and is under | |
30 | * the the following license: | |
31 | * | |
32 | * Copyright (c) 1980, 1987, 1988 The Regents of the University of California. | |
33 | * All rights reserved. | |
34 | * | |
35 | * Redistribution and use in source and binary forms are permitted | |
36 | * provided that the above copyright notice and this paragraph are | |
37 | * duplicated in all such forms and that any documentation, | |
38 | * advertising materials, and other materials related to such | |
39 | * distribution and use acknowledge that the software was developed | |
40 | * by the University of California, Berkeley. The name of the | |
41 | * University may not be used to endorse or promote products derived | |
42 | * from this software without specific prior written permission. | |
43 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
44 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
45 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
46 | */ | |
47 | ||
48 | ||
2b87da3b | 49 | /** |
1d7b9b20 | 50 | ** loginrec.c: platform-independent login recording and lastlog retrieval |
51 | **/ | |
52 | ||
564dd50a | 53 | /* |
fa64c868 | 54 | * The new login code explained |
55 | * ============================ | |
56 | * | |
57 | * This code attempts to provide a common interface to login recording | |
58 | * (utmp and friends) and last login time retrieval. | |
59 | * | |
60 | * Its primary means of achieving this is to use 'struct logininfo', a | |
61 | * union of all the useful fields in the various different types of | |
62 | * system login record structures one finds on UNIX variants. | |
63 | * | |
64 | * We depend on autoconf to define which recording methods are to be | |
65 | * used, and which fields are contained in the relevant data structures | |
66 | * on the local system. Many C preprocessor symbols affect which code | |
67 | * gets compiled here. | |
68 | * | |
69 | * The code is designed to make it easy to modify a particular | |
70 | * recording method, without affecting other methods nor requiring so | |
71 | * many nested conditional compilation blocks as were commonplace in | |
72 | * the old code. | |
73 | * | |
74 | * For login recording, we try to use the local system's libraries as | |
75 | * these are clearly most likely to work correctly. For utmp systems | |
76 | * this usually means login() and logout() or setutent() etc., probably | |
77 | * in libutil, along with logwtmp() etc. On these systems, we fall back | |
78 | * to writing the files directly if we have to, though this method | |
79 | * requires very thorough testing so we do not corrupt local auditing | |
80 | * information. These files and their access methods are very system | |
81 | * specific indeed. | |
82 | * | |
83 | * For utmpx systems, the corresponding library functions are | |
84 | * setutxent() etc. To the author's knowledge, all utmpx systems have | |
85 | * these library functions and so no direct write is attempted. If such | |
86 | * a system exists and needs support, direct analogues of the [uw]tmp | |
87 | * code should suffice. | |
88 | * | |
89 | * Retrieving the time of last login ('lastlog') is in some ways even | |
90 | * more problemmatic than login recording. Some systems provide a | |
91 | * simple table of all users which we seek based on uid and retrieve a | |
92 | * relatively standard structure. Others record the same information in | |
93 | * a directory with a separate file, and others don't record the | |
94 | * information separately at all. For systems in the latter category, | |
95 | * we look backwards in the wtmp or wtmpx file for the last login entry | |
96 | * for our user. Naturally this is slower and on busy systems could | |
97 | * incur a significant performance penalty. | |
98 | * | |
99 | * Calling the new code | |
100 | * -------------------- | |
101 | * | |
102 | * In OpenSSH all login recording and retrieval is performed in | |
103 | * login.c. Here you'll find working examples. Also, in the logintest.c | |
104 | * program there are more examples. | |
105 | * | |
106 | * Internal handler calling method | |
107 | * ------------------------------- | |
108 | * | |
109 | * When a call is made to login_login() or login_logout(), both | |
110 | * routines set a struct logininfo flag defining which action (log in, | |
111 | * or log out) is to be taken. They both then call login_write(), which | |
112 | * calls whichever of the many structure-specific handlers autoconf | |
113 | * selects for the local system. | |
114 | * | |
115 | * The handlers themselves handle system data structure specifics. Both | |
116 | * struct utmp and struct utmpx have utility functions (see | |
117 | * construct_utmp*()) to try to make it simpler to add extra systems | |
118 | * that introduce new features to either structure. | |
119 | * | |
120 | * While it may seem terribly wasteful to replicate so much similar | |
121 | * code for each method, experience has shown that maintaining code to | |
122 | * write both struct utmp and utmpx in one function, whilst maintaining | |
123 | * support for all systems whether they have library support or not, is | |
124 | * a difficult and time-consuming task. | |
125 | * | |
126 | * Lastlog support proceeds similarly. Functions login_get_lastlog() | |
127 | * (and its OpenSSH-tuned friend login_get_lastlog_time()) call | |
128 | * getlast_entry(), which tries one of three methods to find the last | |
129 | * login time. It uses local system lastlog support if it can, | |
130 | * otherwise it tries wtmp or wtmpx before giving up and returning 0, | |
131 | * meaning "tilt". | |
132 | * | |
133 | * Maintenance | |
134 | * ----------- | |
135 | * | |
136 | * In many cases it's possible to tweak autoconf to select the correct | |
137 | * methods for a particular platform, either by improving the detection | |
138 | * code (best), or by presetting DISABLE_<method> or CONF_<method>_FILE | |
139 | * symbols for the platform. | |
140 | * | |
141 | * Use logintest to check which symbols are defined before modifying | |
142 | * configure.ac and loginrec.c. (You have to build logintest yourself | |
143 | * with 'make logintest' as it's not built by default.) | |
144 | * | |
145 | * Otherwise, patches to the specific method(s) are very helpful! | |
146 | */ | |
1d7b9b20 | 147 | |
148 | #include "includes.h" | |
149 | ||
81e73e57 | 150 | #include <sys/types.h> |
151 | #include <sys/stat.h> | |
9794d008 | 152 | #include <sys/socket.h> |
153 | ||
154 | #include <netinet/in.h> | |
81e73e57 | 155 | |
fec71b2f | 156 | #include <errno.h> |
22bbb3e6 | 157 | #include <fcntl.h> |
4130b6f1 | 158 | #ifdef HAVE_PATHS_H |
159 | # include <paths.h> | |
160 | #endif | |
b1842393 | 161 | #include <pwd.h> |
24436b92 | 162 | #include <stdarg.h> |
28cb0a43 | 163 | #include <string.h> |
164 | #include <unistd.h> | |
b1842393 | 165 | |
1d7b9b20 | 166 | #include "xmalloc.h" |
31652869 | 167 | #include "key.h" |
168 | #include "hostfile.h" | |
169 | #include "ssh.h" | |
1d7b9b20 | 170 | #include "loginrec.h" |
42f11eb2 | 171 | #include "log.h" |
172 | #include "atomicio.h" | |
b6610e8f | 173 | #include "packet.h" |
174 | #include "canohost.h" | |
c00e4d75 | 175 | #include "auth.h" |
c53d8c93 | 176 | #include "buffer.h" |
1d7b9b20 | 177 | |
44d5f7f7 | 178 | #ifdef HAVE_UTIL_H |
fa64c868 | 179 | # include <util.h> |
44d5f7f7 | 180 | #endif |
181 | ||
8f523d67 | 182 | #ifdef HAVE_LIBUTIL_H |
fa64c868 | 183 | # include <libutil.h> |
8f523d67 | 184 | #endif |
185 | ||
1d7b9b20 | 186 | /** |
187 | ** prototypes for helper functions in this file | |
188 | **/ | |
189 | ||
190 | #if HAVE_UTMP_H | |
1d7b9b20 | 191 | void set_utmp_time(struct logininfo *li, struct utmp *ut); |
192 | void construct_utmp(struct logininfo *li, struct utmp *ut); | |
193 | #endif | |
194 | ||
195 | #ifdef HAVE_UTMPX_H | |
1d7b9b20 | 196 | void set_utmpx_time(struct logininfo *li, struct utmpx *ut); |
197 | void construct_utmpx(struct logininfo *li, struct utmpx *ut); | |
198 | #endif | |
199 | ||
200 | int utmp_write_entry(struct logininfo *li); | |
201 | int utmpx_write_entry(struct logininfo *li); | |
202 | int wtmp_write_entry(struct logininfo *li); | |
203 | int wtmpx_write_entry(struct logininfo *li); | |
204 | int lastlog_write_entry(struct logininfo *li); | |
205 | int syslogin_write_entry(struct logininfo *li); | |
206 | ||
207 | int getlast_entry(struct logininfo *li); | |
208 | int lastlog_get_entry(struct logininfo *li); | |
209 | int wtmp_get_entry(struct logininfo *li); | |
210 | int wtmpx_get_entry(struct logininfo *li); | |
211 | ||
5ccf88cb | 212 | extern Buffer loginmsg; |
213 | ||
5abcdf8e | 214 | /* pick the shortest string */ |
fa64c868 | 215 | #define MIN_SIZEOF(s1,s2) (sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2)) |
5abcdf8e | 216 | |
1d7b9b20 | 217 | /** |
218 | ** platform-independent login functions | |
219 | **/ | |
220 | ||
fa64c868 | 221 | /* |
222 | * login_login(struct logininfo *) - Record a login | |
2b87da3b | 223 | * |
5abcdf8e | 224 | * Call with a pointer to a struct logininfo initialised with |
225 | * login_init_entry() or login_alloc_entry() | |
226 | * | |
227 | * Returns: | |
228 | * >0 if successful | |
229 | * 0 on failure (will use OpenSSH's logging facilities for diagnostics) | |
230 | */ | |
564dd50a | 231 | int |
fa64c868 | 232 | login_login(struct logininfo *li) |
564dd50a | 233 | { |
234 | li->type = LTYPE_LOGIN; | |
fa64c868 | 235 | return (login_write(li)); |
564dd50a | 236 | } |
1d7b9b20 | 237 | |
238 | ||
fa64c868 | 239 | /* |
240 | * login_logout(struct logininfo *) - Record a logout | |
5abcdf8e | 241 | * |
242 | * Call as with login_login() | |
243 | * | |
244 | * Returns: | |
245 | * >0 if successful | |
246 | * 0 on failure (will use OpenSSH's logging facilities for diagnostics) | |
247 | */ | |
564dd50a | 248 | int |
249 | login_logout(struct logininfo *li) | |
250 | { | |
251 | li->type = LTYPE_LOGOUT; | |
fa64c868 | 252 | return (login_write(li)); |
1d7b9b20 | 253 | } |
254 | ||
fa64c868 | 255 | /* |
256 | * login_get_lastlog_time(int) - Retrieve the last login time | |
5abcdf8e | 257 | * |
258 | * Retrieve the last login time for the given uid. Will try to use the | |
259 | * system lastlog facilities if they are available, but will fall back | |
260 | * to looking in wtmp/wtmpx if necessary | |
261 | * | |
262 | * Returns: | |
263 | * 0 on failure, or if user has never logged in | |
264 | * Time in seconds from the epoch if successful | |
265 | * | |
266 | * Useful preprocessor symbols: | |
267 | * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog | |
268 | * info | |
269 | * USE_LASTLOG: If set, indicates the presence of system lastlog | |
270 | * facilities. If this and DISABLE_LASTLOG are not set, | |
271 | * try to retrieve lastlog information from wtmp/wtmpx. | |
272 | */ | |
564dd50a | 273 | unsigned int |
274 | login_get_lastlog_time(const int uid) | |
275 | { | |
276 | struct logininfo li; | |
1d7b9b20 | 277 | |
5abcdf8e | 278 | if (login_get_lastlog(&li, uid)) |
fa64c868 | 279 | return (li.tv_sec); |
5abcdf8e | 280 | else |
fa64c868 | 281 | return (0); |
564dd50a | 282 | } |
1d7b9b20 | 283 | |
fa64c868 | 284 | /* |
285 | * login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry | |
5abcdf8e | 286 | * |
287 | * Retrieve a logininfo structure populated (only partially) with | |
288 | * information from the system lastlog data, or from wtmp/wtmpx if no | |
289 | * system lastlog information exists. | |
290 | * | |
291 | * Note this routine must be given a pre-allocated logininfo. | |
292 | * | |
293 | * Returns: | |
294 | * >0: A pointer to your struct logininfo if successful | |
295 | * 0 on failure (will use OpenSSH's logging facilities for diagnostics) | |
5abcdf8e | 296 | */ |
564dd50a | 297 | struct logininfo * |
298 | login_get_lastlog(struct logininfo *li, const int uid) | |
299 | { | |
5abcdf8e | 300 | struct passwd *pw; |
5abcdf8e | 301 | |
dbaa2e87 | 302 | memset(li, '\0', sizeof(*li)); |
1d7b9b20 | 303 | li->uid = uid; |
5abcdf8e | 304 | |
2b87da3b | 305 | /* |
9f32ceb4 | 306 | * If we don't have a 'real' lastlog, we need the username to |
5abcdf8e | 307 | * reliably search wtmp(x) for the last login (see |
2b87da3b | 308 | * wtmp_get_entry().) |
9f32ceb4 | 309 | */ |
5abcdf8e | 310 | pw = getpwuid(uid); |
a05a70ab | 311 | if (pw == NULL) |
a233586b | 312 | fatal("%s: Cannot find account for uid %i", __func__, uid); |
2b87da3b | 313 | |
1bfbb762 | 314 | /* No MIN_SIZEOF here - we absolutely *must not* truncate the |
fa64c868 | 315 | * username (XXX - so check for trunc!) */ |
d8caae24 | 316 | strlcpy(li->username, pw->pw_name, sizeof(li->username)); |
a05a70ab | 317 | |
564dd50a | 318 | if (getlast_entry(li)) |
fa64c868 | 319 | return (li); |
564dd50a | 320 | else |
fa64c868 | 321 | return (NULL); |
1d7b9b20 | 322 | } |
323 | ||
1d7b9b20 | 324 | |
fa64c868 | 325 | /* |
326 | * login_alloc_entry(int, char*, char*, char*) - Allocate and initialise | |
2b87da3b | 327 | * a logininfo structure |
328 | * | |
5abcdf8e | 329 | * This function creates a new struct logininfo, a data structure |
330 | * meant to carry the information required to portably record login info. | |
331 | * | |
332 | * Returns a pointer to a newly created struct logininfo. If memory | |
333 | * allocation fails, the program halts. | |
334 | */ | |
564dd50a | 335 | struct |
336 | logininfo *login_alloc_entry(int pid, const char *username, | |
fa64c868 | 337 | const char *hostname, const char *line) |
564dd50a | 338 | { |
339 | struct logininfo *newli; | |
1d7b9b20 | 340 | |
fa64c868 | 341 | newli = xmalloc(sizeof(*newli)); |
342 | login_init_entry(newli, pid, username, hostname, line); | |
343 | return (newli); | |
1d7b9b20 | 344 | } |
345 | ||
346 | ||
5abcdf8e | 347 | /* login_free_entry(struct logininfo *) - free struct memory */ |
1d7b9b20 | 348 | void |
564dd50a | 349 | login_free_entry(struct logininfo *li) |
350 | { | |
351 | xfree(li); | |
1d7b9b20 | 352 | } |
353 | ||
354 | ||
5abcdf8e | 355 | /* login_init_entry(struct logininfo *, int, char*, char*, char*) |
356 | * - initialise a struct logininfo | |
2b87da3b | 357 | * |
5abcdf8e | 358 | * Populates a new struct logininfo, a data structure meant to carry |
359 | * the information required to portably record login info. | |
360 | * | |
361 | * Returns: 1 | |
362 | */ | |
564dd50a | 363 | int |
2b87da3b | 364 | login_init_entry(struct logininfo *li, int pid, const char *username, |
fa64c868 | 365 | const char *hostname, const char *line) |
564dd50a | 366 | { |
d8caae24 | 367 | struct passwd *pw; |
2b87da3b | 368 | |
dbaa2e87 | 369 | memset(li, 0, sizeof(*li)); |
2b87da3b | 370 | |
564dd50a | 371 | li->pid = pid; |
d8caae24 | 372 | |
564dd50a | 373 | /* set the line information */ |
374 | if (line) | |
375 | line_fullname(li->line, line, sizeof(li->line)); | |
1d7b9b20 | 376 | |
d8caae24 | 377 | if (username) { |
564dd50a | 378 | strlcpy(li->username, username, sizeof(li->username)); |
d8caae24 | 379 | pw = getpwnam(li->username); |
fa64c868 | 380 | if (pw == NULL) { |
d1cf9a87 | 381 | fatal("%s: Cannot find user \"%s\"", __func__, |
fa64c868 | 382 | li->username); |
383 | } | |
d8caae24 | 384 | li->uid = pw->pw_uid; |
385 | } | |
a05a70ab | 386 | |
564dd50a | 387 | if (hostname) |
388 | strlcpy(li->hostname, hostname, sizeof(li->hostname)); | |
d8caae24 | 389 | |
fa64c868 | 390 | return (1); |
1d7b9b20 | 391 | } |
392 | ||
d1cf9a87 | 393 | /* |
fa64c868 | 394 | * login_set_current_time(struct logininfo *) - set the current time |
5abcdf8e | 395 | * |
396 | * Set the current time in a logininfo structure. This function is | |
397 | * meant to eliminate the need to deal with system dependencies for | |
398 | * time handling. | |
399 | */ | |
1d7b9b20 | 400 | void |
564dd50a | 401 | login_set_current_time(struct logininfo *li) |
402 | { | |
1d7b9b20 | 403 | struct timeval tv; |
404 | ||
405 | gettimeofday(&tv, NULL); | |
2b87da3b | 406 | |
d8caae24 | 407 | li->tv_sec = tv.tv_sec; |
408 | li->tv_usec = tv.tv_usec; | |
1d7b9b20 | 409 | } |
410 | ||
564dd50a | 411 | /* copy a sockaddr_* into our logininfo */ |
1d7b9b20 | 412 | void |
564dd50a | 413 | login_set_addr(struct logininfo *li, const struct sockaddr *sa, |
fa64c868 | 414 | const unsigned int sa_size) |
564dd50a | 415 | { |
416 | unsigned int bufsize = sa_size; | |
417 | ||
418 | /* make sure we don't overrun our union */ | |
419 | if (sizeof(li->hostaddr) < sa_size) | |
420 | bufsize = sizeof(li->hostaddr); | |
421 | ||
fa64c868 | 422 | memcpy(&li->hostaddr.sa, sa, bufsize); |
1d7b9b20 | 423 | } |
1d7b9b20 | 424 | |
564dd50a | 425 | |
426 | /** | |
427 | ** login_write: Call low-level recording functions based on autoconf | |
428 | ** results | |
429 | **/ | |
1d7b9b20 | 430 | int |
fa64c868 | 431 | login_write(struct logininfo *li) |
564dd50a | 432 | { |
3c62e7eb | 433 | #ifndef HAVE_CYGWIN |
fa64c868 | 434 | if (geteuid() != 0) { |
435 | logit("Attempt to write login records by non-root user (aborting)"); | |
436 | return (1); | |
1d7b9b20 | 437 | } |
3c62e7eb | 438 | #endif |
a05a70ab | 439 | |
1d7b9b20 | 440 | /* set the timestamp */ |
441 | login_set_current_time(li); | |
442 | #ifdef USE_LOGIN | |
443 | syslogin_write_entry(li); | |
444 | #endif | |
445 | #ifdef USE_LASTLOG | |
fa64c868 | 446 | if (li->type == LTYPE_LOGIN) |
1d7b9b20 | 447 | lastlog_write_entry(li); |
1d7b9b20 | 448 | #endif |
449 | #ifdef USE_UTMP | |
450 | utmp_write_entry(li); | |
451 | #endif | |
452 | #ifdef USE_WTMP | |
453 | wtmp_write_entry(li); | |
454 | #endif | |
455 | #ifdef USE_UTMPX | |
456 | utmpx_write_entry(li); | |
457 | #endif | |
458 | #ifdef USE_WTMPX | |
459 | wtmpx_write_entry(li); | |
f5ed3301 | 460 | #endif |
461 | #ifdef CUSTOM_SYS_AUTH_RECORD_LOGIN | |
d1cf9a87 | 462 | if (li->type == LTYPE_LOGIN && |
98c044d0 | 463 | !sys_auth_record_login(li->username,li->hostname,li->line, |
464 | &loginmsg)) | |
f5ed3301 | 465 | logit("Writing login record failed for %s", li->username); |
c00e4d75 | 466 | #endif |
6039eeef | 467 | #ifdef SSH_AUDIT_EVENTS |
c00e4d75 | 468 | if (li->type == LTYPE_LOGIN) |
469 | audit_session_open(li->line); | |
470 | else if (li->type == LTYPE_LOGOUT) | |
471 | audit_session_close(li->line); | |
1d7b9b20 | 472 | #endif |
fa64c868 | 473 | return (0); |
1d7b9b20 | 474 | } |
475 | ||
7e2d5fa4 | 476 | #ifdef LOGIN_NEEDS_UTMPX |
477 | int | |
478 | login_utmp_only(struct logininfo *li) | |
479 | { | |
aff51935 | 480 | li->type = LTYPE_LOGIN; |
9e127e27 | 481 | login_set_current_time(li); |
7e2d5fa4 | 482 | # ifdef USE_UTMP |
483 | utmp_write_entry(li); | |
484 | # endif | |
485 | # ifdef USE_WTMP | |
486 | wtmp_write_entry(li); | |
487 | # endif | |
488 | # ifdef USE_UTMPX | |
489 | utmpx_write_entry(li); | |
490 | # endif | |
491 | # ifdef USE_WTMPX | |
492 | wtmpx_write_entry(li); | |
493 | # endif | |
fa64c868 | 494 | return (0); |
7e2d5fa4 | 495 | } |
496 | #endif | |
497 | ||
564dd50a | 498 | /** |
499 | ** getlast_entry: Call low-level functions to retrieve the last login | |
500 | ** time. | |
501 | **/ | |
1d7b9b20 | 502 | |
564dd50a | 503 | /* take the uid in li and return the last login time */ |
1d7b9b20 | 504 | int |
564dd50a | 505 | getlast_entry(struct logininfo *li) |
506 | { | |
507 | #ifdef USE_LASTLOG | |
9f32ceb4 | 508 | return(lastlog_get_entry(li)); |
a05a70ab | 509 | #else /* !USE_LASTLOG */ |
1d7b9b20 | 510 | |
fa64c868 | 511 | #if defined(DISABLE_LASTLOG) |
2b87da3b | 512 | /* On some systems we shouldn't even try to obtain last login |
3f45f1c3 | 513 | * time, e.g. AIX */ |
fa64c868 | 514 | return (0); |
515 | # elif defined(USE_WTMP) && \ | |
516 | (defined(HAVE_TIME_IN_UTMP) || defined(HAVE_TV_IN_UTMP)) | |
564dd50a | 517 | /* retrieve last login time from utmp */ |
a05a70ab | 518 | return (wtmp_get_entry(li)); |
fa64c868 | 519 | # elif defined(USE_WTMPX) && \ |
520 | (defined(HAVE_TIME_IN_UTMPX) || defined(HAVE_TV_IN_UTMPX)) | |
564dd50a | 521 | /* If wtmp isn't available, try wtmpx */ |
a05a70ab | 522 | return (wtmpx_get_entry(li)); |
fa64c868 | 523 | # else |
564dd50a | 524 | /* Give up: No means of retrieving last login time */ |
fa64c868 | 525 | return (0); |
2b87da3b | 526 | # endif /* DISABLE_LASTLOG */ |
a05a70ab | 527 | #endif /* USE_LASTLOG */ |
564dd50a | 528 | } |
1d7b9b20 | 529 | |
530 | ||
1d7b9b20 | 531 | |
532 | /* | |
564dd50a | 533 | * 'line' string utility functions |
534 | * | |
535 | * These functions process the 'line' string into one of three forms: | |
536 | * | |
1d7b9b20 | 537 | * 1. The full filename (including '/dev') |
538 | * 2. The stripped name (excluding '/dev') | |
564dd50a | 539 | * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00 |
540 | * /dev/pts/1 -> ts/1 ) | |
1d7b9b20 | 541 | * |
542 | * Form 3 is used on some systems to identify a .tmp.? entry when | |
543 | * attempting to remove it. Typically both addition and removal is | |
564dd50a | 544 | * performed by one application - say, sshd - so as long as the choice |
545 | * uniquely identifies a terminal it's ok. | |
1d7b9b20 | 546 | */ |
547 | ||
548 | ||
fa64c868 | 549 | /* |
550 | * line_fullname(): add the leading '/dev/' if it doesn't exist make | |
551 | * sure dst has enough space, if not just copy src (ugh) | |
552 | */ | |
1d7b9b20 | 553 | char * |
44d71ad5 | 554 | line_fullname(char *dst, const char *src, u_int dstsize) |
564dd50a | 555 | { |
1d7b9b20 | 556 | memset(dst, '\0', dstsize); |
fa64c868 | 557 | if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5))) |
1d7b9b20 | 558 | strlcpy(dst, src, dstsize); |
fa64c868 | 559 | else { |
a4d05724 | 560 | strlcpy(dst, "/dev/", dstsize); |
1d7b9b20 | 561 | strlcat(dst, src, dstsize); |
562 | } | |
fa64c868 | 563 | return (dst); |
1d7b9b20 | 564 | } |
565 | ||
564dd50a | 566 | /* line_stripname(): strip the leading '/dev' if it exists, return dst */ |
1d7b9b20 | 567 | char * |
564dd50a | 568 | line_stripname(char *dst, const char *src, int dstsize) |
569 | { | |
1d7b9b20 | 570 | memset(dst, '\0', dstsize); |
571 | if (strncmp(src, "/dev/", 5) == 0) | |
89d7510a | 572 | strlcpy(dst, src + 5, dstsize); |
1d7b9b20 | 573 | else |
574 | strlcpy(dst, src, dstsize); | |
fa64c868 | 575 | return (dst); |
564dd50a | 576 | } |
577 | ||
d1cf9a87 | 578 | /* |
fa64c868 | 579 | * line_abbrevname(): Return the abbreviated (usually four-character) |
564dd50a | 580 | * form of the line (Just use the last <dstsize> characters of the |
581 | * full name.) | |
582 | * | |
583 | * NOTE: use strncpy because we do NOT necessarily want zero | |
fa64c868 | 584 | * termination |
585 | */ | |
1d7b9b20 | 586 | char * |
2b87da3b | 587 | line_abbrevname(char *dst, const char *src, int dstsize) |
a05a70ab | 588 | { |
589 | size_t len; | |
2b87da3b | 590 | |
1d7b9b20 | 591 | memset(dst, '\0', dstsize); |
2b87da3b | 592 | |
daaff4d5 | 593 | /* Always skip prefix if present */ |
594 | if (strncmp(src, "/dev/", 5) == 0) | |
595 | src += 5; | |
2b87da3b | 596 | |
0e8f4eba | 597 | #ifdef WITH_ABBREV_NO_TTY |
598 | if (strncmp(src, "tty", 3) == 0) | |
599 | src += 3; | |
600 | #endif | |
601 | ||
a05a70ab | 602 | len = strlen(src); |
603 | ||
daaff4d5 | 604 | if (len > 0) { |
605 | if (((int)len - dstsize) > 0) | |
606 | src += ((int)len - dstsize); | |
607 | ||
608 | /* note: _don't_ change this to strlcpy */ | |
2b87da3b | 609 | strncpy(dst, src, (size_t)dstsize); |
a05a70ab | 610 | } |
2b87da3b | 611 | |
fa64c868 | 612 | return (dst); |
1d7b9b20 | 613 | } |
614 | ||
1d7b9b20 | 615 | /** |
616 | ** utmp utility functions | |
564dd50a | 617 | ** |
618 | ** These functions manipulate struct utmp, taking system differences | |
619 | ** into account. | |
1d7b9b20 | 620 | **/ |
621 | ||
622 | #if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN) | |
623 | ||
1d7b9b20 | 624 | /* build the utmp structure */ |
625 | void | |
564dd50a | 626 | set_utmp_time(struct logininfo *li, struct utmp *ut) |
627 | { | |
fa64c868 | 628 | # if defined(HAVE_TV_IN_UTMP) |
1d7b9b20 | 629 | ut->ut_tv.tv_sec = li->tv_sec; |
630 | ut->ut_tv.tv_usec = li->tv_usec; | |
fa64c868 | 631 | # elif defined(HAVE_TIME_IN_UTMP) |
1d7b9b20 | 632 | ut->ut_time = li->tv_sec; |
a05a70ab | 633 | # endif |
1d7b9b20 | 634 | } |
635 | ||
636 | void | |
637 | construct_utmp(struct logininfo *li, | |
564dd50a | 638 | struct utmp *ut) |
639 | { | |
9746ee4b | 640 | # ifdef HAVE_ADDR_V6_IN_UTMP |
641 | struct sockaddr_in6 *sa6; | |
fa64c868 | 642 | # endif |
643 | ||
dbaa2e87 | 644 | memset(ut, '\0', sizeof(*ut)); |
5abcdf8e | 645 | |
646 | /* First fill out fields used for both logins and logouts */ | |
647 | ||
a05a70ab | 648 | # ifdef HAVE_ID_IN_UTMP |
1d7b9b20 | 649 | line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id)); |
a05a70ab | 650 | # endif |
1d7b9b20 | 651 | |
a05a70ab | 652 | # ifdef HAVE_TYPE_IN_UTMP |
5abcdf8e | 653 | /* This is done here to keep utmp constants out of struct logininfo */ |
1d7b9b20 | 654 | switch (li->type) { |
655 | case LTYPE_LOGIN: | |
656 | ut->ut_type = USER_PROCESS; | |
ef51930f | 657 | #ifdef _UNICOS |
1a23ac2c | 658 | cray_set_tmpdir(ut); |
659 | #endif | |
1d7b9b20 | 660 | break; |
661 | case LTYPE_LOGOUT: | |
662 | ut->ut_type = DEAD_PROCESS; | |
ef51930f | 663 | #ifdef _UNICOS |
1a23ac2c | 664 | cray_retain_utmp(ut, li->pid); |
665 | #endif | |
1d7b9b20 | 666 | break; |
667 | } | |
a05a70ab | 668 | # endif |
5abcdf8e | 669 | set_utmp_time(li, ut); |
1d7b9b20 | 670 | |
5abcdf8e | 671 | line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line)); |
a05a70ab | 672 | |
673 | # ifdef HAVE_PID_IN_UTMP | |
1d7b9b20 | 674 | ut->ut_pid = li->pid; |
a05a70ab | 675 | # endif |
5abcdf8e | 676 | |
677 | /* If we're logging out, leave all other fields blank */ | |
678 | if (li->type == LTYPE_LOGOUT) | |
fa64c868 | 679 | return; |
5abcdf8e | 680 | |
a05a70ab | 681 | /* |
682 | * These fields are only used when logging in, and are blank | |
2b87da3b | 683 | * for logouts. |
a05a70ab | 684 | */ |
5abcdf8e | 685 | |
686 | /* Use strncpy because we don't necessarily want null termination */ | |
fa64c868 | 687 | strncpy(ut->ut_name, li->username, |
688 | MIN_SIZEOF(ut->ut_name, li->username)); | |
a05a70ab | 689 | # ifdef HAVE_HOST_IN_UTMP |
fa64c868 | 690 | strncpy(ut->ut_host, li->hostname, |
691 | MIN_SIZEOF(ut->ut_host, li->hostname)); | |
a05a70ab | 692 | # endif |
693 | # ifdef HAVE_ADDR_IN_UTMP | |
564dd50a | 694 | /* this is just a 32-bit IP address */ |
695 | if (li->hostaddr.sa.sa_family == AF_INET) | |
696 | ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr; | |
2b87da3b | 697 | # endif |
9746ee4b | 698 | # ifdef HAVE_ADDR_V6_IN_UTMP |
699 | /* this is just a 128-bit IPv6 address */ | |
700 | if (li->hostaddr.sa.sa_family == AF_INET6) { | |
701 | sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa); | |
702 | memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16); | |
703 | if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) { | |
704 | ut->ut_addr_v6[0] = ut->ut_addr_v6[3]; | |
705 | ut->ut_addr_v6[1] = 0; | |
706 | ut->ut_addr_v6[2] = 0; | |
707 | ut->ut_addr_v6[3] = 0; | |
708 | } | |
709 | } | |
710 | # endif | |
564dd50a | 711 | } |
a05a70ab | 712 | #endif /* USE_UTMP || USE_WTMP || USE_LOGIN */ |
564dd50a | 713 | |
1d7b9b20 | 714 | /** |
715 | ** utmpx utility functions | |
564dd50a | 716 | ** |
717 | ** These functions manipulate struct utmpx, accounting for system | |
718 | ** variations. | |
1d7b9b20 | 719 | **/ |
720 | ||
721 | #if defined(USE_UTMPX) || defined (USE_WTMPX) | |
1d7b9b20 | 722 | /* build the utmpx structure */ |
723 | void | |
564dd50a | 724 | set_utmpx_time(struct logininfo *li, struct utmpx *utx) |
725 | { | |
fa64c868 | 726 | # if defined(HAVE_TV_IN_UTMPX) |
1d7b9b20 | 727 | utx->ut_tv.tv_sec = li->tv_sec; |
728 | utx->ut_tv.tv_usec = li->tv_usec; | |
fa64c868 | 729 | # elif defined(HAVE_TIME_IN_UTMPX) |
1d7b9b20 | 730 | utx->ut_time = li->tv_sec; |
fa64c868 | 731 | # endif |
1d7b9b20 | 732 | } |
733 | ||
734 | void | |
564dd50a | 735 | construct_utmpx(struct logininfo *li, struct utmpx *utx) |
736 | { | |
9746ee4b | 737 | # ifdef HAVE_ADDR_V6_IN_UTMP |
738 | struct sockaddr_in6 *sa6; | |
739 | # endif | |
dbaa2e87 | 740 | memset(utx, '\0', sizeof(*utx)); |
fa64c868 | 741 | |
daaff4d5 | 742 | # ifdef HAVE_ID_IN_UTMPX |
1d7b9b20 | 743 | line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id)); |
daaff4d5 | 744 | # endif |
1d7b9b20 | 745 | |
746 | /* this is done here to keep utmp constants out of loginrec.h */ | |
747 | switch (li->type) { | |
748 | case LTYPE_LOGIN: | |
749 | utx->ut_type = USER_PROCESS; | |
750 | break; | |
751 | case LTYPE_LOGOUT: | |
752 | utx->ut_type = DEAD_PROCESS; | |
753 | break; | |
754 | } | |
1d7b9b20 | 755 | line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line)); |
1d7b9b20 | 756 | set_utmpx_time(li, utx); |
5abcdf8e | 757 | utx->ut_pid = li->pid; |
fa64c868 | 758 | |
f3837bc6 | 759 | /* strncpy(): Don't necessarily want null termination */ |
fa64c868 | 760 | strncpy(utx->ut_name, li->username, |
761 | MIN_SIZEOF(utx->ut_name, li->username)); | |
5abcdf8e | 762 | |
763 | if (li->type == LTYPE_LOGOUT) | |
764 | return; | |
765 | ||
a05a70ab | 766 | /* |
767 | * These fields are only used when logging in, and are blank | |
2b87da3b | 768 | * for logouts. |
a05a70ab | 769 | */ |
5abcdf8e | 770 | |
a05a70ab | 771 | # ifdef HAVE_HOST_IN_UTMPX |
fa64c868 | 772 | strncpy(utx->ut_host, li->hostname, |
773 | MIN_SIZEOF(utx->ut_host, li->hostname)); | |
a05a70ab | 774 | # endif |
775 | # ifdef HAVE_ADDR_IN_UTMPX | |
764d4113 | 776 | /* this is just a 32-bit IP address */ |
777 | if (li->hostaddr.sa.sa_family == AF_INET) | |
778 | utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr; | |
a05a70ab | 779 | # endif |
9746ee4b | 780 | # ifdef HAVE_ADDR_V6_IN_UTMP |
781 | /* this is just a 128-bit IPv6 address */ | |
782 | if (li->hostaddr.sa.sa_family == AF_INET6) { | |
783 | sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa); | |
784 | memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16); | |
785 | if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) { | |
786 | ut->ut_addr_v6[0] = ut->ut_addr_v6[3]; | |
787 | ut->ut_addr_v6[1] = 0; | |
788 | ut->ut_addr_v6[2] = 0; | |
789 | ut->ut_addr_v6[3] = 0; | |
790 | } | |
791 | } | |
792 | # endif | |
a05a70ab | 793 | # ifdef HAVE_SYSLEN_IN_UTMPX |
5abcdf8e | 794 | /* ut_syslen is the length of the utx_host string */ |
795 | utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host)); | |
a05a70ab | 796 | # endif |
564dd50a | 797 | } |
a05a70ab | 798 | #endif /* USE_UTMPX || USE_WTMPX */ |
1d7b9b20 | 799 | |
800 | /** | |
564dd50a | 801 | ** Low-level utmp functions |
1d7b9b20 | 802 | **/ |
803 | ||
804 | /* FIXME: (ATL) utmp_write_direct needs testing */ | |
1d7b9b20 | 805 | #ifdef USE_UTMP |
806 | ||
1d7b9b20 | 807 | /* if we can, use pututline() etc. */ |
a05a70ab | 808 | # if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \ |
809 | defined(HAVE_PUTUTLINE) | |
1d7b9b20 | 810 | # define UTMP_USE_LIBRARY |
a05a70ab | 811 | # endif |
1d7b9b20 | 812 | |
813 | ||
814 | /* write a utmp entry with the system's help (pututline() and pals) */ | |
a05a70ab | 815 | # ifdef UTMP_USE_LIBRARY |
1d7b9b20 | 816 | static int |
564dd50a | 817 | utmp_write_library(struct logininfo *li, struct utmp *ut) |
818 | { | |
1d7b9b20 | 819 | setutent(); |
820 | pututline(ut); | |
a05a70ab | 821 | # ifdef HAVE_ENDUTENT |
1d7b9b20 | 822 | endutent(); |
a05a70ab | 823 | # endif |
fa64c868 | 824 | return (1); |
564dd50a | 825 | } |
a05a70ab | 826 | # else /* UTMP_USE_LIBRARY */ |
1d7b9b20 | 827 | |
d1cf9a87 | 828 | /* |
fa64c868 | 829 | * Write a utmp entry direct to the file |
830 | * This is a slightly modification of code in OpenBSD's login.c | |
831 | */ | |
1d7b9b20 | 832 | static int |
564dd50a | 833 | utmp_write_direct(struct logininfo *li, struct utmp *ut) |
834 | { | |
1d7b9b20 | 835 | struct utmp old_ut; |
836 | register int fd; | |
837 | int tty; | |
838 | ||
5abcdf8e | 839 | /* FIXME: (ATL) ttyslot() needs local implementation */ |
dbaa2e87 | 840 | |
698d107e | 841 | #if defined(HAVE_GETTTYENT) |
fa64c868 | 842 | struct ttyent *ty; |
dbaa2e87 | 843 | |
844 | tty=0; | |
dbaa2e87 | 845 | setttyent(); |
fa64c868 | 846 | while (NULL != (ty = getttyent())) { |
dbaa2e87 | 847 | tty++; |
848 | if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line))) | |
849 | break; | |
850 | } | |
851 | endttyent(); | |
852 | ||
fa64c868 | 853 | if (NULL == ty) { |
5f12e050 | 854 | logit("%s: tty not found", __func__); |
855 | return (0); | |
dbaa2e87 | 856 | } |
857 | #else /* FIXME */ | |
858 | ||
1d7b9b20 | 859 | tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */ |
860 | ||
698d107e | 861 | #endif /* HAVE_GETTTYENT */ |
dbaa2e87 | 862 | |
1d7b9b20 | 863 | if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) { |
5f12e050 | 864 | off_t pos, ret; |
865 | ||
866 | pos = (off_t)tty * sizeof(struct utmp); | |
867 | if ((ret = lseek(fd, pos, SEEK_SET)) == -1) { | |
f6d20d59 | 868 | logit("%s: lseek: %s", __func__, strerror(errno)); |
5f12e050 | 869 | return (0); |
870 | } | |
871 | if (ret != pos) { | |
d1cf9a87 | 872 | logit("%s: Couldn't seek to tty %d slot in %s", |
f6d20d59 | 873 | __func__, tty, UTMP_FILE); |
5f12e050 | 874 | return (0); |
875 | } | |
1d7b9b20 | 876 | /* |
877 | * Prevent luser from zero'ing out ut_host. | |
878 | * If the new ut_line is empty but the old one is not | |
dc2a6d09 | 879 | * and ut_line and ut_name match, preserve the old ut_line. |
1d7b9b20 | 880 | */ |
2b87da3b | 881 | if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) && |
fa64c868 | 882 | (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') && |
883 | (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) && | |
884 | (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0)) | |
885 | memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host)); | |
2b87da3b | 886 | |
5f12e050 | 887 | if ((ret = lseek(fd, pos, SEEK_SET)) == -1) { |
f6d20d59 | 888 | logit("%s: lseek: %s", __func__, strerror(errno)); |
5f12e050 | 889 | return (0); |
890 | } | |
891 | if (ret != pos) { | |
f6d20d59 | 892 | logit("%s: Couldn't seek to tty %d slot in %s", |
5f12e050 | 893 | __func__, tty, UTMP_FILE); |
894 | return (0); | |
895 | } | |
fa64c868 | 896 | if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) { |
5f12e050 | 897 | logit("%s: error writing %s: %s", __func__, |
5abcdf8e | 898 | UTMP_FILE, strerror(errno)); |
fa64c868 | 899 | } |
2b87da3b | 900 | |
fa64c868 | 901 | close(fd); |
902 | return (1); | |
9f32ceb4 | 903 | } else { |
fa64c868 | 904 | return (0); |
9f32ceb4 | 905 | } |
564dd50a | 906 | } |
a05a70ab | 907 | # endif /* UTMP_USE_LIBRARY */ |
1d7b9b20 | 908 | |
909 | static int | |
564dd50a | 910 | utmp_perform_login(struct logininfo *li) |
911 | { | |
1d7b9b20 | 912 | struct utmp ut; |
913 | ||
914 | construct_utmp(li, &ut); | |
a05a70ab | 915 | # ifdef UTMP_USE_LIBRARY |
1d7b9b20 | 916 | if (!utmp_write_library(li, &ut)) { |
a233586b | 917 | logit("%s: utmp_write_library() failed", __func__); |
fa64c868 | 918 | return (0); |
1d7b9b20 | 919 | } |
a05a70ab | 920 | # else |
1d7b9b20 | 921 | if (!utmp_write_direct(li, &ut)) { |
a233586b | 922 | logit("%s: utmp_write_direct() failed", __func__); |
fa64c868 | 923 | return (0); |
1d7b9b20 | 924 | } |
a05a70ab | 925 | # endif |
fa64c868 | 926 | return (1); |
564dd50a | 927 | } |
1d7b9b20 | 928 | |
929 | ||
930 | static int | |
564dd50a | 931 | utmp_perform_logout(struct logininfo *li) |
932 | { | |
1d7b9b20 | 933 | struct utmp ut; |
934 | ||
5abcdf8e | 935 | construct_utmp(li, &ut); |
a05a70ab | 936 | # ifdef UTMP_USE_LIBRARY |
5abcdf8e | 937 | if (!utmp_write_library(li, &ut)) { |
a233586b | 938 | logit("%s: utmp_write_library() failed", __func__); |
fa64c868 | 939 | return (0); |
5abcdf8e | 940 | } |
a05a70ab | 941 | # else |
5abcdf8e | 942 | if (!utmp_write_direct(li, &ut)) { |
a233586b | 943 | logit("%s: utmp_write_direct() failed", __func__); |
fa64c868 | 944 | return (0); |
5abcdf8e | 945 | } |
a05a70ab | 946 | # endif |
fa64c868 | 947 | return (1); |
564dd50a | 948 | } |
1d7b9b20 | 949 | |
950 | ||
951 | int | |
564dd50a | 952 | utmp_write_entry(struct logininfo *li) |
953 | { | |
1d7b9b20 | 954 | switch(li->type) { |
955 | case LTYPE_LOGIN: | |
fa64c868 | 956 | return (utmp_perform_login(li)); |
1d7b9b20 | 957 | |
958 | case LTYPE_LOGOUT: | |
fa64c868 | 959 | return (utmp_perform_logout(li)); |
1d7b9b20 | 960 | |
961 | default: | |
a233586b | 962 | logit("%s: invalid type field", __func__); |
fa64c868 | 963 | return (0); |
1d7b9b20 | 964 | } |
564dd50a | 965 | } |
a05a70ab | 966 | #endif /* USE_UTMP */ |
1d7b9b20 | 967 | |
968 | ||
969 | /** | |
564dd50a | 970 | ** Low-level utmpx functions |
1d7b9b20 | 971 | **/ |
972 | ||
973 | /* not much point if we don't want utmpx entries */ | |
974 | #ifdef USE_UTMPX | |
975 | ||
1d7b9b20 | 976 | /* if we have the wherewithall, use pututxline etc. */ |
a05a70ab | 977 | # if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \ |
978 | defined(HAVE_PUTUTXLINE) | |
1d7b9b20 | 979 | # define UTMPX_USE_LIBRARY |
a05a70ab | 980 | # endif |
1d7b9b20 | 981 | |
982 | ||
983 | /* write a utmpx entry with the system's help (pututxline() and pals) */ | |
a05a70ab | 984 | # ifdef UTMPX_USE_LIBRARY |
1d7b9b20 | 985 | static int |
564dd50a | 986 | utmpx_write_library(struct logininfo *li, struct utmpx *utx) |
987 | { | |
1d7b9b20 | 988 | setutxent(); |
989 | pututxline(utx); | |
990 | ||
a05a70ab | 991 | # ifdef HAVE_ENDUTXENT |
1d7b9b20 | 992 | endutxent(); |
a05a70ab | 993 | # endif |
fa64c868 | 994 | return (1); |
564dd50a | 995 | } |
1d7b9b20 | 996 | |
a05a70ab | 997 | # else /* UTMPX_USE_LIBRARY */ |
1d7b9b20 | 998 | |
999 | /* write a utmp entry direct to the file */ | |
1000 | static int | |
564dd50a | 1001 | utmpx_write_direct(struct logininfo *li, struct utmpx *utx) |
2b87da3b | 1002 | { |
a233586b | 1003 | logit("%s: not implemented!", __func__); |
fa64c868 | 1004 | return (0); |
564dd50a | 1005 | } |
a05a70ab | 1006 | # endif /* UTMPX_USE_LIBRARY */ |
1d7b9b20 | 1007 | |
1008 | static int | |
564dd50a | 1009 | utmpx_perform_login(struct logininfo *li) |
1010 | { | |
1d7b9b20 | 1011 | struct utmpx utx; |
1012 | ||
1013 | construct_utmpx(li, &utx); | |
a05a70ab | 1014 | # ifdef UTMPX_USE_LIBRARY |
1d7b9b20 | 1015 | if (!utmpx_write_library(li, &utx)) { |
a233586b | 1016 | logit("%s: utmp_write_library() failed", __func__); |
fa64c868 | 1017 | return (0); |
1d7b9b20 | 1018 | } |
a05a70ab | 1019 | # else |
1d7b9b20 | 1020 | if (!utmpx_write_direct(li, &ut)) { |
a233586b | 1021 | logit("%s: utmp_write_direct() failed", __func__); |
fa64c868 | 1022 | return (0); |
1d7b9b20 | 1023 | } |
a05a70ab | 1024 | # endif |
fa64c868 | 1025 | return (1); |
564dd50a | 1026 | } |
1d7b9b20 | 1027 | |
1028 | ||
1029 | static int | |
564dd50a | 1030 | utmpx_perform_logout(struct logininfo *li) |
1031 | { | |
1d7b9b20 | 1032 | struct utmpx utx; |
1033 | ||
f3837bc6 | 1034 | construct_utmpx(li, &utx); |
a05a70ab | 1035 | # ifdef HAVE_ID_IN_UTMPX |
1d7b9b20 | 1036 | line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id)); |
a05a70ab | 1037 | # endif |
1038 | # ifdef HAVE_TYPE_IN_UTMPX | |
1d7b9b20 | 1039 | utx.ut_type = DEAD_PROCESS; |
a05a70ab | 1040 | # endif |
1d7b9b20 | 1041 | |
a05a70ab | 1042 | # ifdef UTMPX_USE_LIBRARY |
1d7b9b20 | 1043 | utmpx_write_library(li, &utx); |
a05a70ab | 1044 | # else |
1d7b9b20 | 1045 | utmpx_write_direct(li, &utx); |
a05a70ab | 1046 | # endif |
fa64c868 | 1047 | return (1); |
564dd50a | 1048 | } |
1d7b9b20 | 1049 | |
1d7b9b20 | 1050 | int |
564dd50a | 1051 | utmpx_write_entry(struct logininfo *li) |
1052 | { | |
1d7b9b20 | 1053 | switch(li->type) { |
1054 | case LTYPE_LOGIN: | |
fa64c868 | 1055 | return (utmpx_perform_login(li)); |
1d7b9b20 | 1056 | case LTYPE_LOGOUT: |
fa64c868 | 1057 | return (utmpx_perform_logout(li)); |
1d7b9b20 | 1058 | default: |
a233586b | 1059 | logit("%s: invalid type field", __func__); |
fa64c868 | 1060 | return (0); |
1d7b9b20 | 1061 | } |
564dd50a | 1062 | } |
a05a70ab | 1063 | #endif /* USE_UTMPX */ |
1d7b9b20 | 1064 | |
1065 | ||
1066 | /** | |
564dd50a | 1067 | ** Low-level wtmp functions |
1d7b9b20 | 1068 | **/ |
1069 | ||
2b87da3b | 1070 | #ifdef USE_WTMP |
1d7b9b20 | 1071 | |
d1cf9a87 | 1072 | /* |
fa64c868 | 1073 | * Write a wtmp entry direct to the end of the file |
1074 | * This is a slight modification of code in OpenBSD's logwtmp.c | |
1075 | */ | |
1d7b9b20 | 1076 | static int |
564dd50a | 1077 | wtmp_write(struct logininfo *li, struct utmp *ut) |
1078 | { | |
1d7b9b20 | 1079 | struct stat buf; |
1080 | int fd, ret = 1; | |
1081 | ||
1082 | if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) { | |
a233586b | 1083 | logit("%s: problem writing %s: %s", __func__, |
1d7b9b20 | 1084 | WTMP_FILE, strerror(errno)); |
fa64c868 | 1085 | return (0); |
1d7b9b20 | 1086 | } |
2b87da3b | 1087 | if (fstat(fd, &buf) == 0) |
d72f7b79 | 1088 | if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) { |
1d7b9b20 | 1089 | ftruncate(fd, buf.st_size); |
a233586b | 1090 | logit("%s: problem writing %s: %s", __func__, |
1d7b9b20 | 1091 | WTMP_FILE, strerror(errno)); |
1092 | ret = 0; | |
1093 | } | |
fa64c868 | 1094 | close(fd); |
1095 | return (ret); | |
564dd50a | 1096 | } |
1d7b9b20 | 1097 | |
1d7b9b20 | 1098 | static int |
a05a70ab | 1099 | wtmp_perform_login(struct logininfo *li) |
1100 | { | |
1d7b9b20 | 1101 | struct utmp ut; |
1102 | ||
1103 | construct_utmp(li, &ut); | |
fa64c868 | 1104 | return (wtmp_write(li, &ut)); |
564dd50a | 1105 | } |
1d7b9b20 | 1106 | |
1107 | ||
1108 | static int | |
564dd50a | 1109 | wtmp_perform_logout(struct logininfo *li) |
1110 | { | |
1d7b9b20 | 1111 | struct utmp ut; |
1112 | ||
1113 | construct_utmp(li, &ut); | |
fa64c868 | 1114 | return (wtmp_write(li, &ut)); |
564dd50a | 1115 | } |
1d7b9b20 | 1116 | |
1117 | ||
1118 | int | |
564dd50a | 1119 | wtmp_write_entry(struct logininfo *li) |
1120 | { | |
1d7b9b20 | 1121 | switch(li->type) { |
1122 | case LTYPE_LOGIN: | |
fa64c868 | 1123 | return (wtmp_perform_login(li)); |
1d7b9b20 | 1124 | case LTYPE_LOGOUT: |
fa64c868 | 1125 | return (wtmp_perform_logout(li)); |
1d7b9b20 | 1126 | default: |
a233586b | 1127 | logit("%s: invalid type field", __func__); |
fa64c868 | 1128 | return (0); |
1d7b9b20 | 1129 | } |
564dd50a | 1130 | } |
1d7b9b20 | 1131 | |
1132 | ||
d1cf9a87 | 1133 | /* |
fa64c868 | 1134 | * Notes on fetching login data from wtmp/wtmpx |
2b87da3b | 1135 | * |
5abcdf8e | 1136 | * Logouts are usually recorded with (amongst other things) a blank |
1137 | * username on a given tty line. However, some systems (HP-UX is one) | |
1138 | * leave all fields set, but change the ut_type field to DEAD_PROCESS. | |
1139 | * | |
1140 | * Since we're only looking for logins here, we know that the username | |
1141 | * must be set correctly. On systems that leave it in, we check for | |
1142 | * ut_type==USER_PROCESS (indicating a login.) | |
1143 | * | |
1144 | * Portability: Some systems may set something other than USER_PROCESS | |
1145 | * to indicate a login process. I don't know of any as I write. Also, | |
1146 | * it's possible that some systems may both leave the username in | |
1147 | * place and not have ut_type. | |
1148 | */ | |
1149 | ||
5abcdf8e | 1150 | /* return true if this wtmp entry indicates a login */ |
1151 | static int | |
1152 | wtmp_islogin(struct logininfo *li, struct utmp *ut) | |
1153 | { | |
2b87da3b | 1154 | if (strncmp(li->username, ut->ut_name, |
fa64c868 | 1155 | MIN_SIZEOF(li->username, ut->ut_name)) == 0) { |
a05a70ab | 1156 | # ifdef HAVE_TYPE_IN_UTMP |
5abcdf8e | 1157 | if (ut->ut_type & USER_PROCESS) |
fa64c868 | 1158 | return (1); |
a05a70ab | 1159 | # else |
fa64c868 | 1160 | return (1); |
a05a70ab | 1161 | # endif |
5abcdf8e | 1162 | } |
fa64c868 | 1163 | return (0); |
5abcdf8e | 1164 | } |
1165 | ||
1d7b9b20 | 1166 | int |
564dd50a | 1167 | wtmp_get_entry(struct logininfo *li) |
1168 | { | |
1d7b9b20 | 1169 | struct stat st; |
1170 | struct utmp ut; | |
fa64c868 | 1171 | int fd, found = 0; |
5abcdf8e | 1172 | |
1173 | /* Clear the time entries in our logininfo */ | |
1174 | li->tv_sec = li->tv_usec = 0; | |
1d7b9b20 | 1175 | |
1176 | if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) { | |
d1cf9a87 | 1177 | logit("%s: problem opening %s: %s", __func__, |
1d7b9b20 | 1178 | WTMP_FILE, strerror(errno)); |
fa64c868 | 1179 | return (0); |
1d7b9b20 | 1180 | } |
2b87da3b | 1181 | if (fstat(fd, &st) != 0) { |
d1cf9a87 | 1182 | logit("%s: couldn't stat %s: %s", __func__, |
1d7b9b20 | 1183 | WTMP_FILE, strerror(errno)); |
1184 | close(fd); | |
fa64c868 | 1185 | return (0); |
1d7b9b20 | 1186 | } |
1d7b9b20 | 1187 | |
5abcdf8e | 1188 | /* Seek to the start of the last struct utmp */ |
d9d47a26 | 1189 | if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) { |
5abcdf8e | 1190 | /* Looks like we've got a fresh wtmp file */ |
1191 | close(fd); | |
fa64c868 | 1192 | return (0); |
5abcdf8e | 1193 | } |
1194 | ||
1195 | while (!found) { | |
9f32ceb4 | 1196 | if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) { |
d1cf9a87 | 1197 | logit("%s: read of %s failed: %s", __func__, |
1d7b9b20 | 1198 | WTMP_FILE, strerror(errno)); |
1199 | close (fd); | |
fa64c868 | 1200 | return (0); |
1d7b9b20 | 1201 | } |
5abcdf8e | 1202 | if ( wtmp_islogin(li, &ut) ) { |
1203 | found = 1; | |
fa64c868 | 1204 | /* |
1205 | * We've already checked for a time in struct | |
1206 | * utmp, in login_getlast() | |
1207 | */ | |
a05a70ab | 1208 | # ifdef HAVE_TIME_IN_UTMP |
1d7b9b20 | 1209 | li->tv_sec = ut.ut_time; |
a05a70ab | 1210 | # else |
1d7b9b20 | 1211 | # if HAVE_TV_IN_UTMP |
1212 | li->tv_sec = ut.ut_tv.tv_sec; | |
1213 | # endif | |
a05a70ab | 1214 | # endif |
5abcdf8e | 1215 | line_fullname(li->line, ut.ut_line, |
fa64c868 | 1216 | MIN_SIZEOF(li->line, ut.ut_line)); |
a05a70ab | 1217 | # ifdef HAVE_HOST_IN_UTMP |
5abcdf8e | 1218 | strlcpy(li->hostname, ut.ut_host, |
fa64c868 | 1219 | MIN_SIZEOF(li->hostname, ut.ut_host)); |
a05a70ab | 1220 | # endif |
5abcdf8e | 1221 | continue; |
1d7b9b20 | 1222 | } |
5abcdf8e | 1223 | /* Seek back 2 x struct utmp */ |
d9d47a26 | 1224 | if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) { |
5abcdf8e | 1225 | /* We've found the start of the file, so quit */ |
fa64c868 | 1226 | close(fd); |
1227 | return (0); | |
1d7b9b20 | 1228 | } |
5abcdf8e | 1229 | } |
1230 | ||
1231 | /* We found an entry. Tidy up and return */ | |
1232 | close(fd); | |
fa64c868 | 1233 | return (1); |
564dd50a | 1234 | } |
a05a70ab | 1235 | # endif /* USE_WTMP */ |
1d7b9b20 | 1236 | |
1237 | ||
1238 | /** | |
564dd50a | 1239 | ** Low-level wtmpx functions |
1d7b9b20 | 1240 | **/ |
1241 | ||
1242 | #ifdef USE_WTMPX | |
fa64c868 | 1243 | /* |
1244 | * Write a wtmpx entry direct to the end of the file | |
1245 | * This is a slight modification of code in OpenBSD's logwtmp.c | |
1246 | */ | |
1d7b9b20 | 1247 | static int |
564dd50a | 1248 | wtmpx_write(struct logininfo *li, struct utmpx *utx) |
1249 | { | |
41c64c91 | 1250 | #ifndef HAVE_UPDWTMPX |
1d7b9b20 | 1251 | struct stat buf; |
1252 | int fd, ret = 1; | |
1253 | ||
1254 | if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) { | |
d1cf9a87 | 1255 | logit("%s: problem opening %s: %s", __func__, |
1d7b9b20 | 1256 | WTMPX_FILE, strerror(errno)); |
fa64c868 | 1257 | return (0); |
1d7b9b20 | 1258 | } |
1259 | ||
2b87da3b | 1260 | if (fstat(fd, &buf) == 0) |
d72f7b79 | 1261 | if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) { |
1d7b9b20 | 1262 | ftruncate(fd, buf.st_size); |
a233586b | 1263 | logit("%s: problem writing %s: %s", __func__, |
1d7b9b20 | 1264 | WTMPX_FILE, strerror(errno)); |
1265 | ret = 0; | |
1266 | } | |
fa64c868 | 1267 | close(fd); |
1d7b9b20 | 1268 | |
fa64c868 | 1269 | return (ret); |
41c64c91 | 1270 | #else |
1271 | updwtmpx(WTMPX_FILE, utx); | |
fa64c868 | 1272 | return (1); |
41c64c91 | 1273 | #endif |
564dd50a | 1274 | } |
1d7b9b20 | 1275 | |
1276 | ||
1277 | static int | |
564dd50a | 1278 | wtmpx_perform_login(struct logininfo *li) |
1279 | { | |
1d7b9b20 | 1280 | struct utmpx utx; |
1281 | ||
1282 | construct_utmpx(li, &utx); | |
fa64c868 | 1283 | return (wtmpx_write(li, &utx)); |
564dd50a | 1284 | } |
1d7b9b20 | 1285 | |
1286 | ||
1287 | static int | |
564dd50a | 1288 | wtmpx_perform_logout(struct logininfo *li) |
1289 | { | |
1d7b9b20 | 1290 | struct utmpx utx; |
1291 | ||
1292 | construct_utmpx(li, &utx); | |
fa64c868 | 1293 | return (wtmpx_write(li, &utx)); |
564dd50a | 1294 | } |
1d7b9b20 | 1295 | |
1296 | ||
1297 | int | |
564dd50a | 1298 | wtmpx_write_entry(struct logininfo *li) |
1299 | { | |
1d7b9b20 | 1300 | switch(li->type) { |
1301 | case LTYPE_LOGIN: | |
fa64c868 | 1302 | return (wtmpx_perform_login(li)); |
1d7b9b20 | 1303 | case LTYPE_LOGOUT: |
fa64c868 | 1304 | return (wtmpx_perform_logout(li)); |
1d7b9b20 | 1305 | default: |
a233586b | 1306 | logit("%s: invalid type field", __func__); |
fa64c868 | 1307 | return (0); |
1d7b9b20 | 1308 | } |
564dd50a | 1309 | } |
1d7b9b20 | 1310 | |
5abcdf8e | 1311 | /* Please see the notes above wtmp_islogin() for information about the |
1312 | next two functions */ | |
1313 | ||
1314 | /* Return true if this wtmpx entry indicates a login */ | |
1315 | static int | |
1316 | wtmpx_islogin(struct logininfo *li, struct utmpx *utx) | |
1317 | { | |
fa64c868 | 1318 | if (strncmp(li->username, utx->ut_name, |
1319 | MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) { | |
a05a70ab | 1320 | # ifdef HAVE_TYPE_IN_UTMPX |
5abcdf8e | 1321 | if (utx->ut_type == USER_PROCESS) |
fa64c868 | 1322 | return (1); |
a05a70ab | 1323 | # else |
fa64c868 | 1324 | return (1); |
a05a70ab | 1325 | # endif |
5abcdf8e | 1326 | } |
fa64c868 | 1327 | return (0); |
5abcdf8e | 1328 | } |
1329 | ||
1d7b9b20 | 1330 | |
1331 | int | |
564dd50a | 1332 | wtmpx_get_entry(struct logininfo *li) |
1333 | { | |
1d7b9b20 | 1334 | struct stat st; |
1335 | struct utmpx utx; | |
5abcdf8e | 1336 | int fd, found=0; |
1337 | ||
1338 | /* Clear the time entries */ | |
1339 | li->tv_sec = li->tv_usec = 0; | |
1d7b9b20 | 1340 | |
1341 | if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) { | |
d1cf9a87 | 1342 | logit("%s: problem opening %s: %s", __func__, |
1d7b9b20 | 1343 | WTMPX_FILE, strerror(errno)); |
fa64c868 | 1344 | return (0); |
1d7b9b20 | 1345 | } |
2b87da3b | 1346 | if (fstat(fd, &st) != 0) { |
d1cf9a87 | 1347 | logit("%s: couldn't stat %s: %s", __func__, |
fdebdd4f | 1348 | WTMPX_FILE, strerror(errno)); |
1d7b9b20 | 1349 | close(fd); |
fa64c868 | 1350 | return (0); |
1d7b9b20 | 1351 | } |
2b87da3b | 1352 | |
5abcdf8e | 1353 | /* Seek to the start of the last struct utmpx */ |
d9d47a26 | 1354 | if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) { |
5abcdf8e | 1355 | /* probably a newly rotated wtmpx file */ |
1356 | close(fd); | |
fa64c868 | 1357 | return (0); |
5abcdf8e | 1358 | } |
1d7b9b20 | 1359 | |
5abcdf8e | 1360 | while (!found) { |
9f32ceb4 | 1361 | if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) { |
d1cf9a87 | 1362 | logit("%s: read of %s failed: %s", __func__, |
1d7b9b20 | 1363 | WTMPX_FILE, strerror(errno)); |
1364 | close (fd); | |
fa64c868 | 1365 | return (0); |
1d7b9b20 | 1366 | } |
fa64c868 | 1367 | /* |
d1cf9a87 | 1368 | * Logouts are recorded as a blank username on a particular |
fa64c868 | 1369 | * line. So, we just need to find the username in struct utmpx |
1370 | */ | |
1371 | if (wtmpx_islogin(li, &utx)) { | |
d80063fe | 1372 | found = 1; |
fa64c868 | 1373 | # if defined(HAVE_TV_IN_UTMPX) |
1d7b9b20 | 1374 | li->tv_sec = utx.ut_tv.tv_sec; |
fa64c868 | 1375 | # elif defined(HAVE_TIME_IN_UTMPX) |
1d7b9b20 | 1376 | li->tv_sec = utx.ut_time; |
a05a70ab | 1377 | # endif |
a4d05724 | 1378 | line_fullname(li->line, utx.ut_line, sizeof(li->line)); |
fa64c868 | 1379 | # if defined(HAVE_HOST_IN_UTMPX) |
5abcdf8e | 1380 | strlcpy(li->hostname, utx.ut_host, |
fa64c868 | 1381 | MIN_SIZEOF(li->hostname, utx.ut_host)); |
a05a70ab | 1382 | # endif |
5abcdf8e | 1383 | continue; |
1d7b9b20 | 1384 | } |
d9d47a26 | 1385 | if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) { |
fa64c868 | 1386 | close(fd); |
1387 | return (0); | |
1d7b9b20 | 1388 | } |
5abcdf8e | 1389 | } |
1390 | ||
1391 | close(fd); | |
fa64c868 | 1392 | return (1); |
564dd50a | 1393 | } |
f988dce5 | 1394 | #endif /* USE_WTMPX */ |
1d7b9b20 | 1395 | |
1d7b9b20 | 1396 | /** |
564dd50a | 1397 | ** Low-level libutil login() functions |
1d7b9b20 | 1398 | **/ |
1399 | ||
1400 | #ifdef USE_LOGIN | |
1d7b9b20 | 1401 | static int |
564dd50a | 1402 | syslogin_perform_login(struct logininfo *li) |
1403 | { | |
1d7b9b20 | 1404 | struct utmp *ut; |
1405 | ||
7a52470e | 1406 | ut = xmalloc(sizeof(*ut)); |
1d7b9b20 | 1407 | construct_utmp(li, ut); |
1408 | login(ut); | |
6cef88bc | 1409 | free(ut); |
1d7b9b20 | 1410 | |
fa64c868 | 1411 | return (1); |
564dd50a | 1412 | } |
1d7b9b20 | 1413 | |
564dd50a | 1414 | static int |
1415 | syslogin_perform_logout(struct logininfo *li) | |
1416 | { | |
a05a70ab | 1417 | # ifdef HAVE_LOGOUT |
f20d4564 | 1418 | char line[UT_LINESIZE]; |
2b87da3b | 1419 | |
1d7b9b20 | 1420 | (void)line_stripname(line, li->line, sizeof(line)); |
1421 | ||
fa64c868 | 1422 | if (!logout(line)) |
a233586b | 1423 | logit("%s: logout() returned an error", __func__); |
a05a70ab | 1424 | # ifdef HAVE_LOGWTMP |
fa64c868 | 1425 | else |
1d7b9b20 | 1426 | logwtmp(line, "", ""); |
a05a70ab | 1427 | # endif |
5abcdf8e | 1428 | /* FIXME: (ATL - if the need arises) What to do if we have |
1429 | * login, but no logout? what if logout but no logwtmp? All | |
1430 | * routines are in libutil so they should all be there, | |
1431 | * but... */ | |
a05a70ab | 1432 | # endif |
fa64c868 | 1433 | return (1); |
564dd50a | 1434 | } |
1d7b9b20 | 1435 | |
1d7b9b20 | 1436 | int |
564dd50a | 1437 | syslogin_write_entry(struct logininfo *li) |
1438 | { | |
1d7b9b20 | 1439 | switch (li->type) { |
1440 | case LTYPE_LOGIN: | |
fa64c868 | 1441 | return (syslogin_perform_login(li)); |
1d7b9b20 | 1442 | case LTYPE_LOGOUT: |
fa64c868 | 1443 | return (syslogin_perform_logout(li)); |
1d7b9b20 | 1444 | default: |
a233586b | 1445 | logit("%s: Invalid type field", __func__); |
fa64c868 | 1446 | return (0); |
1d7b9b20 | 1447 | } |
564dd50a | 1448 | } |
f988dce5 | 1449 | #endif /* USE_LOGIN */ |
1d7b9b20 | 1450 | |
1451 | /* end of file log-syslogin.c */ | |
1452 | ||
1d7b9b20 | 1453 | /** |
564dd50a | 1454 | ** Low-level lastlog functions |
1d7b9b20 | 1455 | **/ |
1456 | ||
1457 | #ifdef USE_LASTLOG | |
a05a70ab | 1458 | #define LL_FILE 1 |
1459 | #define LL_DIR 2 | |
1460 | #define LL_OTHER 3 | |
1d7b9b20 | 1461 | |
1d7b9b20 | 1462 | static void |
564dd50a | 1463 | lastlog_construct(struct logininfo *li, struct lastlog *last) |
1464 | { | |
1d7b9b20 | 1465 | /* clear the structure */ |
dbaa2e87 | 1466 | memset(last, '\0', sizeof(*last)); |
2b87da3b | 1467 | |
fa64c868 | 1468 | line_stripname(last->ll_line, li->line, sizeof(last->ll_line)); |
5abcdf8e | 1469 | strlcpy(last->ll_host, li->hostname, |
1470 | MIN_SIZEOF(last->ll_host, li->hostname)); | |
1d7b9b20 | 1471 | last->ll_time = li->tv_sec; |
564dd50a | 1472 | } |
1d7b9b20 | 1473 | |
1d7b9b20 | 1474 | static int |
564dd50a | 1475 | lastlog_filetype(char *filename) |
1476 | { | |
1d7b9b20 | 1477 | struct stat st; |
1478 | ||
a05a70ab | 1479 | if (stat(LASTLOG_FILE, &st) != 0) { |
a233586b | 1480 | logit("%s: Couldn't stat %s: %s", __func__, |
fa64c868 | 1481 | LASTLOG_FILE, strerror(errno)); |
1482 | return (0); | |
1d7b9b20 | 1483 | } |
1d7b9b20 | 1484 | if (S_ISDIR(st.st_mode)) |
fa64c868 | 1485 | return (LL_DIR); |
1d7b9b20 | 1486 | else if (S_ISREG(st.st_mode)) |
fa64c868 | 1487 | return (LL_FILE); |
1d7b9b20 | 1488 | else |
fa64c868 | 1489 | return (LL_OTHER); |
564dd50a | 1490 | } |
1d7b9b20 | 1491 | |
1492 | ||
1493 | /* open the file (using filemode) and seek to the login entry */ | |
1494 | static int | |
564dd50a | 1495 | lastlog_openseek(struct logininfo *li, int *fd, int filemode) |
1496 | { | |
1d7b9b20 | 1497 | off_t offset; |
1498 | int type; | |
1499 | char lastlog_file[1024]; | |
1500 | ||
1501 | type = lastlog_filetype(LASTLOG_FILE); | |
1502 | switch (type) { | |
fa64c868 | 1503 | case LL_FILE: |
1504 | strlcpy(lastlog_file, LASTLOG_FILE, | |
1505 | sizeof(lastlog_file)); | |
1506 | break; | |
1507 | case LL_DIR: | |
1508 | snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s", | |
1509 | LASTLOG_FILE, li->username); | |
1510 | break; | |
1511 | default: | |
a233586b | 1512 | logit("%s: %.100s is not a file or directory!", __func__, |
fa64c868 | 1513 | LASTLOG_FILE); |
1514 | return (0); | |
a05a70ab | 1515 | } |
1d7b9b20 | 1516 | |
ee476051 | 1517 | *fd = open(lastlog_file, filemode, 0600); |
fa64c868 | 1518 | if (*fd < 0) { |
a233586b | 1519 | debug("%s: Couldn't open %s: %s", __func__, |
1d7b9b20 | 1520 | lastlog_file, strerror(errno)); |
fa64c868 | 1521 | return (0); |
1d7b9b20 | 1522 | } |
2b87da3b | 1523 | |
d93a7e5a | 1524 | if (type == LL_FILE) { |
1525 | /* find this uid's offset in the lastlog file */ | |
d9d47a26 | 1526 | offset = (off_t) ((long)li->uid * sizeof(struct lastlog)); |
1d7b9b20 | 1527 | |
fa64c868 | 1528 | if (lseek(*fd, offset, SEEK_SET) != offset) { |
a233586b | 1529 | logit("%s: %s->lseek(): %s", __func__, |
1530 | lastlog_file, strerror(errno)); | |
fa64c868 | 1531 | return (0); |
d93a7e5a | 1532 | } |
1d7b9b20 | 1533 | } |
2b87da3b | 1534 | |
fa64c868 | 1535 | return (1); |
564dd50a | 1536 | } |
1d7b9b20 | 1537 | |
1538 | static int | |
564dd50a | 1539 | lastlog_perform_login(struct logininfo *li) |
1540 | { | |
1d7b9b20 | 1541 | struct lastlog last; |
1542 | int fd; | |
1543 | ||
1544 | /* create our struct lastlog */ | |
1545 | lastlog_construct(li, &last); | |
1546 | ||
ee476051 | 1547 | if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT)) |
fa64c868 | 1548 | return (0); |
2b87da3b | 1549 | |
1d7b9b20 | 1550 | /* write the entry */ |
d72f7b79 | 1551 | if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) { |
1a022229 | 1552 | close(fd); |
a233586b | 1553 | logit("%s: Error writing to %s: %s", __func__, |
1a022229 | 1554 | LASTLOG_FILE, strerror(errno)); |
fa64c868 | 1555 | return (0); |
a05a70ab | 1556 | } |
1a022229 | 1557 | |
1558 | close(fd); | |
fa64c868 | 1559 | return (1); |
564dd50a | 1560 | } |
1d7b9b20 | 1561 | |
1d7b9b20 | 1562 | int |
564dd50a | 1563 | lastlog_write_entry(struct logininfo *li) |
1564 | { | |
1d7b9b20 | 1565 | switch(li->type) { |
1566 | case LTYPE_LOGIN: | |
fa64c868 | 1567 | return (lastlog_perform_login(li)); |
1d7b9b20 | 1568 | default: |
a233586b | 1569 | logit("%s: Invalid type field", __func__); |
fa64c868 | 1570 | return (0); |
1d7b9b20 | 1571 | } |
564dd50a | 1572 | } |
1d7b9b20 | 1573 | |
1d7b9b20 | 1574 | static void |
564dd50a | 1575 | lastlog_populate_entry(struct logininfo *li, struct lastlog *last) |
1576 | { | |
1d7b9b20 | 1577 | line_fullname(li->line, last->ll_line, sizeof(li->line)); |
2b87da3b | 1578 | strlcpy(li->hostname, last->ll_host, |
fa64c868 | 1579 | MIN_SIZEOF(li->hostname, last->ll_host)); |
1d7b9b20 | 1580 | li->tv_sec = last->ll_time; |
564dd50a | 1581 | } |
1d7b9b20 | 1582 | |
1d7b9b20 | 1583 | int |
564dd50a | 1584 | lastlog_get_entry(struct logininfo *li) |
1585 | { | |
1d7b9b20 | 1586 | struct lastlog last; |
ed05a983 | 1587 | int fd, ret; |
1d7b9b20 | 1588 | |
ce49121d | 1589 | if (!lastlog_openseek(li, &fd, O_RDONLY)) |
ed05a983 | 1590 | return (0); |
ce49121d | 1591 | |
ed05a983 | 1592 | ret = atomicio(read, fd, &last, sizeof(last)); |
ce49121d | 1593 | close(fd); |
1594 | ||
ed05a983 | 1595 | switch (ret) { |
1596 | case 0: | |
1597 | memset(&last, '\0', sizeof(last)); | |
1598 | /* FALLTHRU */ | |
1599 | case sizeof(last): | |
1600 | lastlog_populate_entry(li, &last); | |
1601 | return (1); | |
1602 | case -1: | |
aff51935 | 1603 | error("%s: Error reading from %s: %s", __func__, |
ed05a983 | 1604 | LASTLOG_FILE, strerror(errno)); |
1605 | return (0); | |
1606 | default: | |
1607 | error("%s: Error reading from %s: Expecting %d, got %d", | |
dfde7f6e | 1608 | __func__, LASTLOG_FILE, (int)sizeof(last), ret); |
ed05a983 | 1609 | return (0); |
1610 | } | |
ce49121d | 1611 | |
ed05a983 | 1612 | /* NOTREACHED */ |
1613 | return (0); | |
564dd50a | 1614 | } |
f988dce5 | 1615 | #endif /* USE_LASTLOG */ |
b6610e8f | 1616 | |
1617 | #ifdef USE_BTMP | |
1618 | /* | |
1619 | * Logs failed login attempts in _PATH_BTMP if that exists. | |
1620 | * The most common login failure is to give password instead of username. | |
1621 | * So the _PATH_BTMP file checked for the correct permission, so that | |
1622 | * only root can read it. | |
1623 | */ | |
1624 | ||
1625 | void | |
1626 | record_failed_login(const char *username, const char *hostname, | |
1627 | const char *ttyn) | |
1628 | { | |
1629 | int fd; | |
1630 | struct utmp ut; | |
1631 | struct sockaddr_storage from; | |
dfde7f6e | 1632 | socklen_t fromlen = sizeof(from); |
b6610e8f | 1633 | struct sockaddr_in *a4; |
1634 | struct sockaddr_in6 *a6; | |
1635 | time_t t; | |
1636 | struct stat fst; | |
1637 | ||
1638 | if (geteuid() != 0) | |
1639 | return; | |
1640 | if ((fd = open(_PATH_BTMP, O_WRONLY | O_APPEND)) < 0) { | |
1641 | debug("Unable to open the btmp file %s: %s", _PATH_BTMP, | |
1642 | strerror(errno)); | |
1643 | return; | |
1644 | } | |
1645 | if (fstat(fd, &fst) < 0) { | |
1646 | logit("%s: fstat of %s failed: %s", __func__, _PATH_BTMP, | |
1647 | strerror(errno)); | |
1648 | goto out; | |
1649 | } | |
1650 | if((fst.st_mode & (S_IRWXG | S_IRWXO)) || (fst.st_uid != 0)){ | |
1651 | logit("Excess permission or bad ownership on file %s", | |
1652 | _PATH_BTMP); | |
1653 | goto out; | |
1654 | } | |
1655 | ||
1656 | memset(&ut, 0, sizeof(ut)); | |
1657 | /* strncpy because we don't necessarily want nul termination */ | |
1658 | strncpy(ut.ut_user, username, sizeof(ut.ut_user)); | |
1659 | strlcpy(ut.ut_line, "ssh:notty", sizeof(ut.ut_line)); | |
1660 | ||
1661 | time(&t); | |
1662 | ut.ut_time = t; /* ut_time is not always a time_t */ | |
1663 | ut.ut_type = LOGIN_PROCESS; | |
1664 | ut.ut_pid = getpid(); | |
1665 | ||
1666 | /* strncpy because we don't necessarily want nul termination */ | |
1667 | strncpy(ut.ut_host, hostname, sizeof(ut.ut_host)); | |
1668 | ||
1669 | if (packet_connection_is_on_socket() && | |
1670 | getpeername(packet_get_connection_in(), | |
1671 | (struct sockaddr *)&from, &fromlen) == 0) { | |
1672 | ipv64_normalise_mapped(&from, &fromlen); | |
1673 | if (from.ss_family == AF_INET) { | |
1674 | a4 = (struct sockaddr_in *)&from; | |
1675 | memcpy(&ut.ut_addr, &(a4->sin_addr), | |
1676 | MIN_SIZEOF(ut.ut_addr, a4->sin_addr)); | |
1677 | } | |
1678 | #ifdef HAVE_ADDR_V6_IN_UTMP | |
1679 | if (from.ss_family == AF_INET6) { | |
1680 | a6 = (struct sockaddr_in6 *)&from; | |
1681 | memcpy(&ut.ut_addr_v6, &(a6->sin6_addr), | |
1682 | MIN_SIZEOF(ut.ut_addr_v6, a6->sin6_addr)); | |
1683 | } | |
1684 | #endif | |
1685 | } | |
1686 | ||
1687 | if (atomicio(vwrite, fd, &ut, sizeof(ut)) != sizeof(ut)) | |
1688 | error("Failed to write to %s: %s", _PATH_BTMP, | |
1689 | strerror(errno)); | |
1690 | ||
1691 | out: | |
1692 | close(fd); | |
1693 | } | |
1694 | #endif /* USE_BTMP */ |