Blame support/checkgid.c

Packit 90a5c9
/* Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
 * contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
 * this work for additional information regarding copyright ownership.
Packit 90a5c9
 * The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
 * (the "License"); you may not use this file except in compliance with
Packit 90a5c9
 * the License.  You may obtain a copy of the License at
Packit 90a5c9
 *
Packit 90a5c9
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
 *
Packit 90a5c9
 * Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
 * See the License for the specific language governing permissions and
Packit 90a5c9
 * limitations under the License.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
/*
Packit 90a5c9
 * Given one or more group identifiers on the command line (e.g.,
Packit 90a5c9
 * "httpd" or "#-1"), figure out whether they'll be valid for
Packit 90a5c9
 * the server to use at run-time.
Packit 90a5c9
 *
Packit 90a5c9
 * If a groupname isn't found, or we can't setgid() to it, return
Packit 90a5c9
 * -1.  If all groups are valid, return 0.
Packit 90a5c9
 *
Packit 90a5c9
 * This may need to be run as the superuser for the setgid() to
Packit 90a5c9
 * succeed; running it as any other user may result in a false
Packit 90a5c9
 * negative.
Packit 90a5c9
 */
Packit 90a5c9
Packit 90a5c9
#include "ap_config.h"
Packit 90a5c9
#if APR_HAVE_STDIO_H
Packit 90a5c9
#include <stdio.h>
Packit 90a5c9
#endif
Packit 90a5c9
#if APR_HAVE_STDLIB_H
Packit 90a5c9
#include <stdlib.h>
Packit 90a5c9
#endif
Packit 90a5c9
#if APR_HAVE_SYS_TYPES_H
Packit 90a5c9
#include <sys/types.h>
Packit 90a5c9
#endif
Packit 90a5c9
#if HAVE_GRP_H
Packit 90a5c9
#include <grp.h>
Packit 90a5c9
#endif
Packit 90a5c9
#if APR_HAVE_UNISTD_H
Packit 90a5c9
#include <unistd.h>
Packit 90a5c9
#endif
Packit 90a5c9
Packit 90a5c9
int main(int argc, char *argv[])
Packit 90a5c9
{
Packit 90a5c9
    int i;
Packit 90a5c9
    int result;
Packit 90a5c9
    gid_t gid;
Packit 90a5c9
    struct group *grent;
Packit 90a5c9
    struct group fake_grent;
Packit 90a5c9
Packit 90a5c9
    /*
Packit 90a5c9
     * Assume success. :-)
Packit 90a5c9
     */
Packit 90a5c9
    result = 0;
Packit 90a5c9
    for (i = 1; i < argc; ++i) {
Packit 90a5c9
        char *arg;
Packit 90a5c9
        arg = argv[i];
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * If it's from a 'Group #-1' statement, get the numeric value
Packit 90a5c9
         * and skip the group lookup stuff.
Packit 90a5c9
         */
Packit 90a5c9
        if (*arg == '#') {
Packit 90a5c9
            gid = atoi(&arg[1]);
Packit 90a5c9
            fake_grent.gr_gid = gid;
Packit 90a5c9
            grent = &fake_grent;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            grent = getgrnam(arg);
Packit 90a5c9
        }
Packit 90a5c9
Packit 90a5c9
        /*
Packit 90a5c9
         * A NULL return means no such group was found, so we're done
Packit 90a5c9
         * with this one.
Packit 90a5c9
         */
Packit 90a5c9
        if (grent == NULL) {
Packit 90a5c9
            fprintf(stderr, "%s: group '%s' not found\n", argv[0], arg);
Packit 90a5c9
            result = -1;
Packit 90a5c9
        }
Packit 90a5c9
        else {
Packit 90a5c9
            int check;
Packit 90a5c9
Packit 90a5c9
            /*
Packit 90a5c9
             * See if we can switch to the numeric GID we have. If so,
Packit 90a5c9
             * all well and good; if not, well..
Packit 90a5c9
             */
Packit 90a5c9
            gid = grent->gr_gid;
Packit 90a5c9
            check = setgid(gid);
Packit 90a5c9
            if (check != 0) {
Packit 90a5c9
                fprintf(stderr, "%s: invalid group '%s'\n", argv[0], arg);
Packit 90a5c9
                perror(argv[0]);
Packit 90a5c9
                result = -1;
Packit 90a5c9
            }
Packit 90a5c9
        }
Packit 90a5c9
    }
Packit 90a5c9
    /*
Packit 90a5c9
     * Worst-case return value.
Packit 90a5c9
     */
Packit 90a5c9
    return result;
Packit 90a5c9
}
Packit 90a5c9
/*
Packit 90a5c9
 * Local Variables:
Packit 90a5c9
 * mode: C
Packit 90a5c9
 * c-file-style: "bsd"
Packit 90a5c9
 * End:
Packit 90a5c9
 */