PostgreSQL Source Code git master
Macros | Functions
fe-secure-gssapi.c File Reference
#include "postgres_fe.h"
#include "fe-gssapi-common.h"
#include "libpq-fe.h"
#include "libpq-int.h"
#include "port/pg_bswap.h"
Include dependency graph for fe-secure-gssapi.c:

Go to the source code of this file.

Macros

#define  GSS_REQUIRED_FLAGS
 
#define  PQ_GSS_MAX_PACKET_SIZE   16384 /* includes uint32 header word */
 
#define  PQ_GSS_AUTH_BUFFER_SIZE   65536 /* includes uint32 header word */
 
#define  PqGSSSendBuffer   (conn->gss_SendBuffer)
 
#define  PqGSSSendLength   (conn->gss_SendLength)
 
#define  PqGSSSendNext   (conn->gss_SendNext)
 
#define  PqGSSSendConsumed   (conn->gss_SendConsumed)
 
#define  PqGSSRecvBuffer   (conn->gss_RecvBuffer)
 
#define  PqGSSRecvLength   (conn->gss_RecvLength)
 
#define  PqGSSResultBuffer   (conn->gss_ResultBuffer)
 
#define  PqGSSResultLength   (conn->gss_ResultLength)
 
#define  PqGSSResultNext   (conn->gss_ResultNext)
 
#define  PqGSSMaxPktSize   (conn->gss_MaxPktSize)
 

Functions

ssize_t  pg_GSS_write (PGconn *conn, const void *ptr, size_t len)
 
ssize_t  pg_GSS_read (PGconn *conn, void *ptr, size_t len)
 
static PostgresPollingStatusType  gss_read (PGconn *conn, void *recv_buffer, size_t length, ssize_t *ret)
 
 
void *  PQgetgssctx (PGconn *conn)
 
 

Macro Definition Documentation

GSS_REQUIRED_FLAGS

#define GSS_REQUIRED_FLAGS
Value:
GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG | \
GSS_C_SEQUENCE_FLAG | GSS_C_CONF_FLAG | GSS_C_INTEG_FLAG

Definition at line 26 of file fe-secure-gssapi.c.

PQ_GSS_AUTH_BUFFER_SIZE

#define PQ_GSS_AUTH_BUFFER_SIZE   65536 /* includes uint32 header word */

Definition at line 61 of file fe-secure-gssapi.c.

PQ_GSS_MAX_PACKET_SIZE

#define PQ_GSS_MAX_PACKET_SIZE   16384 /* includes uint32 header word */

Definition at line 53 of file fe-secure-gssapi.c.

PqGSSMaxPktSize

#define PqGSSMaxPktSize   (conn->gss_MaxPktSize)

Definition at line 77 of file fe-secure-gssapi.c.

PqGSSRecvBuffer

#define PqGSSRecvBuffer   (conn->gss_RecvBuffer)

Definition at line 72 of file fe-secure-gssapi.c.

PqGSSRecvLength

#define PqGSSRecvLength   (conn->gss_RecvLength)

Definition at line 73 of file fe-secure-gssapi.c.

PqGSSResultBuffer

#define PqGSSResultBuffer   (conn->gss_ResultBuffer)

Definition at line 74 of file fe-secure-gssapi.c.

PqGSSResultLength

#define PqGSSResultLength   (conn->gss_ResultLength)

Definition at line 75 of file fe-secure-gssapi.c.

PqGSSResultNext

#define PqGSSResultNext   (conn->gss_ResultNext)

Definition at line 76 of file fe-secure-gssapi.c.

PqGSSSendBuffer

#define PqGSSSendBuffer   (conn->gss_SendBuffer)

Definition at line 68 of file fe-secure-gssapi.c.

PqGSSSendConsumed

#define PqGSSSendConsumed   (conn->gss_SendConsumed)

Definition at line 71 of file fe-secure-gssapi.c.

PqGSSSendLength

#define PqGSSSendLength   (conn->gss_SendLength)

Definition at line 69 of file fe-secure-gssapi.c.

PqGSSSendNext

#define PqGSSSendNext   (conn->gss_SendNext)

Definition at line 70 of file fe-secure-gssapi.c.

Function Documentation

gss_read()

static PostgresPollingStatusType gss_read ( PGconnconn,
void *  recv_buffer,
size_t  length,
ssize_t *  ret 
)
static

Definition at line 435 of file fe-secure-gssapi.c.

436{
437 *ret = pqsecure_raw_read(conn, recv_buffer, length);
438 if (*ret < 0)
439 {
441 SOCK_ERRNO == EINTR)
443 else
445 }
446
447 /* Check for EOF */
448 if (*ret == 0)
449 {
450 int result = pqReadReady(conn);
451
452 if (result < 0)
454
455 if (!result)
457
458 *ret = pqsecure_raw_read(conn, recv_buffer, length);
459 if (*ret < 0)
460 {
462 SOCK_ERRNO == EINTR)
464 else
466 }
467 if (*ret == 0)
469 }
470
471 return PGRES_POLLING_OK;
472}
int pqReadReady(PGconn *conn)
Definition: fe-misc.c:1058
ssize_t pqsecure_raw_read(PGconn *conn, void *ptr, size_t len)
Definition: fe-secure.c:193
@ PGRES_POLLING_OK
Definition: libpq-fe.h:118
@ PGRES_POLLING_READING
Definition: libpq-fe.h:116
@ PGRES_POLLING_FAILED
Definition: libpq-fe.h:115
#define SOCK_ERRNO
Definition: oauth-utils.c:164
PGconn * conn
Definition: streamutil.c:52
#define EINTR
Definition: win32_port.h:364
#define EWOULDBLOCK
Definition: win32_port.h:370
#define EAGAIN
Definition: win32_port.h:362

References conn, EAGAIN, EINTR, EWOULDBLOCK, PGRES_POLLING_FAILED, PGRES_POLLING_OK, PGRES_POLLING_READING, pqReadReady(), pqsecure_raw_read(), and SOCK_ERRNO.

Referenced by pqsecure_open_gss().

pg_GSS_read()

ssize_t pg_GSS_read ( PGconnconn,
void *  ptr,
size_t  len 
)

Definition at line 266 of file fe-secure-gssapi.c.

267{
268 OM_uint32 major,
269 minor;
270 gss_buffer_desc input = GSS_C_EMPTY_BUFFER,
271 output = GSS_C_EMPTY_BUFFER;
272 ssize_t ret;
273 size_t bytes_returned = 0;
274 gss_ctx_id_t gctx = conn->gctx;
275
276 /*
277 * The plan here is to read one incoming encrypted packet into
278 * PqGSSRecvBuffer, decrypt it into PqGSSResultBuffer, and then dole out
279 * data from there to the caller. When we exhaust the current input
280 * packet, read another.
281 */
282 while (bytes_returned < len)
283 {
284 int conf_state = 0;
285
286 /* Check if we have data in our buffer that we can return immediately */
288 {
289 size_t bytes_in_buffer = PqGSSResultLength - PqGSSResultNext;
290 size_t bytes_to_copy = Min(bytes_in_buffer, len - bytes_returned);
291
292 /*
293 * Copy the data from our result buffer into the caller's buffer,
294 * at the point where we last left off filling their buffer.
295 */
296 memcpy((char *) ptr + bytes_returned, PqGSSResultBuffer + PqGSSResultNext, bytes_to_copy);
297 PqGSSResultNext += bytes_to_copy;
298 bytes_returned += bytes_to_copy;
299
300 /*
301 * At this point, we've either filled the caller's buffer or
302 * emptied our result buffer. Either way, return to caller. In
303 * the second case, we could try to read another encrypted packet,
304 * but the odds are good that there isn't one available. (If this
305 * isn't true, we chose too small a max packet size.) In any
306 * case, there's no harm letting the caller process the data we've
307 * already returned.
308 */
309 break;
310 }
311
312 /* Result buffer is empty, so reset buffer pointers */
314
315 /*
316 * Because we chose above to return immediately as soon as we emit
317 * some data, bytes_returned must be zero at this point. Therefore
318 * the failure exits below can just return -1 without worrying about
319 * whether we already emitted some data.
320 */
321 Assert(bytes_returned == 0);
322
323 /*
324 * At this point, our result buffer is empty with more bytes being
325 * requested to be read. We are now ready to load the next packet and
326 * decrypt it (entirely) into our result buffer.
327 */
328
329 /* Collect the length if we haven't already */
330 if (PqGSSRecvLength < sizeof(uint32))
331 {
333 sizeof(uint32) - PqGSSRecvLength);
334
335 /* If ret <= 0, pqsecure_raw_read already set the correct errno */
336 if (ret <= 0)
337 return ret;
338
339 PqGSSRecvLength += ret;
340
341 /* If we still haven't got the length, return to the caller */
342 if (PqGSSRecvLength < sizeof(uint32))
343 {
345 return -1;
346 }
347 }
348
349 /* Decode the packet length and check for overlength packet */
350 input.length = pg_ntoh32(*(uint32 *) PqGSSRecvBuffer);
351
352 if (input.length > PQ_GSS_MAX_PACKET_SIZE - sizeof(uint32))
353 {
354 libpq_append_conn_error(conn, "oversize GSSAPI packet sent by the server (%zu > %zu)",
355 (size_t) input.length,
357 SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
358 return -1;
359 }
360
361 /*
362 * Read as much of the packet as we are able to on this call into
363 * wherever we left off from the last time we were called.
364 */
366 input.length - (PqGSSRecvLength - sizeof(uint32)));
367 /* If ret <= 0, pqsecure_raw_read already set the correct errno */
368 if (ret <= 0)
369 return ret;
370
371 PqGSSRecvLength += ret;
372
373 /* If we don't yet have the whole packet, return to the caller */
374 if (PqGSSRecvLength - sizeof(uint32) < input.length)
375 {
377 return -1;
378 }
379
380 /*
381 * We now have the full packet and we can perform the decryption and
382 * refill our result buffer, then loop back up to pass data back to
383 * the caller. Note that error exits below here must take care of
384 * releasing the gss output buffer.
385 */
386 output.value = NULL;
387 output.length = 0;
388 input.value = PqGSSRecvBuffer + sizeof(uint32);
389
390 major = gss_unwrap(&minor, gctx, &input, &output, &conf_state, NULL);
391 if (major != GSS_S_COMPLETE)
392 {
393 pg_GSS_error(libpq_gettext("GSSAPI unwrap error"), conn,
394 major, minor);
395 ret = -1;
396 SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
397 goto cleanup;
398 }
399
400 if (conf_state == 0)
401 {
402 libpq_append_conn_error(conn, "incoming GSSAPI message did not use confidentiality");
403 ret = -1;
404 SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
405 goto cleanup;
406 }
407
408 memcpy(PqGSSResultBuffer, output.value, output.length);
409 PqGSSResultLength = output.length;
410
411 /* Our receive buffer is now empty, reset it */
412 PqGSSRecvLength = 0;
413
414 /* Release buffer storage allocated by GSSAPI */
415 gss_release_buffer(&minor, &output);
416 }
417
418 ret = bytes_returned;
419
420cleanup:
421 /* Release GSSAPI buffer storage, if we didn't already */
422 if (output.value != NULL)
423 gss_release_buffer(&minor, &output);
424 return ret;
425}
void pg_GSS_error(const char *errmsg, OM_uint32 maj_stat, OM_uint32 min_stat)
static void cleanup(void)
Definition: bootstrap.c:715
#define Min(x, y)
Definition: c.h:1003
uint32_t uint32
Definition: c.h:538
#define PqGSSResultNext
#define PqGSSResultLength
#define PqGSSRecvBuffer
#define PqGSSRecvLength
#define PQ_GSS_MAX_PACKET_SIZE
#define PqGSSResultBuffer
Assert(PointerIsAligned(start, uint64))
FILE * input
FILE * output
#define SOCK_ERRNO_SET(e)
Definition: oauth-utils.c:165
void libpq_append_conn_error(PGconn *conn, const char *fmt,...)
Definition: oauth-utils.c:95
#define libpq_gettext(x)
Definition: oauth-utils.h:86
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
const void size_t len

References Assert(), cleanup(), conn, EWOULDBLOCK, input, len, libpq_append_conn_error(), libpq_gettext, Min, output, pg_GSS_error(), pg_ntoh32, PQ_GSS_MAX_PACKET_SIZE, PqGSSRecvBuffer, PqGSSRecvLength, PqGSSResultBuffer, PqGSSResultLength, PqGSSResultNext, pqsecure_raw_read(), and SOCK_ERRNO_SET.

Referenced by pqsecure_read().

pg_GSS_write()

ssize_t pg_GSS_write ( PGconnconn,
const void *  ptr,
size_t  len 
)

Definition at line 93 of file fe-secure-gssapi.c.

94{
95 OM_uint32 major,
96 minor;
97 gss_buffer_desc input,
98 output = GSS_C_EMPTY_BUFFER;
99 ssize_t ret = -1;
100 size_t bytes_to_encrypt;
101 size_t bytes_encrypted;
102 gss_ctx_id_t gctx = conn->gctx;
103
104 /*
105 * When we get a retryable failure, we must not tell the caller we have
106 * successfully transmitted everything, else it won't retry. For
107 * simplicity, we claim we haven't transmitted anything until we have
108 * successfully transmitted all "len" bytes. Between calls, the amount of
109 * the current input data that's already been encrypted and placed into
110 * PqGSSSendBuffer (and perhaps transmitted) is remembered in
111 * PqGSSSendConsumed. On a retry, the caller *must* be sending that data
112 * again, so if it offers a len less than that, something is wrong.
113 *
114 * Note: it may seem attractive to report partial write completion once
115 * we've successfully sent any encrypted packets. However, doing that
116 * expands the state space of this processing and has been responsible for
117 * bugs in the past (cf. commit d053a879b). We won't save much,
118 * typically, by letting callers discard data early, so don't risk it.
119 */
121 {
123 "GSSAPI caller failed to retransmit all data needing to be retried\n");
124 SOCK_ERRNO_SET(EINVAL);
125 return -1;
126 }
127
128 /* Discount whatever source data we already encrypted. */
129 bytes_to_encrypt = len - PqGSSSendConsumed;
130 bytes_encrypted = PqGSSSendConsumed;
131
132 /*
133 * Loop through encrypting data and sending it out until it's all done or
134 * pqsecure_raw_write() complains (which would likely mean that the socket
135 * is non-blocking and the requested send() would block, or there was some
136 * kind of actual error).
137 */
138 while (bytes_to_encrypt || PqGSSSendLength)
139 {
140 int conf_state = 0;
141 uint32 netlen;
142
143 /*
144 * Check if we have data in the encrypted output buffer that needs to
145 * be sent (possibly left over from a previous call), and if so, try
146 * to send it. If we aren't able to, return that fact back up to the
147 * caller.
148 */
149 if (PqGSSSendLength)
150 {
151 ssize_t retval;
152 ssize_t amount = PqGSSSendLength - PqGSSSendNext;
153
155 if (retval <= 0)
156 return retval;
157
158 /*
159 * Check if this was a partial write, and if so, move forward that
160 * far in our buffer and try again.
161 */
162 if (retval < amount)
163 {
164 PqGSSSendNext += retval;
165 continue;
166 }
167
168 /* We've successfully sent whatever data was in the buffer. */
170 }
171
172 /*
173 * Check if there are any bytes left to encrypt. If not, we're done.
174 */
175 if (!bytes_to_encrypt)
176 break;
177
178 /*
179 * Check how much we are being asked to send, if it's too much, then
180 * we will have to loop and possibly be called multiple times to get
181 * through all the data.
182 */
183 if (bytes_to_encrypt > PqGSSMaxPktSize)
184 input.length = PqGSSMaxPktSize;
185 else
186 input.length = bytes_to_encrypt;
187
188 input.value = (char *) ptr + bytes_encrypted;
189
190 output.value = NULL;
191 output.length = 0;
192
193 /*
194 * Create the next encrypted packet. Any failure here is considered a
195 * hard failure, so we return -1 even if some data has been sent.
196 */
197 major = gss_wrap(&minor, gctx, 1, GSS_C_QOP_DEFAULT,
198 &input, &conf_state, &output);
199 if (major != GSS_S_COMPLETE)
200 {
201 pg_GSS_error(libpq_gettext("GSSAPI wrap error"), conn, major, minor);
202 SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
203 goto cleanup;
204 }
205
206 if (conf_state == 0)
207 {
208 libpq_append_conn_error(conn, "outgoing GSSAPI message would not use confidentiality");
209 SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
210 goto cleanup;
211 }
212
213 if (output.length > PQ_GSS_MAX_PACKET_SIZE - sizeof(uint32))
214 {
215 libpq_append_conn_error(conn, "client tried to send oversize GSSAPI packet (%zu > %zu)",
216 (size_t) output.length,
218 SOCK_ERRNO_SET(EIO); /* for lack of a better idea */
219 goto cleanup;
220 }
221
222 bytes_encrypted += input.length;
223 bytes_to_encrypt -= input.length;
224 PqGSSSendConsumed += input.length;
225
226 /* 4 network-order bytes of length, then payload */
227 netlen = pg_hton32(output.length);
228 memcpy(PqGSSSendBuffer + PqGSSSendLength, &netlen, sizeof(uint32));
229 PqGSSSendLength += sizeof(uint32);
230
231 memcpy(PqGSSSendBuffer + PqGSSSendLength, output.value, output.length);
232 PqGSSSendLength += output.length;
233
234 /* Release buffer storage allocated by GSSAPI */
235 gss_release_buffer(&minor, &output);
236 }
237
238 /* If we get here, our counters should all match up. */
240 Assert(len == bytes_encrypted);
241
242 /* We're reporting all the data as sent, so reset PqGSSSendConsumed. */
244
245 ret = bytes_encrypted;
246
247cleanup:
248 /* Release GSSAPI buffer storage, if we didn't already */
249 if (output.value != NULL)
250 gss_release_buffer(&minor, &output);
251 return ret;
252}
#define PqGSSSendLength
#define PqGSSSendConsumed
#define PqGSSSendBuffer
#define PqGSSMaxPktSize
#define PqGSSSendNext
ssize_t pqsecure_raw_write(PGconn *conn, const void *ptr, size_t len)
Definition: fe-secure.c:316
#define pg_hton32(x)
Definition: pg_bswap.h:121
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367
PQExpBufferData errorMessage
Definition: libpq-int.h:674

References appendPQExpBufferStr(), Assert(), cleanup(), conn, pg_conn::errorMessage, input, len, libpq_append_conn_error(), libpq_gettext, output, pg_GSS_error(), pg_hton32, PQ_GSS_MAX_PACKET_SIZE, PqGSSMaxPktSize, PqGSSSendBuffer, PqGSSSendConsumed, PqGSSSendLength, PqGSSSendNext, pqsecure_raw_write(), and SOCK_ERRNO_SET.

Referenced by pqsecure_write().

PQgetgssctx()

void * PQgetgssctx ( PGconnconn )

Definition at line 759 of file fe-secure-gssapi.c.

760{
761 if (!conn)
762 return NULL;
763
764 return conn->gctx;
765}

References conn.

PQgssEncInUse()

int PQgssEncInUse ( PGconnconn )

Definition at line 771 of file fe-secure-gssapi.c.

772{
773 if (!conn || !conn->gctx)
774 return 0;
775
776 return conn->gssenc;
777}

References conn.

Referenced by printGSSInfo().

pqsecure_open_gss()

PostgresPollingStatusType pqsecure_open_gss ( PGconnconn )

Definition at line 481 of file fe-secure-gssapi.c.

482{
483 ssize_t ret;
484 OM_uint32 major,
485 minor,
486 gss_flags = GSS_REQUIRED_FLAGS;
487 uint32 netlen;
489 gss_buffer_desc input = GSS_C_EMPTY_BUFFER,
490 output = GSS_C_EMPTY_BUFFER;
491
492 /*
493 * If first time through for this connection, allocate buffers and
494 * initialize state variables. By malloc'ing the buffers separately, we
495 * ensure that they are sufficiently aligned for the length-word accesses
496 * that we do in some places in this file.
497 *
498 * We'll use PQ_GSS_AUTH_BUFFER_SIZE-sized buffers until transport
499 * negotiation is complete, then switch to PQ_GSS_MAX_PACKET_SIZE.
500 */
501 if (PqGSSSendBuffer == NULL)
502 {
507 {
508 libpq_append_conn_error(conn, "out of memory");
510 }
513 }
514
515 /*
516 * Check if we have anything to send from a prior call and if so, send it.
517 */
518 if (PqGSSSendLength)
519 {
520 ssize_t amount = PqGSSSendLength - PqGSSSendNext;
521
523 if (ret < 0)
524 {
526 SOCK_ERRNO == EINTR)
528 else
530 }
531
532 if (ret < amount)
533 {
534 PqGSSSendNext += ret;
536 }
537
539 }
540
541 /*
542 * Client sends first, and sending creates a context, therefore this will
543 * be false the first time through, and then when we get called again we
544 * will check for incoming data.
545 */
546 if (conn->gctx)
547 {
548 /* Process any incoming data we might have */
549
550 /* See if we are still trying to get the length */
551 if (PqGSSRecvLength < sizeof(uint32))
552 {
553 /* Attempt to get the length first */
555 if (result != PGRES_POLLING_OK)
556 return result;
557
558 PqGSSRecvLength += ret;
559
560 if (PqGSSRecvLength < sizeof(uint32))
562 }
563
564 /*
565 * Check if we got an error packet
566 *
567 * This is safe to do because we shouldn't ever get a packet over 8192
568 * and therefore the actual length bytes, being that they are in
569 * network byte order, for any real packet will start with two zero
570 * bytes.
571 */
572 if (PqGSSRecvBuffer[0] == 'E')
573 {
574 /*
575 * For an error packet during startup, we don't get a length, so
576 * simply read as much as we can fit into our buffer (as a string,
577 * so leave a spot at the end for a NULL byte too) and report that
578 * back to the caller.
579 */
581 if (result != PGRES_POLLING_OK)
582 return result;
583
584 PqGSSRecvLength += ret;
585
589
591 }
592
593 /*
594 * We should have the whole length at this point, so pull it out and
595 * then read whatever we have left of the packet
596 */
597
598 /* Get the length and check for over-length packet */
599 input.length = pg_ntoh32(*(uint32 *) PqGSSRecvBuffer);
600 if (input.length > PQ_GSS_AUTH_BUFFER_SIZE - sizeof(uint32))
601 {
602 libpq_append_conn_error(conn, "oversize GSSAPI packet sent by the server (%zu > %zu)",
603 (size_t) input.length,
606 }
607
608 /*
609 * Read as much of the packet as we are able to on this call into
610 * wherever we left off from the last time we were called.
611 */
613 input.length - (PqGSSRecvLength - sizeof(uint32)), &ret);
614 if (result != PGRES_POLLING_OK)
615 return result;
616
617 PqGSSRecvLength += ret;
618
619 /*
620 * If we got less than the rest of the packet then we need to return
621 * and be called again.
622 */
623 if (PqGSSRecvLength - sizeof(uint32) < input.length)
625
626 input.value = PqGSSRecvBuffer + sizeof(uint32);
627 }
628
629 /* Load the service name (no-op if already done */
631 if (ret != STATUS_OK)
633
634 if (conn->gssdelegation && conn->gssdelegation[0] == '1')
635 {
636 /* Acquire credentials if possible */
637 if (conn->gcred == GSS_C_NO_CREDENTIAL)
638 (void) pg_GSS_have_cred_cache(&conn->gcred);
639
640 /*
641 * We have credentials and gssdelegation is enabled, so request
642 * credential delegation. This may or may not actually result in
643 * credentials being delegated- it depends on if the forwardable flag
644 * has been set in the credential and if the server is configured to
645 * accept delegated credentials.
646 */
647 if (conn->gcred != GSS_C_NO_CREDENTIAL)
648 gss_flags |= GSS_C_DELEG_FLAG;
649 }
650
651 /*
652 * Call GSS init context, either with an empty input, or with a complete
653 * packet from the server.
654 */
655 major = gss_init_sec_context(&minor, conn->gcred, &conn->gctx,
656 conn->gtarg_nam, GSS_C_NO_OID,
657 gss_flags, 0, 0, &input, NULL,
658 &output, NULL, NULL);
659
660 /* GSS Init Sec Context uses the whole packet, so clear it */
661 PqGSSRecvLength = 0;
662
663 if (GSS_ERROR(major))
664 {
665 pg_GSS_error(libpq_gettext("could not initiate GSSAPI security context"),
666 conn, major, minor);
668 }
669
670 if (output.length == 0)
671 {
672 /*
673 * We're done - hooray! Set flag to tell the low-level I/O routines
674 * to do GSS wrapping/unwrapping.
675 */
676 conn->gssenc = true;
677 conn->gssapi_used = true;
678
679 /* Clean up */
680 gss_release_cred(&minor, &conn->gcred);
681 conn->gcred = GSS_C_NO_CREDENTIAL;
682 gss_release_buffer(&minor, &output);
683
684 /*
685 * Release the large authentication buffers and allocate the ones we
686 * want for normal operation. (This maneuver is safe only because
687 * pqDropConnection will drop the buffers; otherwise, during a
688 * reconnection we'd be at risk of using undersized buffers during
689 * negotiation.)
690 */
698 {
699 libpq_append_conn_error(conn, "out of memory");
701 }
704
705 /*
706 * Determine the max packet size which will fit in our buffer, after
707 * accounting for the length. pg_GSS_write will need this.
708 */
709 major = gss_wrap_size_limit(&minor, conn->gctx, 1, GSS_C_QOP_DEFAULT,
712
713 if (GSS_ERROR(major))
714 {
715 pg_GSS_error(libpq_gettext("GSSAPI size check error"), conn,
716 major, minor);
718 }
719
720 return PGRES_POLLING_OK;
721 }
722
723 /* Must have output.length > 0 */
724 if (output.length > PQ_GSS_AUTH_BUFFER_SIZE - sizeof(uint32))
725 {
726 libpq_append_conn_error(conn, "client tried to send oversize GSSAPI packet (%zu > %zu)",
727 (size_t) output.length,
729 gss_release_buffer(&minor, &output);
731 }
732
733 /* Queue the token for writing */
734 netlen = pg_hton32(output.length);
735
736 memcpy(PqGSSSendBuffer, &netlen, sizeof(uint32));
737 PqGSSSendLength += sizeof(uint32);
738
739 memcpy(PqGSSSendBuffer + PqGSSSendLength, output.value, output.length);
740 PqGSSSendLength += output.length;
741
742 /* We don't bother with PqGSSSendConsumed here */
743
744 /* Release buffer storage allocated by GSSAPI */
745 gss_release_buffer(&minor, &output);
746
747 /* Ask to be called again to write data */
749}
#define STATUS_OK
Definition: c.h:1168
int pg_GSS_load_servicename(PGconn *conn)
bool pg_GSS_have_cred_cache(gss_cred_id_t *cred_out)
#define PQ_GSS_AUTH_BUFFER_SIZE
#define GSS_REQUIRED_FLAGS
static PostgresPollingStatusType gss_read(PGconn *conn, void *recv_buffer, size_t length, ssize_t *ret)
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
PostgresPollingStatusType
Definition: libpq-fe.h:114
@ PGRES_POLLING_WRITING
Definition: libpq-fe.h:117
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
char * gssdelegation
Definition: libpq-int.h:422
bool gssapi_used
Definition: libpq-int.h:509

References appendPQExpBuffer(), Assert(), conn, EAGAIN, EINTR, pg_conn::errorMessage, EWOULDBLOCK, free, gss_read(), GSS_REQUIRED_FLAGS, pg_conn::gssapi_used, pg_conn::gssdelegation, input, libpq_append_conn_error(), libpq_gettext, malloc, output, pg_GSS_error(), pg_GSS_have_cred_cache(), pg_GSS_load_servicename(), pg_hton32, pg_ntoh32, PGRES_POLLING_FAILED, PGRES_POLLING_OK, PGRES_POLLING_READING, PGRES_POLLING_WRITING, PQ_GSS_AUTH_BUFFER_SIZE, PQ_GSS_MAX_PACKET_SIZE, PqGSSMaxPktSize, PqGSSRecvBuffer, PqGSSRecvLength, PqGSSResultBuffer, PqGSSResultLength, PqGSSResultNext, PqGSSSendBuffer, PqGSSSendConsumed, PqGSSSendLength, PqGSSSendNext, pqsecure_raw_write(), SOCK_ERRNO, and STATUS_OK.

Referenced by PQconnectPoll().

AltStyle によって変換されたページ (->オリジナル) /