Subject: Re: port-i386/33974
To: None <port-i386-maintainer@netbsd.org, gnats-admin@netbsd.org,>
From: Matthias Drochner <M.Drochner@fz-juelich.de>
List: netbsd-bugs
Date: 07/12/2006 21:10:03
The following reply was made to PR port-i386/33974; it has been noted by GNATS.

From: Matthias Drochner <M.Drochner@fz-juelich.de>
To: gnats-bugs@NetBSD.org
Cc: port-i386-maintainer@NetBSD.org, gnats-admin@NetBSD.org,
	netbsd-bugs@NetBSD.org, elekktretterr@exemail.com.au
Subject: Re: port-i386/33974 
Date: Wed, 12 Jul 2006 23:09:46 +0200

 This is a multipart MIME message.
 
 --==_Exmh_16973218962900
 Content-Type: text/plain; charset=us-ascii
 
 
 elekktretterr@exemail.com.au said:
 > Increasing grpbuf to 2048 fixed the issue as you suspected
 
 Great to hear that at least your most pressing problem is solved.
 
 > What exactly did you want me to printf from those functions?
 
 OK, here is a patch which combines some diagnostic output
 with an attempt to provide a fix which hopefully makes the code
 sustain more than 1000 users in a group.
 Could you please give it a try?
 
 best regards
 Matthias
 
 
 
 --==_Exmh_16973218962900
 Content-Type: text/plain ; name="grpbuf.txt"; charset=us-ascii
 Content-Description: grpbuf.txt
 Content-Disposition: attachment; filename="grpbuf.txt"
 
 Index: files/netbsd.c
 ===================================================================
 RCS file: /cvsroot/pkgsrc/databases/nss_ldap/files/netbsd.c,v
 retrieving revision 1.5
 diff -u -p -r1.5 netbsd.c
 --- files/netbsd.c	15 Mar 2006 19:23:47 -0000	1.5
 +++ files/netbsd.c	12 Jul 2006 21:00:08 -0000
 @@ -8,6 +8,9 @@
  #include <stdlib.h>
  #include <string.h>
  
 +#include <errno.h>
 +#include <stdio.h>
 +
  #include "netbsd.h"
  typedef enum nss_status NSS_STATUS;
  
 @@ -55,7 +58,9 @@ static int nss2netbsderr[] = {
  };
  static struct passwd rpw;
  static struct group rg;
 -static char pwbuf[1024], grpbuf[1024]; /* two necessary? */
 +static char pwbuf[1024];
 +static char *grpbuf;
 +static size_t grpbuflen;
  
  static ns_mtab methods[] = {
  	{ NSDB_PASSWD, "setpwent", netbsd_setpwent, 0 },
 @@ -263,6 +268,21 @@ netbsd_getpwuid_r(void *rv, void *cb_dat
  	return nss2netbsderr[s];
  }
  
 +static int
 +resize_grpbuf()
 +{
 +
 +	if (grpbuflen > 50000) /* safety guard */
 +		return (-1);
 +	grpbuflen += 1024;
 +	if (grpbuf)
 +		free(grpbuf);
 +	grpbuf = malloc(grpbuflen);
 +	if (!grpbuf)
 +		return (-1);
 +	printf("resize_grpbuf: now %d\n", grpbuflen);
 +	return (0);
 +}
  
  static int
  netbsd_setgrent(void *rv, void *cb_data, va_list ap)
 @@ -309,11 +329,15 @@ netbsd_getgrent(void *rv, void *cb_data,
  	NSS_STATUS s;
  	struct group **retval = va_arg(ap, struct group **);
  
 +tryagain:
  	memset(&rg, 0, sizeof(rg));
 -	s = _nss_ldap_getgrent_r(&rg, grpbuf, sizeof(grpbuf), &err);
 +	s = _nss_ldap_getgrent_r(&rg, grpbuf, grpbuflen, &err);
  
  	if (s == NSS_STATUS_SUCCESS)
  		*retval = &rg;
 +	else if ((s == NSS_STATUS_TRYAGAIN) && (err == ERANGE)
 +		 && (resize_grpbuf() == 0))
 +		goto tryagain;
  	else
  		*retval = 0;
  
 @@ -355,13 +379,19 @@ netbsd_getgrnam(void *rv, void *cb_data,
  	struct group **retval = va_arg(ap, struct group **);
  	const char *name = va_arg(ap, const char *);
  
 +tryagain:
  	memset(&rg, 0, sizeof(rg));
 -	s = _nss_ldap_getgrnam_r(name, &rg, grpbuf, sizeof(grpbuf), &err);
 +	s = _nss_ldap_getgrnam_r(name, &rg, grpbuf, grpbuflen, &err);
  
  	if (s == NSS_STATUS_SUCCESS)
  		*retval = &rg;
 -	else
 +	else if ((s == NSS_STATUS_TRYAGAIN) && (err == ERANGE)
 +		 && (resize_grpbuf() == 0))
 +		goto tryagain;
 +	else {
 +		printf("netbsd_getgrnam: %d/%d\n", s, err);
  		*retval = 0;
 +	}
  
  	return nss2netbsderr[s];
  }
 @@ -402,13 +432,19 @@ netbsd_getgrgid(void *rv, void *cb_data,
  	struct group **retval = va_arg(ap, struct group **);
  	gid_t gid = va_arg(ap, gid_t);
  
 +tryagain:
  	memset(&rg, 0, sizeof(rg));
 -	s = _nss_ldap_getgrgid_r(gid, &rg, grpbuf, sizeof(grpbuf), &err);
 +	s = _nss_ldap_getgrgid_r(gid, &rg, grpbuf, grpbuflen, &err);
  
  	if (s == NSS_STATUS_SUCCESS)
  		*retval = &rg;
 -	else
 +	else if ((s == NSS_STATUS_TRYAGAIN) && (err == ERANGE)
 +		 && (resize_grpbuf() == 0))
 +		goto tryagain;
 +	else {
 +		printf("netbsd_getgrgid: %d/%d\n", s, err);
  		*retval = 0;
 +	}
  
  	return nss2netbsderr[s];
  }
 @@ -505,5 +541,9 @@ nss_module_register(const char *source, 
  {
  	*mtabsize = sizeof(methods)/sizeof(methods[0]);
  	*unreg = NULL;
 +
 +	if (resize_grpbuf())
 +		return 0;
 +
  	return (methods);
  }
 
 --==_Exmh_16973218962900--