summaryrefslogtreecommitdiff
path: root/src/context.c
blob: dd89de9e4d2377933cbfae2169c42d9fa5d44899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* context.c - Context specific interface.
 * Copyright (C) 2009 Free Software Foundation, Inc.
 *
 * This file is part of Assuan.
 *
 * Assuan is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Assuan is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 * SPDX-License-Identifier: LGPL-2.1+
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "assuan-defs.h"
#include "debug.h"


/* Set user-data in a context.  */
void
assuan_set_pointer (assuan_context_t ctx, void *user_pointer)
{
  TRACE1 (ctx, ASSUAN_LOG_CTX, "assuan_set_pointer", ctx,
	  "user_pointer=%p", user_pointer);

  if (ctx)
    ctx->user_pointer = user_pointer;
}


/* Get user-data in a context.  */
void *
assuan_get_pointer (assuan_context_t ctx)
{
#if 0
  /* This is called often.  */
  TRACE1 (ctx, ASSUAN_LOG_CTX, "assuan_get_pointer", ctx,
	  "ctx->user_pointer=%p", ctx ? ctx->user_pointer : NULL);
#endif

  if (! ctx)
    return NULL;

  return ctx->user_pointer;
}


/* For context CTX, set the flag FLAG to VALUE.  Values for flags
   are usually 1 or 0 but certain flags might allow for other values;
   see the description of the type assuan_flag_t for details.  */
void
assuan_set_flag (assuan_context_t ctx, assuan_flag_t flag, int value)
{
  TRACE2 (ctx, ASSUAN_LOG_CTX, "assuan_set_flag", ctx,
	  "flag=%i,value=%i", flag, value);

  if (!ctx)
    return;

  switch (flag)
    {
    case ASSUAN_NO_WAITPID:
      ctx->flags.no_waitpid = value;
      break;

    case ASSUAN_CONFIDENTIAL:
      ctx->flags.confidential = value;
      if (ctx->flags.in_inq_cb && value)
        ctx->flags.confidential_inquiry = value;
      break;

    case ASSUAN_NO_FIXSIGNALS:
      ctx->flags.no_fixsignals = value;
      break;

    case ASSUAN_CONVEY_COMMENTS:
      ctx->flags.convey_comments = value;
      break;

    case ASSUAN_NO_LOGGING:
      ctx->flags.no_logging = value;
      break;

    case ASSUAN_FORCE_CLOSE:
      ctx->flags.force_close = 1;
      break;
    }
}


/* Return the VALUE of FLAG in context CTX.  */
int
assuan_get_flag (assuan_context_t ctx, assuan_flag_t flag)
{
  int res = 0;
  TRACE_BEG1 (ctx, ASSUAN_LOG_CTX, "assuan_get_flag", ctx,
	      "flag=%i", flag);

  if (! ctx)
    return 0;

  switch (flag)
    {
    case ASSUAN_NO_WAITPID:
      res = ctx->flags.no_waitpid;
      break;

    case ASSUAN_CONFIDENTIAL:
      res = ctx->flags.confidential;
      break;

    case ASSUAN_NO_FIXSIGNALS:
      res = ctx->flags.no_fixsignals;
      break;

    case ASSUAN_CONVEY_COMMENTS:
      res = ctx->flags.convey_comments;
      break;

    case ASSUAN_NO_LOGGING:
      res = ctx->flags.no_logging;
      break;

    case ASSUAN_FORCE_CLOSE:
      res = ctx->flags.force_close;
      break;
    }

  return TRACE_SUC1 ("flag_value=%i", res);
}


/* Same as assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 1).  */
void
assuan_begin_confidential (assuan_context_t ctx)
{
  assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 1);
}


/* Same as assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 0).  */
void
assuan_end_confidential (assuan_context_t ctx)
{
  assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 0);
}


/* Set the system callbacks.  */
void
assuan_ctx_set_system_hooks (assuan_context_t ctx,
			     assuan_system_hooks_t system_hooks)
{
  TRACE2 (ctx, ASSUAN_LOG_CTX, "assuan_set_system_hooks", ctx,
	  "system_hooks=%p (version %i)", system_hooks,
	  system_hooks->version);

  _assuan_system_hooks_copy (&ctx->system, system_hooks);
}


/* Set the IO monitor function.  */
void assuan_set_io_monitor (assuan_context_t ctx,
			    assuan_io_monitor_t io_monitor, void *hook_data)
{
  TRACE2 (ctx, ASSUAN_LOG_CTX, "assuan_set_io_monitor", ctx,
	  "io_monitor=%p,hook_data=%p", io_monitor, hook_data);

  if (! ctx)
    return;

  ctx->io_monitor = io_monitor;
  ctx->io_monitor_data = hook_data;
}


/* Store the error in the context so that the error sending function
   can take out a descriptive text.  Inside the assuan code, use the
   macro set_error instead of this function. */
gpg_error_t
assuan_set_error (assuan_context_t ctx, gpg_error_t err, const char *text)
{
  TRACE4 (ctx, ASSUAN_LOG_CTX, "assuan_set_error", ctx,
	  "err=%i (%s,%s),text=%s", err, gpg_strsource (err),
	  gpg_strerror (err), text?text:"(none)");

  ctx->err_no = err;
  ctx->err_str = text;
  return err;
}


/* Return the PID of the peer or ASSUAN_INVALID_PID if not known.
   This function works in some situations where assuan_get_ucred
   fails. */
pid_t
assuan_get_pid (assuan_context_t ctx)
{
  TRACE1 (ctx, ASSUAN_LOG_CTX, "assuan_get_pid", ctx,
	  "pid=%i", ctx ? ctx->pid : -1);

  return (ctx && ctx->pid) ? ctx->pid : ASSUAN_INVALID_PID;
}


/* Return user credentials.  For getting the pid of the peer the
   assuan_get_pid is usually better suited. */
gpg_error_t
assuan_get_peercred (assuan_context_t ctx, assuan_peercred_t *peercred)
{
  TRACE (ctx, ASSUAN_LOG_CTX, "assuan_get_peercred", ctx);

  if (!ctx)
    return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
  if (!ctx->peercred_valid)
    return _assuan_error (ctx, GPG_ERR_ASS_GENERAL);

  *peercred = &ctx->peercred;

  return 0;
}