GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/authz/helper_util.cc
Date: 2025-06-22 02:36:02
Exec Total Coverage
Lines: 0 60 0.0%
Branches: 0 50 0.0%

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.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
29 /**
30 * Helper binaries are supposed to be called from the cvmfs client, not
31 * stand-alone.
32 */
33 void CheckCallContext() {
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
94 /**
95 * Get bytes from stdin.
96 */
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
106 /**
107 * Reads a complete message from the cvmfs client.
108 */
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
123 /**
124 * Send bytes to stdout.
125 */
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
135 /**
136 * Sends a (JSON formatted) message back to the cvmfs client.
137 */
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 }
148