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 Jérémie Andréi)
+   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
