| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* This file is part of the CernVM File System. |
| 3 |
|
|
*/ |
| 4 |
|
|
|
| 5 |
|
|
#include "helper_util.h" |
| 6 |
|
|
|
| 7 |
|
|
#include <alloca.h> |
| 8 |
|
|
#include <errno.h> |
| 9 |
|
|
#include <stdint.h> |
| 10 |
|
|
#include <unistd.h> |
| 11 |
|
|
|
| 12 |
|
|
#include <cassert> |
| 13 |
|
|
#include <cstdio> |
| 14 |
|
|
#include <cstdlib> |
| 15 |
|
|
#include <cstring> |
| 16 |
|
|
|
| 17 |
|
|
#include "authz/helper_log.h" |
| 18 |
|
|
#include "json_document.h" |
| 19 |
|
|
|
| 20 |
|
|
#ifdef __APPLE__ |
| 21 |
|
|
#define strdupa(s) \ |
| 22 |
|
|
strcpy(/* NOLINT(runtime/printf) */ \ |
| 23 |
|
|
reinterpret_cast<char *>(alloca(strlen((s)) + 1)), (s)) |
| 24 |
|
|
#endif |
| 25 |
|
|
|
| 26 |
|
|
using namespace std; // NOLINT |
| 27 |
|
|
|
| 28 |
|
|
/** |
| 29 |
|
|
* Helper binaries are supposed to be called from the cvmfs client, not |
| 30 |
|
|
* stand-alone. |
| 31 |
|
|
*/ |
| 32 |
|
✗ |
void CheckCallContext() { |
| 33 |
|
✗ |
if (getenv("CVMFS_AUTHZ_HELPER") == NULL) { |
| 34 |
|
✗ |
printf("This program is supposed to be called from the CernVM-FS client."); |
| 35 |
|
✗ |
printf("\n"); |
| 36 |
|
✗ |
abort(); |
| 37 |
|
|
} |
| 38 |
|
|
} |
| 39 |
|
|
|
| 40 |
|
|
|
| 41 |
|
✗ |
void ParseHandshakeInit(const string &msg) { |
| 42 |
|
✗ |
JSON j = JSON::parse(msg); |
| 43 |
|
|
|
| 44 |
|
✗ |
if (j.contains("cvmfs_authz_v1")) { |
| 45 |
|
✗ |
const JSON &config = j["cvmfs_authz_v1"]; |
| 46 |
|
|
|
| 47 |
|
✗ |
if (config.contains("debug_log")) { |
| 48 |
|
✗ |
SetLogAuthzDebug(config["debug_log"].get<string>() + ".authz"); |
| 49 |
|
|
} |
| 50 |
|
✗ |
if (config.contains("fqrn")) { |
| 51 |
|
✗ |
const string fqrn = config["fqrn"].get<string>(); |
| 52 |
|
✗ |
LogAuthz(kLogAuthzDebug, "fqrn is %s", fqrn.c_str()); |
| 53 |
|
✗ |
SetLogAuthzSyslogPrefix(fqrn); |
| 54 |
|
|
} |
| 55 |
|
✗ |
if (config.contains("syslog_level")) { |
| 56 |
|
✗ |
SetLogAuthzSyslogLevel(config["syslog_level"].get<int>()); |
| 57 |
|
|
} |
| 58 |
|
✗ |
if (config.contains("syslog_facility")) { |
| 59 |
|
✗ |
SetLogAuthzSyslogFacility(config["syslog_facility"].get<int>()); |
| 60 |
|
|
} |
| 61 |
|
|
} |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
|
|
| 65 |
|
✗ |
void ParseRequest(const string &msg) { |
| 66 |
|
✗ |
JSON j = JSON::parse(msg); |
| 67 |
|
|
|
| 68 |
|
✗ |
if (j.contains("cvmfs_authz_v1")) { |
| 69 |
|
✗ |
const JSON &req = j["cvmfs_authz_v1"]; |
| 70 |
|
|
|
| 71 |
|
✗ |
if (req.contains("msgid")) { |
| 72 |
|
✗ |
if (req["msgid"].get<int>() == 4) { /* kAuthzMsgQuit */ |
| 73 |
|
✗ |
LogAuthz(kLogAuthzDebug, "shut down"); |
| 74 |
|
✗ |
exit(0); |
| 75 |
|
|
} |
| 76 |
|
|
} |
| 77 |
|
|
} |
| 78 |
|
|
} |
| 79 |
|
|
|
| 80 |
|
|
|
| 81 |
|
|
/** |
| 82 |
|
|
* Get bytes from stdin. |
| 83 |
|
|
*/ |
| 84 |
|
✗ |
static void Read(void *buf, size_t nbyte) { |
| 85 |
|
|
int num_bytes; |
| 86 |
|
|
do { |
| 87 |
|
✗ |
num_bytes = read(fileno(stdin), buf, nbyte); |
| 88 |
|
✗ |
} while ((num_bytes < 0) && (errno == EINTR)); |
| 89 |
|
✗ |
assert((num_bytes >= 0) && (static_cast<size_t>(num_bytes) == nbyte)); |
| 90 |
|
|
} |
| 91 |
|
|
|
| 92 |
|
|
|
| 93 |
|
|
/** |
| 94 |
|
|
* Reads a complete message from the cvmfs client. |
| 95 |
|
|
*/ |
| 96 |
|
✗ |
string ReadMsg() { |
| 97 |
|
|
uint32_t version; |
| 98 |
|
|
uint32_t length; |
| 99 |
|
✗ |
Read(&version, sizeof(version)); |
| 100 |
|
✗ |
assert(version == kProtocolVersion); |
| 101 |
|
✗ |
Read(&length, sizeof(length)); |
| 102 |
|
✗ |
if (length == 0) |
| 103 |
|
✗ |
return ""; |
| 104 |
|
✗ |
char *buf = reinterpret_cast<char *>(alloca(length)); |
| 105 |
|
✗ |
Read(buf, length); |
| 106 |
|
✗ |
return string(buf, length); |
| 107 |
|
|
} |
| 108 |
|
|
|
| 109 |
|
|
|
| 110 |
|
|
/** |
| 111 |
|
|
* Send bytes to stdout. |
| 112 |
|
|
*/ |
| 113 |
|
✗ |
static void Write(const void *buf, size_t nbyte) { |
| 114 |
|
|
int num_bytes; |
| 115 |
|
|
do { |
| 116 |
|
✗ |
num_bytes = write(fileno(stdout), buf, nbyte); |
| 117 |
|
✗ |
} while ((num_bytes < 0) && (errno == EINTR)); |
| 118 |
|
✗ |
assert((num_bytes >= 0) && (static_cast<size_t>(num_bytes) == nbyte)); |
| 119 |
|
|
} |
| 120 |
|
|
|
| 121 |
|
|
|
| 122 |
|
|
/** |
| 123 |
|
|
* Sends a (JSON formatted) message back to the cvmfs client. |
| 124 |
|
|
*/ |
| 125 |
|
✗ |
void WriteMsg(const string &msg) { |
| 126 |
|
|
struct { |
| 127 |
|
|
uint32_t version; |
| 128 |
|
|
uint32_t length; |
| 129 |
|
|
} header; |
| 130 |
|
✗ |
header.version = kProtocolVersion; |
| 131 |
|
✗ |
header.length = msg.length(); |
| 132 |
|
✗ |
Write(&header, sizeof(header)); |
| 133 |
|
✗ |
Write(msg.data(), header.length); |
| 134 |
|
|
} |
| 135 |
|
|
|