Debian Codegraph @

runtime/libcry_common.c

raw
1 /* libgcry_common.c
2 * This file hosts functions both being used by the rsyslog runtime as
3 * well as tools who do not use the runtime (so we can maintain the
4 * code at a single place).
5 *
6 * Copyright 2013 Adiscon GmbH.
7 *
8 * This file is part of rsyslog.
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 * -or-
16 * see COPYING.ASL20 in the source distribution
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24 #if HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include <stdio.h>
28 #include <sys/stat.h>
29 #include <sys/uio.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <string.h>
35
36 #include "rsyslog.h" /* we need data typedefs */
37 #include "libcry_common.h"
38
39 /* read a key from a key file
40 * @param[out] key - key buffer, must be freed by caller
41 * @param[out] keylen - length of buffer
42 * @returns 0 if OK, something else otherwise (we do not use
43 * iRet as this is also called from non-rsyslog w/o runtime)
44 * on error, errno is set and can be queried
45 * The key length is limited to 64KiB to prevent DoS.
46 * Note well: key is a blob, not a C string (NUL may be present!)
47 */
48 int cryGetKeyFromFile(const char *const fn, char **const key, unsigned *const keylen) {
49 struct stat sb;
50 int r = -1;
51
52 const int fd = open(fn, O_RDONLY);
53 if (fd < 0) goto done;
54 if (fstat(fd, &sb) == -1) goto done;
55 if (sb.st_size > 64 * 1024) {
56 errno = EMSGSIZE;
57 goto done;
58 }
59 if (sb.st_size == 0) {
60 errno = EINVAL;
61 goto done;
62 }
63 if ((*key = malloc(sb.st_size)) == NULL) goto done;
64 if (read(fd, *key, sb.st_size) != sb.st_size) goto done;
65 *keylen = sb.st_size;
66 r = 0;
67 done:
68 if (fd >= 0) {
69 close(fd);
70 }
71 return r;
72 }
73
74
75 /* execute the child process (must be called in child context
76 * after fork).
77 */
78
79 static void execKeyScript(char *cmd, int pipefd[]) {
80 char *newargv[] = {NULL};
81 char *newenviron[] = {NULL};
82
83 dup2(pipefd[0], STDIN_FILENO);
84 dup2(pipefd[1], STDOUT_FILENO);
85
86 /* finally exec child */
87 fprintf(stderr, "pre execve: %s\n", cmd);
88 execve(cmd, newargv, newenviron);
89 /* switch to?
90 execlp((char*)program, (char*) program, (char*)arg, NULL);
91 */
92
93 /* we should never reach this point, but if we do, we terminate */
94 return;
95 }
96
97
98 static int openPipe(char *cmd, int *fd) {
99 int pipefd[2];
100 pid_t cpid;
101 int r;
102
103 if (pipe(pipefd) == -1) {
104 r = 1;
105 goto done;
106 }
107
108 cpid = fork();
109 if (cpid == -1) {
110 r = 1;
111 goto done;
112 }
113
114 if (cpid == 0) {
115 /* we are the child */
116 execKeyScript(cmd, pipefd);
117 exit(1);
118 }
119
120 close(pipefd[1]);
121 *fd = pipefd[0];
122 r = 0;
123 done:
124 return r;
125 }
126
127
128 /* Read a character from the program's output. */
129 static int readProgChar(int fd, char *c) {
130 int r;
131 if (read(fd, c, 1) != 1) {
132 r = 1;
133 goto done;
134 }
135 r = 0;
136 done:
137 return r;
138 }
139
140 /* Read a line from the script. Line is terminated by LF, which
141 * is NOT put into the buffer.
142 * buf must be 64KiB
143 */
144 static int readProgLine(int fd, char *buf) {
145 char c;
146 int r;
147 unsigned i;
148
149 for (i = 0; i < 64 * 1024; ++i) {
150 if ((r = readProgChar(fd, &c)) != 0) goto done;
151 if (c == '\n') break;
152 buf[i] = c;
153 };
154 if (i >= 64 * 1024) {
155 r = 1;
156 goto done;
157 }
158 buf[i] = '\0';
159 r = 0;
160 done:
161 return r;
162 }
163 static int readProgKey(int fd, char *buf, unsigned keylen) {
164 char c;
165 int r;
166 unsigned i;
167
168 for (i = 0; i < keylen; ++i) {
169 if ((r = readProgChar(fd, &c)) != 0) goto done;
170 buf[i] = c;
171 };
172 r = 0;
173 done:
174 return r;
175 }
176
177 int cryGetKeyFromProg(char *cmd, char **key, unsigned *keylen) {
178 int r;
179 int fd;
180 char rcvBuf[64 * 1024];
181
182 if ((r = openPipe(cmd, &fd)) != 0) goto done;
183 if ((r = readProgLine(fd, rcvBuf)) != 0) goto done;
184 if (strcmp(rcvBuf, "RSYSLOG-KEY-PROVIDER:0")) {
185 r = 2;
186 goto done;
187 }
188 if ((r = readProgLine(fd, rcvBuf)) != 0) goto done;
189 *keylen = atoi(rcvBuf);
190 if ((*key = malloc(*keylen)) == NULL) {
191 r = -1;
192 goto done;
193 }
194 if ((r = readProgKey(fd, *key, *keylen)) != 0) goto done;
195 done:
196 return r;
197 }