Bug 45896 - setnetgrent(3) should return error code
Summary: setnetgrent(3) should return error code
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 4.7-RELEASE
Hardware: Any Any
: Normal Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-12-01 12:00 UTC by Petter Reinholdtsen
Modified: 2018-06-05 21:27 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Petter Reinholdtsen 2002-12-01 12:00:02 UTC
	The current setnetgrent() call do not return any value.  This
	make it impossible to detect the difference between an non-existand
	netgroup, and an empty netgroup.  The same call return 'int' on most
	other Unices (Solaris, HP/UX, Tru64 Unix, Darwin, Linux).  The value
	on these unixes is 1 if the group exist, and 0 if it is missing.

Fix: 

Change the API for setnetgrent() to return 1 on success, and 0
	on failure.
How-To-Repeat: 	The following code fail on FreeBSD.
		#include <netdb.h>
		int main()
		{
			int found = setnetgrent("non-existand-netgroup");
			return 0;
		}
Comment 1 Mike Barcroft freebsd_committer freebsd_triage 2002-12-01 15:26:24 UTC
Responsible Changed
From-To: freebsd-standards->freebsd-bugs

Not a standards issue.
Comment 2 dwmalone freebsd_committer freebsd_triage 2002-12-21 15:42:17 UTC
Responsible Changed
From-To: freebsd-bugs->dwmalone

I've suggested a patch.
Comment 3 dwmalone 2002-12-21 15:55:09 UTC
Could you test the following patch and check it does what you wanted?

	David.

Index: gen/getnetgrent.3
===================================================================
RCS file: /cvs/FreeBSD-CVS/src/lib/libc/gen/getnetgrent.3,v
retrieving revision 1.11
diff -u -r1.11 getnetgrent.3
--- gen/getnetgrent.3	18 Dec 2002 10:13:54 -0000	1.11
+++ gen/getnetgrent.3	21 Dec 2002 15:46:58 -0000
@@ -49,7 +49,7 @@
 .Fn getnetgrent "char **host" "char **user" "char **domain"
 .Ft int
 .Fn innetgr "const char *netgroup" "const char *host" "const char *user" "const char *domain"
-.Ft void
+.Ft int
 .Fn setnetgrent "const char *netgroup"
 .Ft void
 .Fn endnetgrent void
@@ -105,14 +105,14 @@
 The function
 .Fn getnetgrent
 returns 0 for ``no more netgroup members'' and 1 otherwise.
-The function
-.Fn innetgr
-returns 1 for a successful match and 0 otherwise.
 The functions
-.Fn setnetgrent
+.Fn innetgr
 and
+.Fn setnetgrent
+return 1 for success and 0 otherwise.
+The function
 .Fn endnetgrent
-have no return value.
+has no return value.
 .Sh FILES
 .Bl -tag -width /etc/netgroup -compact
 .It Pa /etc/netgroup
Index: gen/getnetgrent.c
===================================================================
RCS file: /cvs/FreeBSD-CVS/src/lib/libc/gen/getnetgrent.c,v
retrieving revision 1.28
diff -u -r1.28 getnetgrent.c
--- gen/getnetgrent.c	25 Apr 2002 18:14:39 -0000	1.28
+++ gen/getnetgrent.c	21 Dec 2002 15:50:24 -0000
@@ -137,8 +137,8 @@
 static FILE *netf = (FILE *)0;
 static int parse_netgrp();
 static struct linelist *read_for_group();
-void setnetgrent(), endnetgrent();
-int getnetgrent(), innetgr();
+void endnetgrent();
+int setnetgrent(), getnetgrent(), innetgr();
 
 #define	LINSIZ	1024	/* Length of netgroup file line */
 
@@ -148,7 +148,7 @@
  * of netgrp structures. Let parse_netgrp() and read_for_group() do
  * most of the work.
  */
-void
+int
 setnetgrent(group)
 	char *group;
 {
@@ -160,7 +160,7 @@
 	/* Sanity check */
 
 	if (group == NULL || !strlen(group))
-		return;
+		return 0;
 
 	if (grouphead.gr == (struct netgrp *)0 ||
 		strcmp(group, grouphead.grname)) {
@@ -196,7 +196,7 @@
 				/* dohw! */
 				if (netf != NULL)
 					fclose(netf);
-				return;
+				return 0;
 			}
 #else
 		if (netf = fopen(_PATH_NETGROUP, "r")) {
@@ -213,6 +213,7 @@
 		}
 	}
 	nextgrp = grouphead.gr;
+	return 1;
 }
 
 /*
Index: gen/getpwent.c
===================================================================
RCS file: /cvs/FreeBSD-CVS/src/lib/libc/gen/getpwent.c,v
retrieving revision 1.67
diff -u -r1.67 getpwent.c
--- gen/getpwent.c	7 May 2002 23:26:00 -0000	1.67
+++ gen/getpwent.c	21 Dec 2002 15:45:09 -0000
@@ -64,7 +64,7 @@
 #endif
 #include "un-namespace.h"
 
-extern void setnetgrent(char *);
+extern int setnetgrent(char *);
 extern int getnetgrent(char **, char **, char **);
 extern int innetgr(const char *, const char *, const char *, const char *);
Comment 4 Petter Reinholdtsen 2003-01-07 15:17:40 UTC
[David Malone]
> Could you test the following patch and check it does what you wanted?

Checking the source, I believe it does the right thing.
Unfortunately, I do not have access to a FreeBSD-box where I can
recompile the c library, so I can't test the patch.

If you have such box, you can fetch
ftp://ftp.hungry.com/pub/hungry/ng-utils/ng-utils-0.3.tar.gz and see
if it detects a broken or correct setnetgrent.

Should the patch update a public header file as well?
Comment 5 Eitan Adler freebsd_committer freebsd_triage 2012-07-10 04:41:48 UTC
Responsible Changed
From-To: dwmalone->freebsd-bugs

over to the pool (approved by bugmeister)
Comment 6 Harrison Grundy 2014-12-17 12:21:13 UTC
The patch, after cleaning up some fuzz, does resolve the reported issue.
Comment 7 Eitan Adler freebsd_committer freebsd_triage 2018-05-20 23:51:40 UTC
For bugs matching the following conditions:
- Status == In Progress
- Assignee == "bugs@FreeBSD.org"
- Last Modified Year <= 2017

Do
- Set Status to "Open"
Comment 8 David Bright freebsd_committer freebsd_triage 2018-06-05 21:27:52 UTC
This appears to have been fixed long ago (it was correct in base r1573), so I'm going to close it.