1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
/*
* Copyright (C) 1999-2008 by CERN/IT/PDP/DM
* All rights reserved
*/
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: Cupv_check.c,v $ $Revision: 1.11 $ $Date: 2008/01/29 10:09:39 $ CERN IT-DS/HSM Ben Couturier";
#endif /* not lint */
#include <errno.h>
#ifndef USE_CUPV
#include <stdio.h>
#endif
#include <string.h>
#include <sys/types.h>
#if defined(_WIN32)
#include <winsock2.h>
#else
#include <unistd.h>
#include <netinet/in.h>
#endif
#include "marshall.h"
#include "Cupv_api.h"
#ifndef USE_CUPV
#ifndef _WIN32
#if defined(_REENTRANT) || defined(_THREAD_SAFE)
#define strtok(X,Y) strtok_r(X,Y,&last)
#endif /* _REENTRANT || _THREAD_SAFE */
#endif /* _WIN32 */
#include "Cns.h"
#else
#include "Cupv.h"
#endif
#include "serrno.h"
extern char localdomain[CA_MAXHOSTNAMELEN+1];
Cupv_check(uid_t priv_uid, gid_t priv_gid, const char *src, const char *tgt, int priv)
{
int c;
char fqn[CA_MAXHOSTNAMELEN+1];
char func[16];
char *getconfent();
gid_t gid;
int l;
#ifndef USE_CUPV
#ifndef _WIN32
#if defined(_REENTRANT) || defined(_THREAD_SAFE)
char *last = NULL;
#endif
#endif
#endif
int msglen;
char *p;
char *q;
char *sbp;
char sendbuf[REQBUFSZ];
struct Cupv_api_thread_info *thip;
uid_t uid;
int lensrc, lentgt;
strcpy (func, "Cupv_check");
#ifdef USE_CUPV
if (Cupv_apiinit (&thip))
return (-1);
uid = geteuid();
gid = getegid();
#if defined(_WIN32)
if (uid < 0 || gid < 0) {
Cupv_errmsg (func, CUP53);
serrno = SENOMAPFND;
return (-1);
}
#endif
#endif
if (priv_uid < 0 || priv_gid < 0 || priv < 0) {
serrno = EINVAL;
return (-1);
}
if (src == NULL) {
lensrc = 0;
} else {
lensrc = strlen(src);
}
if (tgt == NULL) {
lentgt = 0;
} else {
lentgt = strlen(tgt);
}
if (lensrc > CA_MAXREGEXPLEN || lentgt > CA_MAXREGEXPLEN) {
serrno = EINVAL;
return(-1);
}
/* Applying a first check to see if the request is for root */
/* In this case just return without asking the server */
if (priv_uid == 0) {
if (src == NULL && tgt == NULL) {
/* Both NULL, authorized */
return(0);
} else if (strcmp(src, tgt)==0) {
/* src == tmp */
return(0);
}
} /* In other cases, a message is sent to the server for validation */
#ifndef USE_CUPV
if (priv_uid != 0) {
serrno = EACCES;
return (-1);
}
if ((p = getconfent (CNS_SCE, "TRUST", 1)) == NULL) {
serrno = EACCES;
return (-1);
}
l = strlen (localdomain);
for (q = strtok (p, "\t "); q; q = strtok (NULL, "\t ")) {
if (strcmp (src, q) == 0)
return (0);
if (strchr (q, '.'))
continue;
if (strlen (q) + l + 1 > CA_MAXHOSTNAMELEN)
continue;
sprintf (fqn, "%s.%s", q, localdomain);
if (strcmp (src, fqn) == 0)
return (0);
}
serrno = EACCES;
return (-1);
#else
/* Build request header */
sbp = sendbuf;
marshall_LONG (sbp, CUPV_MAGIC);
marshall_LONG (sbp, CUPV_CHECK);
q = sbp; /* save pointer. The next field will be updated */
msglen = 3 * LONGSIZE;
marshall_LONG(sbp, msglen);
marshall_LONG (sbp, uid);
marshall_LONG (sbp, gid);
marshall_LONG (sbp, priv_uid);
marshall_LONG (sbp, priv_gid);
if (src == NULL) {
marshall_STRING (sbp, "");
} else {
marshall_STRING(sbp, src);
}
if (tgt == NULL) {
marshall_STRING (sbp, "");
} else {
marshall_STRING(sbp, tgt);
}
marshall_LONG (sbp, priv);
msglen = sbp - sendbuf;
marshall_LONG (q, msglen); /* update length field */
while ((c = send2Cupv (NULL, sendbuf, msglen, NULL, 0)) &&
serrno == ECUPVNACT)
sleep (RETRYI);
return (c);
#endif
}
#ifndef USE_CUPV
Cupv_seterrbuf(char *buffer, int buflen)
{
return (0);
}
#endif
|