root/opal/mca/pmix/pmix4x/pmix/src/util/getid.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pmix_util_getid

   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2013 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2007      Cisco Systems, Inc.  All rights reserved.
  13  * Copyright (c) 2015      Intel, Inc. All rights reserved.
  14  * $COPYRIGHT$
  15  *
  16  * Additional copyrights may follow
  17  *
  18  * $HEADER$
  19  */
  20 
  21 /*
  22  * Buffer safe printf functions for portability to archaic platforms.
  23  */
  24 
  25 #include <src/include/pmix_config.h>
  26 #include "include/pmix_common.h"
  27 #include "src/include/pmix_socket_errno.h"
  28 
  29 #ifdef HAVE_UNISTD_H
  30 #include <unistd.h>
  31 #endif
  32 #ifdef HAVE_SYS_TYPES_H
  33 #include <sys/types.h>
  34 #endif
  35 
  36 #include "src/include/pmix_globals.h"
  37 #include "src/util/error.h"
  38 #include "src/util/output.h"
  39 
  40 #include "src/util/getid.h"
  41 
  42 pmix_status_t pmix_util_getid(int sd, uid_t *uid, gid_t *gid)
  43 {
  44 #if defined(SO_PEERCRED)
  45 #ifdef HAVE_STRUCT_SOCKPEERCRED_UID
  46 #define HAVE_STRUCT_UCRED_UID
  47     struct sockpeercred ucred;
  48 #else
  49     struct ucred ucred;
  50 #endif
  51     socklen_t crlen = sizeof (ucred);
  52 #endif
  53 
  54 #if defined(SO_PEERCRED) && (defined(HAVE_STRUCT_UCRED_UID) || defined(HAVE_STRUCT_UCRED_CR_UID))
  55     /* Ignore received 'cred' and validate ucred for socket instead. */
  56     pmix_output_verbose(2, pmix_globals.debug_output,
  57                         "getid: checking getsockopt for peer credentials");
  58     if (getsockopt(sd, SOL_SOCKET, SO_PEERCRED, &ucred, &crlen) < 0) {
  59         pmix_output_verbose(2, pmix_globals.debug_output,
  60                             "getid: getsockopt SO_PEERCRED failed: %s",
  61                             strerror (pmix_socket_errno));
  62         return PMIX_ERR_INVALID_CRED;
  63     }
  64 #if defined(HAVE_STRUCT_UCRED_UID)
  65     *uid = ucred.uid;
  66     *gid = ucred.gid;
  67 #else
  68     *uid = ucred.cr_uid;
  69     *gid = ucred.cr_gid;
  70 #endif
  71 
  72 #elif defined(HAVE_GETPEEREID)
  73     pmix_output_verbose(2, pmix_globals.debug_output,
  74                         "getid: checking getpeereid for peer credentials");
  75     if (0 != getpeereid(sd, uid, gid)) {
  76         pmix_output_verbose(2, pmix_globals.debug_output,
  77                             "getid: getsockopt getpeereid failed: %s",
  78                             strerror (pmix_socket_errno));
  79         return PMIX_ERR_INVALID_CRED;
  80     }
  81 #else
  82     return PMIX_ERR_NOT_SUPPORTED;
  83 #endif
  84 
  85     return PMIX_SUCCESS;
  86 }

/* [<][>][^][v][top][bottom][index][help] */