Re: [PATCH] Improve handling of getpwnam_r() and getgrnam_r()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



ACK

On 4/3/2013 5:04 PM, Jan Friesse wrote:
> ERANGE from above functions are now correctly handled. Error is
> correctly returned back (instead of using errno what is nonsense for
> reentrant functions). Existence of user/group is now correctly
> handled. Also numerical uid/gid works as expected.
> 
> Backport of 52f88d04eaf2ad6c65df34a3401417d0583c6a45,
> 005e7fd3b9de79bc32894f9f15431a7cf32a3b27 and
> 86b074dc1a72f9696ab5a3d302c7719b2961d8d6 from master
> 
> Signed-off-by: Jan Friesse <jfriesse@xxxxxxxxxx>
> ---
>  exec/mainconfig.c |   84 ++++++++++++++++++++++++++++++++++++++++++-----------
>  1 files changed, 67 insertions(+), 17 deletions(-)
> 
> diff --git a/exec/mainconfig.c b/exec/mainconfig.c
> index 7961876..e69652d 100644
> --- a/exec/mainconfig.c
> +++ b/exec/mainconfig.c
> @@ -580,21 +580,46 @@ static int uid_determine (const char *req_user)
>  	struct passwd* pwdptr = &passwd;
>  	struct passwd* temp_pwd_pt;
>  	char *pwdbuffer;
> -	int  pwdlinelen;
> +	int  pwdlinelen, rc;
> +	long int id;
> +	char *ep;
> +
> +	id = strtol(req_user, &ep, 10);
> +	if (*ep == '\0' && id >= 0 && id <= UINT_MAX) {
> +		return (id);
> +	}
>  
>  	pwdlinelen = sysconf (_SC_GETPW_R_SIZE_MAX);
>  
>  	if (pwdlinelen == -1) {
> -		pwdlinelen = 256;
> +	        pwdlinelen = 256;
>  	}
>  
>  	pwdbuffer = malloc (pwdlinelen);
>  
> -	if ((getpwnam_r (req_user, pwdptr, pwdbuffer, pwdlinelen, &temp_pwd_pt)) != 0) {
> -		log_printf (LOGSYS_LEVEL_ERROR,
> -			"ERROR: The '%s' user is not found in /etc/passwd, please read the documentation.\n",
> -			req_user);
> -		corosync_exit_error (AIS_DONE_UID_DETERMINE);
> +	while ((rc = getpwnam_r (req_user, pwdptr, pwdbuffer, pwdlinelen, &temp_pwd_pt)) == ERANGE) {
> +		char *n;
> +
> +		pwdlinelen *= 2;
> +		if (pwdlinelen <= 32678) {
> +			n = realloc (pwdbuffer, pwdlinelen);
> +			if (n != NULL) {
> +				pwdbuffer = n;
> +				continue;
> +			}
> +		}
> +	}
> +	if (rc != 0) {
> +		free (pwdbuffer);
> +	        log_printf (LOGSYS_LEVEL_ERROR, "getpwnam_r(): %s", strerror(rc));
> +	        corosync_exit_error (AIS_DONE_UID_DETERMINE);
> +	}
> +	if (temp_pwd_pt == NULL) {
> +		free (pwdbuffer);
> +	        log_printf (LOGSYS_LEVEL_ERROR,
> +	                "The '%s' user is not found in /etc/passwd, please read the documentation.",
> +	                req_user);
> +	        corosync_exit_error (AIS_DONE_UID_DETERMINE);
>  	}
>  	pw_uid = passwd.pw_uid;
>  	free (pwdbuffer);
> @@ -604,31 +629,56 @@ static int uid_determine (const char *req_user)
>  
>  static int gid_determine (const char *req_group)
>  {
> -	int ais_gid = 0;
> +	int corosync_gid = 0;
>  	struct group group;
>  	struct group * grpptr = &group;
>  	struct group * temp_grp_pt;
>  	char *grpbuffer;
> -	int  grplinelen;
> +	int  grplinelen, rc;
> +	long int id;
> +	char *ep;
> +
> +	id = strtol(req_group, &ep, 10);
> +	if (*ep == '\0' && id >= 0 && id <= UINT_MAX) {
> +		return (id);
> +	}
>  
>  	grplinelen = sysconf (_SC_GETGR_R_SIZE_MAX);
>  
>  	if (grplinelen == -1) {
> -		grplinelen = 256;
> +	        grplinelen = 256;
>  	}
>  
>  	grpbuffer = malloc (grplinelen);
>  
> -	if ((getgrnam_r (req_group, grpptr, grpbuffer, grplinelen, &temp_grp_pt)) != 0) {
> -		log_printf (LOGSYS_LEVEL_ERROR,
> -			"ERROR: The '%s' group is not found in /etc/group, please read the documentation.\n",
> -			req_group);
> -		corosync_exit_error (AIS_DONE_GID_DETERMINE);
> +	while ((rc = getgrnam_r (req_group, grpptr, grpbuffer, grplinelen, &temp_grp_pt)) == ERANGE) {
> +		char *n;
> +
> +		grplinelen *= 2;
> +		if (grplinelen <= 32678) {
> +			n = realloc (grpbuffer, grplinelen);
> +			if (n != NULL) {
> +				grpbuffer = n;
> +				continue;
> +			}
> +		}
> +	}
> +	if (rc != 0) {
> +		free (grpbuffer);
> +	        log_printf (LOGSYS_LEVEL_ERROR, "getgrnam_r(): %s", strerror(rc));
> +	        corosync_exit_error (AIS_DONE_GID_DETERMINE);
> +	}
> +	if (temp_grp_pt == NULL) {
> +		free (grpbuffer);
> +	        log_printf (LOGSYS_LEVEL_ERROR,
> +	                "The '%s' group is not found in /etc/group, please read the documentation.",
> +	                req_group);
> +	        corosync_exit_error (AIS_DONE_GID_DETERMINE);
>  	}
> -	ais_gid = group.gr_gid;
> +	corosync_gid = group.gr_gid;
>  	free (grpbuffer);
>  
> -	return ais_gid;
> +	return corosync_gid;
>  }
>  
>  static unsigned int logging_handle_find (
> 

_______________________________________________
discuss mailing list
discuss@xxxxxxxxxxxx
http://lists.corosync.org/mailman/listinfo/discuss




[Index of Archives]     [Linux Clusters]     [Corosync Project]     [Linux USB Devel]     [Linux Audio Users]     [Photo]     [Yosemite News]    [Yosemite Photos]    [Linux Kernel]     [Linux SCSI]     [X.Org]

  Powered by Linux