"Fossies" - the Fresh Open Source Software Archive

Member "openbgpd-6.5p0/compat/bsd-setresuid.c" (23 Jan 2019, 1796 Bytes) of package /linux/privat/openbgpd-6.5p0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Copyright (c) 2004, 2005 Darren Tucker (dtucker at zip com au).
    3  *
    4  * Permission to use, copy, modify, and distribute this software for any
    5  * purpose with or without fee is hereby granted, provided that the above
    6  * copyright notice and this permission notice appear in all copies.
    7  *
    8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
    9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   15  */
   16 
   17 #include <sys/types.h>
   18 
   19 #include <errno.h>
   20 #include <unistd.h>
   21 
   22 int
   23 setresuid(uid_t ruid, uid_t euid, uid_t suid)
   24 {
   25     uid_t ouid;
   26     int ret = -1;
   27 
   28     /* Allow only the tested configuration. */
   29     if (ruid != euid || euid != suid) {
   30         errno = ENOSYS;
   31         return -1;
   32     }
   33     ouid = getuid();
   34 
   35 # if defined(HAVE_SETREUID) && !defined(BROKEN_SETREUID)
   36     if ((ret = setreuid(euid, euid)) == -1)
   37         return -1;
   38 # else
   39 #  ifndef SETEUID_BREAKS_SETUID
   40     if (seteuid(euid) == -1)
   41         return -1;
   42 #  endif
   43     if ((ret = setuid(ruid)) == -1)
   44         return -1;
   45 # endif
   46 
   47     /*
   48      * When real, effective and saved uids are the same and we have
   49      * changed uids, sanity check that we cannot restore the old uid.
   50      */
   51     if (ruid == euid && euid == suid && ouid != ruid &&
   52         setuid(ouid) != -1 && seteuid(ouid) != -1) {
   53         errno = EINVAL;
   54         return -1;
   55     }
   56 
   57     /*
   58      * Finally, check that the real and effective uids are what we
   59      * expect.
   60      */
   61     if (getuid() != ruid || geteuid() != euid) {
   62         errno = EACCES;
   63         return -1;
   64     }
   65 
   66     return ret;
   67 }