From f79b2cf4177844fb64773a119f1cb1e41c8ccaf8 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Mon, 12 Aug 2024 17:37:03 +0200 Subject: [PATCH 1/2] Treat out of memory as error Otherwise it might happen that a configured file location is not taken into account. A NULL value would lead to default file location. --- pam_lastlog2/src/pam_lastlog2.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pam_lastlog2/src/pam_lastlog2.c b/pam_lastlog2/src/pam_lastlog2.c index e800700bf58..c3ca989c1cb 100644 --- a/pam_lastlog2/src/pam_lastlog2.c +++ b/pam_lastlog2/src/pam_lastlog2.c @@ -189,6 +189,8 @@ write_login_data (pam_handle_t *pamh, int ctrl, const char *user) return PAM_SYSTEM_ERR; struct ll2_context *context = ll2_new_context(lastlog2_path); + if (context == NULL) + return PAM_SYSTEM_ERR; if (ll2_write_entry (context, user, ll_time, tty, rhost, pam_service, &error) != 0) { if (error) { @@ -220,6 +222,8 @@ show_lastlogin (pam_handle_t *pamh, int ctrl, const char *user) return retval; struct ll2_context *context = ll2_new_context(lastlog2_path); + if (context == NULL) + return PAM_SYSTEM_ERR; if (ll2_read_entry (context, user, &ll_time, &tty, &rhost, &service, &error) != 0) { if (errno == ENOENT) From 8818717f2771adebb9e3e0f0031bca9f19bfff86 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Mon, 12 Aug 2024 17:37:53 +0200 Subject: [PATCH 2/2] lastlog2: Improve comments and documentation Fix grammar issues and unify style across documentation and code comments. No functional change. --- liblastlog2/README.md | 6 ++--- liblastlog2/man/lastlog2.3.adoc | 2 +- liblastlog2/man/ll2_import_lastlog.3.adoc | 4 +-- liblastlog2/man/ll2_new_context.3.adoc | 8 +++--- liblastlog2/man/ll2_read_all.3.adoc | 2 +- liblastlog2/man/ll2_read_entry.3.adoc | 2 +- liblastlog2/man/ll2_remove_entry.3.adoc | 4 +-- liblastlog2/man/ll2_rename_user.3.adoc | 4 +-- liblastlog2/man/ll2_unref_context.3.adoc | 4 +-- liblastlog2/man/ll2_update_login_time.3.adoc | 6 ++--- liblastlog2/man/ll2_write_entry.3.adoc | 4 +-- liblastlog2/src/lastlog2.c | 28 ++++++++++++-------- misc-utils/lastlog2.8.adoc | 2 +- 13 files changed, 41 insertions(+), 35 deletions(-) diff --git a/liblastlog2/README.md b/liblastlog2/README.md index 8bcfb39b655..7095edd5798 100644 --- a/liblastlog2/README.md +++ b/liblastlog2/README.md @@ -18,15 +18,15 @@ Since there are only few applications which really support `lastlog`, the data i ## lastlog2 -`lastlog2` tries to solve this problems: +`lastlog2` tries to solve these problems: * It's using sqlite3 as database backend. -* Data is only collected via a PAM module, so that every tools can make use of it, without modifying existing packages. +* Data is only collected via a PAM module, so that all tools can make use of it, without modifying existing packages. * The output is as compatible as possible with the old lastlog implementation. * The old `/var/log/lastlog` file can be imported into the new database. * The size of the database depends on the amount of users, not how big the biggest UID is. -**IMPORTANT** To be Y2038 safe on 32bit architectures, the binaries needs to be build with a **64bit time_t**. This should be the standard on 64bit architectures. +**IMPORTANT** To be Y2038 safe on 32bit architectures, the binaries need to be built with a **64bit time_t**. This should be the standard on 64bit architectures. Besides the lastlog2 library in this directory there are additional parts like service definition, PAM module and applications: diff --git a/liblastlog2/man/lastlog2.3.adoc b/liblastlog2/man/lastlog2.3.adoc index 05cab12187b..405bdb2a533 100644 --- a/liblastlog2/man/lastlog2.3.adoc +++ b/liblastlog2/man/lastlog2.3.adoc @@ -19,7 +19,7 @@ lastlog2 - Y2038 safe version of lastlog library. *lastlog2* reports the last login of a given user or of all users who did ever login on a system. -It's using sqlite3 as database backend. Data is only collected via a PAM module, so that every +It's using sqlite3 as database backend. Data is only collected via a PAM module, so that all tools can make use of it, without modifying existing packages. The output is as compatible as possible with the old lastlog implementation. By default the database will be written as `/var/lib/lastlog/lastlog2.db`. diff --git a/liblastlog2/man/ll2_import_lastlog.3.adoc b/liblastlog2/man/ll2_import_lastlog.3.adoc index 9ddf54b1679..672a92cd167 100644 --- a/liblastlog2/man/ll2_import_lastlog.3.adoc +++ b/liblastlog2/man/ll2_import_lastlog.3.adoc @@ -9,7 +9,7 @@ == NAME -ll2_import_lastlog - Import old lastlog file. +ll2_import_lastlog - Imports old lastlog file. == SYNOPSIS @@ -21,7 +21,7 @@ ll2_import_lastlog - Import old lastlog file. == DESCRIPTION -Importing all entries from _lastlog_file_ file (lastlog(8)) into +Imports all entries from _lastlog_file_ file (lastlog(8)) into lastlog2 database defined with _context_. If _context_ is NULL, the default database, defined in _LL2_DEFAULT_DATABASE_, will be taken. diff --git a/liblastlog2/man/ll2_new_context.3.adoc b/liblastlog2/man/ll2_new_context.3.adoc index 9a46646a7d1..8bb280422ec 100644 --- a/liblastlog2/man/ll2_new_context.3.adoc +++ b/liblastlog2/man/ll2_new_context.3.adoc @@ -9,7 +9,7 @@ == NAME -ll2_new_context - Context which defines the lastlog2 environment. +ll2_new_context - Allocates context which defines the lastlog2 environment. == SYNOPSIS @@ -19,8 +19,8 @@ ll2_new_context - Context which defines the lastlog2 environment. == DESCRIPTION -Defining lastlog2 context e.g. database file, which will be used for -any other lastlog2 calls. If _db_path_ is NULL, the default path defined +Defines lastlog2 context e.g. database file, which will be used for +any other lastlog2 call. If _db_path_ is NULL, the default path defined in _LL2_DEFAULT_DATABASE_ will be taken. -------------------------------------- @@ -32,7 +32,7 @@ ll2_context *context = ll2_new_context(db_path); == RETURN VALUE -Returns context which will used for all other lastlog2 library calls. +Returns context which will be used for all other lastlog2 library calls. This context should be released with _ll2_unref_context_ when it is not needed anymore. Returns NULL on an error. diff --git a/liblastlog2/man/ll2_read_all.3.adoc b/liblastlog2/man/ll2_read_all.3.adoc index 8f1a120a935..26617d6178f 100644 --- a/liblastlog2/man/ll2_read_all.3.adoc +++ b/liblastlog2/man/ll2_read_all.3.adoc @@ -46,7 +46,7 @@ Returns 0 on success, -ENOMEM or -1 on other failure. _error_ contains an error string if the return value is -1. _error_ is not guaranteed to contain an error string, could also be NULL. _error_ should be freed by the caller. -If lastlog2 database does not exist at all, the errno ENOENT has been set +If lastlog2 database does not exist at all, the errno ENOENT is set and can be checked. == AUTHORS diff --git a/liblastlog2/man/ll2_read_entry.3.adoc b/liblastlog2/man/ll2_read_entry.3.adoc index 3bef1a83788..063861284a5 100644 --- a/liblastlog2/man/ll2_read_entry.3.adoc +++ b/liblastlog2/man/ll2_read_entry.3.adoc @@ -42,7 +42,7 @@ Returns 0 on success, -ENOMEM or -1 on other failure. _error_ contains an error string if the return value is -1. _error_ is not guaranteed to contain an error string, could also be NULL. _error_ should be freed by the caller. -If lastlog2 database does not exist at all, the errno ENOENT has been set +If lastlog2 database does not exist at all, the errno ENOENT is set and can be checked. The evaluated values are returned by _ll_time_, _tty_, _rhost_ and _pam_service_. diff --git a/liblastlog2/man/ll2_remove_entry.3.adoc b/liblastlog2/man/ll2_remove_entry.3.adoc index f38ab66b336..622c3c65f55 100644 --- a/liblastlog2/man/ll2_remove_entry.3.adoc +++ b/liblastlog2/man/ll2_remove_entry.3.adoc @@ -9,7 +9,7 @@ == NAME -ll2_remove_entry - Remove all entries of an user. +ll2_remove_entry - Removes all entries of a user. == SYNOPSIS @@ -20,7 +20,7 @@ ll2_remove_entry - Remove all entries of an user. == DESCRIPTION -Removing all database entries, defined in _context_, with the user name _user_. +Removes all database entries, defined in _context_, with the user name _user_. If _context_ is NULL, the default database, defined in _LL2_DEFAULT_DATABASE_, will be taken. diff --git a/liblastlog2/man/ll2_rename_user.3.adoc b/liblastlog2/man/ll2_rename_user.3.adoc index 2202fe23164..d3687a9dca9 100644 --- a/liblastlog2/man/ll2_rename_user.3.adoc +++ b/liblastlog2/man/ll2_rename_user.3.adoc @@ -9,7 +9,7 @@ == NAME -ll2_rename_user - Renames an user entry. +ll2_rename_user - Renames a user entry. == SYNOPSIS @@ -20,7 +20,7 @@ ll2_rename_user - Renames an user entry. == DESCRIPTION -Changing user name from _user_ to _newname_ of one entry in +Changes user name from _user_ to _newname_ of one entry in database, which is defined by _context_. All other entries with the user _user_ will be deleted. If _context_ is NULL, the default database, defined in _LL2_DEFAULT_DATABASE_, diff --git a/liblastlog2/man/ll2_unref_context.3.adoc b/liblastlog2/man/ll2_unref_context.3.adoc index 5e52ca60ef1..937de5974ef 100644 --- a/liblastlog2/man/ll2_unref_context.3.adoc +++ b/liblastlog2/man/ll2_unref_context.3.adoc @@ -9,7 +9,7 @@ == NAME -ll2_unref_context - Freeing lastlog2 context. +ll2_unref_context - Frees lastlog2 context. == SYNOPSIS @@ -19,7 +19,7 @@ ll2_unref_context - Freeing lastlog2 context. == DESCRIPTION -Freeing lastlog2 context, which has been generated by +Frees lastlog2 context, which has been allocated by _ll2_new_context_. -------------------------------------- diff --git a/liblastlog2/man/ll2_update_login_time.3.adoc b/liblastlog2/man/ll2_update_login_time.3.adoc index 9cc43e164f2..3a7aed58998 100644 --- a/liblastlog2/man/ll2_update_login_time.3.adoc +++ b/liblastlog2/man/ll2_update_login_time.3.adoc @@ -9,7 +9,7 @@ == NAME -ll2_update_login_time - Writes an *new* entry with updated login time. +ll2_update_login_time - Writes a *new* entry with updated login time. == SYNOPSIS @@ -21,7 +21,7 @@ ll2_update_login_time - Writes an *new* entry with updated login time. == DESCRIPTION -Writes an *new* entry to database, defined in _context_, for user _user_. +Writes a *new* entry to database, defined in _context_, for user _user_. Time is set by _ll_time_ whereas the other values are taken from an already existing entry. If _context_ is NULL, the default database, defined in _LL2_DEFAULT_DATABASE_, @@ -39,7 +39,7 @@ int ret = ll2_update_login_time (NULL, user, login_time, &error); Returns 0 on success, -ENOMEM or -1 on other failure. _error_ contains an error string if the return value is -1. -_error_ is not guaranteed to contain an error string. It could also be NULL if the return value is -1. +_error_ is not guaranteed to contain an error string, could also be NULL. _error_ should be freed by the caller. == AUTHORS diff --git a/liblastlog2/man/ll2_write_entry.3.adoc b/liblastlog2/man/ll2_write_entry.3.adoc index 6dca1b5c661..fc8e92b7541 100644 --- a/liblastlog2/man/ll2_write_entry.3.adoc +++ b/liblastlog2/man/ll2_write_entry.3.adoc @@ -9,7 +9,7 @@ == NAME -ll2_write_entry - Write a new entry into the database. +ll2_write_entry - Writes a new entry into the database. == SYNOPSIS @@ -22,7 +22,7 @@ ll2_write_entry - Write a new entry into the database. == DESCRIPTION -Writes an new entry into database, which is defined in _context_. +Writes a new entry into database, which is defined in _context_. If _context_ is NULL, the default database, defined in _LL2_DEFAULT_DATABASE_, will be taken. diff --git a/liblastlog2/src/lastlog2.c b/liblastlog2/src/lastlog2.c index cafcb017ee4..2d3ab205017 100644 --- a/liblastlog2/src/lastlog2.c +++ b/liblastlog2/src/lastlog2.c @@ -39,7 +39,7 @@ #include "lastlog2P.h" #include "strutils.h" -/* Set the ll2 context/environment */ +/* Sets the ll2 context/environment. */ /* Returns the context or NULL if an error has happened. */ extern struct ll2_context * ll2_new_context(const char *db_path) { @@ -61,7 +61,7 @@ extern struct ll2_context * ll2_new_context(const char *db_path) return context; } -/* Release ll2 context/environment */ +/* Releases ll2 context/environment. */ extern void ll2_unref_context(struct ll2_context *context) { if (context) @@ -206,7 +206,8 @@ read_entry(sqlite3 *db, const char *user, return retval; } -/* reads 1 entry from database and returns that. Returns 0 on success, -ENOMEM or -1 on other failure. */ +/* Reads one entry from database and returns that. + Returns 0 on success, -ENOMEM or -1 on other failure. */ int ll2_read_entry(struct ll2_context *context, const char *user, int64_t *ll_time, char **tty, char **rhost, @@ -225,7 +226,8 @@ ll2_read_entry(struct ll2_context *context, const char *user, return retval; } -/* Write a new entry. Returns 0 on success, -ENOMEM or -1 on other failure. */ +/* Writes a new entry. + Returns 0 on success, -ENOMEM or -1 on other failure. */ static int write_entry(sqlite3 *db, const char *user, int64_t ll_time, const char *tty, const char *rhost, @@ -324,7 +326,8 @@ write_entry(sqlite3 *db, const char *user, return retval; } -/* Write a new entry. Returns 0 on success, -ENOMEM or -1 on other failure. */ +/* Writes a new entry. + Returns 0 on success, -ENOMEM or -1 on other failure. */ int ll2_write_entry(struct ll2_context *context, const char *user, int64_t ll_time, const char *tty, const char *rhost, @@ -343,7 +346,7 @@ ll2_write_entry(struct ll2_context *context, const char *user, return retval; } -/* Write a new entry with updated login time. +/* Writes a new entry with updated login time. Returns 0 on success, -ENOMEM or -1 on other failure. */ int ll2_update_login_time(struct ll2_context *context, const char *user, @@ -409,7 +412,7 @@ ll2_read_all(struct ll2_context *context, char **error) { sqlite3 *db; - char *err_msg = 0; + char *err_msg = NULL; int retval = 0; if ((retval = open_database_ro(context, &db, error)) != 0) @@ -431,7 +434,8 @@ ll2_read_all(struct ll2_context *context, return retval; } -/* Remove an user entry. Returns 0 on success, -ENOMEM or -1 on other failure. */ +/* Removes a user entry. + Returns 0 on success, -ENOMEM or -1 on other failure. */ static int remove_entry(sqlite3 *db, const char *user, char **error) { @@ -480,7 +484,8 @@ remove_entry(sqlite3 *db, const char *user, char **error) return retval; } -/* Remove an user entry. Returns 0 on success, -ENOMEM or -1 on other failure. */ +/* Removes a user entry. + Returns 0 on success, -ENOMEM or -1 on other failure. */ int ll2_remove_entry(struct ll2_context *context, const char *user, char **error) @@ -498,7 +503,8 @@ ll2_remove_entry(struct ll2_context *context, const char *user, return retval; } -/* Renames an user entry. Returns 0 on success, -ENOMEM or -1 on other failure. */ +/* Renames a user entry. + Returns 0 on success, -ENOMEM or -1 on other failure. */ int ll2_rename_user(struct ll2_context *context, const char *user, const char *newname, char **error) @@ -536,7 +542,7 @@ ll2_rename_user(struct ll2_context *context, const char *user, return retval; } -/* Import old lastlog file. +/* Imports old lastlog file. Returns 0 on success, -ENOMEM or -1 on other failure. */ int ll2_import_lastlog(struct ll2_context *context, const char *lastlog_file, diff --git a/misc-utils/lastlog2.8.adoc b/misc-utils/lastlog2.8.adoc index 99e322be338..c34af24123b 100644 --- a/misc-utils/lastlog2.8.adoc +++ b/misc-utils/lastlog2.8.adoc @@ -12,7 +12,7 @@ This file may be copied under the terms of the GNU General Public License. == NAME -lastlog2 - display date of last login for all users or a specific one +lastlog2 - displays date of last login for all users or a specific one == SYNOPSIS