1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
From: =?utf-8?q?Christian_G=C3=B6ttsche?= <cgzones@googlemail.com>
Date: Fri, 24 Jul 2020 16:01:15 +0200
Subject: chkwtmp
Minor fixes to avoid compiler warnings and overflows.
a) use strncpy not memcopy when setting wtmpfile
From: =?utf-8?q?Christian_G=C3=B6ttsche?= <cgzones@googlemail.com>
Date: Fri, 24 Jul 2020 16:01:15 +0200
b) include stdlib.h
From: Giuseppe Iuculano <giuseppe@iuculano.it>
Date: Sun, 9 Jul 2017 18:42:55 +0200
Items c-g from richard.lewis.debian@googlemail.com, Nov 2024
c) chkwtmp: fix compilation errors and indentation: declare args
of printit(), fix indentation (tabs) and remove trailing whitespace
d) Fix arg parsing and encoding
- arg passing code should not assume the file is 127 bytes long.
This could actually read bits of envp into wtmpfile.
- Fix accents in comment to be valid utf8
e) Ensure return code is not too large - should be 0..255, not an arbitrary int.
f) Remove duplicate #ifdefs
g) Do not silently do nothing on an unsupported platform
Forwarded: yes
(Forwarded by email: 21 Dec 2024)
---
chkwtmp.c | 41 +++++++++++++++++++----------------------
1 file changed, 19 insertions(+), 22 deletions(-)
diff --git a/chkwtmp.c b/chkwtmp.c
index c207721..66515ba 100644
--- a/chkwtmp.c
+++ b/chkwtmp.c
@@ -15,14 +15,16 @@
Nelson Murilo, nmurilo@gmail.com
07/08/04 - fix del counter value (Thanks to Dietrich Raisin)
Nelson Murilo, nmurilo@gmail.com
- 09/12/05 - fix Segfault (Thanks to Jrmie Andri)
+ 09/12/05 - fix Segfault (Thanks to Jérémie Andréi)
Nelson Murilo, nmurilo@gmail.com
*/
-#if __FreeBSD__ > 9
-int main () { return 0; }
-#else
#include <stdio.h>
+
+#if __FreeBSD__ > 9
+int main (void){ fprintf(stderr,"Unsupported operating system\n"); return 1; }
+#else
+
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
@@ -33,20 +35,15 @@ int main () { return 0; }
#ifdef SOLARIS2
#include <fcntl.h>
#endif
+#include <stdlib.h>
-#ifdef __FreeBSD__
-#define WTMP_FILENAME "/var/log/wtmp"
-#else
#ifndef WTMP_FILENAME
#define WTMP_FILENAME "/var/log/wtmp"
#endif
-#endif
-void printit(counter, start, end)
-int counter;
-long start,end;
+void printit(int counter, long start, long end)
{
- char buffer[30];
+ char buffer[30];
printf("%d deletion(s) between ", counter);
strncpy(buffer, ctime( (time_t *) &start), 30);
@@ -55,27 +52,27 @@ long start,end;
}
-int main(int argc, char*argv[]) {
+int main(int argc, char* argv[]) {
int filehandle;
struct utmp utmp_ent;
struct timeval mytime;
struct timezone dummy;
long start_time, act_time;
int del_counter, t_del;
- char wtmpfile[128];
+ char wtmpfile[128];
del_counter=t_del=0;
start_time=0;
gettimeofday(&mytime, &dummy);
- act_time=mytime.tv_sec;
- wtmpfile[127]='\0';
- memcpy(wtmpfile, WTMP_FILENAME, 127);
- if ( argc == 3 && !memcmp("-f", argv[1], 2) && *argv[2])
- memcpy(wtmpfile, argv[2], 127);
+ act_time=mytime.tv_sec;
+ wtmpfile[127]='\0';
+ strncpy(wtmpfile, WTMP_FILENAME, 127);
+ if ( argc == 3 && !memcmp("-f", argv[1], 2) && *argv[2] && strlen(argv[2])<127)
+ memcpy(wtmpfile, argv[2], strlen(argv[2])+1);
if ((filehandle=open(wtmpfile,O_RDONLY)) < 0) {
- fprintf(stderr, "unable to open wtmp-file %s\n", wtmpfile);
+ fprintf(stderr, "unable to open wtmp file %s\n", wtmpfile);
return(2);
}
@@ -94,7 +91,7 @@ int main(int argc, char*argv[]) {
}
close(filehandle);
if (del_counter)
- printit(del_counter, start_time, act_time);
- exit((int) t_del+del_counter);
+ printit(del_counter, start_time, act_time);
+ exit(t_del+del_counter > 0); // exit codes should be 0..255
}
#endif
|