From maneo@staff.seccuris.com  Fri Feb 27 09:53:06 2004
Return-Path: <maneo@staff.seccuris.com>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 46A9C16A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 27 Feb 2004 09:53:06 -0800 (PST)
Received: from staff.seccuris.com (staff.seccuris.com [204.112.0.40])
	by mx1.FreeBSD.org (Postfix) with SMTP id C835D43D2D
	for <FreeBSD-gnats-submit@freebsd.org>; Fri, 27 Feb 2004 09:53:05 -0800 (PST)
	(envelope-from maneo@staff.seccuris.com)
Received: (qmail 56999 invoked by uid 1006); 27 Feb 2004 17:53:03 -0000
Message-Id: <20040227175303.56998.qmail@staff.seccuris.com>
Date: 27 Feb 2004 17:53:03 -0000
From: Christian S.J.Peron <maneo@bsdpro.com>
Reply-To: Christian S.J.Peron <maneo@bsdpro.com>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: [patch] list IPC mechanisms based on username/UID
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         63463
>Category:       bin
>Synopsis:       [patch] list IPC mechanisms based on username/UID
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bmilekic
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri Feb 27 10:00:40 PST 2004
>Closed-Date:    Tue Mar 23 20:02:44 PST 2004
>Last-Modified:  Tue Mar 23 20:02:44 PST 2004
>Originator:     Christian S.J. Peron
>Release:        FreeBSD 5.2-CURRENT i386
>Organization:
>Environment:
FreeBSD movl 5.2-CURRENT FreeBSD 5.2-CURRENT #8: Mon Feb  2 22:04:49 GMT 2004     modulus@movl:/usr/src/sys/i386/compile/ROUTER  i386

	
>Description:

	Currently there is no way to list IPC mechanisms owned by
	a specific user.

	I think it would be usefull to have this option. I have
	enclosed a patch which implements this functionality.

	
>How-To-Repeat:
	N/A
>Fix:

--- usr.bin/ipcs/ipcs.c.bak	Fri Feb 27 04:04:15 2004
+++ usr.bin/ipcs/ipcs.c	Fri Feb 27 16:57:04 2004
@@ -74,6 +74,7 @@
 void	sysctlgatherstruct(void *addr, size_t size, struct scgs_vector *vec);
 void	kget(int idx, void *addr, size_t size);
 void	usage(void);
+uid_t	user2uid(char *username);
 
 static struct nlist symbols[] = {
 	{"sema"},
@@ -188,11 +189,12 @@
 {
 	int     display = SHMINFO | MSGINFO | SEMINFO;
 	int     option = 0;
-	char   *core = NULL, *namelist = NULL;
+	char   *core = NULL, *user = NULL, *namelist = NULL;
 	char	kvmoferr[_POSIX2_LINE_MAX];  /* Error buf for kvm_openfiles. */
 	int     i;
+	uid_t   uid;
 
-	while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTy")) != -1)
+	while ((i = getopt(argc, argv, "MmQqSsabC:cN:optTu:y")) != -1)
 		switch (i) {
 		case 'M':
 			display = SHMTOTAL;
@@ -242,6 +244,10 @@
 		case 'y':
 			use_sysctl = 0;
 			break;
+		case 'u':
+			user = optarg;
+			uid = user2uid(user);
+			break;
 		default:
 			usage();
 		}
@@ -320,6 +326,9 @@
 					        ctime_buf[100];
 					struct msqid_ds *msqptr = &xmsqids[i];
 
+					if (user)
+						if (uid != msqptr->msg_perm.uid)
+							continue;
 					cvt_time(msqptr->msg_stime, stime_buf);
 					cvt_time(msqptr->msg_rtime, rtime_buf);
 					cvt_time(msqptr->msg_ctime, ctime_buf);
@@ -409,6 +418,9 @@
 					        ctime_buf[100];
 					struct shmid_ds *shmptr = &xshmids[i];
 
+					if (user)
+						if (uid != shmptr->shm_perm.uid)
+							continue;
 					cvt_time(shmptr->shm_atime, atime_buf);
 					cvt_time(shmptr->shm_dtime, dtime_buf);
 					cvt_time(shmptr->shm_ctime, ctime_buf);
@@ -502,6 +514,9 @@
 					char    ctime_buf[100], otime_buf[100];
 					struct semid_ds *semaptr = &xsema[i];
 
+					if (user)
+						if (uid != semaptr->sem_perm.uid)
+							continue;
 					cvt_time(semaptr->sem_otime, otime_buf);
 					cvt_time(semaptr->sem_ctime, ctime_buf);
 
@@ -649,11 +664,27 @@
 	}
 }
 
+uid_t 
+user2uid(char *username)
+{
+	struct passwd *pwd;
+	uid_t uid;
+	char *r;
+
+	uid = strtoul(username, &r, 0);
+	if (!*r && r != username)
+		return (uid);
+	if ((pwd = getpwnam(username)) == NULL)
+		errx(1, "getpwnam failed: No such user");
+	endpwent();
+	return (pwd->pw_uid);
+}
+
 void
 usage()
 {
 
 	fprintf(stderr,
-	    "usage: ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist]\n");
+	    "usage: ipcs [-abcmopqstyMQST] [-C corefile] [-N namelist] [-u user]\n");
 	exit(1);
 }
>Release-Note:
>Audit-Trail:

From: Ceri Davies <ceri@FreeBSD.org>
To: "Christian S.J.Peron" <maneo@bsdpro.com>
Cc: FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/63463: [patch] list IPC mechanisms based on username/UID
Date: Fri, 27 Feb 2004 18:31:31 +0000

 While I'm personally unsure of how useful this is, your patch stands
 more chance of being committed if you also include an update for the
 manpage.
 
 Ceri
 -- 

From: "Christian S.J. Peron" <maneo@bsdpro.com>
To: freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/63463: [patch] list IPC mechanisms based on username/UID
Date: Fri, 27 Feb 2004 21:01:57 +0000

 I have enclosed a man page patch for this option:
 
 --- usr.bin/ipcs/ipcs.1.bak	Fri Feb 27 20:55:48 2004
 +++ usr.bin/ipcs/ipcs.1	Fri Feb 27 21:00:38 2004
 @@ -131,6 +131,11 @@
  .Xr kvm 3
  will require read privileges to
  .Pa /dev/kmem .
 +.It Fl u Ar user
 +Display information about IPC mechanisms owned by 
 +.Pa user .
 +User specification can be in the form of a numeric UID or
 +a login name.
  .El
  .Pp
  If none of the
Responsible-Changed-From-To: freebsd-bugs->bmilekic 
Responsible-Changed-By: bmilekic 
Responsible-Changed-When: Tue Mar 23 19:52:05 PST 2004 
Responsible-Changed-Why:  
I'll take this and commit it for Christian. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=63463 
State-Changed-From-To: open->closed 
State-Changed-By: bmilekic 
State-Changed-When: Tue Mar 23 20:02:11 PST 2004 
State-Changed-Why:  
Committed.  Thank you for your submission. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=63463 
>Unformatted:
