Final Suggestion (Re: suggested bsd-setproctitle.c)

Ben Lindstrom mouring at pconline.com
Wed Nov 8 15:59:57 EST 2000


On Tue, 7 Nov 2000, Mark D. Roth wrote:

> On Tue Nov 07 15:46 2000 +0100, Kevin Steves wrote:
> > How does the attached patch work?  I tested on HP-UX
> > !defined(HAVE___PROGNAME) and Linux defined(HAVE___PROGNAME).  I don't
> > use const now to keep things consistent, and went back to #ifdef
> > HAVE___PROGNAME.
> 
> Unfortunately, you're still setting __progname to a hard-coded value
> instead of basename(argv[0]), which means that you don't get a
> reasonable value when you invoke one of these programs using something
> other than the default name.
> 
> I'd suggest the attached patch instead.  I'm not super familiar with
> the code, so please let me know if I'm missing something here...
> Thanks!

May I suggest the attached patch.  Since It's been compiled against NeXT
it pretty much ensure that all __progname is setup right (since NeXT
supports it).

I would perfer using basename() over strrchar() and NULL check.  Even it
it required me to snag basename() from the OpenBSD tree. (Which I was
happy to see compiled without any changes!! =)

Anyone have any comments?

- Ben
-------------- next part --------------
diff -urN openssh/ChangeLog ossh/ChangeLog
--- openssh/ChangeLog	Tue Nov  7 19:58:00 2000
+++ ossh/ChangeLog	Tue Nov  7 23:41:45 2000
@@ -1,3 +1,8 @@
+20001108
+ - (stevek & bal) Clean up __progname definations w/ suggestion from
+   David Terrell <dbt at meat.net>
+ - (bal) Added bsd-basename.[ch] since some platforms lack it.
+
 20001107
  - (bal) acconfig.in - removed the double "USE_PIPES" entry. Patch by
    Mark Miller <markm at swoon.net>
diff -urN openssh/Makefile.in ossh/Makefile.in
--- openssh/Makefile.in	Sun Nov  5 04:27:54 2000
+++ ossh/Makefile.in	Tue Nov  7 22:32:07 2000
@@ -37,7 +37,7 @@
 
 LIBSSH_OBJS=atomicio.o authfd.o authfile.o bufaux.o buffer.o canohost.o channels.o cipher.o cli.o compat.o compress.o crc32.o cygwin_util.o deattack.o dispatch.o dsa.o hmac.o hostfile.o key.o kex.o log.o match.o mpaux.o nchan.o packet.o radix.o rijndael.o entropy.o readpass.o rsa.o tildexpand.o ttymodes.o uidswap.o util.o uuencode.o xmalloc.o 
 
-LIBOPENBSD_COMPAT_OBJS=bsd-arc4random.o bsd-base64.o bsd-bindresvport.o bsd-daemon.o bsd-getcwd.o bsd-inet_aton.o bsd-inet_ntoa.o bsd-misc.o bsd-mktemp.o bsd-realpath.o bsd-rresvport.o bsd-setenv.o bsd-sigaction.o bsd-snprintf.o bsd-strlcat.o bsd-strlcpy.o bsd-strsep.o bsd-strtok.o bsd-vis.o bsd-setproctitle.o bsd-waitpid.o fake-getaddrinfo.o fake-getnameinfo.o next-posix.o
+LIBOPENBSD_COMPAT_OBJS=bsd-arc4random.o bsd-base64.o bsd-basename.o bsd-bindresvport.o bsd-daemon.o bsd-getcwd.o bsd-inet_aton.o bsd-inet_ntoa.o bsd-misc.o bsd-mktemp.o bsd-realpath.o bsd-rresvport.o bsd-setenv.o bsd-sigaction.o bsd-snprintf.o bsd-strlcat.o bsd-strlcpy.o bsd-strsep.o bsd-strtok.o bsd-vis.o bsd-setproctitle.o bsd-waitpid.o fake-getaddrinfo.o fake-getnameinfo.o next-posix.o
 
 SSHOBJS= ssh.o sshconnect.o sshconnect1.o sshconnect2.o log-client.o readconf.o clientloop.o
 
diff -urN openssh/bsd-basename.c ossh/bsd-basename.c
--- openssh/bsd-basename.c	Wed Dec 31 18:00:00 1969
+++ ossh/bsd-basename.c	Tue Nov  7 23:36:36 2000
@@ -0,0 +1,78 @@
+/*	$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $	*/
+
+/*
+ * Copyright (c) 1997 Todd C. Miller <Todd.Miller at courtesan.com>
+ * All rights reserved.
+ *
+ * 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.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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 "includes.h"
+#ifndef HAVE_BASENAME
+
+#ifndef lint
+static char *rcsid = "$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $";
+#endif /* not lint */
+
+#include <errno.h>
+#include <string.h>
+#include <sys/param.h>
+
+char *
+basename(const char *path)
+{
+	static char bname[MAXPATHLEN];
+	register const char *endp, *startp;
+
+	/* Empty or NULL string gets treated as "." */
+	if (path == NULL || *path == '\0') {
+		(void)strcpy(bname, ".");
+		return(bname);
+	}
+
+	/* Strip trailing slashes */
+	endp = path + strlen(path) - 1;
+	while (endp > path && *endp == '/')
+		endp--;
+
+	/* All slashes becomes "/" */
+	if (endp == path && *endp == '/') {
+		(void)strcpy(bname, "/");
+		return(bname);
+	}
+
+	/* Find the start of the base */
+	startp = endp;
+	while (startp > path && *(startp - 1) != '/')
+		startp--;
+
+	if (endp - startp + 1 > sizeof(bname)) {
+		errno = ENAMETOOLONG;
+		return(NULL);
+	}
+	(void)strncpy(bname, startp, endp - startp + 1);
+	bname[endp - startp + 1] = '\0';
+	return(bname);
+}
+
+#endif /* !HAVE_BASENAME */
diff -urN openssh/bsd-basename.h ossh/bsd-basename.h
--- openssh/bsd-basename.h	Wed Dec 31 18:00:00 1969
+++ ossh/bsd-basename.h	Tue Nov  7 22:32:07 2000
@@ -0,0 +1,34 @@
+/*	$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $	*/
+
+/*
+ * Copyright (c) 1997 Todd C. Miller <Todd.Miller at courtesan.com>
+ * All rights reserved.
+ *
+ * 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.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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 HAVE_BASENAME
+
+char *basename(const char *path);
+
+#endif /* !HAVE_BASENAME */
diff -urN openssh/bsd-setproctitle.c ossh/bsd-setproctitle.c
--- openssh/bsd-setproctitle.c	Mon Nov  6 02:07:18 2000
+++ ossh/bsd-setproctitle.c	Tue Nov  7 22:32:07 2000
@@ -56,11 +56,7 @@
 
 #define	MAX_PROCTITLE	2048
 
-#ifdef HAVE___PROGNAME
 extern char *__progname;
-#else
-static const char *__progname = "sshd";
-#endif /* HAVE___PROGNAME */
 
 /*
  * Set Process Title (SPT) defines.  Modeled after sendmail's
diff -urN openssh/configure.in ossh/configure.in
--- openssh/configure.in	Mon Nov  6 02:06:02 2000
+++ ossh/configure.in	Tue Nov  7 22:34:15 2000
@@ -287,7 +287,7 @@
 AC_CHECK_HEADERS(bstring.h endian.h floatingpoint.h getopt.h lastlog.h limits.h login.h login_cap.h maillock.h netdb.h netgroup.h netinet/in_systm.h paths.h poll.h pty.h shadow.h security/pam_appl.h sys/bitypes.h sys/bsdtty.h sys/cdefs.h sys/poll.h sys/select.h sys/stat.h sys/stropts.h sys/sysmacros.h sys/time.h sys/ttcompat.h sys/un.h stddef.h time.h ttyent.h usersec.h util.h utmp.h utmpx.h vis.h)
 
 dnl    Checks for library functions.
-AC_CHECK_FUNCS(arc4random atexit b64_ntop bcopy bindresvport_af clock fchmod freeaddrinfo futimes gai_strerror getcwd getaddrinfo getnameinfo getrusage getttyent inet_aton inet_ntoa innetgr login_getcapbool md5_crypt memmove mkdtemp on_exit openpty realpath rresvport_af setenv seteuid setlogin setproctitle setreuid setrlimit setsid sigaction sigvec snprintf strerror strlcat strlcpy strsep strtok_r vsnprintf vhangup vis waitpid _getpty __b64_ntop)
+AC_CHECK_FUNCS(arc4random atexit basename b64_ntop bcopy bindresvport_af clock fchmod freeaddrinfo futimes gai_strerror getcwd getaddrinfo getnameinfo getrusage getttyent inet_aton inet_ntoa innetgr login_getcapbool md5_crypt memmove mkdtemp on_exit openpty realpath rresvport_af setenv seteuid setlogin setproctitle setreuid setrlimit setsid sigaction sigvec snprintf strerror strlcat strlcpy strsep strtok_r vsnprintf vhangup vis waitpid _getpty __b64_ntop)
 dnl    Checks for time functions
 AC_CHECK_FUNCS(gettimeofday time)
 dnl    Checks for libutil functions
diff -urN openssh/log-server.c ossh/log-server.c
--- openssh/log-server.c	Fri Sep 15 21:29:09 2000
+++ ossh/log-server.c	Tue Nov  7 22:32:07 2000
@@ -43,12 +43,6 @@
 #include "xmalloc.h"
 #include "ssh.h"
 
-#ifdef HAVE___PROGNAME
-extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "sshd";
-#endif /* HAVE___PROGNAME */
-
 static LogLevel log_level = SYSLOG_LEVEL_INFO;
 static int log_on_stderr = 0;
 static int log_facility = LOG_AUTH;
@@ -129,6 +123,7 @@
 	char fmtbuf[MSGBUFSIZ];
 	char *txt = NULL;
 	int pri = LOG_INFO;
+	extern char *__progname;
 
 	if (level > log_level)
 		return;
diff -urN openssh/openbsd-compat.h ossh/openbsd-compat.h
--- openssh/openbsd-compat.h	Sun Nov  5 04:01:03 2000
+++ ossh/openbsd-compat.h	Tue Nov  7 22:32:07 2000
@@ -5,6 +5,7 @@
 
 /* BSD function replacements */
 #include "bsd-arc4random.h"
+#include "bsd-basename.h"
 #include "bsd-bindresvport.h"
 #include "bsd-getcwd.h"
 #include "bsd-realpath.h"
diff -urN openssh/scp.c ossh/scp.c
--- openssh/scp.c	Fri Oct 27 22:19:58 2000
+++ ossh/scp.c	Tue Nov  7 23:44:25 2000
@@ -84,6 +84,12 @@
 #define _PATH_CP "cp"
 #endif
 
+#ifdef HAVE___PROGNAME
+extern char *__progname;
+#else
+char *__progname;
+#endif
+
 /* For progressmeter() -- number of seconds before xfer considered "stalled" */
 #define STALLTIME	5
 
@@ -248,6 +254,9 @@
 	char *targ;
 	extern char *optarg;
 	extern int optind;
+#ifndef HAVE___PROGNAME
+	__progname = xstrdup(basename(argv[0]));
+#endif
 
 	args.list = NULL;
 	addargs("ssh");	 	/* overwritten with ssh_program */
diff -urN openssh/session.c ossh/session.c
--- openssh/session.c	Fri Oct 27 22:19:58 2000
+++ ossh/session.c	Tue Nov  7 22:32:07 2000
@@ -128,12 +128,7 @@
 
 /* import */
 extern ServerOptions options;
-#ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "sshd";
-#endif /* HAVE___PROGNAME */
-
 extern int log_stderr;
 extern int debug_flag;
 extern unsigned int utmp_len;
diff -urN openssh/sftp-server.c ossh/sftp-server.c
--- openssh/sftp-server.c	Fri Sep 22 22:58:32 2000
+++ ossh/sftp-server.c	Tue Nov  7 22:32:07 2000
@@ -90,6 +90,12 @@
 #define get_string(lenp)		buffer_get_string(&iqueue, lenp);
 #define TRACE				log
 
+#ifdef HAVE___PROGNAME
+extern char *__progname;
+#else
+char *__progname;
+#endif
+
 /* input and output queue */
 Buffer iqueue;
 Buffer oqueue;
@@ -1014,6 +1020,9 @@
 	fd_set rset, wset;
 	int in, out, max;
 	ssize_t len, olen;
+#ifndef HAVE___PROGNAME
+        __progname = xstrdup(basename(av[0]));
+#endif        
 
 	handle_init();
 
diff -urN openssh/ssh-add.c ossh/ssh-add.c
--- openssh/ssh-add.c	Tue Oct 17 07:22:28 2000
+++ ossh/ssh-add.c	Tue Nov  7 22:32:07 2000
@@ -50,9 +50,9 @@
 
 #ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "ssh-add";
-#endif /* HAVE___PROGNAME */
+#else
+char *__progname;
+#endif
 
 void
 delete_file(AuthenticationConnection *ac, const char *filename)
@@ -247,6 +247,9 @@
 	int no_files = 1;
 	int i;
 	int deleting = 0;
+#ifndef HAVE___PROGNAME
+        __progname = xstrdup(basename(argv[0]));
+#endif        
 
 	init_rng();
 
diff -urN openssh/ssh-agent.c ossh/ssh-agent.c
--- openssh/ssh-agent.c	Fri Sep 29 07:01:37 2000
+++ ossh/ssh-agent.c	Tue Nov  7 22:32:07 2000
@@ -94,9 +94,9 @@
 
 #ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "ssh-agent";
-#endif /* HAVE___PROGNAME */
+#else
+char *__progname;
+#endif
 
 void
 idtab_init(void)
@@ -667,6 +667,9 @@
 	pid_t pid;
 	char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
 	extern int optind;
+#ifndef HAVE___PROGNAME
+        __progname = xstrdup(basename(av[0]));
+#endif        
 	
 	init_rng();
 	
diff -urN openssh/ssh-keygen.c ossh/ssh-keygen.c
--- openssh/ssh-keygen.c	Sat Oct 14 00:23:12 2000
+++ ossh/ssh-keygen.c	Tue Nov  7 22:32:07 2000
@@ -72,9 +72,9 @@
 /* argv0 */
 #ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "ssh-keygen";
-#endif /* HAVE___PROGNAME */
+#else
+char *__progname;
+#endif
 
 char hostname[MAXHOSTNAMELEN];
 
@@ -602,6 +602,9 @@
 	Key *public;
 	extern int optind;
 	extern char *optarg;
+#ifndef HAVE___PROGNAME
+        __progname = xstrdup(basename(av[0]));
+#endif        
 
 	init_rng();
 
diff -urN openssh/ssh.c ossh/ssh.c
--- openssh/ssh.c	Fri Oct 27 22:19:58 2000
+++ ossh/ssh.c	Tue Nov  7 22:32:07 2000
@@ -61,9 +61,9 @@
 
 #ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "ssh";
-#endif /* HAVE___PROGNAME */
+#else
+char *__progname;
+#endif
 
 /* Flag indicating whether IPv4 or IPv6.  This can be set on the command line.
    Default value is AF_UNSPEC means both IPv4 and IPv6. */
@@ -234,6 +234,9 @@
 	struct passwd *pw, pwcopy;
 	int dummy;
 	uid_t original_effective_uid;
+#ifndef HAVE___PROGNAME
+        __progname = xstrdup(basename(av[0]));
+#endif        
 
 	init_rng();
 
diff -urN openssh/sshconnect.c ossh/sshconnect.c
--- openssh/sshconnect.c	Sat Sep 23 01:15:57 2000
+++ ossh/sshconnect.c	Tue Nov  7 22:32:07 2000
@@ -35,11 +35,7 @@
 char *server_version_string = NULL;
 
 extern Options options;
-#ifdef HAVE___PROGNAME
 extern char *__progname;
-#else /* HAVE___PROGNAME */
-static const char *__progname = "ssh";
-#endif /* HAVE___PROGNAME */
 
 /*
  * Connect to the given ssh server using a proxy command.
diff -urN openssh/sshd.c ossh/sshd.c
--- openssh/sshd.c	Sat Oct 14 00:23:13 2000
+++ ossh/sshd.c	Tue Nov  7 22:32:07 2000
@@ -79,6 +79,12 @@
 #define O_NOCTTY	0
 #endif
 
+#ifdef HAVE___PROGNAME
+extern char *__progname;
+#else
+char *__progname;
+#endif
+
 /* Server configuration options. */
 ServerOptions options;
 
@@ -491,6 +497,9 @@
 	int listen_sock, maxfd;
 	int startup_p[2];
 	int startups = 0;
+#ifndef HAVE___PROGNAME
+        __progname = xstrdup(basename(av[0]));
+#endif        
 
 	init_rng();
 


More information about the openssh-unix-dev mailing list