2
0
mirror of https://github.com/sudo-project/sudo.git synced 2025-09-05 16:55:57 +00:00

Add sudo_login_name_max() and sudo_host_name_max()

These convenience functions cache the value and handle any potenial
errors from sysconf().
This commit is contained in:
Todd C. Miller
2025-01-14 12:43:13 -07:00
parent 7c075c100d
commit 405070d482
8 changed files with 107 additions and 50 deletions

View File

@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 1996, 1998-2005, 2007-2018
* Copyright (c) 1996, 1998-2005, 2007-2021, 2023-2025
* Todd C. Miller <Todd.Miller@sudo.ws>
*
* Permission to use, copy, modify, and distribute this software for any
@@ -363,12 +363,12 @@ PREFIX(make_gidlist_item)(const struct passwd *pw, int ngids, GETGROUPS_T *gids,
struct cache_item *
PREFIX(make_grlist_item)(const struct passwd *pw, char * const *unused1)
{
const size_t groupname_len = sudo_login_name_max();
size_t len, ngroups, nsize, total;
struct cache_item_grlist *grlitem;
struct group_list *grlist;
struct gid_list *gidlist;
struct group *grp = NULL;
long groupname_len;
char *cp;
int i;
debug_decl(sudo_make_grlist_item, SUDOERS_DEBUG_NSS);
@@ -381,19 +381,11 @@ PREFIX(make_grlist_item)(const struct passwd *pw, char * const *unused1)
debug_return_ptr(NULL);
}
#ifdef _SC_LOGIN_NAME_MAX
groupname_len = sysconf(_SC_LOGIN_NAME_MAX);
if (groupname_len < 32)
groupname_len = 32;
#else
groupname_len = MAX(LOGIN_NAME_MAX, 32);
#endif
/* Allocate in one big chunk for easy freeing. */
nsize = strlen(pw->pw_name) + 1;
total = sizeof(*grlitem) + nsize;
total += sizeof(char *) * (size_t)gidlist->ngids;
total += (size_t)(groupname_len * gidlist->ngids);
total += groupname_len * (size_t)gidlist->ngids;
again:
if ((grlitem = calloc(1, total)) == NULL) {
@@ -432,7 +424,7 @@ again:
if ((grp = sudo_getgrgid(gidlist->gids[i])) != NULL) {
len = strlen(grp->gr_name) + 1;
if ((size_t)(cp - (char *)grlitem) + len > total) {
total += len + (size_t)groupname_len;
total += len + groupname_len;
free(grlitem);
sudo_gr_delref(grp);
goto again;