Bug 167932

Summary: [PATCH] examples/csh/dot.cshrc SSH_AUTH_SOCK example incorrect
Product: Documentation Reporter: Guido Falsi <madpilot>
Component: Books & ArticlesAssignee: Eitan Adler <eadler>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Latest   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description Guido Falsi freebsd_committer freebsd_triage 2012-05-15 17:30:02 UTC
While experimenting with the .cshrc examples I discovered that the
sample for automatically setting SSH_AUTH_SOCK does not work as is.

It's easily tricked by other processes and cut seems to split on
every single space character, not grouping them.

I was using this revision of the file:

# $FreeBSD: stable/9/share/examples/csh/dot.cshrc 234978 2012-05-03 19:55:36Z eadler $

I tested a few possible solutions. I'm quite aware that none of
these is rock solid, but they do perform better than the sample
code in the file. I'm sure someone will come up with an even better
implementation.

It is my personal opinion that sample code should be as correctly
working as possible.

The simpliest and most legible (used in the attached patch):

setenv SSH_AUTH_SOCK `sockstat | grep "${USER}" | grep ssh-agent | awk '{ print $6 }'`

a slightly less legible one, but avoiding the double grep:

setenv SSH_AUTH_SOCK `sockstat | egrep "${USER}.*ssh-agent" | awk '{ print $6 }'`

and a simplier(but with horrible legibility) one relying only on sed:

setenv SSH_AUTH_SOCK `sockstat | sed -n "/${USER}.*ssh-agent/s/[^\/]*\(.*\)/\1/p"`

I know this is a minor glitch, but I thought it was worth reporting
anyway.

How-To-Repeat: 
Enable the sample code to populate SSH_AUTH_SOCK.
launch an ssh-agent on teh target machine.
Login via ssh.
Run "env" and notice that the SSH_AUTH_SOCK variable will be empty
even if there is an ssh-agent running.
Comment 1 Warren Block 2012-05-15 19:50:26 UTC
On Tue, 15 May 2012, Guido Falsi wrote:

> It is my personal opinion that sample code should be as correctly
> working as possible.
>
> The simpliest and most legible (used in the attached patch):
>
> setenv SSH_AUTH_SOCK `sockstat | grep "${USER}" | grep ssh-agent | awk '{ print $6 }'`

awk can do a little more to eliminate the second grep:

setenv SSH_AUTH_SOCK `sockstat | grep "^${USER} " | awk '/ssh-agent/ { print $6 }'`

The anchor before and space after the username in the grep are to make 
it an exact match and not a subset ("jsm" versus "jsmith").  Although I 
don't know what sockstat does with a username longer than eight 
characters.

> I know this is a minor glitch, but I thought it was worth reporting
> anyway.

It is.  Accuracy and precision are the goal.
Comment 2 Eitan Adler freebsd_committer freebsd_triage 2012-05-15 20:35:44 UTC
Responsible Changed
From-To: freebsd-doc->eadler

I'll take it.
Comment 3 dfilter service freebsd_committer freebsd_triage 2012-05-15 21:15:30 UTC
Author: eadler
Date: Tue May 15 20:15:17 2012
New Revision: 235481
URL: http://svn.freebsd.org/changeset/base/235481

Log:
  Rework sample code which sets SSH_AUTH_SOCK to the user's ssh-agent
  socket path if running
  
  PR:		167932
  Submitted by:	madpilot (pr)
  Submitted by:	wblock (final patch)
  Reviewed by:	If someone else reviewed your modification.
  Approved by:	cperciva (implicit)
  MFC after:	3 days

Modified:
  head/share/examples/csh/dot.cshrc

Modified: head/share/examples/csh/dot.cshrc
==============================================================================
--- head/share/examples/csh/dot.cshrc	Tue May 15 17:21:46 2012	(r235480)
+++ head/share/examples/csh/dot.cshrc	Tue May 15 20:15:17 2012	(r235481)
@@ -5,7 +5,7 @@
 
 # Sets SSH_AUTH_SOCK to the user's ssh-agent socket path if running
 if (${?SSH_AUTH_SOCK} != "1") then
-	setenv SSH_AUTH_SOCK `sockstat | grep "${USER}" | cut -d ' ' -f 6` 
+	setenv SSH_AUTH_SOCK `sockstat | grep "^${USER} " | awk '/ssh-agent/ { print $6 }'`
 endif
 
 # Change only root's prompt
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 4 Eitan Adler freebsd_committer freebsd_triage 2012-05-15 22:48:32 UTC
State Changed
From-To: open->patched

committed in r235481
Comment 5 dfilter service freebsd_committer freebsd_triage 2012-06-01 07:40:19 UTC
Author: eadler
Date: Fri Jun  1 06:39:55 2012
New Revision: 236382
URL: http://svn.freebsd.org/changeset/base/236382

Log:
  MFC r235481:
  	Rework sample code which sets SSH_AUTH_SOCK to the user's ssh-agent
  	socket path if running
  
  PR:		167932
  Approved by:	cperciva (implicit)

Modified:
  stable/9/share/examples/csh/dot.cshrc
Directory Properties:
  stable/9/share/examples/csh/   (props changed)

Modified: stable/9/share/examples/csh/dot.cshrc
==============================================================================
--- stable/9/share/examples/csh/dot.cshrc	Fri Jun  1 06:20:39 2012	(r236381)
+++ stable/9/share/examples/csh/dot.cshrc	Fri Jun  1 06:39:55 2012	(r236382)
@@ -5,7 +5,7 @@
 
 # Sets SSH_AUTH_SOCK to the user's ssh-agent socket path if running
 if (${?SSH_AUTH_SOCK} != "1") then
-	setenv SSH_AUTH_SOCK `sockstat | grep "${USER}" | cut -d ' ' -f 6` 
+	setenv SSH_AUTH_SOCK `sockstat | grep "^${USER} " | awk '/ssh-agent/ { print $6 }'`
 endif
 
 # Change only root's prompt
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 6 dfilter service freebsd_committer freebsd_triage 2012-06-01 07:40:41 UTC
Author: eadler
Date: Fri Jun  1 06:40:29 2012
New Revision: 236383
URL: http://svn.freebsd.org/changeset/base/236383

Log:
  MFC r235481:
  	Rework sample code which sets SSH_AUTH_SOCK to the user's ssh-agent
  	socket path if running
  
  PR:		167932
  Approved by:	cperciva (implicit)

Modified:
  stable/8/share/examples/csh/dot.cshrc
Directory Properties:
  stable/8/share/examples/csh/   (props changed)

Modified: stable/8/share/examples/csh/dot.cshrc
==============================================================================
--- stable/8/share/examples/csh/dot.cshrc	Fri Jun  1 06:39:55 2012	(r236382)
+++ stable/8/share/examples/csh/dot.cshrc	Fri Jun  1 06:40:29 2012	(r236383)
@@ -5,7 +5,7 @@
 
 # Sets SSH_AUTH_SOCK to the user's ssh-agent socket path if running
 if (${?SSH_AUTH_SOCK} != "1") then
-	setenv SSH_AUTH_SOCK `sockstat | grep "${USER}" | cut -d ' ' -f 6` 
+	setenv SSH_AUTH_SOCK `sockstat | grep "^${USER} " | awk '/ssh-agent/ { print $6 }'`
 endif
 
 # Change only root's prompt
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 7 dfilter service freebsd_committer freebsd_triage 2012-06-01 07:41:08 UTC
Author: eadler
Date: Fri Jun  1 06:40:48 2012
New Revision: 236384
URL: http://svn.freebsd.org/changeset/base/236384

Log:
  MFC r235481:
  	Rework sample code which sets SSH_AUTH_SOCK to the user's ssh-agent
  	socket path if running
  
  PR:		167932
  Approved by:	cperciva (implicit)

Modified:
  stable/7/share/examples/csh/dot.cshrc
Directory Properties:
  stable/7/share/examples/csh/   (props changed)

Modified: stable/7/share/examples/csh/dot.cshrc
==============================================================================
--- stable/7/share/examples/csh/dot.cshrc	Fri Jun  1 06:40:29 2012	(r236383)
+++ stable/7/share/examples/csh/dot.cshrc	Fri Jun  1 06:40:48 2012	(r236384)
@@ -5,7 +5,7 @@
 
 # Sets SSH_AUTH_SOCK to the user's ssh-agent socket path if running
 if (${?SSH_AUTH_SOCK} != "1") then
-	setenv SSH_AUTH_SOCK `sockstat | grep "${USER}" | cut -d ' ' -f 6` 
+	setenv SSH_AUTH_SOCK `sockstat | grep "^${USER} " | awk '/ssh-agent/ { print $6 }'`
 endif
 
 # Change only root's prompt
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
Comment 8 Eitan Adler freebsd_committer freebsd_triage 2012-06-05 06:17:10 UTC
State Changed
From-To: patched->closed

Committed. Thanks!