Mercurial > dropbear
comparison loginrec.c @ 150:82fcf3185616
Cleaning out various dead wood found with -dead_strip
bignum.c: mptobytes now resides in dss.c
loginrec.c: remove lastlog code since it isn't used.
dbutil.c: removed obselete usingsyslog variable
channel.h: client channel type only defined for client compile
common-algo.c: s/rijndael/aes/
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 20 Dec 2004 14:24:57 +0000 |
parents | fb7147e2fb04 |
children | 6ba95762ec4e |
comparison
equal
deleted
inserted
replaced
149:ed9ca2a9705c | 150:82fcf3185616 |
---|---|
26 */ | 26 */ |
27 | 27 |
28 /** | 28 /** |
29 ** loginrec.c: platform-independent login recording and lastlog retrieval | 29 ** loginrec.c: platform-independent login recording and lastlog retrieval |
30 **/ | 30 **/ |
31 | |
32 /* For now lastlog code has been removed as it wasn't being used by Dropbear. */ | |
31 | 33 |
32 /* | 34 /* |
33 The new login code explained | 35 The new login code explained |
34 ============================ | 36 ============================ |
35 | 37 |
172 | 174 |
173 int utmp_write_entry(struct logininfo *li); | 175 int utmp_write_entry(struct logininfo *li); |
174 int utmpx_write_entry(struct logininfo *li); | 176 int utmpx_write_entry(struct logininfo *li); |
175 int wtmp_write_entry(struct logininfo *li); | 177 int wtmp_write_entry(struct logininfo *li); |
176 int wtmpx_write_entry(struct logininfo *li); | 178 int wtmpx_write_entry(struct logininfo *li); |
177 int lastlog_write_entry(struct logininfo *li); | |
178 int syslogin_write_entry(struct logininfo *li); | 179 int syslogin_write_entry(struct logininfo *li); |
179 | 180 |
180 int getlast_entry(struct logininfo *li); | |
181 int lastlog_get_entry(struct logininfo *li); | |
182 int wtmp_get_entry(struct logininfo *li); | 181 int wtmp_get_entry(struct logininfo *li); |
183 int wtmpx_get_entry(struct logininfo *li); | 182 int wtmpx_get_entry(struct logininfo *li); |
184 | 183 |
185 /* pick the shortest string */ | 184 /* pick the shortest string */ |
186 #define MIN_SIZEOF(s1,s2) ( sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2) ) | 185 #define MIN_SIZEOF(s1,s2) ( sizeof(s1) < sizeof(s2) ? sizeof(s1) : sizeof(s2) ) |
217 int | 216 int |
218 login_logout(struct logininfo *li) | 217 login_logout(struct logininfo *li) |
219 { | 218 { |
220 li->type = LTYPE_LOGOUT; | 219 li->type = LTYPE_LOGOUT; |
221 return login_write(li); | 220 return login_write(li); |
222 } | |
223 | |
224 /* login_get_lastlog_time(int) - Retrieve the last login time | |
225 * | |
226 * Retrieve the last login time for the given uid. Will try to use the | |
227 * system lastlog facilities if they are available, but will fall back | |
228 * to looking in wtmp/wtmpx if necessary | |
229 * | |
230 * Returns: | |
231 * 0 on failure, or if user has never logged in | |
232 * Time in seconds from the epoch if successful | |
233 * | |
234 * Useful preprocessor symbols: | |
235 * DISABLE_LASTLOG: If set, *never* even try to retrieve lastlog | |
236 * info | |
237 * USE_LASTLOG: If set, indicates the presence of system lastlog | |
238 * facilities. If this and DISABLE_LASTLOG are not set, | |
239 * try to retrieve lastlog information from wtmp/wtmpx. | |
240 */ | |
241 unsigned int | |
242 login_get_lastlog_time(const int uid) | |
243 { | |
244 struct logininfo li; | |
245 | |
246 if (login_get_lastlog(&li, uid)) | |
247 return li.tv_sec; | |
248 else | |
249 return 0; | |
250 } | |
251 | |
252 /* login_get_lastlog(struct logininfo *, int) - Retrieve a lastlog entry | |
253 * | |
254 * Retrieve a logininfo structure populated (only partially) with | |
255 * information from the system lastlog data, or from wtmp/wtmpx if no | |
256 * system lastlog information exists. | |
257 * | |
258 * Note this routine must be given a pre-allocated logininfo. | |
259 * | |
260 * Returns: | |
261 * >0: A pointer to your struct logininfo if successful | |
262 * 0 on failure (will use OpenSSH's logging facilities for diagnostics) | |
263 * | |
264 */ | |
265 struct logininfo * | |
266 login_get_lastlog(struct logininfo *li, const int uid) | |
267 { | |
268 struct passwd *pw; | |
269 | |
270 memset(li, '\0', sizeof(*li)); | |
271 li->uid = uid; | |
272 | |
273 /* | |
274 * If we don't have a 'real' lastlog, we need the username to | |
275 * reliably search wtmp(x) for the last login (see | |
276 * wtmp_get_entry().) | |
277 */ | |
278 pw = getpwuid(uid); | |
279 if (pw == NULL) | |
280 dropbear_exit("login_get_lastlog: Cannot find account for uid %i", uid); | |
281 | |
282 /* No MIN_SIZEOF here - we absolutely *must not* truncate the | |
283 * username */ | |
284 strlcpy(li->username, pw->pw_name, sizeof(li->username)); | |
285 | |
286 if (getlast_entry(li)) | |
287 return li; | |
288 else | |
289 return NULL; | |
290 } | 221 } |
291 | 222 |
292 | 223 |
293 /* login_alloc_entry(int, char*, char*, char*) - Allocate and initialise | 224 /* login_alloc_entry(int, char*, char*, char*) - Allocate and initialise |
294 * a logininfo structure | 225 * a logininfo structure |
447 wtmpx_write_entry(li); | 378 wtmpx_write_entry(li); |
448 # endif | 379 # endif |
449 return 0; | 380 return 0; |
450 } | 381 } |
451 #endif | 382 #endif |
452 | |
453 /** | |
454 ** getlast_entry: Call low-level functions to retrieve the last login | |
455 ** time. | |
456 **/ | |
457 | |
458 /* take the uid in li and return the last login time */ | |
459 int | |
460 getlast_entry(struct logininfo *li) | |
461 { | |
462 #ifdef USE_LASTLOG | |
463 return(lastlog_get_entry(li)); | |
464 #else /* !USE_LASTLOG */ | |
465 | |
466 #ifdef DISABLE_LASTLOG | |
467 /* On some systems we shouldn't even try to obtain last login | |
468 * time, e.g. AIX */ | |
469 return 0; | |
470 # else /* DISABLE_LASTLOG */ | |
471 /* Try to retrieve the last login time from wtmp */ | |
472 # if defined(USE_WTMP) && (defined(HAVE_STRUCT_UTMP_UT_TIME) || defined(HAVE_STRUCT_UTMP_UT_TV)) | |
473 /* retrieve last login time from utmp */ | |
474 return (wtmp_get_entry(li)); | |
475 # else /* defined(USE_WTMP) && (defined(HAVE_STRUCT_UTMP_UT_TIME) || defined(HAVE_STRUCT_UTMP_UT_TV)) */ | |
476 /* If wtmp isn't available, try wtmpx */ | |
477 # if defined(USE_WTMPX) && (defined(HAVE_STRUCT_UTMPX_UT_TIME) || defined(HAVE_STRUCT_UTMPX_UT_TV)) | |
478 /* retrieve last login time from utmpx */ | |
479 return (wtmpx_get_entry(li)); | |
480 # else | |
481 /* Give up: No means of retrieving last login time */ | |
482 return 0; | |
483 # endif /* USE_WTMPX && (HAVE_STRUCT_UTMPX_UT_TIME || HAVE_STRUCT_UTMPX_UT_TV) */ | |
484 # endif /* USE_WTMP && (HAVE_STRUCT_UTMP_UT_TIME || HAVE_STRUCT_UTMP_UT_TV) */ | |
485 # endif /* DISABLE_LASTLOG */ | |
486 #endif /* USE_LASTLOG */ | |
487 } | |
488 | 383 |
489 | 384 |
490 | 385 |
491 /* | 386 /* |
492 * 'line' string utility functions | 387 * 'line' string utility functions |
1493 dropbear_log(LOG_WARNING, "lastlog_write_entry: Invalid type field"); | 1388 dropbear_log(LOG_WARNING, "lastlog_write_entry: Invalid type field"); |
1494 return 0; | 1389 return 0; |
1495 } | 1390 } |
1496 } | 1391 } |
1497 | 1392 |
1498 static void | |
1499 lastlog_populate_entry(struct logininfo *li, struct lastlog *last) | |
1500 { | |
1501 line_fullname(li->line, last->ll_line, sizeof(li->line)); | |
1502 strlcpy(li->hostname, last->ll_host, | |
1503 MIN_SIZEOF(li->hostname, last->ll_host)); | |
1504 li->tv_sec = last->ll_time; | |
1505 } | |
1506 | |
1507 int | |
1508 lastlog_get_entry(struct logininfo *li) | |
1509 { | |
1510 struct lastlog last; | |
1511 int fd, ret; | |
1512 | |
1513 if (!lastlog_openseek(li, &fd, O_RDONLY)) | |
1514 return (0); | |
1515 | |
1516 ret = atomicio(read, fd, &last, sizeof(last)); | |
1517 close(fd); | |
1518 | |
1519 switch (ret) { | |
1520 case 0: | |
1521 memset(&last, '\0', sizeof(last)); | |
1522 /* FALLTHRU */ | |
1523 case sizeof(last): | |
1524 lastlog_populate_entry(li, &last); | |
1525 return (1); | |
1526 case -1: | |
1527 dropbear_log(LOG_ERR, "Error reading from %s: %s", | |
1528 LASTLOG_FILE, strerror(errno)); | |
1529 return (0); | |
1530 default: | |
1531 dropbear_log(LOG_ERR, "Error reading from %s: Expecting %d, got %d", | |
1532 LASTLOG_FILE, sizeof(last), ret); | |
1533 return (0); | |
1534 } | |
1535 | |
1536 /* NOTREACHED */ | |
1537 return (0); | |
1538 } | |
1539 #endif /* USE_LASTLOG */ | 1393 #endif /* USE_LASTLOG */ |