CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
helper_util.cc
Go to the documentation of this file.
1 
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.h"
19 typedef struct json_value JSON;
20 
21 #ifdef __APPLE__
22 #define strdupa(s) \
23  strcpy(/* NOLINT(runtime/printf) */ \
24  reinterpret_cast<char *>(alloca(strlen((s)) + 1)), (s))
25 #endif
26 
27 using namespace std; // NOLINT
28 
34  if (getenv("CVMFS_AUTHZ_HELPER") == NULL) {
35  printf("This program is supposed to be called from the CernVM-FS client.");
36  printf("\n");
37  abort();
38  }
39 }
40 
41 
42 void ParseHandshakeInit(const string &msg) {
43  block_allocator allocator(2048);
44  char *err_pos;
45  char *err_desc;
46  int err_line;
47  JSON *json = json_parse(strdupa(msg.c_str()), &err_pos, &err_desc, &err_line,
48  &allocator);
49  assert((json != NULL) && (json->first_child != NULL));
50  json = json->first_child;
51  assert((string(json->name) == "cvmfs_authz_v1"));
52  json = json->first_child;
53  while (json) {
54  const string name(json->name);
55  if (name == "debug_log") {
56  SetLogAuthzDebug(string(json->string_value) + ".authz");
57  } else if (name == "fqrn") {
58  LogAuthz(kLogAuthzDebug, "fqrn is %s", json->string_value);
59  SetLogAuthzSyslogPrefix(string(json->string_value));
60  } else if (name == "syslog_level") {
61  SetLogAuthzSyslogLevel(json->int_value);
62  } else if (name == "syslog_facility") {
63  SetLogAuthzSyslogFacility(json->int_value);
64  }
65  json = json->next_sibling;
66  }
67 }
68 
69 
70 void ParseRequest(const string &msg) {
71  block_allocator allocator(2048);
72  char *err_pos;
73  char *err_desc;
74  int err_line;
75  JSON *json = json_parse(strdupa(msg.c_str()), &err_pos, &err_desc, &err_line,
76  &allocator);
77  assert((json != NULL) && (json->first_child != NULL));
78  json = json->first_child;
79  assert((string(json->name) == "cvmfs_authz_v1"));
80  json = json->first_child;
81  while (json) {
82  const string name(json->name);
83  if (name == "msgid") {
84  if (json->int_value == 4) { /* kAuthzMsgQuit */
85  LogAuthz(kLogAuthzDebug, "shut down");
86  exit(0);
87  }
88  }
89  json = json->next_sibling;
90  }
91 }
92 
93 
97 static void Read(void *buf, size_t nbyte) {
98  int num_bytes;
99  do {
100  num_bytes = read(fileno(stdin), buf, nbyte);
101  } while ((num_bytes < 0) && (errno == EINTR));
102  assert((num_bytes >= 0) && (static_cast<size_t>(num_bytes) == nbyte));
103 }
104 
105 
109 string ReadMsg() {
110  uint32_t version;
111  uint32_t length;
112  Read(&version, sizeof(version));
113  assert(version == kProtocolVersion);
114  Read(&length, sizeof(length));
115  if (length == 0)
116  return "";
117  char *buf = reinterpret_cast<char *>(alloca(length));
118  Read(buf, length);
119  return string(buf, length);
120 }
121 
122 
126 static void Write(const void *buf, size_t nbyte) {
127  int num_bytes;
128  do {
129  num_bytes = write(fileno(stdout), buf, nbyte);
130  } while ((num_bytes < 0) && (errno == EINTR));
131  assert((num_bytes >= 0) && (static_cast<size_t>(num_bytes) == nbyte));
132 }
133 
134 
138 void WriteMsg(const string &msg) {
139  struct {
140  uint32_t version;
141  uint32_t length;
142  } header;
143  header.version = kProtocolVersion;
144  header.length = msg.length();
145  Write(&header, sizeof(header));
146  Write(msg.data(), header.length);
147 }
void ParseHandshakeInit(const string &msg)
Definition: helper_util.cc:42
static void Read(void *buf, size_t nbyte)
Definition: helper_util.cc:97
const unsigned kLogAuthzDebug
Definition: helper_log.h:10
assert((mem||(size==0))&&"Out Of Memory")
void SetLogAuthzSyslogPrefix(const string &prefix)
Definition: helper_log.cc:95
string ReadMsg()
Definition: helper_util.cc:109
#define strdupa(s)
Definition: platform_osx.h:285
void SetLogAuthzSyslogFacility(const int local_facility)
Definition: helper_log.cc:63
void WriteMsg(const string &msg)
Definition: helper_util.cc:138
void CheckCallContext()
Definition: helper_util.cc:33
void SetLogAuthzDebug(const string &path)
Definition: helper_log.cc:30
static void Write(const void *buf, size_t nbyte)
Definition: helper_util.cc:126
void ParseRequest(const string &msg)
Definition: helper_util.cc:70
void LogAuthz(const int flags, const char *format,...)
Definition: helper_log.cc:111
const unsigned kProtocolVersion
Definition: helper_util.h:10
struct json_value JSON
Definition: helper_allow.cc:11
void SetLogAuthzSyslogLevel(const int level)
Definition: helper_log.cc:45