/src/dropbear/src/loginrec.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2000 Andre Lucas. All rights reserved. |
3 | | * Portions copyright (c) 1998 Todd C. Miller |
4 | | * Portions copyright (c) 1996 Jason Downs |
5 | | * Portions copyright (c) 1996 Theo de Raadt |
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. |
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 | | |
28 | | /** |
29 | | ** loginrec.c: platform-independent login recording and lastlog retrieval |
30 | | **/ |
31 | | |
32 | | /* For now lastlog code has been removed as it wasn't being used by Dropbear. */ |
33 | | |
34 | | /* |
35 | | The new login code explained |
36 | | ============================ |
37 | | |
38 | | This code attempts to provide a common interface to login recording |
39 | | (utmp and friends) and last login time retrieval. |
40 | | |
41 | | Its primary means of achieving this is to use 'struct logininfo', a |
42 | | union of all the useful fields in the various different types of |
43 | | system login record structures one finds on UNIX variants. |
44 | | |
45 | | We depend on autoconf to define which recording methods are to be |
46 | | used, and which fields are contained in the relevant data structures |
47 | | on the local system. Many C preprocessor symbols affect which code |
48 | | gets compiled here. |
49 | | |
50 | | The code is designed to make it easy to modify a particular |
51 | | recording method, without affecting other methods nor requiring so |
52 | | many nested conditional compilation blocks as were commonplace in |
53 | | the old code. |
54 | | |
55 | | For login recording, we try to use the local system's libraries as |
56 | | these are clearly most likely to work correctly. For utmp systems |
57 | | this usually means login() and logout() or setutent() etc., probably |
58 | | in libutil, along with logwtmp() etc. On these systems, we fall back |
59 | | to writing the files directly if we have to, though this method |
60 | | requires very thorough testing so we do not corrupt local auditing |
61 | | information. These files and their access methods are very system |
62 | | specific indeed. |
63 | | |
64 | | For utmpx systems, the corresponding library functions are |
65 | | setutxent() etc. To the author's knowledge, all utmpx systems have |
66 | | these library functions and so no direct write is attempted. If such |
67 | | a system exists and needs support, direct analogues of the [uw]tmp |
68 | | code should suffice. |
69 | | |
70 | | Retrieving the time of last login ('lastlog') is in some ways even |
71 | | more problemmatic than login recording. Some systems provide a |
72 | | simple table of all users which we seek based on uid and retrieve a |
73 | | relatively standard structure. Others record the same information in |
74 | | a directory with a separate file, and others don't record the |
75 | | information separately at all. For systems in the latter category, |
76 | | we look backwards in the wtmp or wtmpx file for the last login entry |
77 | | for our user. Naturally this is slower and on busy systems could |
78 | | incur a significant performance penalty. |
79 | | |
80 | | Calling the new code |
81 | | -------------------- |
82 | | |
83 | | In OpenSSH all login recording and retrieval is performed in |
84 | | login.c. Here you'll find working examples. Also, in the logintest.c |
85 | | program there are more examples. |
86 | | |
87 | | Internal handler calling method |
88 | | ------------------------------- |
89 | | |
90 | | When a call is made to login_login() or login_logout(), both |
91 | | routines set a struct logininfo flag defining which action (log in, |
92 | | or log out) is to be taken. They both then call login_write(), which |
93 | | calls whichever of the many structure-specific handlers autoconf |
94 | | selects for the local system. |
95 | | |
96 | | The handlers themselves handle system data structure specifics. Both |
97 | | struct utmp and struct utmpx have utility functions (see |
98 | | construct_utmp*()) to try to make it simpler to add extra systems |
99 | | that introduce new features to either structure. |
100 | | |
101 | | While it may seem terribly wasteful to replicate so much similar |
102 | | code for each method, experience has shown that maintaining code to |
103 | | write both struct utmp and utmpx in one function, whilst maintaining |
104 | | support for all systems whether they have library support or not, is |
105 | | a difficult and time-consuming task. |
106 | | |
107 | | Lastlog support proceeds similarly. Functions login_get_lastlog() |
108 | | (and its OpenSSH-tuned friend login_get_lastlog_time()) call |
109 | | getlast_entry(), which tries one of three methods to find the last |
110 | | login time. It uses local system lastlog support if it can, |
111 | | otherwise it tries wtmp or wtmpx before giving up and returning 0, |
112 | | meaning "tilt". |
113 | | |
114 | | Maintenance |
115 | | ----------- |
116 | | |
117 | | In many cases it's possible to tweak autoconf to select the correct |
118 | | methods for a particular platform, either by improving the detection |
119 | | code (best), or by presetting DISABLE_<method> or CONF_<method>_FILE |
120 | | symbols for the platform. |
121 | | |
122 | | Use logintest to check which symbols are defined before modifying |
123 | | configure.ac and loginrec.c. (You have to build logintest yourself |
124 | | with 'make logintest' as it's not built by default.) |
125 | | |
126 | | Otherwise, patches to the specific method(s) are very helpful! |
127 | | |
128 | | */ |
129 | | |
130 | | /** |
131 | | ** TODO: |
132 | | ** homegrown ttyslot() |
133 | | ** test, test, test |
134 | | ** |
135 | | ** Platform status: |
136 | | ** ---------------- |
137 | | ** |
138 | | ** Known good: |
139 | | ** Linux (Redhat 6.2, Debian) |
140 | | ** Solaris |
141 | | ** HP-UX 10.20 (gcc only) |
142 | | ** IRIX |
143 | | ** NeXT - M68k/HPPA/Sparc (4.2/3.3) |
144 | | ** |
145 | | ** Testing required: Please send reports! |
146 | | ** NetBSD |
147 | | ** HP-UX 11 |
148 | | ** AIX |
149 | | ** |
150 | | ** Platforms with known problems: |
151 | | ** Some variants of Slackware Linux |
152 | | ** |
153 | | **/ |
154 | | |
155 | | |
156 | | #include "includes.h" |
157 | | #include "loginrec.h" |
158 | | #include "dbutil.h" |
159 | | #include "atomicio.h" |
160 | | |
161 | | /** |
162 | | ** prototypes for helper functions in this file |
163 | | **/ |
164 | | |
165 | | #if HAVE_UTMP_H |
166 | | void set_utmp_time(struct logininfo *li, struct utmp *ut); |
167 | | void construct_utmp(struct logininfo *li, struct utmp *ut); |
168 | | #endif |
169 | | |
170 | | #ifdef HAVE_UTMPX_H |
171 | | void set_utmpx_time(struct logininfo *li, struct utmpx *ut); |
172 | | void construct_utmpx(struct logininfo *li, struct utmpx *ut); |
173 | | #endif |
174 | | |
175 | | int utmp_write_entry(struct logininfo *li); |
176 | | int utmpx_write_entry(struct logininfo *li); |
177 | | int wtmp_write_entry(struct logininfo *li); |
178 | | int wtmpx_write_entry(struct logininfo *li); |
179 | | int lastlog_write_entry(struct logininfo *li); |
180 | | int syslogin_write_entry(struct logininfo *li); |
181 | | |
182 | | int wtmp_get_entry(struct logininfo *li); |
183 | | int wtmpx_get_entry(struct logininfo *li); |
184 | | |
185 | | /* pick the shortest string */ |
186 | 0 | #define MIN_SIZEOF(s1,s2) ( sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2) ) |
187 | | |
188 | | /** |
189 | | ** platform-independent login functions |
190 | | **/ |
191 | | |
192 | | /* login_login(struct logininfo *) -Record a login |
193 | | * |
194 | | * Call with a pointer to a struct logininfo initialised with |
195 | | * login_init_entry() or login_alloc_entry() |
196 | | */ |
197 | | void |
198 | | login_login (struct logininfo *li) |
199 | 0 | { |
200 | 0 | li->type = LTYPE_LOGIN; |
201 | 0 | login_write(li); |
202 | 0 | } |
203 | | |
204 | | |
205 | | /* login_logout(struct logininfo *) - Record a logout |
206 | | * |
207 | | * Call as with login_login() |
208 | | */ |
209 | | void |
210 | | login_logout(struct logininfo *li) |
211 | 0 | { |
212 | 0 | li->type = LTYPE_LOGOUT; |
213 | 0 | login_write(li); |
214 | 0 | } |
215 | | |
216 | | |
217 | | /* login_alloc_entry(int, char*, char*, char*) - Allocate and initialise |
218 | | * a logininfo structure |
219 | | * |
220 | | * This function creates a new struct logininfo, a data structure |
221 | | * meant to carry the information required to portably record login info. |
222 | | * |
223 | | * Returns a pointer to a newly created struct logininfo. If memory |
224 | | * allocation fails, the program halts. |
225 | | */ |
226 | | struct |
227 | | logininfo *login_alloc_entry(int pid, const char *username, |
228 | | const char *hostname, const char *line) |
229 | 0 | { |
230 | 0 | struct logininfo *newli; |
231 | |
|
232 | 0 | newli = (struct logininfo *) m_malloc (sizeof(*newli)); |
233 | 0 | (void)login_init_entry(newli, pid, username, hostname, line); |
234 | 0 | return newli; |
235 | 0 | } |
236 | | |
237 | | |
238 | | /* login_free_entry(struct logininfo *) - free struct memory */ |
239 | | void |
240 | | login_free_entry(struct logininfo *li) |
241 | 0 | { |
242 | 0 | m_free(li); |
243 | 0 | } |
244 | | |
245 | | |
246 | | /* login_init_entry(struct logininfo *, int, char*, char*, char*) |
247 | | * - initialise a struct logininfo |
248 | | * |
249 | | * Populates a new struct logininfo, a data structure meant to carry |
250 | | * the information required to portably record login info. |
251 | | * |
252 | | * Returns: 1 |
253 | | */ |
254 | | int |
255 | | login_init_entry(struct logininfo *li, int pid, const char *username, |
256 | | const char *hostname, const char *line) |
257 | 0 | { |
258 | 0 | struct passwd *pw; |
259 | |
|
260 | 0 | memset(li, 0, sizeof(*li)); |
261 | |
|
262 | 0 | li->pid = pid; |
263 | | |
264 | | /* set the line information */ |
265 | 0 | if (line) |
266 | 0 | line_fullname(li->line, line, sizeof(li->line)); |
267 | |
|
268 | 0 | if (username) { |
269 | 0 | strlcpy(li->username, username, sizeof(li->username)); |
270 | 0 | pw = getpwnam(li->username); |
271 | 0 | if (pw == NULL) |
272 | 0 | dropbear_exit("login_init_entry: Cannot find user \"%s\"", |
273 | 0 | li->username); |
274 | 0 | li->uid = pw->pw_uid; |
275 | 0 | } |
276 | | |
277 | 0 | if (hostname) |
278 | 0 | strlcpy(li->hostname, hostname, sizeof(li->hostname)); |
279 | |
|
280 | 0 | return 1; |
281 | 0 | } |
282 | | |
283 | | /* login_set_current_time(struct logininfo *) - set the current time |
284 | | * |
285 | | * Set the current time in a logininfo structure. This function is |
286 | | * meant to eliminate the need to deal with system dependencies for |
287 | | * time handling. |
288 | | */ |
289 | | void |
290 | | login_set_current_time(struct logininfo *li) |
291 | 0 | { |
292 | 0 | struct timeval tv; |
293 | |
|
294 | 0 | gettimeofday(&tv, NULL); |
295 | |
|
296 | 0 | li->tv_sec = tv.tv_sec; |
297 | 0 | li->tv_usec = tv.tv_usec; |
298 | 0 | } |
299 | | |
300 | | /** |
301 | | ** login_write: Call low-level recording functions based on autoconf |
302 | | ** results |
303 | | **/ |
304 | | void |
305 | | login_write (struct logininfo *li) |
306 | 0 | { |
307 | | /* set the timestamp */ |
308 | 0 | login_set_current_time(li); |
309 | 0 | #ifdef USE_LOGIN |
310 | 0 | syslogin_write_entry(li); |
311 | 0 | #endif |
312 | 0 | #ifdef USE_LASTLOG |
313 | 0 | if (li->type == LTYPE_LOGIN) { |
314 | 0 | lastlog_write_entry(li); |
315 | 0 | } |
316 | 0 | #endif |
317 | | #ifdef USE_UTMP |
318 | | utmp_write_entry(li); |
319 | | #endif |
320 | | #ifdef USE_WTMP |
321 | | wtmp_write_entry(li); |
322 | | #endif |
323 | | #ifdef USE_UTMPX |
324 | | utmpx_write_entry(li); |
325 | | #endif |
326 | | #ifdef USE_WTMPX |
327 | | wtmpx_write_entry(li); |
328 | | #endif |
329 | 0 | } |
330 | | |
331 | | #ifdef LOGIN_NEEDS_UTMPX |
332 | | int |
333 | | login_utmp_only(struct logininfo *li) |
334 | | { |
335 | | li->type = LTYPE_LOGIN; |
336 | | login_set_current_time(li); |
337 | | # ifdef USE_UTMP |
338 | | utmp_write_entry(li); |
339 | | # endif |
340 | | # ifdef USE_WTMP |
341 | | wtmp_write_entry(li); |
342 | | # endif |
343 | | # ifdef USE_UTMPX |
344 | | utmpx_write_entry(li); |
345 | | # endif |
346 | | # ifdef USE_WTMPX |
347 | | wtmpx_write_entry(li); |
348 | | # endif |
349 | | return 0; |
350 | | } |
351 | | #endif |
352 | | |
353 | | |
354 | | |
355 | | /* |
356 | | * 'line' string utility functions |
357 | | * |
358 | | * These functions process the 'line' string into one of three forms: |
359 | | * |
360 | | * 1. The full filename (including '/dev') |
361 | | * 2. The stripped name (excluding '/dev') |
362 | | * 3. The abbreviated name (e.g. /dev/ttyp00 -> yp00 |
363 | | * /dev/pts/1 -> ts/1 ) |
364 | | * |
365 | | * Form 3 is used on some systems to identify a .tmp.? entry when |
366 | | * attempting to remove it. Typically both addition and removal is |
367 | | * performed by one application - say, sshd - so as long as the choice |
368 | | * uniquely identifies a terminal it's ok. |
369 | | */ |
370 | | |
371 | | |
372 | | /* line_fullname(): add the leading '/dev/' if it doesn't exist make |
373 | | * sure dst has enough space, if not just copy src (ugh) */ |
374 | | char * |
375 | | line_fullname(char *dst, const char *src, size_t dstsize) |
376 | 0 | { |
377 | 0 | memset(dst, '\0', dstsize); |
378 | 0 | if ((strncmp(src, "/dev/", 5) == 0) || (dstsize < (strlen(src) + 5))) { |
379 | 0 | strlcpy(dst, src, dstsize); |
380 | 0 | } else { |
381 | 0 | strlcpy(dst, "/dev/", dstsize); |
382 | 0 | strlcat(dst, src, dstsize); |
383 | 0 | } |
384 | 0 | return dst; |
385 | 0 | } |
386 | | |
387 | | /* line_stripname(): strip the leading '/dev' if it exists, return dst */ |
388 | | char * |
389 | | line_stripname(char *dst, const char *src, size_t dstsize) |
390 | 0 | { |
391 | 0 | memset(dst, '\0', dstsize); |
392 | 0 | if (strncmp(src, "/dev/", 5) == 0) |
393 | 0 | strlcpy(dst, src + 5, dstsize); |
394 | 0 | else |
395 | 0 | strlcpy(dst, src, dstsize); |
396 | 0 | return dst; |
397 | 0 | } |
398 | | |
399 | | /* line_abbrevname(): Return the abbreviated (usually four-character) |
400 | | * form of the line (Just use the last <dstsize> characters of the |
401 | | * full name.) |
402 | | * |
403 | | * NOTE: use strncpy because we do NOT necessarily want zero |
404 | | * termination */ |
405 | | char * |
406 | | line_abbrevname(char *dst, const char *src, size_t dstsize) |
407 | 0 | { |
408 | 0 | size_t len; |
409 | |
|
410 | 0 | memset(dst, '\0', dstsize); |
411 | | |
412 | | /* Always skip prefix if present */ |
413 | 0 | if (strncmp(src, "/dev/", 5) == 0) |
414 | 0 | src += 5; |
415 | |
|
416 | | #ifdef WITH_ABBREV_NO_TTY |
417 | | if (strncmp(src, "tty", 3) == 0) |
418 | | src += 3; |
419 | | #endif |
420 | |
|
421 | 0 | len = strlen(src); |
422 | |
|
423 | 0 | if (len > 0) { |
424 | 0 | if (((int)len - dstsize) > 0) |
425 | 0 | src += ((int)len - dstsize); |
426 | | |
427 | | /* note: _don't_ change this to strlcpy */ |
428 | 0 | strncpy(dst, src, (size_t)dstsize); |
429 | 0 | } |
430 | |
|
431 | 0 | return dst; |
432 | 0 | } |
433 | | |
434 | | /** |
435 | | ** utmp utility functions |
436 | | ** |
437 | | ** These functions manipulate struct utmp, taking system differences |
438 | | ** into account. |
439 | | **/ |
440 | | |
441 | | #if defined(USE_UTMP) || defined (USE_WTMP) || defined (USE_LOGIN) |
442 | | |
443 | | /* build the utmp structure */ |
444 | | void |
445 | | set_utmp_time(struct logininfo *li, struct utmp *ut) |
446 | 0 | { |
447 | | /* struct utmp in glibc isn't y2038 safe yet */ |
448 | 0 | # ifdef HAVE_STRUCT_UTMP_UT_TV |
449 | 0 | ut->ut_tv.tv_sec = li->tv_sec; |
450 | 0 | ut->ut_tv.tv_usec = li->tv_usec; |
451 | | # else |
452 | | # ifdef HAVE_STRUCT_UTMP_UT_TIME |
453 | | ut->ut_time = li->tv_sec; |
454 | | # endif |
455 | | # endif |
456 | 0 | } |
457 | | |
458 | | void |
459 | | construct_utmp(struct logininfo *li, |
460 | | struct utmp *ut) |
461 | 0 | { |
462 | | # ifdef HAVE_ADDR_V6_IN_UTMP |
463 | | struct sockaddr_in6 *sa6; |
464 | | # endif |
465 | 0 | memset(ut, '\0', sizeof(*ut)); |
466 | | |
467 | | /* First fill out fields used for both logins and logouts */ |
468 | |
|
469 | 0 | # ifdef HAVE_STRUCT_UTMP_UT_ID |
470 | 0 | line_abbrevname(ut->ut_id, li->line, sizeof(ut->ut_id)); |
471 | 0 | # endif |
472 | |
|
473 | 0 | # ifdef HAVE_STRUCT_UTMP_UT_TYPE |
474 | | /* This is done here to keep utmp constants out of struct logininfo */ |
475 | 0 | switch (li->type) { |
476 | 0 | case LTYPE_LOGIN: |
477 | 0 | ut->ut_type = USER_PROCESS; |
478 | | #ifdef _UNICOS |
479 | | cray_set_tmpdir(ut); |
480 | | #endif |
481 | 0 | break; |
482 | 0 | case LTYPE_LOGOUT: |
483 | 0 | ut->ut_type = DEAD_PROCESS; |
484 | | #ifdef _UNICOS |
485 | | cray_retain_utmp(ut, li->pid); |
486 | | #endif |
487 | 0 | break; |
488 | 0 | } |
489 | 0 | # endif |
490 | 0 | set_utmp_time(li, ut); |
491 | |
|
492 | 0 | line_stripname(ut->ut_line, li->line, sizeof(ut->ut_line)); |
493 | |
|
494 | 0 | # ifdef HAVE_STRUCT_UTMP_UT_PID |
495 | 0 | ut->ut_pid = li->pid; |
496 | 0 | # endif |
497 | | |
498 | | /* If we're logging out, leave all other fields blank */ |
499 | 0 | if (li->type == LTYPE_LOGOUT) |
500 | 0 | return; |
501 | | |
502 | | /* |
503 | | * These fields are only used when logging in, and are blank |
504 | | * for logouts. |
505 | | */ |
506 | | |
507 | | /* Use strncpy because we don't necessarily want null termination */ |
508 | 0 | strncpy(ut->ut_name, li->username, MIN_SIZEOF(ut->ut_name, li->username)); |
509 | 0 | # ifdef HAVE_STRUCT_UTMP_UT_HOST |
510 | 0 | strncpy(ut->ut_host, li->hostname, MIN_SIZEOF(ut->ut_host, li->hostname)); |
511 | 0 | # endif |
512 | 0 | # ifdef HAVE_STRUCT_UTMP_UT_ADDR |
513 | | /* this is just a 32-bit IP address */ |
514 | 0 | if (li->hostaddr.sa.sa_family == AF_INET) |
515 | 0 | ut->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr; |
516 | 0 | # endif |
517 | | # ifdef HAVE_ADDR_V6_IN_UTMP |
518 | | /* this is just a 128-bit IPv6 address */ |
519 | | if (li->hostaddr.sa.sa_family == AF_INET6) { |
520 | | sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa); |
521 | | memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16); |
522 | | if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) { |
523 | | ut->ut_addr_v6[0] = ut->ut_addr_v6[3]; |
524 | | ut->ut_addr_v6[1] = 0; |
525 | | ut->ut_addr_v6[2] = 0; |
526 | | ut->ut_addr_v6[3] = 0; |
527 | | } |
528 | | } |
529 | | # endif |
530 | 0 | } |
531 | | #endif /* USE_UTMP || USE_WTMP || USE_LOGIN */ |
532 | | |
533 | | /** |
534 | | ** utmpx utility functions |
535 | | ** |
536 | | ** These functions manipulate struct utmpx, accounting for system |
537 | | ** variations. |
538 | | **/ |
539 | | |
540 | | #if defined(USE_UTMPX) || defined (USE_WTMPX) |
541 | | /* build the utmpx structure */ |
542 | | void |
543 | | set_utmpx_time(struct logininfo *li, struct utmpx *utx) |
544 | | { |
545 | | # ifdef HAVE_STRUCT_UTMPX_UT_TV |
546 | | utx->ut_tv.tv_sec = li->tv_sec; |
547 | | utx->ut_tv.tv_usec = li->tv_usec; |
548 | | # else /* HAVE_STRUCT_UTMPX_UT_TV */ |
549 | | # ifdef HAVE_STRUCT_UTMPX_UT_TIME |
550 | | utx->ut_time = li->tv_sec; |
551 | | # endif /* HAVE_STRUCT_UTMPX_UT_TIME */ |
552 | | # endif /* HAVE_STRUCT_UTMPX_UT_TV */ |
553 | | } |
554 | | |
555 | | void |
556 | | construct_utmpx(struct logininfo *li, struct utmpx *utx) |
557 | | { |
558 | | # ifdef HAVE_ADDR_V6_IN_UTMP |
559 | | struct sockaddr_in6 *sa6; |
560 | | # endif |
561 | | memset(utx, '\0', sizeof(*utx)); |
562 | | # ifdef HAVE_STRUCT_UTMPX_UT_ID |
563 | | line_abbrevname(utx->ut_id, li->line, sizeof(utx->ut_id)); |
564 | | # endif |
565 | | |
566 | | /* this is done here to keep utmp constants out of loginrec.h */ |
567 | | switch (li->type) { |
568 | | case LTYPE_LOGIN: |
569 | | utx->ut_type = USER_PROCESS; |
570 | | break; |
571 | | case LTYPE_LOGOUT: |
572 | | utx->ut_type = DEAD_PROCESS; |
573 | | break; |
574 | | } |
575 | | line_stripname(utx->ut_line, li->line, sizeof(utx->ut_line)); |
576 | | set_utmpx_time(li, utx); |
577 | | utx->ut_pid = li->pid; |
578 | | /* strncpy(): Don't necessarily want null termination */ |
579 | | strncpy(utx->ut_name, li->username, MIN_SIZEOF(utx->ut_name, li->username)); |
580 | | |
581 | | if (li->type == LTYPE_LOGOUT) |
582 | | return; |
583 | | |
584 | | /* |
585 | | * These fields are only used when logging in, and are blank |
586 | | * for logouts. |
587 | | */ |
588 | | |
589 | | # ifdef HAVE_STRUCT_UTMPX_UT_HOST |
590 | | strncpy(utx->ut_host, li->hostname, MIN_SIZEOF(utx->ut_host, li->hostname)); |
591 | | # endif |
592 | | # ifdef HAVE_STRUCT_UTMPX_UT_ADDR |
593 | | /* this is just a 32-bit IP address */ |
594 | | if (li->hostaddr.sa.sa_family == AF_INET) |
595 | | utx->ut_addr = li->hostaddr.sa_in.sin_addr.s_addr; |
596 | | # endif |
597 | | # ifdef HAVE_ADDR_V6_IN_UTMP |
598 | | /* this is just a 128-bit IPv6 address */ |
599 | | if (li->hostaddr.sa.sa_family == AF_INET6) { |
600 | | sa6 = ((struct sockaddr_in6 *)&li->hostaddr.sa); |
601 | | memcpy(ut->ut_addr_v6, sa6->sin6_addr.s6_addr, 16); |
602 | | if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) { |
603 | | ut->ut_addr_v6[0] = ut->ut_addr_v6[3]; |
604 | | ut->ut_addr_v6[1] = 0; |
605 | | ut->ut_addr_v6[2] = 0; |
606 | | ut->ut_addr_v6[3] = 0; |
607 | | } |
608 | | } |
609 | | # endif |
610 | | # ifdef HAVE_STRUCT_UTMPX_UT_SYSLEN |
611 | | /* ut_syslen is the length of the utx_host string */ |
612 | | utx->ut_syslen = MIN(strlen(li->hostname), sizeof(utx->ut_host)); |
613 | | # endif |
614 | | } |
615 | | #endif /* USE_UTMPX || USE_WTMPX */ |
616 | | |
617 | | /** |
618 | | ** Low-level utmp functions |
619 | | **/ |
620 | | |
621 | | /* FIXME: (ATL) utmp_write_direct needs testing */ |
622 | | #ifdef USE_UTMP |
623 | | |
624 | | /* if we can, use pututline() etc. */ |
625 | | # if !defined(DISABLE_PUTUTLINE) && defined(HAVE_SETUTENT) && \ |
626 | | defined(HAVE_PUTUTLINE) |
627 | | # define UTMP_USE_LIBRARY |
628 | | # endif |
629 | | |
630 | | |
631 | | /* write a utmp entry with the system's help (pututline() and pals) */ |
632 | | # ifdef UTMP_USE_LIBRARY |
633 | | static int |
634 | | utmp_write_library(struct logininfo *li, struct utmp *ut) |
635 | | { |
636 | | setutent(); |
637 | | pututline(ut); |
638 | | |
639 | | # ifdef HAVE_ENDUTENT |
640 | | endutent(); |
641 | | # endif |
642 | | return 1; |
643 | | } |
644 | | # else /* UTMP_USE_LIBRARY */ |
645 | | |
646 | | /* write a utmp entry direct to the file */ |
647 | | /* This is a slightly modification of code in OpenBSD's login.c */ |
648 | | static int |
649 | | utmp_write_direct(struct logininfo *li, struct utmp *ut) |
650 | | { |
651 | | struct utmp old_ut; |
652 | | register int fd; |
653 | | int tty; |
654 | | |
655 | | /* FIXME: (ATL) ttyslot() needs local implementation */ |
656 | | |
657 | | #if defined(HAVE_GETTTYENT) |
658 | | register struct ttyent *ty; |
659 | | |
660 | | tty=0; |
661 | | |
662 | | setttyent(); |
663 | | while ((struct ttyent *)0 != (ty = getttyent())) { |
664 | | tty++; |
665 | | if (!strncmp(ty->ty_name, ut->ut_line, sizeof(ut->ut_line))) |
666 | | break; |
667 | | } |
668 | | endttyent(); |
669 | | |
670 | | if((struct ttyent *)0 == ty) { |
671 | | dropbear_log(LOG_WARNING, "utmp_write_entry: tty not found"); |
672 | | return(1); |
673 | | } |
674 | | #else /* FIXME */ |
675 | | |
676 | | tty = ttyslot(); /* seems only to work for /dev/ttyp? style names */ |
677 | | |
678 | | #endif /* HAVE_GETTTYENT */ |
679 | | |
680 | | if (tty > 0 && (fd = open(UTMP_FILE, O_RDWR|O_CREAT, 0644)) >= 0) { |
681 | | (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); |
682 | | /* |
683 | | * Prevent luser from zero'ing out ut_host. |
684 | | * If the new ut_line is empty but the old one is not |
685 | | * and ut_line and ut_name match, preserve the old ut_line. |
686 | | */ |
687 | | if (atomicio(read, fd, &old_ut, sizeof(old_ut)) == sizeof(old_ut) && |
688 | | (ut->ut_host[0] == '\0') && (old_ut.ut_host[0] != '\0') && |
689 | | (strncmp(old_ut.ut_line, ut->ut_line, sizeof(ut->ut_line)) == 0) && |
690 | | (strncmp(old_ut.ut_name, ut->ut_name, sizeof(ut->ut_name)) == 0)) { |
691 | | (void)memcpy(ut->ut_host, old_ut.ut_host, sizeof(ut->ut_host)); |
692 | | } |
693 | | |
694 | | (void)lseek(fd, (off_t)(tty * sizeof(struct utmp)), SEEK_SET); |
695 | | if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) |
696 | | dropbear_log(LOG_WARNING, "utmp_write_direct: error writing %s: %s", |
697 | | UTMP_FILE, strerror(errno)); |
698 | | |
699 | | (void)close(fd); |
700 | | return 1; |
701 | | } else { |
702 | | return 0; |
703 | | } |
704 | | } |
705 | | # endif /* UTMP_USE_LIBRARY */ |
706 | | |
707 | | static int |
708 | | utmp_perform_login(struct logininfo *li) |
709 | | { |
710 | | struct utmp ut; |
711 | | |
712 | | construct_utmp(li, &ut); |
713 | | # ifdef UTMP_USE_LIBRARY |
714 | | if (!utmp_write_library(li, &ut)) { |
715 | | dropbear_log(LOG_WARNING, "utmp_perform_login: utmp_write_library() failed"); |
716 | | return 0; |
717 | | } |
718 | | # else |
719 | | if (!utmp_write_direct(li, &ut)) { |
720 | | dropbear_log(LOG_WARNING, "utmp_perform_login: utmp_write_direct() failed"); |
721 | | return 0; |
722 | | } |
723 | | # endif |
724 | | return 1; |
725 | | } |
726 | | |
727 | | |
728 | | static int |
729 | | utmp_perform_logout(struct logininfo *li) |
730 | | { |
731 | | struct utmp ut; |
732 | | |
733 | | construct_utmp(li, &ut); |
734 | | # ifdef UTMP_USE_LIBRARY |
735 | | if (!utmp_write_library(li, &ut)) { |
736 | | dropbear_log(LOG_WARNING, "utmp_perform_logout: utmp_write_library() failed"); |
737 | | return 0; |
738 | | } |
739 | | # else |
740 | | if (!utmp_write_direct(li, &ut)) { |
741 | | dropbear_log(LOG_WARNING, "utmp_perform_logout: utmp_write_direct() failed"); |
742 | | return 0; |
743 | | } |
744 | | # endif |
745 | | return 1; |
746 | | } |
747 | | |
748 | | |
749 | | int |
750 | | utmp_write_entry(struct logininfo *li) |
751 | | { |
752 | | switch(li->type) { |
753 | | case LTYPE_LOGIN: |
754 | | return utmp_perform_login(li); |
755 | | |
756 | | case LTYPE_LOGOUT: |
757 | | return utmp_perform_logout(li); |
758 | | |
759 | | default: |
760 | | dropbear_log(LOG_WARNING, "utmp_write_entry: invalid type field"); |
761 | | return 0; |
762 | | } |
763 | | } |
764 | | #endif /* USE_UTMP */ |
765 | | |
766 | | |
767 | | /** |
768 | | ** Low-level utmpx functions |
769 | | **/ |
770 | | |
771 | | /* not much point if we don't want utmpx entries */ |
772 | | #ifdef USE_UTMPX |
773 | | |
774 | | /* if we have the wherewithall, use pututxline etc. */ |
775 | | # if !defined(DISABLE_PUTUTXLINE) && defined(HAVE_SETUTXENT) && \ |
776 | | defined(HAVE_PUTUTXLINE) |
777 | | # define UTMPX_USE_LIBRARY |
778 | | # endif |
779 | | |
780 | | |
781 | | /* write a utmpx entry with the system's help (pututxline() and pals) */ |
782 | | # ifdef UTMPX_USE_LIBRARY |
783 | | static int |
784 | | utmpx_write_library(struct logininfo *li, struct utmpx *utx) |
785 | | { |
786 | | setutxent(); |
787 | | pututxline(utx); |
788 | | |
789 | | # ifdef HAVE_ENDUTXENT |
790 | | endutxent(); |
791 | | # endif |
792 | | return 1; |
793 | | } |
794 | | |
795 | | # else /* UTMPX_USE_LIBRARY */ |
796 | | |
797 | | /* write a utmp entry direct to the file */ |
798 | | static int |
799 | | utmpx_write_direct(struct logininfo *li, struct utmpx *utx) |
800 | | { |
801 | | dropbear_log(LOG_WARNING, "utmpx_write_direct: not implemented!"); |
802 | | return 0; |
803 | | } |
804 | | # endif /* UTMPX_USE_LIBRARY */ |
805 | | |
806 | | static int |
807 | | utmpx_perform_login(struct logininfo *li) |
808 | | { |
809 | | struct utmpx utx; |
810 | | |
811 | | construct_utmpx(li, &utx); |
812 | | # ifdef UTMPX_USE_LIBRARY |
813 | | if (!utmpx_write_library(li, &utx)) { |
814 | | dropbear_log(LOG_WARNING, "utmpx_perform_login: utmp_write_library() failed"); |
815 | | return 0; |
816 | | } |
817 | | # else |
818 | | if (!utmpx_write_direct(li, &utx)) { |
819 | | dropbear_log(LOG_WARNING, "utmpx_perform_login: utmp_write_direct() failed"); |
820 | | return 0; |
821 | | } |
822 | | # endif |
823 | | return 1; |
824 | | } |
825 | | |
826 | | |
827 | | static int |
828 | | utmpx_perform_logout(struct logininfo *li) |
829 | | { |
830 | | struct utmpx utx; |
831 | | |
832 | | construct_utmpx(li, &utx); |
833 | | # ifdef HAVE_STRUCT_UTMPX_UT_ID |
834 | | line_abbrevname(utx.ut_id, li->line, sizeof(utx.ut_id)); |
835 | | # endif |
836 | | # ifdef HAVE_STRUCT_UTMPX_UT_TYPE |
837 | | utx.ut_type = DEAD_PROCESS; |
838 | | # endif |
839 | | |
840 | | # ifdef UTMPX_USE_LIBRARY |
841 | | utmpx_write_library(li, &utx); |
842 | | # else |
843 | | utmpx_write_direct(li, &utx); |
844 | | # endif |
845 | | return 1; |
846 | | } |
847 | | |
848 | | int |
849 | | utmpx_write_entry(struct logininfo *li) |
850 | | { |
851 | | switch(li->type) { |
852 | | case LTYPE_LOGIN: |
853 | | return utmpx_perform_login(li); |
854 | | case LTYPE_LOGOUT: |
855 | | return utmpx_perform_logout(li); |
856 | | default: |
857 | | dropbear_log(LOG_WARNING, "utmpx_write_entry: invalid type field"); |
858 | | return 0; |
859 | | } |
860 | | } |
861 | | #endif /* USE_UTMPX */ |
862 | | |
863 | | |
864 | | /** |
865 | | ** Low-level wtmp functions |
866 | | **/ |
867 | | |
868 | | #ifdef USE_WTMP |
869 | | |
870 | | /* write a wtmp entry direct to the end of the file */ |
871 | | /* This is a slight modification of code in OpenBSD's logwtmp.c */ |
872 | | static int |
873 | | wtmp_write(struct logininfo *li, struct utmp *ut) |
874 | | { |
875 | | struct stat buf; |
876 | | int fd, ret = 1; |
877 | | |
878 | | if ((fd = open(WTMP_FILE, O_WRONLY|O_APPEND, 0)) < 0) { |
879 | | dropbear_log(LOG_WARNING, "wtmp_write: problem writing %s: %s", |
880 | | WTMP_FILE, strerror(errno)); |
881 | | return 0; |
882 | | } |
883 | | if (fstat(fd, &buf) == 0) |
884 | | if (atomicio(vwrite, fd, ut, sizeof(*ut)) != sizeof(*ut)) { |
885 | | ftruncate(fd, buf.st_size); |
886 | | dropbear_log(LOG_WARNING, "wtmp_write: problem writing %s: %s", |
887 | | WTMP_FILE, strerror(errno)); |
888 | | ret = 0; |
889 | | } |
890 | | (void)close(fd); |
891 | | return ret; |
892 | | } |
893 | | |
894 | | static int |
895 | | wtmp_perform_login(struct logininfo *li) |
896 | | { |
897 | | struct utmp ut; |
898 | | |
899 | | construct_utmp(li, &ut); |
900 | | return wtmp_write(li, &ut); |
901 | | } |
902 | | |
903 | | |
904 | | static int |
905 | | wtmp_perform_logout(struct logininfo *li) |
906 | | { |
907 | | struct utmp ut; |
908 | | |
909 | | construct_utmp(li, &ut); |
910 | | return wtmp_write(li, &ut); |
911 | | } |
912 | | |
913 | | |
914 | | int |
915 | | wtmp_write_entry(struct logininfo *li) |
916 | | { |
917 | | switch(li->type) { |
918 | | case LTYPE_LOGIN: |
919 | | return wtmp_perform_login(li); |
920 | | case LTYPE_LOGOUT: |
921 | | return wtmp_perform_logout(li); |
922 | | default: |
923 | | dropbear_log(LOG_WARNING, "wtmp_write_entry: invalid type field"); |
924 | | return 0; |
925 | | } |
926 | | } |
927 | | |
928 | | |
929 | | /* Notes on fetching login data from wtmp/wtmpx |
930 | | * |
931 | | * Logouts are usually recorded with (amongst other things) a blank |
932 | | * username on a given tty line. However, some systems (HP-UX is one) |
933 | | * leave all fields set, but change the ut_type field to DEAD_PROCESS. |
934 | | * |
935 | | * Since we're only looking for logins here, we know that the username |
936 | | * must be set correctly. On systems that leave it in, we check for |
937 | | * ut_type==USER_PROCESS (indicating a login.) |
938 | | * |
939 | | * Portability: Some systems may set something other than USER_PROCESS |
940 | | * to indicate a login process. I don't know of any as I write. Also, |
941 | | * it's possible that some systems may both leave the username in |
942 | | * place and not have ut_type. |
943 | | */ |
944 | | |
945 | | /* return true if this wtmp entry indicates a login */ |
946 | | static int |
947 | | wtmp_islogin(struct logininfo *li, struct utmp *ut) |
948 | | { |
949 | | if (strncmp(li->username, ut->ut_name, |
950 | | MIN_SIZEOF(li->username, ut->ut_name)) == 0) { |
951 | | # ifdef HAVE_STRUCT_UTMP_UT_TYPE |
952 | | if (ut->ut_type & USER_PROCESS) |
953 | | return 1; |
954 | | # else |
955 | | return 1; |
956 | | # endif |
957 | | } |
958 | | return 0; |
959 | | } |
960 | | |
961 | | int |
962 | | wtmp_get_entry(struct logininfo *li) |
963 | | { |
964 | | struct stat st; |
965 | | struct utmp ut; |
966 | | int fd, found=0; |
967 | | |
968 | | /* Clear the time entries in our logininfo */ |
969 | | li->tv_sec = li->tv_usec = 0; |
970 | | |
971 | | if ((fd = open(WTMP_FILE, O_RDONLY)) < 0) { |
972 | | dropbear_log(LOG_WARNING, "wtmp_get_entry: problem opening %s: %s", |
973 | | WTMP_FILE, strerror(errno)); |
974 | | return 0; |
975 | | } |
976 | | if (fstat(fd, &st) != 0) { |
977 | | dropbear_log(LOG_WARNING, "wtmp_get_entry: couldn't stat %s: %s", |
978 | | WTMP_FILE, strerror(errno)); |
979 | | close(fd); |
980 | | return 0; |
981 | | } |
982 | | |
983 | | /* Seek to the start of the last struct utmp */ |
984 | | if (lseek(fd, -(off_t)sizeof(struct utmp), SEEK_END) == -1) { |
985 | | /* Looks like we've got a fresh wtmp file */ |
986 | | close(fd); |
987 | | return 0; |
988 | | } |
989 | | |
990 | | while (!found) { |
991 | | if (atomicio(read, fd, &ut, sizeof(ut)) != sizeof(ut)) { |
992 | | dropbear_log(LOG_WARNING, "wtmp_get_entry: read of %s failed: %s", |
993 | | WTMP_FILE, strerror(errno)); |
994 | | close (fd); |
995 | | return 0; |
996 | | } |
997 | | if ( wtmp_islogin(li, &ut) ) { |
998 | | found = 1; |
999 | | /* We've already checked for a time in struct |
1000 | | * utmp, in login_getlast(). */ |
1001 | | # ifdef HAVE_STRUCT_UTMP_UT_TIME |
1002 | | li->tv_sec = ut.ut_time; |
1003 | | # else |
1004 | | # if HAVE_STRUCT_UTMP_UT_TV |
1005 | | li->tv_sec = ut.ut_tv.tv_sec; |
1006 | | # endif |
1007 | | # endif |
1008 | | line_fullname(li->line, ut.ut_line, |
1009 | | MIN_SIZEOF(li->line, ut.ut_line)); |
1010 | | # ifdef HAVE_STRUCT_UTMP_UT_HOST |
1011 | | strlcpy(li->hostname, ut.ut_host, |
1012 | | MIN_SIZEOF(li->hostname, ut.ut_host)); |
1013 | | # endif |
1014 | | continue; |
1015 | | } |
1016 | | /* Seek back 2 x struct utmp */ |
1017 | | if (lseek(fd, -(off_t)(2 * sizeof(struct utmp)), SEEK_CUR) == -1) { |
1018 | | /* We've found the start of the file, so quit */ |
1019 | | close (fd); |
1020 | | return 0; |
1021 | | } |
1022 | | } |
1023 | | |
1024 | | /* We found an entry. Tidy up and return */ |
1025 | | close(fd); |
1026 | | return 1; |
1027 | | } |
1028 | | # endif /* USE_WTMP */ |
1029 | | |
1030 | | |
1031 | | /** |
1032 | | ** Low-level wtmpx functions |
1033 | | **/ |
1034 | | |
1035 | | #ifdef USE_WTMPX |
1036 | | /* write a wtmpx entry direct to the end of the file */ |
1037 | | /* This is a slight modification of code in OpenBSD's logwtmp.c */ |
1038 | | static int |
1039 | | wtmpx_write(struct logininfo *li, struct utmpx *utx) |
1040 | | { |
1041 | | struct stat buf; |
1042 | | int fd, ret = 1; |
1043 | | |
1044 | | if ((fd = open(WTMPX_FILE, O_WRONLY|O_APPEND, 0)) < 0) { |
1045 | | dropbear_log(LOG_WARNING, "wtmpx_write: problem opening %s: %s", |
1046 | | WTMPX_FILE, strerror(errno)); |
1047 | | return 0; |
1048 | | } |
1049 | | |
1050 | | if (fstat(fd, &buf) == 0) |
1051 | | if (atomicio(vwrite, fd, utx, sizeof(*utx)) != sizeof(*utx)) { |
1052 | | ftruncate(fd, buf.st_size); |
1053 | | dropbear_log(LOG_WARNING, "wtmpx_write: problem writing %s: %s", |
1054 | | WTMPX_FILE, strerror(errno)); |
1055 | | ret = 0; |
1056 | | } |
1057 | | (void)close(fd); |
1058 | | |
1059 | | return ret; |
1060 | | } |
1061 | | |
1062 | | |
1063 | | static int |
1064 | | wtmpx_perform_login(struct logininfo *li) |
1065 | | { |
1066 | | struct utmpx utx; |
1067 | | |
1068 | | construct_utmpx(li, &utx); |
1069 | | return wtmpx_write(li, &utx); |
1070 | | } |
1071 | | |
1072 | | |
1073 | | static int |
1074 | | wtmpx_perform_logout(struct logininfo *li) |
1075 | | { |
1076 | | struct utmpx utx; |
1077 | | |
1078 | | construct_utmpx(li, &utx); |
1079 | | return wtmpx_write(li, &utx); |
1080 | | } |
1081 | | |
1082 | | |
1083 | | int |
1084 | | wtmpx_write_entry(struct logininfo *li) |
1085 | | { |
1086 | | switch(li->type) { |
1087 | | case LTYPE_LOGIN: |
1088 | | return wtmpx_perform_login(li); |
1089 | | case LTYPE_LOGOUT: |
1090 | | return wtmpx_perform_logout(li); |
1091 | | default: |
1092 | | dropbear_log(LOG_WARNING, "wtmpx_write_entry: invalid type field"); |
1093 | | return 0; |
1094 | | } |
1095 | | } |
1096 | | |
1097 | | /* Please see the notes above wtmp_islogin() for information about the |
1098 | | next two functions */ |
1099 | | |
1100 | | /* Return true if this wtmpx entry indicates a login */ |
1101 | | static int |
1102 | | wtmpx_islogin(struct logininfo *li, struct utmpx *utx) |
1103 | | { |
1104 | | if ( strncmp(li->username, utx->ut_name, |
1105 | | MIN_SIZEOF(li->username, utx->ut_name)) == 0 ) { |
1106 | | # ifdef HAVE_STRUCT_UTMPX_UT_TYPE |
1107 | | if (utx->ut_type == USER_PROCESS) |
1108 | | return 1; |
1109 | | # else |
1110 | | return 1; |
1111 | | # endif |
1112 | | } |
1113 | | return 0; |
1114 | | } |
1115 | | |
1116 | | |
1117 | | int |
1118 | | wtmpx_get_entry(struct logininfo *li) |
1119 | | { |
1120 | | struct stat st; |
1121 | | struct utmpx utx; |
1122 | | int fd, found=0; |
1123 | | |
1124 | | /* Clear the time entries */ |
1125 | | li->tv_sec = li->tv_usec = 0; |
1126 | | |
1127 | | if ((fd = open(WTMPX_FILE, O_RDONLY)) < 0) { |
1128 | | dropbear_log(LOG_WARNING, "wtmpx_get_entry: problem opening %s: %s", |
1129 | | WTMPX_FILE, strerror(errno)); |
1130 | | return 0; |
1131 | | } |
1132 | | if (fstat(fd, &st) != 0) { |
1133 | | dropbear_log(LOG_WARNING, "wtmpx_get_entry: couldn't stat %s: %s", |
1134 | | WTMPX_FILE, strerror(errno)); |
1135 | | close(fd); |
1136 | | return 0; |
1137 | | } |
1138 | | |
1139 | | /* Seek to the start of the last struct utmpx */ |
1140 | | if (lseek(fd, -(off_t)sizeof(struct utmpx), SEEK_END) == -1 ) { |
1141 | | /* probably a newly rotated wtmpx file */ |
1142 | | close(fd); |
1143 | | return 0; |
1144 | | } |
1145 | | |
1146 | | while (!found) { |
1147 | | if (atomicio(read, fd, &utx, sizeof(utx)) != sizeof(utx)) { |
1148 | | dropbear_log(LOG_WARNING, "wtmpx_get_entry: read of %s failed: %s", |
1149 | | WTMPX_FILE, strerror(errno)); |
1150 | | close (fd); |
1151 | | return 0; |
1152 | | } |
1153 | | /* Logouts are recorded as a blank username on a particular line. |
1154 | | * So, we just need to find the username in struct utmpx */ |
1155 | | if ( wtmpx_islogin(li, &utx) ) { |
1156 | | found = 1; |
1157 | | # ifdef HAVE_STRUCT_UTMPX_UT_TV |
1158 | | li->tv_sec = utx.ut_tv.tv_sec; |
1159 | | # else |
1160 | | # ifdef HAVE_STRUCT_UTMPX_UT_TIME |
1161 | | li->tv_sec = utx.ut_time; |
1162 | | # endif |
1163 | | # endif |
1164 | | line_fullname(li->line, utx.ut_line, sizeof(li->line)); |
1165 | | # ifdef HAVE_STRUCT_UTMPX_UT_HOST |
1166 | | strlcpy(li->hostname, utx.ut_host, |
1167 | | MIN_SIZEOF(li->hostname, utx.ut_host)); |
1168 | | # endif |
1169 | | continue; |
1170 | | } |
1171 | | if (lseek(fd, -(off_t)(2 * sizeof(struct utmpx)), SEEK_CUR) == -1) { |
1172 | | close (fd); |
1173 | | return 0; |
1174 | | } |
1175 | | } |
1176 | | |
1177 | | close(fd); |
1178 | | return 1; |
1179 | | } |
1180 | | #endif /* USE_WTMPX */ |
1181 | | |
1182 | | /** |
1183 | | ** Low-level libutil login() functions |
1184 | | **/ |
1185 | | |
1186 | | #ifdef USE_LOGIN |
1187 | | static int |
1188 | | syslogin_perform_login(struct logininfo *li) |
1189 | 0 | { |
1190 | 0 | struct utmp *ut; |
1191 | |
|
1192 | 0 | if (! (ut = (struct utmp *)malloc(sizeof(*ut)))) { |
1193 | 0 | dropbear_log(LOG_WARNING, "syslogin_perform_login: couldn't malloc()"); |
1194 | 0 | return 0; |
1195 | 0 | } |
1196 | 0 | construct_utmp(li, ut); |
1197 | 0 | login(ut); |
1198 | 0 | free(ut); |
1199 | |
|
1200 | 0 | return 1; |
1201 | 0 | } |
1202 | | |
1203 | | static int |
1204 | | syslogin_perform_logout(struct logininfo *li) |
1205 | 0 | { |
1206 | 0 | # ifdef HAVE_LOGOUT |
1207 | 0 | char line[8]; |
1208 | |
|
1209 | 0 | (void)line_stripname(line, li->line, sizeof(line)); |
1210 | |
|
1211 | 0 | if (!logout(line)) { |
1212 | 0 | dropbear_log(LOG_WARNING, "syslogin_perform_logout: logout(%s) returned an error: %s", line, strerror(errno)); |
1213 | 0 | # ifdef HAVE_LOGWTMP |
1214 | 0 | } else { |
1215 | 0 | logwtmp(line, "", ""); |
1216 | 0 | # endif |
1217 | 0 | } |
1218 | | /* FIXME: (ATL - if the need arises) What to do if we have |
1219 | | * login, but no logout? what if logout but no logwtmp? All |
1220 | | * routines are in libutil so they should all be there, |
1221 | | * but... */ |
1222 | 0 | # endif |
1223 | 0 | return 1; |
1224 | 0 | } |
1225 | | |
1226 | | int |
1227 | | syslogin_write_entry(struct logininfo *li) |
1228 | 0 | { |
1229 | 0 | switch (li->type) { |
1230 | 0 | case LTYPE_LOGIN: |
1231 | 0 | return syslogin_perform_login(li); |
1232 | 0 | case LTYPE_LOGOUT: |
1233 | 0 | return syslogin_perform_logout(li); |
1234 | 0 | default: |
1235 | 0 | dropbear_log(LOG_WARNING, "syslogin_write_entry: Invalid type field"); |
1236 | 0 | return 0; |
1237 | 0 | } |
1238 | 0 | } |
1239 | | #endif /* USE_LOGIN */ |
1240 | | |
1241 | | /* end of file log-syslogin.c */ |
1242 | | |
1243 | | /** |
1244 | | ** Low-level lastlog functions |
1245 | | **/ |
1246 | | |
1247 | | #ifdef USE_LASTLOG |
1248 | 0 | #define LL_FILE 1 |
1249 | 0 | #define LL_DIR 2 |
1250 | 0 | #define LL_OTHER 3 |
1251 | | |
1252 | | static void |
1253 | | lastlog_construct(struct logininfo *li, struct lastlog *last) |
1254 | 0 | { |
1255 | | /* clear the structure */ |
1256 | 0 | memset(last, '\0', sizeof(*last)); |
1257 | |
|
1258 | 0 | (void)line_stripname(last->ll_line, li->line, sizeof(last->ll_line)); |
1259 | 0 | strlcpy(last->ll_host, li->hostname, |
1260 | 0 | MIN_SIZEOF(last->ll_host, li->hostname)); |
1261 | | /* struct lastlog in glibc isn't y2038 safe yet */ |
1262 | 0 | last->ll_time = li->tv_sec; |
1263 | 0 | } |
1264 | | |
1265 | | static int |
1266 | | lastlog_filetype(char *filename) |
1267 | 0 | { |
1268 | 0 | struct stat st; |
1269 | |
|
1270 | 0 | if (stat(filename, &st) != 0) { |
1271 | 0 | dropbear_log(LOG_WARNING, "lastlog_perform_login: Couldn't stat %s: %s", filename, |
1272 | 0 | strerror(errno)); |
1273 | 0 | return 0; |
1274 | 0 | } |
1275 | 0 | if (S_ISDIR(st.st_mode)) |
1276 | 0 | return LL_DIR; |
1277 | 0 | else if (S_ISREG(st.st_mode)) |
1278 | 0 | return LL_FILE; |
1279 | 0 | else |
1280 | 0 | return LL_OTHER; |
1281 | 0 | } |
1282 | | |
1283 | | |
1284 | | /* open the file (using filemode) and seek to the login entry */ |
1285 | | static int |
1286 | | lastlog_openseek(struct logininfo *li, int *fd, int filemode) |
1287 | 0 | { |
1288 | 0 | off_t offset; |
1289 | 0 | int type; |
1290 | 0 | char lastlog_file[1024]; |
1291 | |
|
1292 | 0 | type = lastlog_filetype(LASTLOG_FILE); |
1293 | 0 | switch (type) { |
1294 | 0 | case LL_FILE: |
1295 | 0 | strlcpy(lastlog_file, LASTLOG_FILE, sizeof(lastlog_file)); |
1296 | 0 | break; |
1297 | 0 | case LL_DIR: |
1298 | 0 | snprintf(lastlog_file, sizeof(lastlog_file), "%s/%s", |
1299 | 0 | LASTLOG_FILE, li->username); |
1300 | 0 | break; |
1301 | 0 | default: |
1302 | 0 | dropbear_log(LOG_WARNING, "lastlog_openseek: %.100s is not a file or directory!", |
1303 | 0 | LASTLOG_FILE); |
1304 | 0 | return 0; |
1305 | 0 | } |
1306 | | |
1307 | 0 | *fd = open(lastlog_file, filemode, 0600); |
1308 | 0 | if ( *fd < 0) { |
1309 | 0 | dropbear_log(LOG_INFO, "lastlog_openseek: Couldn't open %s: %s", |
1310 | 0 | lastlog_file, strerror(errno)); |
1311 | 0 | return 0; |
1312 | 0 | } |
1313 | | |
1314 | 0 | if (type == LL_FILE) { |
1315 | | /* find this uid's offset in the lastlog file */ |
1316 | 0 | offset = (off_t) ((long)li->uid * sizeof(struct lastlog)); |
1317 | |
|
1318 | 0 | if ( lseek(*fd, offset, SEEK_SET) != offset ) { |
1319 | 0 | dropbear_log(LOG_WARNING, "lastlog_openseek: %s->lseek(): %s", |
1320 | 0 | lastlog_file, strerror(errno)); |
1321 | 0 | m_close(*fd); |
1322 | 0 | return 0; |
1323 | 0 | } |
1324 | 0 | } |
1325 | | |
1326 | 0 | return 1; |
1327 | 0 | } |
1328 | | |
1329 | | static int |
1330 | | lastlog_perform_login(struct logininfo *li) |
1331 | 0 | { |
1332 | 0 | struct lastlog last; |
1333 | 0 | int fd; |
1334 | | |
1335 | | /* create our struct lastlog */ |
1336 | 0 | lastlog_construct(li, &last); |
1337 | |
|
1338 | 0 | if (!lastlog_openseek(li, &fd, O_RDWR|O_CREAT)) |
1339 | 0 | return(0); |
1340 | | |
1341 | | /* write the entry */ |
1342 | 0 | if (atomicio(vwrite, fd, &last, sizeof(last)) != sizeof(last)) { |
1343 | 0 | close(fd); |
1344 | 0 | dropbear_log(LOG_WARNING, "lastlog_write_filemode: Error writing to %s: %s", |
1345 | 0 | LASTLOG_FILE, strerror(errno)); |
1346 | 0 | return 0; |
1347 | 0 | } |
1348 | | |
1349 | 0 | close(fd); |
1350 | 0 | return 1; |
1351 | 0 | } |
1352 | | |
1353 | | int |
1354 | | lastlog_write_entry(struct logininfo *li) |
1355 | 0 | { |
1356 | 0 | switch(li->type) { |
1357 | 0 | case LTYPE_LOGIN: |
1358 | 0 | return lastlog_perform_login(li); |
1359 | 0 | default: |
1360 | | dropbear_log(LOG_WARNING, "lastlog_write_entry: Invalid type field"); |
1361 | 0 | return 0; |
1362 | 0 | } |
1363 | 0 | } |
1364 | | |
1365 | | #endif /* USE_LASTLOG */ |