| 1 |
/* gss-misc.c |
| 2 |
* This is a miscellaneous helper class for gss-api features. |
| 3 |
* |
| 4 |
* Copyright 2007-2017 Rainer Gerhards and Adiscon GmbH. |
| 5 |
* |
| 6 |
* This file is part of rsyslog. |
| 7 |
* |
| 8 |
* Rsyslog is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* Rsyslog is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with Rsyslog. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
* A copy of the GPL can be found in the file "COPYING" in this distribution. |
| 22 |
*/ |
| 23 |
#include "config.h" |
| 24 |
#include "rsyslog.h" |
| 25 |
#include <stdio.h> |
| 26 |
#include <stdarg.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <time.h> |
| 30 |
#include <netinet/in.h> |
| 31 |
#include <netdb.h> |
| 32 |
#include <arpa/inet.h> |
| 33 |
#include <fnmatch.h> |
| 34 |
#include <assert.h> |
| 35 |
#include <errno.h> |
| 36 |
#include <ctype.h> |
| 37 |
#include <unistd.h> |
| 38 |
#include <pthread.h> |
| 39 |
#include <gssapi/gssapi.h> |
| 40 |
#include "dirty.h" |
| 41 |
#include "syslogd-types.h" |
| 42 |
#include "srUtils.h" |
| 43 |
#include "net.h" |
| 44 |
#include "template.h" |
| 45 |
#include "msg.h" |
| 46 |
#include "module-template.h" |
| 47 |
#include "obj.h" |
| 48 |
#include "errmsg.h" |
| 49 |
#include "gss-misc.h" |
| 50 |
#include "gss-token-util.h" |
| 51 |
#include "debug.h" |
| 52 |
#include "glbl.h" |
| 53 |
#include "unlimited_select.h" |
| 54 |
|
| 55 |
MODULE_TYPE_LIB |
| 56 |
MODULE_TYPE_NOKEEP; |
| 57 |
|
| 58 |
/* static data */ |
| 59 |
DEFobjStaticHelpers; |
| 60 |
DEFobjCurrIf(glbl) |
| 61 |
|
| 62 |
static void display_status_(char *m, OM_uint32 code, int type) { |
| 63 |
OM_uint32 maj_stat, min_stat, msg_ctx = 0; |
| 64 |
gss_buffer_desc msg; |
| 65 |
|
| 66 |
do { |
| 67 |
maj_stat = gss_display_status(&min_stat, code, type, GSS_C_NO_OID, &msg_ctx, &msg); |
| 68 |
if (maj_stat != GSS_S_COMPLETE) { |
| 69 |
LogError(0, NO_ERRCODE, "GSS-API error in gss_display_status called from <%s>\n", m); |
| 70 |
break; |
| 71 |
} else { |
| 72 |
char buf[1024]; |
| 73 |
snprintf(buf, sizeof(buf), "GSS-API error %s: %s\n", m, (char *)msg.value); |
| 74 |
buf[sizeof(buf) - 1] = '\0'; |
| 75 |
LogError(0, NO_ERRCODE, "%s", buf); |
| 76 |
} |
| 77 |
if (msg.length != 0) gss_release_buffer(&min_stat, &msg); |
| 78 |
} while (msg_ctx); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
static void display_status(char *m, OM_uint32 maj_stat, OM_uint32 min_stat) { |
| 83 |
display_status_(m, maj_stat, GSS_C_GSS_CODE); |
| 84 |
display_status_(m, min_stat, GSS_C_MECH_CODE); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
static void display_ctx_flags(OM_uint32 flags) { |
| 89 |
if (flags & GSS_C_DELEG_FLAG) dbgprintf("GSS_C_DELEG_FLAG\n"); |
| 90 |
if (flags & GSS_C_MUTUAL_FLAG) dbgprintf("GSS_C_MUTUAL_FLAG\n"); |
| 91 |
if (flags & GSS_C_REPLAY_FLAG) dbgprintf("GSS_C_REPLAY_FLAG\n"); |
| 92 |
if (flags & GSS_C_SEQUENCE_FLAG) dbgprintf("GSS_C_SEQUENCE_FLAG\n"); |
| 93 |
if (flags & GSS_C_CONF_FLAG) dbgprintf("GSS_C_CONF_FLAG\n"); |
| 94 |
if (flags & GSS_C_INTEG_FLAG) dbgprintf("GSS_C_INTEG_FLAG\n"); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
static ssize_t read_all(int fd, char *buf, size_t nbyte, time_t deadline) { |
| 99 |
ssize_t ret; |
| 100 |
char *ptr; |
| 101 |
struct timeval tv; |
| 102 |
struct timeval *ptv; |
| 103 |
#ifdef USE_UNLIMITED_SELECT |
| 104 |
fd_set *pRfds = malloc(glbl.GetFdSetSize()); |
| 105 |
|
| 106 |
if (pRfds == NULL) return -1; |
| 107 |
#else |
| 108 |
fd_set rfds; |
| 109 |
fd_set *pRfds = &rfds; |
| 110 |
#endif |
| 111 |
|
| 112 |
for (ptr = buf; nbyte;) { |
| 113 |
if (deadline != 0) { |
| 114 |
const time_t now = time(NULL); |
| 115 |
if (now == (time_t)-1 || now >= deadline) { |
| 116 |
errno = ETIMEDOUT; |
| 117 |
freeFdSet(pRfds); |
| 118 |
return -1; |
| 119 |
} |
| 120 |
tv.tv_sec = 1; |
| 121 |
tv.tv_usec = 0; |
| 122 |
ptv = &tv; |
| 123 |
} else { |
| 124 |
ptv = NULL; |
| 125 |
} |
| 126 |
FD_ZERO(pRfds); |
| 127 |
FD_SET(fd, pRfds); |
| 128 |
|
| 129 |
ret = select(fd + 1, pRfds, NULL, NULL, ptv); |
| 130 |
if (ret < 0) { |
| 131 |
if (errno == EINTR) continue; |
| 132 |
freeFdSet(pRfds); |
| 133 |
return ret; |
| 134 |
} |
| 135 |
if (ret == 0) continue; |
| 136 |
if (!FD_ISSET(fd, pRfds)) { |
| 137 |
errno = EIO; |
| 138 |
freeFdSet(pRfds); |
| 139 |
return -1; |
| 140 |
} |
| 141 |
ret = recv(fd, ptr, nbyte, 0); |
| 142 |
if (ret < 0) { |
| 143 |
if (errno == EINTR) continue; |
| 144 |
freeFdSet(pRfds); |
| 145 |
return (ret); |
| 146 |
} else if (ret == 0) { |
| 147 |
freeFdSet(pRfds); |
| 148 |
return (ptr - buf); |
| 149 |
} |
| 150 |
ptr += ret; |
| 151 |
nbyte -= ret; |
| 152 |
} |
| 153 |
|
| 154 |
freeFdSet(pRfds); |
| 155 |
return (ptr - buf); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
static int write_all(int fd, char *buf, unsigned int nbyte) { |
| 160 |
int ret; |
| 161 |
char *ptr; |
| 162 |
|
| 163 |
for (ptr = buf; nbyte;) { |
| 164 |
ret = send(fd, ptr, nbyte, 0); |
| 165 |
if (ret < 0) { |
| 166 |
if (errno == EINTR) continue; |
| 167 |
return (ret); |
| 168 |
} else if (ret == 0) { |
| 169 |
return (ptr - buf); |
| 170 |
} |
| 171 |
ptr += ret; |
| 172 |
nbyte -= ret; |
| 173 |
} |
| 174 |
|
| 175 |
return (ptr - buf); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
static int recv_token(int s, gss_buffer_t tok, size_t max_tok_len, unsigned int timeout_secs) { |
| 180 |
ssize_t ret; |
| 181 |
unsigned char lenbuf[4] = "xxx"; // initialized to make clang static analyzer happy |
| 182 |
size_t len = 0; |
| 183 |
time_t deadline = 0; |
| 184 |
|
| 185 |
assert(tok != NULL); |
| 186 |
tok->length = 0; |
| 187 |
tok->value = NULL; |
| 188 |
|
| 189 |
if (timeout_secs > 0) { |
| 190 |
deadline = time(NULL); |
| 191 |
if (deadline == (time_t)-1) { |
| 192 |
errno = EIO; |
| 193 |
LogError(errno, NO_ERRCODE, "GSS-API error obtaining current time for token deadline"); |
| 194 |
return -1; |
| 195 |
} |
| 196 |
deadline += timeout_secs; |
| 197 |
} |
| 198 |
|
| 199 |
ret = read_all(s, (char *)lenbuf, 4, deadline); |
| 200 |
if (ret < 0) { |
| 201 |
LogError(0, NO_ERRCODE, "GSS-API error reading token length"); |
| 202 |
return -1; |
| 203 |
} else if (!ret) { |
| 204 |
return 0; |
| 205 |
} else if (ret != 4) { |
| 206 |
LogError(0, NO_ERRCODE, "GSS-API error reading token length"); |
| 207 |
return -1; |
| 208 |
} |
| 209 |
|
| 210 |
if (gssTokenDecodeLength(lenbuf, max_tok_len, &len) != RS_RET_OK) { |
| 211 |
LogError(0, NO_ERRCODE, "GSS-API rejecting token length %zu that exceeds limit %zu", len, max_tok_len); |
| 212 |
return -1; |
| 213 |
} |
| 214 |
tok->length = len; |
| 215 |
|
| 216 |
tok->value = (char *)malloc(tok->length ? tok->length : 1); |
| 217 |
if (tok->length && tok->value == NULL) { |
| 218 |
LogError(0, NO_ERRCODE, "Out of memory allocating token data\n"); |
| 219 |
return -1; |
| 220 |
} |
| 221 |
|
| 222 |
ret = read_all(s, (char *)tok->value, tok->length, deadline); |
| 223 |
if (ret < 0) { |
| 224 |
LogError(0, NO_ERRCODE, "GSS-API error reading token data"); |
| 225 |
free(tok->value); |
| 226 |
return -1; |
| 227 |
} else if ((size_t)ret != tok->length) { |
| 228 |
LogError(0, NO_ERRCODE, "GSS-API error reading token data"); |
| 229 |
free(tok->value); |
| 230 |
return -1; |
| 231 |
} |
| 232 |
|
| 233 |
return 1; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
static int send_token(int s, gss_buffer_t tok) { |
| 238 |
int ret; |
| 239 |
unsigned char lenbuf[4]; |
| 240 |
unsigned int len; |
| 241 |
|
| 242 |
if (tok->length > 0xffffffffUL) { |
| 243 |
LogError(0, NO_ERRCODE, "GSS-API token length %lu exceeds wire format limit", (unsigned long)tok->length); |
| 244 |
return -1; |
| 245 |
} |
| 246 |
len = htonl(tok->length); |
| 247 |
lenbuf[0] = (len >> 24) & 0xff; |
| 248 |
lenbuf[1] = (len >> 16) & 0xff; |
| 249 |
lenbuf[2] = (len >> 8) & 0xff; |
| 250 |
lenbuf[3] = len & 0xff; |
| 251 |
|
| 252 |
ret = write_all(s, (char *)lenbuf, 4); |
| 253 |
if (ret < 0) { |
| 254 |
LogError(0, NO_ERRCODE, "GSS-API error sending token length"); |
| 255 |
return -1; |
| 256 |
} else if (ret != 4) { |
| 257 |
LogError(0, NO_ERRCODE, "GSS-API error sending token length"); |
| 258 |
return -1; |
| 259 |
} |
| 260 |
|
| 261 |
ret = write_all(s, tok->value, tok->length); |
| 262 |
if (ret < 0) { |
| 263 |
LogError(0, NO_ERRCODE, "GSS-API error sending token data"); |
| 264 |
return -1; |
| 265 |
} else if (ret != (int)tok->length) { |
| 266 |
LogError(0, NO_ERRCODE, "GSS-API error sending token data"); |
| 267 |
return -1; |
| 268 |
} |
| 269 |
|
| 270 |
return 0; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
/* queryInterface function |
| 275 |
* rgerhards, 2008-02-29 |
| 276 |
*/ |
| 277 |
BEGINobjQueryInterface(gssutil) |
| 278 |
CODESTARTobjQueryInterface(gssutil); |
| 279 |
if (pIf->ifVersion != gssutilCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 280 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 281 |
} |
| 282 |
|
| 283 |
/* ok, we have the right interface, so let's fill it |
| 284 |
* Please note that we may also do some backwards-compatibility |
| 285 |
* work here (if we can support an older interface version - that, |
| 286 |
* of course, also affects the "if" above). |
| 287 |
*/ |
| 288 |
pIf->recv_token = recv_token; |
| 289 |
pIf->send_token = send_token; |
| 290 |
pIf->display_status = display_status; |
| 291 |
pIf->display_ctx_flags = display_ctx_flags; |
| 292 |
|
| 293 |
finalize_it: |
| 294 |
ENDobjQueryInterface(gssutil) |
| 295 |
|
| 296 |
|
| 297 |
/* exit our class |
| 298 |
* rgerhards, 2008-03-10 |
| 299 |
*/ |
| 300 |
BEGINObjClassExit(gssutil, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */ |
| 301 |
CODESTARTObjClassExit(gssutil); |
| 302 |
/* release objects we no longer need */ |
| 303 |
objRelease(glbl, CORE_COMPONENT); |
| 304 |
ENDObjClassExit(gssutil) |
| 305 |
|
| 306 |
|
| 307 |
/* Initialize our class. Must be called as the very first method |
| 308 |
* before anything else is called inside this class. |
| 309 |
* rgerhards, 2008-02-29 |
| 310 |
*/ |
| 311 |
BEGINAbstractObjClassInit(gssutil, 1, OBJ_IS_LOADABLE_MODULE) /* class, version - CHANGE class also in END MACRO! */ |
| 312 |
/* request objects we use */ |
| 313 |
CHKiRet(objUse(glbl, CORE_COMPONENT)); |
| 314 |
ENDObjClassInit(gssutil) |
| 315 |
|
| 316 |
|
| 317 |
/* --------------- here now comes the plumbing that makes as a library module --------------- */ |
| 318 |
|
| 319 |
|
| 320 |
BEGINmodExit |
| 321 |
CODESTARTmodExit; |
| 322 |
gssutilClassExit(); |
| 323 |
ENDmodExit |
| 324 |
|
| 325 |
|
| 326 |
BEGINqueryEtryPt |
| 327 |
CODESTARTqueryEtryPt; |
| 328 |
CODEqueryEtryPt_STD_LIB_QUERIES; |
| 329 |
ENDqueryEtryPt |
| 330 |
|
| 331 |
|
| 332 |
BEGINmodInit() |
| 333 |
CODESTARTmodInit; |
| 334 |
*ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */ |
| 335 |
|
| 336 |
/* Initialize all classes that are in our module - this includes ourselfs */ |
| 337 |
CHKiRet(gssutilClassInit(pModInfo)); /* must be done after tcps_sess, as we use it */ |
| 338 |
ENDmodInit |