Debian Codegraph @

liblastlog2/src/lastlog2.c

raw
1 /* SPDX-License-Identifier: BSD-2-Clause
2
3 Copyright (c) 2023, Thorsten Kukuk <[email protected]>
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <pwd.h>
29 #include <errno.h>
30 #include <time.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <sys/stat.h>
36 #include <sqlite3.h>
37 #include <lastlog.h>
38
39 #include "lastlog2P.h"
40 #include "strutils.h"
41
42 #define LASTLOG2_BUSY_TIMEOUT 3000
43
44 static int
45 set_busy_timeout(sqlite3 *db, const char *path, char **error)
46 {
47 int ret = 0;
48
49 if (sqlite3_busy_timeout(db, LASTLOG2_BUSY_TIMEOUT) != SQLITE_OK) {
50 ret = -1;
51 if (error && asprintf(error, "Cannot set busy timeout (%s): %s",
52 path, sqlite3_errmsg(db)) < 0)
53 ret = -ENOMEM;
54 }
55
56 return ret;
57 }
58
59 /* Sets the ll2 context/environment. */
60 /* Returns the context or NULL if an error has happened. */
61 extern struct ll2_context * ll2_new_context(const char *db_path)
62 {
63 struct ll2_context *context = (struct ll2_context *)malloc(sizeof(struct ll2_context));
64
65 if (context) {
66 if (db_path) {
67 if ((context->lastlog2_path = strdup(db_path)) == NULL) {
68 free(context);
69 context = NULL;
70 }
71 } else {
72 if ((context->lastlog2_path = strdup(LL2_DEFAULT_DATABASE)) == NULL) {
73 free(context);
74 context = NULL;
75 }
76 }
77 }
78 return context;
79 }
80
81 /* Releases ll2 context/environment. */
82 extern void ll2_unref_context(struct ll2_context *context)
83 {
84 if (context)
85 free(context->lastlog2_path);
86 free(context);
87 }
88
89 /* Returns 0 on success, -ENOMEM or -1 on other failure. */
90 static int
91 open_database_ro(struct ll2_context *context, sqlite3 **db, char **error)
92 {
93 int ret = 0;
94 char *path = LL2_DEFAULT_DATABASE;
95
96 if (context && context->lastlog2_path)
97 path = context->lastlog2_path;
98
99 if (sqlite3_open_v2(path, db, SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
100 ret = -1;
101 if (error)
102 if (asprintf(error, "Cannot open database (%s): %s",
103 path, sqlite3_errmsg(*db)) < 0)
104 ret = -ENOMEM;
105
106 sqlite3_close(*db);
107 return ret;
108 }
109
110 ret = set_busy_timeout(*db, path, error);
111 if (ret != 0) {
112 sqlite3_close(*db);
113 return ret;
114 }
115
116 return ret;
117 }
118
119 /* Returns 0 on success, -ENOMEM or -1 on other failure. */
120 static int
121 open_database_rw(struct ll2_context *context, sqlite3 **db, char **error)
122 {
123 int ret = 0;
124 char *path = LL2_DEFAULT_DATABASE;
125
126 if (context && context->lastlog2_path)
127 path = context->lastlog2_path;
128
129 if (sqlite3_open(path, db) != SQLITE_OK) {
130 ret = -1;
131 if (error)
132 if (asprintf(error, "Cannot create/open database (%s): %s",
133 path, sqlite3_errmsg(*db)) < 0)
134 ret = -ENOMEM;
135
136 sqlite3_close(*db);
137 return ret;
138 }
139
140 ret = set_busy_timeout(*db, path, error);
141 if (ret != 0) {
142 sqlite3_close(*db);
143 return ret;
144 }
145
146 return ret;
147 }
148
149 /* Reads one entry from database and returns that.
150 Returns 0 on success, -ENOMEM or -1 on other failure. */
151 static int
152 read_entry(sqlite3 *db, const char *user,
153 int64_t *ll_time, char **tty, char **rhost,
154 char **pam_service, char **error)
155 {
156 int retval = 0;
157 sqlite3_stmt *res = NULL;
158 static const char *sql = "SELECT Name,Time,TTY,RemoteHost,Service FROM Lastlog2 WHERE Name = ?";
159
160 if (sqlite3_prepare_v2(db, sql, -1, &res, 0) != SQLITE_OK) {
161 retval = -1;
162 if (error)
163 if (asprintf(error, "Failed to execute statement: %s",
164 sqlite3_errmsg(db)) < 0)
165 retval = -ENOMEM;
166 goto out_read_entry;
167 }
168
169 if (sqlite3_bind_text(res, 1, user, -1, SQLITE_STATIC) != SQLITE_OK) {
170 retval = -1;
171 if (error)
172 if (asprintf(error, "Failed to create search query: %s",
173 sqlite3_errmsg(db)) < 0)
174 retval = -ENOMEM;
175 goto out_read_entry;
176 }
177
178 int step = sqlite3_step(res);
179
180 if (step == SQLITE_ROW) {
181 const unsigned char *luser = sqlite3_column_text(res, 0);
182 const unsigned char *uc;
183
184 if (strcmp((const char *)luser, user) != 0) {
185 retval = -1;
186 if (error)
187 if (asprintf(error, "Returned data is for %s, not %s", luser, user) < 0)
188 retval = -ENOMEM;
189 goto out_read_entry;
190 }
191
192 if (ll_time)
193 *ll_time = sqlite3_column_int64(res, 1);
194
195 if (tty) {
196 uc = sqlite3_column_text(res, 2);
197 if (uc != NULL && strlen((const char *)uc) > 0)
198 if ((*tty = strdup((const char *)uc)) == NULL) {
199 retval = -ENOMEM;
200 goto out_read_entry;
201 }
202 }
203 if (rhost) {
204 uc = sqlite3_column_text(res, 3);
205 if (uc != NULL && strlen((const char *)uc) > 0)
206 if ((*rhost = strdup((const char *)uc)) == NULL) {
207 retval = -ENOMEM;
208 goto out_read_entry;
209 }
210 }
211 if (pam_service) {
212 uc = sqlite3_column_text(res, 4);
213 if (uc != NULL && strlen((const char *)uc) > 0)
214 if ((*pam_service = strdup((const char *)uc)) == NULL) {
215 retval = -ENOMEM;
216 goto out_read_entry;
217 }
218 }
219 } else if (step == SQLITE_DONE) {
220 retval = -ENOENT;
221 } else if (step == SQLITE_BUSY) {
222 retval = -1;
223 if (error)
224 if ((*error = strdup ("Database busy")) == NULL)
225 retval = -ENOMEM;
226 } else if (step == SQLITE_ERROR) {
227 retval = -1;
228 if (error)
229 if (asprintf (error, "Error stepping through database: %s", sqlite3_errmsg (db)) < 0)
230 retval = -ENOMEM;
231 }
232
233 out_read_entry:
234 if (res)
235 sqlite3_finalize(res);
236
237 return retval;
238 }
239
240 /* Reads one entry from database and returns that.
241 Returns 0 on success, -ENOMEM or -1 on other failure. */
242 int
243 ll2_read_entry(struct ll2_context *context, const char *user,
244 int64_t *ll_time, char **tty, char **rhost,
245 char **pam_service, char **error)
246 {
247 sqlite3 *db;
248 int retval;
249
250 retval = open_database_ro(context, &db, error);
251 if (retval != 0)
252 return retval;
253
254 retval = read_entry(db, user, ll_time, tty, rhost, pam_service, error);
255
256 sqlite3_close(db);
257
258 return retval;
259 }
260
261 /* Writes a new entry.
262 Returns 0 on success, -ENOMEM or -1 on other failure. */
263 static int
264 write_entry(sqlite3 *db, const char *user,
265 int64_t ll_time, const char *tty, const char *rhost,
266 const char *pam_service, char **error)
267 {
268 int retval = 0;
269 char *err_msg = NULL;
270 sqlite3_stmt *res = NULL;
271 static const char *sql_table = "CREATE TABLE IF NOT EXISTS Lastlog2(Name TEXT PRIMARY KEY, Time INTEGER, TTY TEXT, RemoteHost TEXT, Service TEXT);";
272 static const char *sql_replace = "REPLACE INTO Lastlog2 VALUES(?,?,?,?,?);";
273
274 if (sqlite3_exec(db, sql_table, 0, 0, &err_msg) != SQLITE_OK) {
275 retval = -1;
276 if (error)
277 if (asprintf(error, "SQL error: %s", err_msg) < 0)
278 retval = -ENOMEM;
279
280 sqlite3_free(err_msg);
281 goto out_ll2_read_entry;
282 }
283
284 if (sqlite3_prepare_v2(db, sql_replace, -1, &res, 0) != SQLITE_OK) {
285 retval = -1;
286 if (error)
287 if (asprintf(error, "Failed to execute statement: %s",
288 sqlite3_errmsg(db)) < 0)
289 retval = -ENOMEM;
290 goto out_ll2_read_entry;
291 }
292
293 if (sqlite3_bind_text(res, 1, user, -1, SQLITE_STATIC) != SQLITE_OK) {
294 retval = -1;
295 if (error)
296 if (asprintf(error, "Failed to create replace statement for user: %s",
297 sqlite3_errmsg(db)) < 0)
298 retval = -ENOMEM;
299 goto out_ll2_read_entry;
300 }
301
302 if (sqlite3_bind_int64(res, 2, ll_time) != SQLITE_OK) {
303 retval = -1;
304 if (error)
305 if (asprintf(error, "Failed to create replace statement for ll_time: %s",
306 sqlite3_errmsg(db)) < 0)
307 retval = -ENOMEM;
308 goto out_ll2_read_entry;
309 }
310
311 if (sqlite3_bind_text(res, 3, tty, -1, SQLITE_STATIC) != SQLITE_OK) {
312 retval = -1;
313 if (error)
314 if (asprintf(error, "Failed to create replace statement for tty: %s",
315 sqlite3_errmsg(db)) < 0)
316 retval = -ENOMEM;
317 goto out_ll2_read_entry;
318 }
319
320 if (sqlite3_bind_text(res, 4, rhost, -1, SQLITE_STATIC) != SQLITE_OK) {
321 retval = -1;
322 if (error)
323 if (asprintf(error, "Failed to create replace statement for rhost: %s",
324 sqlite3_errmsg(db)) < 0)
325 retval = -ENOMEM;
326 goto out_ll2_read_entry;
327 }
328
329 if (sqlite3_bind_text(res, 5, pam_service, -1, SQLITE_STATIC) != SQLITE_OK) {
330 retval = -1;
331 if (error)
332 if (asprintf(error, "Failed to create replace statement for PAM service: %s",
333 sqlite3_errmsg(db)) < 0)
334 retval = -ENOMEM;
335 goto out_ll2_read_entry;
336 }
337
338 int step = sqlite3_step(res);
339
340 if (step != SQLITE_DONE) {
341 retval = -1;
342 if (error) {
343 if (step == SQLITE_ERROR) {
344 if (asprintf (error, "Failed to write entry: %s",
345 sqlite3_errmsg (db)) < 0)
346 retval = -ENOMEM;
347 } else {
348 if (asprintf (error, "Write entry did not return SQLITE_DONE: %d",
349 step) < 0)
350 retval = -ENOMEM;
351 }
352 }
353 }
354 out_ll2_read_entry:
355 if (res)
356 sqlite3_finalize(res);
357
358 return retval;
359 }
360
361 /* Writes a new entry.
362 Returns 0 on success, -ENOMEM or -1 on other failure. */
363 int
364 ll2_write_entry(struct ll2_context *context, const char *user,
365 int64_t ll_time, const char *tty, const char *rhost,
366 const char *pam_service, char **error)
367 {
368 sqlite3 *db;
369 int retval;
370
371 retval = open_database_rw(context, &db, error);
372 if (retval != 0)
373 return retval;
374
375 retval = write_entry(db, user, ll_time, tty, rhost, pam_service, error);
376
377 sqlite3_close(db);
378
379 return retval;
380 }
381
382 /* Writes a new entry with updated login time.
383 Returns 0 on success, -ENOMEM or -1 on other failure. */
384 int
385 ll2_update_login_time(struct ll2_context *context, const char *user,
386 int64_t ll_time, char **error)
387 {
388 sqlite3 *db;
389 int retval;
390 char *tty;
391 char *rhost;
392 char *pam_service;
393
394 retval = open_database_rw(context , &db, error);
395 if (retval != 0)
396 return retval;
397
398 retval = read_entry(db, user, 0, &tty, &rhost, &pam_service, error);
399 if (retval != 0) {
400 sqlite3_close(db);
401 return retval;
402 }
403
404 retval = write_entry(db, user, ll_time, tty, rhost, pam_service, error);
405
406 sqlite3_close(db);
407
408 free(tty);
409 free(rhost);
410 free(pam_service);
411
412 return retval;
413 }
414
415
416 typedef int (*callback_f)(const char *user, int64_t ll_time,
417 const char *tty, const char *rhost,
418 const char *pam_service, const char *cb_error);
419
420 static int
421 callback(void *cb_func, __attribute__((unused)) int argc, char **argv, __attribute__((unused)) char **azColName)
422 {
423 char *endptr;
424 callback_f print_entry = cb_func;
425
426 errno = 0;
427 char *cb_error = NULL;
428 int64_t ll_time = strtoll(argv[1], &endptr, 10);
429 if ((errno == ERANGE && (ll_time == INT64_MAX || ll_time == INT64_MIN))
430 || (endptr == argv[1]) || (*endptr != '\0'))
431 if (asprintf(&cb_error, "Invalid numeric time entry for '%s': '%s'\n", argv[0], argv[1]) < 0)
432 return -1;
433
434 print_entry(argv[0], ll_time, argv[2], argv[3], argv[4], cb_error);
435 free(cb_error);
436
437 return 0;
438 }
439
440 /* Reads all entries from database and calls the callback function for each entry.
441 Returns 0 on success, -ENOMEM or -1 on other failure. */
442 int
443 ll2_read_all(struct ll2_context *context,
444 int (*cb_func)(const char *user, int64_t ll_time,
445 const char *tty, const char *rhost,
446 const char *pam_service, const char *cb_error),
447 char **error)
448 {
449 sqlite3 *db;
450 char *err_msg = NULL;
451 int retval = 0;
452
453 retval = open_database_ro(context, &db, error);
454 if (retval != 0)
455 return retval;
456
457 static const char *sql = "SELECT Name,Time,TTY,RemoteHost,Service FROM Lastlog2 ORDER BY Name ASC";
458
459 if (sqlite3_exec(db, sql, callback, cb_func, &err_msg) != SQLITE_OK) {
460 retval = -1;
461 if (error)
462 if (asprintf(error, "SQL error: %s", err_msg) < 0)
463 retval = -ENOMEM;
464
465 sqlite3_free(err_msg);
466 }
467
468 sqlite3_close(db);
469
470 return retval;
471 }
472
473 /* Removes a user entry.
474 Returns 0 on success, -ENOMEM or -1 on other failure. */
475 static int
476 remove_entry(sqlite3 *db, const char *user, char **error)
477 {
478 int retval = 0;
479 sqlite3_stmt *res = NULL;
480 static const char *sql = "DELETE FROM Lastlog2 WHERE Name = ?";
481
482 if (sqlite3_prepare_v2(db, sql, -1, &res, 0) != SQLITE_OK) {
483 if (error)
484 if (asprintf(error, "Failed to execute statement: %s",
485 sqlite3_errmsg(db)) < 0)
486 return -ENOMEM;
487
488 return -1;
489 }
490
491 if (sqlite3_bind_text(res, 1, user, -1, SQLITE_STATIC) != SQLITE_OK) {
492 retval = -1;
493 if (error)
494 if (asprintf(error, "Failed to create delete statement: %s",
495 sqlite3_errmsg(db)) < 0)
496 retval = -ENOMEM;
497 goto out_remove_entry;
498 }
499
500 int step = sqlite3_step(res);
501
502 if (step != SQLITE_DONE) {
503 retval = -1;
504 if (error) {
505 if (step == SQLITE_ERROR) {
506 if (asprintf (error, "Delete statement failed: %s",
507 sqlite3_errmsg (db)) < 0)
508 retval = -ENOMEM;
509 } else {
510 if (asprintf (error, "Delete statement did not return SQLITE_DONE: %d",
511 step) < 0)
512 retval = -ENOMEM;
513 }
514 }
515 }
516 out_remove_entry:
517 if (res)
518 sqlite3_finalize(res);
519
520 return retval;
521 }
522
523 /* Removes a user entry.
524 Returns 0 on success, -ENOMEM or -1 on other failure. */
525 int
526 ll2_remove_entry(struct ll2_context *context, const char *user,
527 char **error)
528 {
529 sqlite3 *db;
530 int retval;
531
532 retval = open_database_rw(context, &db, error);
533 if (retval != 0)
534 return retval;
535
536 retval = remove_entry(db, user, error);
537
538 sqlite3_close(db);
539
540 return retval;
541 }
542
543 /* Renames a user entry.
544 Returns 0 on success, -ENOMEM or -1 on other failure. */
545 int
546 ll2_rename_user(struct ll2_context *context, const char *user,
547 const char *newname, char **error)
548 {
549 sqlite3 *db;
550 int64_t ll_time;
551 char *tty;
552 char *rhost;
553 char *pam_service;
554 int retval;
555
556 retval = open_database_rw(context, &db, error);
557 if (retval != 0)
558 return retval;
559
560 retval = read_entry(db, user, &ll_time, &tty, &rhost, &pam_service, error);
561 if (retval != 0) {
562 sqlite3_close(db);
563 return retval;
564 }
565
566 retval = write_entry(db, newname, ll_time, tty, rhost, pam_service, error);
567 if (retval != 0) {
568 sqlite3_close(db);
569 free(tty);
570 free(rhost);
571 return retval;
572 }
573
574 retval = remove_entry(db, user, error);
575
576 sqlite3_close(db);
577
578 free(tty);
579 free(rhost);
580 free(pam_service);
581
582 return retval;
583 }
584
585 /* Imports old lastlog file.
586 Returns 0 on success, -ENOMEM or -1 on other failure. */
587 int
588 ll2_import_lastlog(struct ll2_context *context, const char *lastlog_file,
589 char **error)
590 {
591 const struct passwd *pw;
592 struct stat statll;
593 sqlite3 *db;
594 FILE *ll_fp;
595 int retval = 0;
596
597 retval = open_database_rw(context, &db, error);
598 if (retval != 0)
599 return retval;
600
601 ll_fp = fopen(lastlog_file, "r");
602 if (ll_fp == NULL) {
603 if (error && asprintf(error, "Failed to open '%s': %m",
604 lastlog_file) < 0)
605 return -ENOMEM;
606
607 return -1;
608 }
609
610
611 if (fstat(fileno(ll_fp), &statll) != 0) {
612 retval = -1;
613 if (error && asprintf(error, "Cannot get size of '%s': %m",
614 lastlog_file) < 0)
615 retval = -ENOMEM;
616
617 goto done;
618 }
619
620 setpwent();
621 while ((pw = getpwent()) != NULL ) {
622 off_t offset;
623 struct lastlog ll;
624
625 offset = (off_t) pw->pw_uid * sizeof (ll);
626
627 if ((offset + (off_t)sizeof(ll)) <= statll.st_size) {
628 if (fseeko(ll_fp, offset, SEEK_SET) == -1)
629 continue; /* Ignore seek error */
630
631 if (fread(&ll, sizeof(ll), 1, ll_fp) != 1) {
632 retval = -1;
633 if (error)
634 if (asprintf(error, "Failed to get the entry for UID '%lu'",
635 (unsigned long int)pw->pw_uid) < 0)
636 retval = -ENOMEM;
637 goto out_import_lastlog;
638 }
639
640 if (ll.ll_time != 0) {
641 int64_t ll_time;
642 char tty[sizeof(ll.ll_line) + 1];
643 char rhost[sizeof(ll.ll_host) + 1];
644
645 ll_time = ll.ll_time;
646 mem2strcpy(tty, ll.ll_line, sizeof(ll.ll_line), sizeof(tty));
647 mem2strcpy(rhost, ll.ll_host, sizeof(ll.ll_host), sizeof(rhost));
648
649 if ((retval = write_entry(db, pw->pw_name, ll_time, tty,
650 rhost, NULL, error)) != 0)
651 goto out_import_lastlog;
652 }
653 }
654 }
655 out_import_lastlog:
656 endpwent();
657 sqlite3_close(db);
658 done:
659 fclose(ll_fp);
660
661 return retval;
662 }