2011-07-20 16:54:12 -04:00
|
|
|
/*
|
2019-04-29 07:21:51 -06:00
|
|
|
* SPDX-License-Identifier: ISC
|
|
|
|
*
|
2017-12-03 17:53:40 -07:00
|
|
|
* Copyright (c) 2011-2012, 2014-2016 Todd C. Miller <Todd.Miller@sudo.ws>
|
2011-07-20 16:54:12 -04:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-10-26 08:39:09 -06:00
|
|
|
/*
|
|
|
|
* This is an open source non-commercial project. Dear PVS-Studio, please check it.
|
|
|
|
* PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
|
|
|
*/
|
2018-10-21 08:46:05 -06:00
|
|
|
|
2011-07-20 16:54:12 -04:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
2015-06-19 14:29:27 -06:00
|
|
|
#include <stdlib.h>
|
2015-07-02 09:08:28 -06:00
|
|
|
#include <unistd.h>
|
2011-07-20 16:54:12 -04:00
|
|
|
#include <errno.h>
|
2011-08-23 10:14:52 -04:00
|
|
|
#include <grp.h>
|
2011-07-20 16:54:12 -04:00
|
|
|
#include <limits.h>
|
|
|
|
|
2014-07-22 14:25:16 -06:00
|
|
|
#include "sudo_compat.h"
|
2011-10-22 14:40:21 -04:00
|
|
|
#include "sudo_debug.h"
|
2013-12-12 18:29:07 -07:00
|
|
|
#include "sudo_util.h"
|
2011-07-20 16:54:12 -04:00
|
|
|
|
|
|
|
int
|
2014-07-22 11:26:17 -06:00
|
|
|
sudo_setgroups_v1(int ngids, const GETGROUPS_T *gids)
|
2011-07-20 16:54:12 -04:00
|
|
|
{
|
2016-09-08 16:38:08 -06:00
|
|
|
int maxgids, ret;
|
2015-02-01 08:24:49 -07:00
|
|
|
debug_decl(sudo_setgroups, SUDO_DEBUG_UTIL)
|
2011-07-20 16:54:12 -04:00
|
|
|
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = setgroups(ngids, (GETGROUPS_T *)gids);
|
|
|
|
if (ret == -1 && errno == EINVAL) {
|
2011-07-20 16:54:12 -04:00
|
|
|
/* Too many groups, try again with fewer. */
|
2011-07-22 15:33:33 -04:00
|
|
|
maxgids = (int)sysconf(_SC_NGROUPS_MAX);
|
2011-07-20 16:54:12 -04:00
|
|
|
if (maxgids == -1)
|
|
|
|
maxgids = NGROUPS_MAX;
|
2011-07-22 15:33:33 -04:00
|
|
|
if (ngids > maxgids)
|
2016-09-08 16:38:08 -06:00
|
|
|
ret = setgroups(maxgids, (GETGROUPS_T *)gids);
|
2011-07-20 16:54:12 -04:00
|
|
|
}
|
2016-09-08 16:38:08 -06:00
|
|
|
debug_return_int(ret);
|
2011-07-20 16:54:12 -04:00
|
|
|
}
|