/* Allocate supplementary groups buffer */ static bool my_getgrouplist_alloc(char *user, gid_t gid, struct group_data *gdata) { int ngroups = 0; gid_t *groups, *groups2; /* We call getgrouplist() with 0 ngroups first. This should always * return -1, and ngroups should be set to the actual number of * groups the user is in. The manpage doesn't say anything * about errno value, it was usually zero but was set to 34 * (ERANGE) under some environments. ngroups was set correctly * no matter what the errno value is! * * We assume that ngroups is correctly set, no matter what the * errno value is. The man page says, "The ngroups argument * is a value-result argument: on return it always contains * the number of groups found for user." */ (void)getgrouplist(user, gid, NULL, &ngroups); /* Allocate gdata->groups with the right size then call * getgrouplist() a second time to get the actual group list */ groups = gsh_malloc(ngroups * sizeof(gid_t)); if (groups == NULL) return false; if (getgrouplist(user, gid, groups, &ngroups) == -1) { LogEvent(COMPONENT_IDMAPPER, "getgrouplist for user: %s failed retrying", user); gsh_free(groups); /* Try with the largest ngroups we support */ ngroups = 1000; groups2 = gsh_malloc(ngroups * sizeof(gid_t)); if (groups2 == NULL) return false; if (getgrouplist(user, gid, groups2, &ngroups) == -1) { LogWarn(COMPONENT_IDMAPPER, "getgrouplist for user:%s failed, ngroups: %d", user, ngroups); gsh_free(groups2); return false; } /* Resize the buffer */ groups = gsh_realloc(groups2, ngroups * sizeof(gid_t)); if (groups == NULL) /* Use the large buffer! */ groups = groups2; } gdata->groups = groups; gdata->nbgroups = ngroups; return true; }
int nl_rangelist_incremente_size(nl_rangelist_t *array) { int fstatus = -1; array->pre_allocated_ranges += DEFAULT_RANGELIST_INC_SIZE; array->array = gsh_realloc(array->array, array->pre_allocated_ranges * sizeof(nl_range_t)); if (array->array != NULL) fstatus = 0; return fstatus; }
static void memstream_grow(struct memstream *ms, size_t newsize) { char *buf; if (newsize > *ms->lenp) { buf = gsh_realloc(*ms->cp, newsize + 1); if (buf != NULL) { #ifdef DEBUG fprintf(stderr, "MS: %p growing from %zd to %zd\n", ms, *ms->lenp, newsize); #endif memset(buf + *ms->lenp + 1, 0, newsize - *ms->lenp); *ms->cp = buf; *ms->lenp = newsize; } } }
bool pwentuid2grp(uid_t uid, struct gsh_buffdesc *name, struct group_data *pgdata) { char buff[1024]; struct passwd p; struct passwd *pp; if ((getpwuid_r(uid, &p, buff, MAXPATHLEN, &pp) != 0) || (pp == NULL)) { LogEvent(COMPONENT_IDMAPPER, "getpwnam_r %u failed", uid); return false; } /** @todo Waste of memory here. To be fixed */ pgdata->pgroups = (gid_t *) gsh_malloc(MAX_GRP * sizeof(gid_t)); if (pgdata->pgroups == NULL) return false; pgdata->nbgroups = MAX_GRP; if (getgrouplist (p.pw_name, p.pw_gid, pgdata->pgroups, &pgdata->nbgroups) == -1) { LogEvent(COMPONENT_IDMAPPER, "getgrouplist %s failed", p.pw_name); gsh_free(pgdata->pgroups); return false; } /* Resize pgroups to what it should be */ pgdata->pgroups = gsh_realloc(pgdata->pgroups, pgdata->nbgroups * sizeof(gid_t)); /* Set puid */ name->addr = p.pw_name; name->len = strlen(p.pw_name); /* Set uid/gid */ pgdata->uid = p.pw_uid; pgdata->gid = p.pw_gid; return true; }
/* * Read the ACL in GlusterFS format and convert it into fsal ACL before * storing it in fsalattr */ fsal_status_t glusterfs_get_acl(struct glusterfs_export *glfs_export, struct glfs_object *glhandle, glusterfs_fsal_xstat_t *buffxstat, struct attrlist *fsalattr) { fsal_status_t status = { ERR_FSAL_NO_ERROR, 0 }; fsal_acl_data_t acldata; fsal_acl_status_t aclstatus; fsal_ace_t *pace = NULL; int e_count = 0, i_count = 0, new_count = 0, new_i_count = 0; if (fsalattr->acl != NULL) { /* We should never be passed attributes that have an * ACL attached, but just in case some future code * path changes that assumption, let's release the * old ACL properly. */ int acl_status; acl_status = nfs4_acl_release_entry(fsalattr->acl); if (acl_status != NFS_V4_ACL_SUCCESS) LogCrit(COMPONENT_FSAL, "Failed to release old acl, status=%d", acl_status); fsalattr->acl = NULL; } if (NFSv4_ACL_SUPPORT) { buffxstat->e_acl = glfs_h_acl_get(glfs_export->gl_fs->fs, glhandle, ACL_TYPE_ACCESS); if (!buffxstat->e_acl) { status = gluster2fsal_error(errno); return status; } e_count = ace_count(buffxstat->e_acl); if (buffxstat->is_dir) { buffxstat->i_acl = glfs_h_acl_get(glfs_export->gl_fs->fs, glhandle, ACL_TYPE_DEFAULT); i_count = ace_count(buffxstat->i_acl); } /* Allocating memory for both ALLOW and DENY entries */ acldata.naces = 2 * (e_count + i_count); LogDebug(COMPONENT_FSAL, "No of aces present in fsal_acl_t = %d" , acldata.naces); if (!acldata.naces) return status; FSAL_SET_MASK(buffxstat->attr_valid, XATTR_ACL); acldata.aces = (fsal_ace_t *) nfs4_ace_alloc(acldata.naces); pace = acldata.aces; new_count = posix_acl_2_fsal_acl(buffxstat->e_acl, buffxstat->is_dir, false, &pace); if (new_count < 0) return fsalstat(ERR_FSAL_NO_ACE, -1); if (i_count > 0) { new_i_count = posix_acl_2_fsal_acl(buffxstat->i_acl, true, true, &pace); if (new_i_count > 0) new_count += new_i_count; else LogDebug(COMPONENT_FSAL, "Inherit acl is not set for this directory"); } /* Reallocating acldata into the required size */ acldata.aces = (fsal_ace_t *) gsh_realloc(acldata.aces, new_count*sizeof(fsal_ace_t)); acldata.naces = new_count; fsalattr->acl = nfs4_acl_new_entry(&acldata, &aclstatus); LogDebug(COMPONENT_FSAL, "fsal acl = %p, fsal_acl_status = %u", fsalattr->acl, aclstatus); if (fsalattr->acl == NULL) { LogCrit(COMPONENT_FSAL, "failed to create a new acl entry"); return fsalstat(ERR_FSAL_NOMEM, -1); } fsalattr->valid_mask |= ATTR_ACL; } else { /* We were asked for ACL but do not support. */ status = fsalstat(ERR_FSAL_NOTSUPP, 0); } return status; }
/* ---------------------------------------------------------------------------------------------------------------- appends_and_extends_string -------------------------- Appends a char* giving a char* to append and an optionnal separator (NULL if no separator) char** p_io_string : pointer on a char* that will be appended and maybe extended if no enough memory allocated size_t* p_current_length : pointer on a size_t structure that contains the current size of the char* that will be write size_t inc_length : the incrementation step that could be use to extend char* memory allocation char* string2append : string to append char* separator : separator to put between current char* and sting to append return 0 if it succeeds -1 otherwise ---------------------------------------------------------------------------------------------------------------- */ int nodelist_common_string_appends_and_extends(char **p_io_string, size_t * p_current_length, size_t inc_length, char *string2append, char *separator) { int fstatus = -1; size_t new_output_length; size_t new_string_length; size_t output_string_length; size_t separator_length; size_t append_length; char *default_separator = " "; char *local_separator; if (*p_io_string != NULL && string2append != NULL) { if (separator != NULL) local_separator = separator; else local_separator = default_separator; if (strlen(*p_io_string) == 0) local_separator = ""; output_string_length = strlen(*p_io_string); separator_length = strlen(local_separator); append_length = strlen(string2append); new_string_length = output_string_length + separator_length + append_length; if (new_string_length > *p_current_length) { new_output_length = *p_current_length; while (new_string_length > new_output_length) new_output_length += inc_length; *p_io_string = gsh_realloc(*p_io_string, (new_output_length + 1) * sizeof(char)); if (*p_io_string != NULL) *p_current_length = new_output_length; else *p_current_length = 0; } if (*p_io_string != NULL) { strncpy(*p_io_string + output_string_length, local_separator, separator_length); strncpy(*p_io_string + output_string_length + separator_length, string2append, append_length); *(*p_io_string + output_string_length + separator_length + append_length) = '\0'; fstatus = 0; } } return fstatus; }