1 /*
   2  * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Ucred.xs contains XS wrappers for the process privilege maniplulation
   6  * functions.
   7  */
   8 
   9 
  10 /* Solaris includes. */
  11 #include <ucred.h>
  12 #include <priv.h>
  13 
  14 /* Perl includes. */
  15 #include "EXTERN.h"
  16 #include "perl.h"
  17 #include "XSUB.h"
  18 
  19 typedef int sysret;
  20 typedef priv_set_t Sun__Solaris__Privilege__Privset;
  21 typedef ucred_t Sun__Solaris__Ucred__Ucred;
  22 
  23 static priv_set_t *
  24 dupset(const priv_set_t *s)
  25 {
  26         priv_set_t *new = priv_allocset();
  27         if (new == NULL)
  28                 return (NULL);
  29 
  30         priv_copyset(s, new);
  31         return (new);
  32 }
  33 
  34 #define RETPRIVSET(set)         \
  35         ST(0) = sv_newmortal();                                         \
  36         sv_setref_pv(ST(0), "Sun::Solaris::Privilege::PrivsetPtr",      \
  37             (void*)(set));                                              \
  38         SvREADONLY_on(SvRV(ST(0)))
  39 
  40 #define RETUCRED(uc)            \
  41         ST(0) = sv_newmortal();                                         \
  42         sv_setref_pv(ST(0), "Sun::Solaris::Ucred::UcredPtr",            \
  43             (void*)(uc));                                               \
  44         SvREADONLY_on(SvRV(ST(0)))
  45 /*
  46  * The XS code exported to perl is below here.  Note that the XS preprocessor
  47  * has its own commenting syntax, so all comments from this point on are in
  48  * that form.
  49  */
  50 
  51 MODULE = Sun::Solaris::Ucred PACKAGE = Sun::Solaris::Ucred
  52 PROTOTYPES: ENABLE
  53 
  54 Sun::Solaris::Ucred::Ucred *
  55 ucred_get(pid);
  56         pid_t pid;
  57 
  58 uid_t
  59 ucred_geteuid(uc)
  60         Sun::Solaris::Ucred::Ucred *uc;
  61 
  62 uid_t
  63 ucred_getruid(uc)
  64         Sun::Solaris::Ucred::Ucred *uc;
  65 
  66 uid_t
  67 ucred_getsuid(uc)
  68         Sun::Solaris::Ucred::Ucred *uc;
  69 
  70 gid_t
  71 ucred_getegid(uc)
  72         Sun::Solaris::Ucred::Ucred *uc;
  73 
  74 gid_t
  75 ucred_getrgid(uc)
  76         Sun::Solaris::Ucred::Ucred *uc;
  77 
  78 gid_t
  79 ucred_getsgid(uc)
  80         Sun::Solaris::Ucred::Ucred *uc;
  81 
  82 pid_t
  83 ucred_getpid(uc)
  84         Sun::Solaris::Ucred::Ucred *uc;
  85 
  86 zoneid_t
  87 ucred_getzoneid(uc)
  88         Sun::Solaris::Ucred::Ucred *uc;
  89 
  90 projid_t
  91 ucred_getprojid(uc)
  92         Sun::Solaris::Ucred::Ucred *uc;
  93 
  94 uint_t
  95 ucred_getpflags(uc, flags)
  96         Sun::Solaris::Ucred::Ucred *uc;
  97         uint_t flags;
  98 
  99 Sun::Solaris::Privilege::Privset *
 100 ucred_getprivset(uc, which)
 101         Sun::Solaris::Ucred::Ucred *uc;
 102         const char *which;
 103 PREINIT:
 104         const priv_set_t *val;
 105 CODE:
 106         /*
 107          * Since this function returns a pointer into the ucred_t, we need
 108          * to copy it or perl may free one before the other; and the
 109          * priv_set_t * returned by it doesn't react kindly to free().
 110          */
 111         val = ucred_getprivset(uc, which);
 112         if (val == NULL || (RETVAL = dupset(val)) == NULL)
 113                 XSRETURN_UNDEF;
 114         RETPRIVSET(RETVAL);
 115 
 116 Sun::Solaris::Ucred::Ucred *
 117 getpeerucred(fd)
 118         int fd;
 119 CODE:
 120         RETVAL = NULL;
 121         if (getpeerucred(fd, &RETVAL) != 0)
 122                 XSRETURN_UNDEF;
 123         RETUCRED(RETVAL);
 124 
 125 void
 126 ucred_getgroups(uc)
 127         Sun::Solaris::Ucred::Ucred *uc;
 128 PREINIT:
 129         const gid_t *gids;
 130         int n;
 131 PPCODE:
 132         n = ucred_getgroups(uc, &gids);
 133         if (n < 0)
 134                 XSRETURN_UNDEF;
 135 
 136         PUTBACK;
 137         if (GIMME_V == G_SCALAR) {
 138                 EXTEND(SP, 1);
 139                 PUSHs(sv_2mortal(newSViv(n)));
 140                 PUTBACK;
 141                 XSRETURN(1);
 142         } else if (GIMME_V == G_ARRAY) {
 143                 int i;
 144                 EXTEND(SP, n);
 145 
 146                 for (i = 0; i < n; i++)
 147                         PUSHs(sv_2mortal(newSViv(gids[i])));
 148                 PUTBACK;
 149                 XSRETURN(n);
 150         } else {
 151                 PUTBACK;
 152                 XSRETURN(0);
 153         }
 154 
 155 
 156 
 157 
 158 MODULE = Sun::Solaris::Ucred PACKAGE = Sun::Solaris::Ucred::UcredPtr PREFIX = Ucred_
 159 
 160 void
 161 Ucred_DESTROY(uc)
 162         Sun::Solaris::Ucred::Ucred *uc;
 163 CODE:
 164         ucred_free(uc);
 165