PostgreSQL Source Code  git master
be-gssapi-common.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * be-gssapi-common.c
4  * Common code for GSSAPI authentication and encryption
5  *
6  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  * src/backend/libpq/be-gssapi-common.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 
15 #include "postgres.h"
16 
17 #include "libpq/be-gssapi-common.h"
18 
19 /*
20  * Helper function for getting all strings of a GSSAPI error (of specified
21  * stat). Call once for GSS_CODE and once for MECH_CODE.
22  */
23 static void
24 pg_GSS_error_int(char *s, size_t len, OM_uint32 stat, int type)
25 {
26  gss_buffer_desc gmsg;
27  size_t i = 0;
28  OM_uint32 lmin_s,
29  msg_ctx = 0;
30 
31  gmsg.value = NULL;
32  gmsg.length = 0;
33 
34  do
35  {
36  gss_display_status(&lmin_s, stat, type,
37  GSS_C_NO_OID, &msg_ctx, &gmsg);
38  strlcpy(s + i, gmsg.value, len - i);
39  i += gmsg.length;
40  gss_release_buffer(&lmin_s, &gmsg);
41  }
42  while (msg_ctx && i < len);
43 
44  if (msg_ctx || i == len)
46  (errmsg_internal("incomplete GSS error report")));
47 }
48 
49 /*
50  * Fetch and report all error messages from GSSAPI. To avoid allocation,
51  * total error size is capped (at 128 bytes for each of major and minor). No
52  * known mechanisms will produce error messages beyond this cap.
53  */
54 void
55 pg_GSS_error(int severity, const char *errmsg,
56  OM_uint32 maj_stat, OM_uint32 min_stat)
57 {
58  char msg_major[128],
59  msg_minor[128];
60 
61  /* Fetch major status message */
62  pg_GSS_error_int(msg_major, sizeof(msg_major), maj_stat, GSS_C_GSS_CODE);
63 
64  /* Fetch mechanism minor status message */
65  pg_GSS_error_int(msg_minor, sizeof(msg_minor), min_stat, GSS_C_MECH_CODE);
66 
67  /*
68  * errmsg_internal, since translation of the first part must be done
69  * before calling this function anyway.
70  */
71  ereport(severity,
72  (errmsg_internal("%s", errmsg),
73  errdetail_internal("%s: %s", msg_major, msg_minor)));
74 }
int errdetail_internal(const char *fmt,...)
Definition: elog.c:982
static void pg_GSS_error_int(char *s, size_t len, OM_uint32 stat, int type)
#define ereport(elevel, rest)
Definition: elog.h:141
#define WARNING
Definition: elog.h:40
#define stat(a, b)
Definition: win32_port.h:255
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg_internal(const char *fmt,...)
Definition: elog.c:909
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
void pg_GSS_error(int severity, const char *errmsg, OM_uint32 maj_stat, OM_uint32 min_stat)