diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 19:33:30 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 19:33:30 +0000 |
commit | c61e14d3a8412cd50d98aab604e607692c844c8a (patch) | |
tree | 4925aca0e6b64c8664ea2f3fdfa99a52dc93d5da /liblastlog2/src | |
parent | Adding upstream version 2.39.3. (diff) | |
download | util-linux-c61e14d3a8412cd50d98aab604e607692c844c8a.tar.xz util-linux-c61e14d3a8412cd50d98aab604e607692c844c8a.zip |
Adding upstream version 2.40.upstream/2.40
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'liblastlog2/src')
-rw-r--r-- | liblastlog2/src/Makemodule.am | 99 | ||||
-rw-r--r-- | liblastlog2/src/lastlog2.c | 595 | ||||
-rw-r--r-- | liblastlog2/src/lastlog2.h | 91 | ||||
-rw-r--r-- | liblastlog2/src/lastlog2P.h | 37 | ||||
-rw-r--r-- | liblastlog2/src/liblastlog2.sym | 13 | ||||
-rw-r--r-- | liblastlog2/src/tests/tst_dlopen.c | 40 | ||||
-rw-r--r-- | liblastlog2/src/tests/tst_pam_lastlog2_output.c | 115 | ||||
-rw-r--r-- | liblastlog2/src/tests/tst_remove_entry.c | 88 | ||||
-rw-r--r-- | liblastlog2/src/tests/tst_rename_user.c | 112 | ||||
-rw-r--r-- | liblastlog2/src/tests/tst_write_read_user.c | 165 | ||||
-rw-r--r-- | liblastlog2/src/tests/tst_y2038_ll2_read_all.c | 156 | ||||
-rw-r--r-- | liblastlog2/src/tests/tst_y2038_sqlite3_time.c | 83 |
12 files changed, 1594 insertions, 0 deletions
diff --git a/liblastlog2/src/Makemodule.am b/liblastlog2/src/Makemodule.am new file mode 100644 index 0000000..04b8e97 --- /dev/null +++ b/liblastlog2/src/Makemodule.am @@ -0,0 +1,99 @@ +# includes +lastlog2incdir = $(includedir)/liblastlog2 +lastlog2inc_HEADERS = liblastlog2/src/lastlog2.h + +usrlib_exec_LTLIBRARIES += liblastlog2.la + +liblastlog2_la_SOURCES = \ + liblastlog2/src/lastlog2.c \ + liblastlog2/src/lastlog2P.h + +EXTRA_liblastlog2_la_DEPENDENCIES = \ + liblastlog2/src/liblastlog2.sym + +liblastlog2_la_LIBADD = $(SQLITE3_LIBS) + +liblastlog2_la_CFLAGS = \ + $(AM_CFLAGS) \ + $(SOLIB_CFLAGS) \ + -I$(ul_liblastlog2_incdir) \ + -I$(top_srcdir)/liblastlog2/src + +liblastlog2_la_LDFLAGS = $(SOLIB_LDFLAGS) +if HAVE_VSCRIPT +liblastlog2_la_LDFLAGS += $(VSCRIPT_LDFLAGS),$(top_srcdir)/liblastlog2/src/liblastlog2.sym +endif +liblastlog2_la_LDFLAGS += -version-info $(LIBLASTLOG2_VERSION_INFO) + +EXTRA_DIST += liblastlog2/src/liblastlog2.sym + + +if BUILD_LIBLASTLOG2_TESTS +check_PROGRAMS += \ + test_lastlog2_dlopen \ + test_lastlog2_pam_lastlog2_output \ + test_lastlog2_remove_entry \ + test_lastlog2_rename_user \ + test_lastlog2_write_read_user \ + test_lastlog2_y2038_ll2_read_all \ + test_lastlog2_y2038_sqlite3_time + +lastlog2_tests_cflags = -DTEST_PROGRAM $(liblastlog2_la_CFLAGS) +lastlog2_tests_ldflags = -static +lastlog2_tests_ldadd = $(LDADD) liblastlog2.la $(SOLIB_LDFLAGS) $(SQLITE3_LIBS) + +test_lastlog2_dlopen_SOURCES = liblastlog2/src/tests/tst_dlopen.c +test_lastlog2_dlopen_CFLAGS = $(lastlog2_tests_cflags) +test_lastlog2_dlopen_LDFLAGS = $(lastlog2_tests_ldflags) -ldl +test_lastlog2_dlopen_LDADD = $(lastlog2_tests_ldadd) + +test_lastlog2_pam_lastlog2_output_SOURCES = liblastlog2/src/tests/tst_pam_lastlog2_output.c +test_lastlog2_pam_lastlog2_output_CFLAGS = $(lastlog2_tests_cflags) +test_lastlog2_pam_lastlog2_output_LDFLAGS = $(lastlog2_tests_ldflags) +test_lastlog2_pam_lastlog2_output_LDADD = $(lastlog2_tests_ldadd) + +test_lastlog2_remove_entry_SOURCES = liblastlog2/src/tests/tst_remove_entry.c +test_lastlog2_remove_entry_CFLAGS = $(lastlog2_tests_cflags) +test_lastlog2_remove_entry_LDFLAGS = $(lastlog2_tests_ldflags) +test_lastlog2_remove_entry_LDADD = $(lastlog2_tests_ldadd) + +test_lastlog2_rename_user_SOURCES = liblastlog2/src/tests/tst_rename_user.c +test_lastlog2_rename_user_CFLAGS = $(lastlog2_tests_cflags) +test_lastlog2_rename_user_LDFLAGS = $(lastlog2_tests_ldflags) +test_lastlog2_rename_user_LDADD = $(lastlog2_tests_ldadd) + +test_lastlog2_write_read_user_SOURCES = liblastlog2/src/tests/tst_write_read_user.c +test_lastlog2_write_read_user_CFLAGS = $(lastlog2_tests_cflags) +test_lastlog2_write_read_user_LDFLAGS = $(lastlog2_tests_ldflags) +test_lastlog2_write_read_user_LDADD = $(lastlog2_tests_ldadd) + +test_lastlog2_y2038_ll2_read_all_SOURCES = liblastlog2/src/tests/tst_y2038_ll2_read_all.c +test_lastlog2_y2038_ll2_read_all_CFLAGS = $(lastlog2_tests_cflags) +test_lastlog2_y2038_ll2_read_all_LDFLAGS = $(lastlog2_tests_ldflags) +test_lastlog2_y2038_ll2_read_all_LDADD = $(lastlog2_tests_ldadd) + +test_lastlog2_y2038_sqlite3_time_SOURCES = liblastlog2/src/tests/tst_y2038_sqlite3_time.c +test_lastlog2_y2038_sqlite3_time_CFLAGS = $(lastlog2_tests_cflags) +test_lastlog2_y2038_sqlite3_time_LDFLAGS = $(lastlog2_tests_ldflags) +test_lastlog2_y2038_sqlite3_time_LDADD = $(lastlog2_tests_ldadd) + +endif #BUILD_LIBLIBLASTLOG2_TESTS + + +# move lib from $(usrlib_execdir) to $(libdir) if needed +install-exec-hook-liblastlog2: + if test "$(usrlib_execdir)" != "$(libdir)" -a -f "$(DESTDIR)$(usrlib_execdir)/liblastlog2.so"; then \ + $(MKDIR_P) $(DESTDIR)$(libdir); \ + mv $(DESTDIR)$(usrlib_execdir)/liblastlog2.so.* $(DESTDIR)$(libdir); \ + so_img_name=$$(readlink $(DESTDIR)$(usrlib_execdir)/liblastlog2.so); \ + so_img_rel_target=$$(echo $(usrlib_execdir) | sed 's,\(^/\|\)[^/][^/]*,..,g'); \ + (cd $(DESTDIR)$(usrlib_execdir) && \ + rm -f liblastlog2.so && \ + $(LN_S) $$so_img_rel_target$(libdir)/$$so_img_name liblastlog2.so); \ + fi + +uninstall-hook-liblastlog2: + rm -f $(DESTDIR)$(libdir)/liblastlog2.so* + +INSTALL_EXEC_HOOKS += install-exec-hook-liblastlog2 +UNINSTALL_HOOKS += uninstall-hook-liblastlog2 diff --git a/liblastlog2/src/lastlog2.c b/liblastlog2/src/lastlog2.c new file mode 100644 index 0000000..744d41f --- /dev/null +++ b/liblastlog2/src/lastlog2.c @@ -0,0 +1,595 @@ +/* SPDX-License-Identifier: BSD-2-Clause + + Copyright (c) 2023, Thorsten Kukuk <kukuk@suse.com> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +#include <pwd.h> +#include <errno.h> +#include <time.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <limits.h> +#include <sys/stat.h> +#include <sqlite3.h> +#include <lastlog.h> + +#include "lastlog2P.h" +#include "strutils.h" + +/* Set 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) +{ + struct ll2_context *context = (struct ll2_context *)malloc(sizeof(struct ll2_context)); + + if (context) { + if (db_path) { + if ((context->lastlog2_path = strdup(db_path)) == NULL) { + free(context); + context = NULL; + } + } else { + if ((context->lastlog2_path = strdup(LL2_DEFAULT_DATABASE)) == NULL) { + free(context); + context = NULL; + } + } + } + return context; +} + +/* Release ll2 context/environment */ +extern void ll2_unref_context(struct ll2_context *context) +{ + if (context) + free(context->lastlog2_path); + free(context); +} + +/* Returns 0 on success, -ENOMEM or -1 on other failure. */ +static int +open_database_ro(struct ll2_context *context, sqlite3 **db, char **error) +{ + int ret = 0; + char *path = LL2_DEFAULT_DATABASE; + + if (context && context->lastlog2_path) + path = context->lastlog2_path; + + if (sqlite3_open_v2(path, db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) { + ret = -1; + if (error) + if (asprintf(error, "Cannot open database (%s): %s", + path, sqlite3_errmsg(*db)) < 0) + ret = -ENOMEM; + + sqlite3_close(*db); + } + + return ret; +} + +/* Returns 0 on success, -ENOMEM or -1 on other failure. */ +static int +open_database_rw(struct ll2_context *context, sqlite3 **db, char **error) +{ + int ret = 0; + char *path = LL2_DEFAULT_DATABASE; + + if (context && context->lastlog2_path) + path = context->lastlog2_path; + + if (sqlite3_open(path, db) != SQLITE_OK) { + ret = -1; + if (error) + if (asprintf(error, "Cannot create/open database (%s): %s", + path, sqlite3_errmsg(*db)) < 0) + ret = -ENOMEM; + + sqlite3_close(*db); + } + + return ret; +} + +/* Reads one entry from database and returns that. + Returns 0 on success, -ENOMEM or -1 on other failure. */ +static int +read_entry(sqlite3 *db, const char *user, + int64_t *ll_time, char **tty, char **rhost, + char **pam_service, char **error) +{ + int retval = 0; + sqlite3_stmt *res = NULL; + static const char *sql = "SELECT Name,Time,TTY,RemoteHost,Service FROM Lastlog2 WHERE Name = ?"; + + if (sqlite3_prepare_v2(db, sql, -1, &res, 0) != SQLITE_OK) { + retval = -1; + if (error) + if (asprintf(error, "Failed to execute statement: %s", + sqlite3_errmsg(db)) < 0) + retval = -ENOMEM; + goto out_read_entry; + } + + if (sqlite3_bind_text(res, 1, user, -1, SQLITE_STATIC) != SQLITE_OK) { + retval = -1; + if (error) + if (asprintf(error, "Failed to create search query: %s", + sqlite3_errmsg(db)) < 0) + retval = -ENOMEM; + goto out_read_entry; + } + + int step = sqlite3_step(res); + + if (step == SQLITE_ROW) { + const unsigned char *luser = sqlite3_column_text(res, 0); + const unsigned char *uc; + + if (strcmp((const char *)luser, user) != 0) { + retval = -1; + if (error) + if (asprintf(error, "Returned data is for %s, not %s", luser, user) < 0) + retval = -ENOMEM; + goto out_read_entry; + } + + if (ll_time) + *ll_time = sqlite3_column_int64(res, 1); + + if (tty) { + uc = sqlite3_column_text(res, 2); + if (uc != NULL && strlen((const char *)uc) > 0) + if ((*tty = strdup((const char *)uc)) == NULL) { + retval = -ENOMEM; + goto out_read_entry; + } + } + if (rhost) { + uc = sqlite3_column_text(res, 3); + if (uc != NULL && strlen((const char *)uc) > 0) + if ((*rhost = strdup((const char *)uc)) == NULL) { + retval = -ENOMEM; + goto out_read_entry; + } + } + if (pam_service) { + uc = sqlite3_column_text(res, 4); + if (uc != NULL && strlen((const char *)uc) > 0) + if ((*pam_service = strdup((const char *)uc)) == NULL) { + retval = -ENOMEM; + goto out_read_entry; + } + } + } else { + retval = -1; + if (error) + if (asprintf(error, "User '%s' not found (%d)", user, step) < 0) + retval = -ENOMEM; + } + +out_read_entry: + if (res) + sqlite3_finalize(res); + + return retval; +} + +/* reads 1 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, + char **pam_service, char **error) +{ + sqlite3 *db; + int retval; + + if ((retval = open_database_ro(context, &db, error)) != 0) + return retval; + + retval = read_entry(db, user, ll_time, tty, rhost, pam_service, error); + + sqlite3_close(db); + + return retval; +} + +/* Write 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, + const char *pam_service, char **error) +{ + int retval = 0; + char *err_msg = NULL; + sqlite3_stmt *res = NULL; + static const char *sql_table = "CREATE TABLE IF NOT EXISTS Lastlog2(Name TEXT PRIMARY KEY, Time INTEGER, TTY TEXT, RemoteHost TEXT, Service TEXT);"; + static const char *sql_replace = "REPLACE INTO Lastlog2 VALUES(?,?,?,?,?);"; + + if (sqlite3_exec(db, sql_table, 0, 0, &err_msg) != SQLITE_OK) { + retval = -1; + if (error) + if (asprintf(error, "SQL error: %s", err_msg) < 0) + retval = -ENOMEM; + + sqlite3_free(err_msg); + goto out_ll2_read_entry; + } + + if (sqlite3_prepare_v2(db, sql_replace, -1, &res, 0) != SQLITE_OK) { + retval = -1; + if (error) + if (asprintf(error, "Failed to execute statement: %s", + sqlite3_errmsg(db)) < 0) + retval = -ENOMEM; + goto out_ll2_read_entry; + } + + if (sqlite3_bind_text(res, 1, user, -1, SQLITE_STATIC) != SQLITE_OK) { + retval = -1; + if (error) + if (asprintf(error, "Failed to create replace statement for user: %s", + sqlite3_errmsg(db)) < 0) + retval = -ENOMEM; + goto out_ll2_read_entry; + } + + if (sqlite3_bind_int64(res, 2, ll_time) != SQLITE_OK) { + retval = -1; + if (error) + if (asprintf(error, "Failed to create replace statement for ll_time: %s", + sqlite3_errmsg(db)) < 0) + retval = -ENOMEM; + goto out_ll2_read_entry; + } + + if (sqlite3_bind_text(res, 3, tty, -1, SQLITE_STATIC) != SQLITE_OK) { + retval = -1; + if (error) + if (asprintf(error, "Failed to create replace statement for tty: %s", + sqlite3_errmsg(db)) < 0) + retval = -ENOMEM; + goto out_ll2_read_entry; + } + + if (sqlite3_bind_text(res, 4, rhost, -1, SQLITE_STATIC) != SQLITE_OK) { + retval = -1; + if (error) + if (asprintf(error, "Failed to create replace statement for rhost: %s", + sqlite3_errmsg(db)) < 0) + retval = -ENOMEM; + goto out_ll2_read_entry; + } + + if (sqlite3_bind_text(res, 5, pam_service, -1, SQLITE_STATIC) != SQLITE_OK) { + retval = -1; + if (error) + if (asprintf(error, "Failed to create replace statement for PAM service: %s", + sqlite3_errmsg(db)) < 0) + retval = -ENOMEM; + goto out_ll2_read_entry; + } + + int step = sqlite3_step(res); + + if (step != SQLITE_DONE) { + retval = -1; + if (error) + if (asprintf(error, "Delete statement did not return SQLITE_DONE: %d", + step) < 0) + retval = -ENOMEM; + goto out_ll2_read_entry; + } +out_ll2_read_entry: + if (res) + sqlite3_finalize(res); + + return retval; +} + +/* Write 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, + const char *pam_service, char **error) +{ + sqlite3 *db; + int retval; + + if ((retval = open_database_rw(context, &db, error)) != 0) + return retval; + + retval = write_entry(db, user, ll_time, tty, rhost, pam_service, error); + + sqlite3_close(db); + + return retval; +} + +/* Write 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, + int64_t ll_time, char **error) +{ + sqlite3 *db; + int retval; + char *tty; + char *rhost; + char *pam_service; + + if ((retval = open_database_rw(context , &db, error)) != 0) + return retval; + + if ((retval = read_entry(db, user, 0, &tty, &rhost, &pam_service, error)) != 0) { + sqlite3_close(db); + return retval; + } + + retval = write_entry(db, user, ll_time, tty, rhost, pam_service, error); + + sqlite3_close(db); + + free(tty); + free(rhost); + free(pam_service); + + return retval; +} + + +typedef int (*callback_f)(const char *user, int64_t ll_time, + const char *tty, const char *rhost, + const char *pam_service, const char *cb_error); + +static int +callback(void *cb_func, __attribute__((unused)) int argc, char **argv, __attribute__((unused)) char **azColName) +{ + char *endptr; + callback_f print_entry = cb_func; + + errno = 0; + char *cb_error = NULL; + int64_t ll_time = strtoll(argv[1], &endptr, 10); + if ((errno == ERANGE && (ll_time == INT64_MAX || ll_time == INT64_MIN)) + || (endptr == argv[1]) || (*endptr != '\0')) + if (asprintf(&cb_error, "Invalid numeric time entry for '%s': '%s'\n", argv[0], argv[1]) < 0) + return -1; + + print_entry(argv[0], ll_time, argv[2], argv[3], argv[4], cb_error); + free(cb_error); + + return 0; +} + +/* Reads all entries from database and calls the callback function for each entry. + Returns 0 on success, -ENOMEM or -1 on other failure. */ +int +ll2_read_all(struct ll2_context *context, + int (*cb_func)(const char *user, int64_t ll_time, + const char *tty, const char *rhost, + const char *pam_service, const char *cb_error), + char **error) +{ + sqlite3 *db; + char *err_msg = 0; + int retval = 0; + + if ((retval = open_database_ro(context, &db, error)) != 0) + return retval; + + static const char *sql = "SELECT Name,Time,TTY,RemoteHost,Service FROM Lastlog2 ORDER BY Name ASC"; + + if (sqlite3_exec(db, sql, callback, cb_func, &err_msg) != SQLITE_OK) { + retval = -1; + if (error) + if (asprintf(error, "SQL error: %s", err_msg) < 0) + retval = -ENOMEM; + + sqlite3_free(err_msg); + } + + sqlite3_close(db); + + return retval; +} + +/* Remove an user entry. Returns 0 on success, -ENOMEM or -1 on other failure. */ +static int +remove_entry(sqlite3 *db, const char *user, char **error) +{ + int retval = 0; + sqlite3_stmt *res = NULL; + static const char *sql = "DELETE FROM Lastlog2 WHERE Name = ?"; + + if (sqlite3_prepare_v2(db, sql, -1, &res, 0) != SQLITE_OK) { + if (error) + if (asprintf(error, "Failed to execute statement: %s", + sqlite3_errmsg(db)) < 0) + return -ENOMEM; + + return -1; + } + + if (sqlite3_bind_text(res, 1, user, -1, SQLITE_STATIC) != SQLITE_OK) { + retval = -1; + if (error) + if (asprintf(error, "Failed to create delete statement: %s", + sqlite3_errmsg(db)) < 0) + retval = -ENOMEM; + goto out_remove_entry; + } + + int step = sqlite3_step(res); + + if (step != SQLITE_DONE) { + retval = -1; + if (error) + if (asprintf(error, "Delete statement did not return SQLITE_DONE: %d", + step) < 0) + retval = -ENOMEM; + } +out_remove_entry: + if (res) + sqlite3_finalize(res); + + return retval; +} + +/* Remove an 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) +{ + sqlite3 *db; + int retval; + + if ((retval = open_database_rw(context, &db, error)) != 0) + return retval; + + retval = remove_entry(db, user, error); + + sqlite3_close(db); + + return retval; +} + +/* Renames an 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) +{ + sqlite3 *db; + int64_t ll_time; + char *tty; + char *rhost; + char *pam_service; + int retval; + + if ((retval = open_database_rw(context, &db, error)) != 0) + return retval; + + if ((retval = read_entry(db, user, &ll_time, &tty, &rhost, &pam_service, error) != 0)) { + sqlite3_close(db); + return retval; + } + + if ((retval = write_entry(db, newname, ll_time, tty, rhost, pam_service, error) != 0)) { + sqlite3_close(db); + free(tty); + free(rhost); + return retval; + } + + retval = remove_entry(db, user, error); + + sqlite3_close(db); + + free(tty); + free(rhost); + free(pam_service); + + return retval; +} + +/* Import 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, + char **error) +{ + const struct passwd *pw; + struct stat statll; + sqlite3 *db; + FILE *ll_fp; + int retval = 0; + + if ((retval = open_database_rw(context, &db, error)) != 0) + return retval; + + ll_fp = fopen(lastlog_file, "r"); + if (ll_fp == NULL) { + if (error && asprintf(error, "Failed to open '%s': %s", + lastlog_file, strerror(errno)) < 0) + return -ENOMEM; + + return -1; + } + + + if (fstat(fileno(ll_fp), &statll) != 0) { + retval = -1; + if (error && asprintf(error, "Cannot get size of '%s': %s", + lastlog_file, strerror(errno)) < 0) + retval = -ENOMEM; + + goto done; + } + + setpwent(); + while ((pw = getpwent()) != NULL ) { + off_t offset; + struct lastlog ll; + + offset = (off_t) pw->pw_uid * sizeof (ll); + + if ((offset + (off_t)sizeof(ll)) <= statll.st_size) { + if (fseeko(ll_fp, offset, SEEK_SET) == -1) + continue; /* Ignore seek error */ + + if (fread(&ll, sizeof(ll), 1, ll_fp) != 1) { + retval = -1; + if (error) + if (asprintf(error, "Failed to get the entry for UID '%lu'", + (unsigned long int)pw->pw_uid) < 0) + retval = -ENOMEM; + goto out_import_lastlog; + } + + if (ll.ll_time != 0) { + int64_t ll_time; + char tty[sizeof(ll.ll_line) + 1]; + char rhost[sizeof(ll.ll_host) + 1]; + + ll_time = ll.ll_time; + mem2strcpy(tty, ll.ll_line, sizeof(ll.ll_line), sizeof(tty)); + mem2strcpy(rhost, ll.ll_host, sizeof(ll.ll_host), sizeof(rhost)); + + if ((retval = write_entry(db, pw->pw_name, ll_time, tty, + rhost, NULL, error)) != 0) + goto out_import_lastlog; + } + } + } +out_import_lastlog: + endpwent(); + sqlite3_close(db); +done: + fclose(ll_fp); + + return retval; +} diff --git a/liblastlog2/src/lastlog2.h b/liblastlog2/src/lastlog2.h new file mode 100644 index 0000000..280f387 --- /dev/null +++ b/liblastlog2/src/lastlog2.h @@ -0,0 +1,91 @@ +/* SPDX-License-Identifier: BSD-2-Clause + + Copyright (c) 2023, Thorsten Kukuk <kukuk@suse.com> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef _LIBLASTLOG2_H +#define _LIBLASTLOG2_H + +#ifdef __cplusplus +extern "C" { +#endif + +#define LL2_DEFAULT_DATABASE _PATH_LOCALSTATEDIR "/lib/lastlog/lastlog2.db" + +#include <stdint.h> + +struct ll2_context; + +/* Set 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); + +/* Release ll2 context/environment */ +extern void ll2_unref_context(struct ll2_context *context); + +/* Writes a new entry. Returns 0 on success, -ENOMEM or -1 on other failure. */ +extern int ll2_write_entry (struct ll2_context *context, const char *user, + int64_t ll_time, const char *tty, + const char *rhost, const char *pam_service, + char **error); + +/* Calling a defined function for each entry. Returns 0 on success, -ENOMEM or -1 on other failure. */ +extern int ll2_read_all (struct ll2_context *context, + int (*callback)(const char *user, int64_t ll_time, + const char *tty, const char *rhost, + const char *pam_service, const char *cb_error), + char **error); + +/* Reads one entry from database and returns that. + Returns 0 on success, -ENOMEM or -1 on other failure. */ +extern int ll2_read_entry (struct ll2_context *context, const char *user, + int64_t *ll_time, char **tty, char **rhost, + char **pam_service, char **error); + +/* Write a new entry with updated login time. + Returns 0 on success, -ENOMEM or -1 on other failure. */ +extern int ll2_update_login_time (struct ll2_context *context, + const char *user, int64_t ll_time, + char **error); + +/* Remove an user entry. Returns 0 on success, -ENOMEM or -1 on other failure. */ +extern int ll2_remove_entry (struct ll2_context *context, const char *user, + char **error); + +/* Renames an user entry. Returns 0 on success, -ENOMEM or -1 on other failure. */ +extern int ll2_rename_user (struct ll2_context *context, const char *user, + const char *newname, char **error); + + +/* Import old lastlog file. + Returns 0 on success, -ENOMEM or -1 on other failure. */ +extern int ll2_import_lastlog (struct ll2_context *context, + const char *lastlog_file, char **error); + +#ifdef __cplusplus +} +#endif + +#endif /* _LIBLASTLOG2_H */ diff --git a/liblastlog2/src/lastlog2P.h b/liblastlog2/src/lastlog2P.h new file mode 100644 index 0000000..b39f660 --- /dev/null +++ b/liblastlog2/src/lastlog2P.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: BSD-2-Clause + + Copyright (c) 2024, Thorsten Kukuk <kukuk@suse.com> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef _LIBLASTLOG2_P_H +#define _LIBLASTLOG2_P_H + +#include "lastlog2.h" + +struct ll2_context { + char *lastlog2_path; +}; + +#endif /* _LIBLASTLOG2_P_H */ diff --git a/liblastlog2/src/liblastlog2.sym b/liblastlog2/src/liblastlog2.sym new file mode 100644 index 0000000..c21cced --- /dev/null +++ b/liblastlog2/src/liblastlog2.sym @@ -0,0 +1,13 @@ +LIBLASTLOG2_2_40 { + global: + ll2_new_context; + ll2_unref_context; + ll2_read_all; + ll2_read_entry; + ll2_remove_entry; + ll2_rename_user; + ll2_write_entry; + ll2_update_login_time; + ll2_import_lastlog; + local: *; +}; diff --git a/liblastlog2/src/tests/tst_dlopen.c b/liblastlog2/src/tests/tst_dlopen.c new file mode 100644 index 0000000..112564b --- /dev/null +++ b/liblastlog2/src/tests/tst_dlopen.c @@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later + + Copyright (C) Nalin Dahyabhai <nalin@redhat.com> 2003 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#include <dlfcn.h> +#include <stdio.h> +#include <limits.h> +#include <sys/stat.h> + +/* Simple program to see if dlopen() would succeed. */ +int main(int argc, char **argv) +{ + int i; + struct stat st; + char buf[PATH_MAX]; + + for (i = 1; i < argc; i++) { + if (dlopen(argv[i], RTLD_NOW)) { + fprintf(stdout, "dlopen() of \"%s\" succeeded.\n", + argv[i]); + } else { + snprintf(buf, sizeof(buf), "./%s", argv[i]); + if ((stat(buf, &st) == 0) && dlopen(buf, RTLD_NOW)) { + fprintf(stdout, "dlopen() of \"./%s\" " + "succeeded.\n", argv[i]); + } else { + fprintf(stdout, "dlopen() of \"%s\" failed: " + "%s\n", argv[i], dlerror()); + return 1; + } + } + } + return 0; +} diff --git a/liblastlog2/src/tests/tst_pam_lastlog2_output.c b/liblastlog2/src/tests/tst_pam_lastlog2_output.c new file mode 100644 index 0000000..1fd1eec --- /dev/null +++ b/liblastlog2/src/tests/tst_pam_lastlog2_output.c @@ -0,0 +1,115 @@ +/* SPDX-License-Identifier: BSD-2-Clause + + Copyright (c) 2023, Thorsten Kukuk <kukuk@suse.com> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +/* Test case: + Store defined data into the database, read it, create the time + string like pam_lastlog2 does, compare the result. +*/ + +#include <time.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> + +#include "lastlog2P.h" + +const char *expected = "Last login: Mon Mar 13 07:13:41 UTC 2023 from 192.168.122.1 on pts/0"; +const time_t login_time = 1678691621; +int +main(void) +{ + const char *user = "root"; + struct ll2_context *context = ll2_new_context("pam_lastlog2-output.db"); + int64_t ll_time = 0; + char *tty = NULL; + char *rhost = NULL; + char *date = NULL; + char the_time[256]; + char *error = NULL; + char *output = NULL; + + if (ll2_write_entry (context, user, login_time, "pts/0", + "192.168.122.1", NULL, &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "ll2_write_entry failed\n"); + ll2_unref_context(context); + return 1; + } + + if (ll2_read_entry (context, user, &ll_time, &tty, &rhost, + NULL, &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "Unknown error reading database %s", context ? context->lastlog2_path : "NULL"); + ll2_unref_context(context); + return 1; + } + + if (ll_time) { + struct tm *tm, tm_buf; + /* this is necessary if you compile this on architectures with + a 32bit time_t type. */ + time_t t_time = ll_time; + + if ((tm = localtime_r (&t_time, &tm_buf)) != NULL) { + strftime (the_time, sizeof (the_time), + " %a %b %e %H:%M:%S %Z %Y", tm); + date = the_time; + } + } + + if (asprintf (&output, "Last login:%s%s%s%s%s", + date ? date : "", + rhost ? " from " : "", + rhost ? rhost : "", + tty ? " on " : "", + tty ? tty : "") < 0) { + fprintf (stderr, "Out of memory!\n"); + ll2_unref_context(context); + return 1; + } + + if (strcmp (output, expected) != 0) { + fprintf (stderr, "Output '%s'\n does not match '%s'\n", + output, expected); + ll2_unref_context(context); + return 1; + } + + ll2_unref_context(context); + free (output); + free (tty); + free (rhost); + + return 0; +} diff --git a/liblastlog2/src/tests/tst_remove_entry.c b/liblastlog2/src/tests/tst_remove_entry.c new file mode 100644 index 0000000..39079c8 --- /dev/null +++ b/liblastlog2/src/tests/tst_remove_entry.c @@ -0,0 +1,88 @@ +/* SPDX-License-Identifier: BSD-2-Clause + + Copyright (c) 2023, Thorsten Kukuk <kukuk@suse.com> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +/* Test case: + Create an entry, delete that entry, and try to read entry again. + Reading the entry should fail. +*/ + +#include <time.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "lastlog2.h" + +int +main(void) +{ + const char *user = "user"; + struct ll2_context *context = ll2_new_context("tst-delete-user.db"); + int64_t ll_time = 0; + char *tty = NULL; + char *rhost = NULL; + char *service = NULL; + char *error = NULL; + + if (ll2_write_entry (context, user, time (NULL), "test-tty", + "localhost", "sshd", &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "ll2_write_entry failed\n"); + ll2_unref_context(context); + return 1; + } + + if (ll2_remove_entry (context, user, &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "ll2_remove_entry failed\n"); + ll2_unref_context(context); + return 1; + } + + /* this needs to fail, as the old entry shouldn't exist anymore. */ + if (ll2_read_entry (context, user, &ll_time, &tty, &rhost, &service, &error) == 0) { + fprintf (stderr, "Reading old user from database did not fail!\n"); + fprintf (stderr, "ll_time=%lld, tty='%s', rhost='%s', service='%s'\n", + (long long int)ll_time, tty, rhost, service); + ll2_unref_context(context); + return 1; + } + + ll2_unref_context(context); + free (error); + free (tty); + free (rhost); + free (service); + + return 0; +} diff --git a/liblastlog2/src/tests/tst_rename_user.c b/liblastlog2/src/tests/tst_rename_user.c new file mode 100644 index 0000000..a1788b5 --- /dev/null +++ b/liblastlog2/src/tests/tst_rename_user.c @@ -0,0 +1,112 @@ +/* SPDX-License-Identifier: BSD-2-Clause + + Copyright (c) 2023, Thorsten Kukuk <kukuk@suse.com> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +/* Test case: + Create an entry, rename that entry, and try to read the old and + new entry again. Reading the old entry should fail. +*/ + +#include <time.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "lastlog2P.h" + +int +main(void) +{ + const char *user = "user"; + const char *newname = "new"; + struct ll2_context *context = ll2_new_context("tst-rename-user.db"); + int64_t ll_time = 0; + char *tty = NULL; + char *rhost = NULL; + char *service = NULL; + char *error = NULL; + + if (ll2_write_entry (context, user, time (NULL), "test-tty", + "localhost", "test-service", &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } + else + fprintf (stderr, "ll2_write_entry failed\n"); + ll2_unref_context(context); + return 1; + } + + if (ll2_rename_user (context, user, newname, &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } + else + fprintf (stderr, "ll2_rename_user failed\n"); + ll2_unref_context(context); + return 1; + } + + /* this needs to fail, as the old entry shouldn't exist anymore. */ + if (ll2_read_entry (context, user, &ll_time, &tty, &rhost, + &service, &error) == 0) { + fprintf (stderr, "Reading old user from database did not fail!\n"); + fprintf (stderr, "ll_time=%lld, tty='%s', rhost='%s', service='%s'\n", + (long long int)ll_time, tty, rhost, service); + ll2_unref_context(context); + return 1; + } + + if (error) { + free (error); + error = NULL; + } + + if (ll2_read_entry (context, newname, &ll_time, &tty, &rhost, &service, + &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "Unknown error reading database %s", context->lastlog2_path); + ll2_unref_context(context); + return 1; + } + + if (strcmp (tty, "test-tty") != 0 || strcmp (rhost, "localhost") != 0 || + strcmp (service, "test-service") != 0) { + fprintf (stderr, "New entry data does not match old entry data!\n"); + } + + ll2_unref_context(context); + free (tty); + free (rhost); + free (service); + + return 0; +} diff --git a/liblastlog2/src/tests/tst_write_read_user.c b/liblastlog2/src/tests/tst_write_read_user.c new file mode 100644 index 0000000..dbf1db7 --- /dev/null +++ b/liblastlog2/src/tests/tst_write_read_user.c @@ -0,0 +1,165 @@ +/* SPDX-License-Identifier: BSD-2-Clause + + Copyright (c) 2023, Thorsten Kukuk <kukuk@suse.com> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +/* Test case: + Create an entry, rename that entry, and try to read the old and + new entry again. Reading the old entry should fail. +*/ + +#include <time.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +#include "lastlog2P.h" + +static int +test_args (struct ll2_context *context, const char *user, int64_t ll_time, + const char *tty, const char *rhost, const char *service) +{ + char *error = NULL; + int64_t res_time; + char *res_tty = NULL; + char *res_rhost = NULL; + char *res_service = NULL; + + if (ll2_write_entry (context, user, ll_time, tty, rhost, service, &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "ll2_write_entry failed\n"); + return 1; + } + + if (ll2_read_entry (context, user, &res_time, &res_tty, &res_rhost, &res_service, &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "Unknown error reading database %s", context->lastlog2_path); + return 1; + } + + if (ll_time != res_time) { + fprintf (stderr, "Wrong time: got %lld, expect %lld\n", + (long long int)res_time, (long long int)ll_time); + return 1; + } + + if ((tty == NULL && res_tty != NULL) || + (tty != NULL && res_tty == NULL) || + (tty != NULL && res_tty != NULL && strcmp (tty, res_tty) != 0)) { + fprintf (stderr, "Wrong tty: got %s, expect %s\n", tty, res_tty); + return 1; + } + + if ((rhost == NULL && res_rhost != NULL) || + (rhost != NULL && res_rhost == NULL) || + (rhost != NULL && res_rhost != NULL && strcmp (rhost, res_rhost) != 0)) { + fprintf (stderr, "Wrong rhost: got %s, expect %s\n", rhost, res_rhost); + return 1; + } + + if ((service == NULL && res_service != NULL) || + (service != NULL && res_service == NULL) || + (service != NULL && res_service != NULL && strcmp (service, res_service) != 0)) { + fprintf (stderr, "Wrong service: got %s, expect %s\n", service, res_service); + return 1; + } + + + free (res_tty); + free (res_rhost); + free (res_service); + + return 0; +} + +int +main(void) +{ + struct ll2_context *context = ll2_new_context("tst-write-read-user.db"); + char *error = NULL; + int64_t res_time; + char *res_tty = NULL; + char *res_rhost = NULL; + char *res_service = NULL; + + if (test_args (context, "user1", time (NULL), "test-tty", "localhost", "test") != 0) { + ll2_unref_context(context); + return 1; + } + if (test_args (context, "user2", 0, NULL, NULL, NULL) != 0) { + ll2_unref_context(context); + return 1; + } + if (test_args (context, "user3", time (NULL), NULL, NULL, NULL) != 0) { + ll2_unref_context(context); + return 1; + } + if (test_args (context, "user4", time (NULL), "test-tty", NULL, NULL) != 0) { + ll2_unref_context(context); + return 1; + } + if (test_args (context, "user5", time (NULL), NULL, "localhost", NULL) != 0) { + ll2_unref_context(context); + return 1; + } + + /* Checking errno if the db file does not exist */ + struct ll2_context *context_not_found = ll2_new_context("no_file"); + if (ll2_read_entry (context_not_found, "user", &res_time, &res_tty, &res_rhost, &res_service, &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "Couldn't read entries for all users\n"); + + if(errno) { + if (errno == ENOENT) + { + fprintf (stderr, "Returning the correct errno: %s\n", + strerror (errno)); + ll2_unref_context(context_not_found); + return 0; + } + fprintf (stderr, "errno: %s\n", + strerror (errno)); + } else { + fprintf (stderr, "errno: NULL\n"); + } + + ll2_unref_context(context_not_found); + ll2_unref_context(context); + return 1; + } + + ll2_unref_context(context); + return 0; +} diff --git a/liblastlog2/src/tests/tst_y2038_ll2_read_all.c b/liblastlog2/src/tests/tst_y2038_ll2_read_all.c new file mode 100644 index 0000000..014ae9d --- /dev/null +++ b/liblastlog2/src/tests/tst_y2038_ll2_read_all.c @@ -0,0 +1,156 @@ +/* SPDX-License-Identifier: BSD-2-Clause + + Copyright (c) 2023, Thorsten Kukuk <kukuk@suse.com> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +/* Test case: + Create an entry with an 3*INT32_MAX timestamp, store that, + read that via ll2_read_all callback again and make sure the + timestamp is correct. +*/ + +#include <time.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <errno.h> + +#include "lastlog2P.h" + +#define BIG_TIME_VALUE ((int64_t)3*INT32_MAX) + +const char *user = "y2038"; +const char *on_tty = "pts/test"; +const char *rhost = NULL; +const char *service = "sshd"; + +static int +check_y2038 (const char *res_user, int64_t ll_time, const char *res_tty, + const char *res_rhost, const char *res_service, const char *error) +{ + + if (strcmp (user, res_user) != 0) { + fprintf (stderr, "write/read entry user mismatch: written: %s, got: %s\n", + user, res_user); + exit (1); + } + + if (ll_time != BIG_TIME_VALUE) { + fprintf (stderr, "write/read entry time mismatch: written: %lld, got: %lld\n", + (long long int)BIG_TIME_VALUE, (long long int)ll_time); + exit (1); + } + + if (strcmp (on_tty, res_tty) != 0) { + fprintf (stderr, "write/read entry tty mismatch: written: %s, got: %s\n", + on_tty, res_tty); + exit (1); + } + + if (rhost != NULL) { + fprintf (stderr, "write/read entry rhost mismatch: written: NULL, got: %s\n", + res_rhost); + exit (1); + } + + if (strcmp (service, res_service) != 0) { + fprintf (stderr, "write/read entry service mismatch: written: %s, got: %s\n", + service, res_service); + exit (1); + } + + if (error != NULL) { + fprintf (stderr, "got error: %s\n", + error); + exit (1); + } + + return 0; +} + +int +main(void) +{ + struct ll2_context *context = ll2_new_context("y2038-ll2_read_all.db"); + char *error = NULL; + + remove (context->lastlog2_path); + + printf ("Big time value is: %lld\n", (long long int)BIG_TIME_VALUE); + + if (ll2_write_entry (context, user, BIG_TIME_VALUE, on_tty, rhost, service, + &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } + else + fprintf (stderr, "ll2_write_entry failed\n"); + ll2_unref_context(context); + return 1; + } + + if (ll2_read_all (context, check_y2038, &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "Couldn't read entries for all users\n"); + ll2_unref_context(context); + return 1; + } + + /* Checking errno if the db file does not exist */ + remove (context->lastlog2_path); + + if (ll2_read_all (context, check_y2038, &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "Couldn't read entries for all users\n"); + + if(errno) { + if (errno == ENOENT) + { + fprintf (stderr, "Returning the correct errno: %s\n", + strerror (errno)); + ll2_unref_context(context); + return 0; + } + fprintf (stderr, "errno: %s\n", + strerror (errno)); + } else { + fprintf (stderr, "errno: NULL\n"); + } + + ll2_unref_context(context); + return 1; + } + + ll2_unref_context(context); + return 0; +} diff --git a/liblastlog2/src/tests/tst_y2038_sqlite3_time.c b/liblastlog2/src/tests/tst_y2038_sqlite3_time.c new file mode 100644 index 0000000..a296634 --- /dev/null +++ b/liblastlog2/src/tests/tst_y2038_sqlite3_time.c @@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: BSD-2-Clause + + Copyright (c) 2023, Thorsten Kukuk <kukuk@suse.com> + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. +*/ + +/* Test case: + Create an entry with an INT64_MAX-1000 timestamp, store that, + read that again and make sure the timestamp is correct. +*/ + +#include <time.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> + +#include "lastlog2P.h" + +#define BIG_TIME_VALUE (INT64_MAX - 1000) + +int +main(void) +{ + const char *user = "y2038"; + struct ll2_context *context = ll2_new_context("y2038-sqlite3-time.db"); + int64_t ll_time = 0; + char *error = NULL; + + printf ("Big time value is: %lld\n", (long long int)BIG_TIME_VALUE); + + if (ll2_write_entry (context, user, BIG_TIME_VALUE, NULL, NULL, + NULL, &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "ll2_write_entry failed\n"); + ll2_unref_context(context); + return 1; + } + + if (ll2_read_entry (context, user, &ll_time, NULL, NULL, NULL, + &error) != 0) { + if (error) { + fprintf (stderr, "%s\n", error); + free (error); + } else + fprintf (stderr, "Unknown error reading database %s", context->lastlog2_path); + ll2_unref_context(context); + return 1; + } + + if (ll_time != BIG_TIME_VALUE) { + fprintf (stderr, "write/read entry time mismatch: written: %lld, got: %lld\n", + (long long int)BIG_TIME_VALUE, (long long int)ll_time); + ll2_unref_context(context); + return 1; + } + + ll2_unref_context(context); + return 0; +} |