Line data Source code
1 : /* context.c - Context specific interface.
2 : Copyright (C) 2009 Free Software Foundation, Inc.
3 :
4 : This file is part of Assuan.
5 :
6 : Assuan is free software; you can redistribute it and/or modify it
7 : under the terms of the GNU Lesser General Public License as
8 : published by the Free Software Foundation; either version 2.1 of
9 : the License, or (at your option) any later version.
10 :
11 : Assuan is distributed in the hope that it will be useful, but
12 : WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Lesser General Public License for more details.
15 :
16 : You should have received a copy of the GNU Lesser General Public
17 : License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include <config.h>
23 : #endif
24 :
25 : #include "assuan-defs.h"
26 : #include "debug.h"
27 :
28 :
29 : /* Set user-data in a context. */
30 : void
31 0 : assuan_set_pointer (assuan_context_t ctx, void *user_pointer)
32 : {
33 0 : TRACE1 (ctx, ASSUAN_LOG_CTX, "assuan_set_pointer", ctx,
34 : "user_pointer=%p", user_pointer);
35 :
36 0 : if (ctx)
37 0 : ctx->user_pointer = user_pointer;
38 0 : }
39 :
40 :
41 : /* Get user-data in a context. */
42 : void *
43 0 : assuan_get_pointer (assuan_context_t ctx)
44 : {
45 : #if 0
46 : /* This is called often. */
47 : TRACE1 (ctx, ASSUAN_LOG_CTX, "assuan_get_pointer", ctx,
48 : "ctx->user_pointer=%p", ctx ? ctx->user_pointer : NULL);
49 : #endif
50 :
51 0 : if (! ctx)
52 0 : return NULL;
53 :
54 0 : return ctx->user_pointer;
55 : }
56 :
57 :
58 : /* For context CTX, set the flag FLAG to VALUE. Values for flags
59 : are usually 1 or 0 but certain flags might allow for other values;
60 : see the description of the type assuan_flag_t for details. */
61 : void
62 0 : assuan_set_flag (assuan_context_t ctx, assuan_flag_t flag, int value)
63 : {
64 0 : TRACE2 (ctx, ASSUAN_LOG_CTX, "assuan_set_flag", ctx,
65 : "flag=%i,value=%i", flag, value);
66 :
67 0 : if (!ctx)
68 0 : return;
69 :
70 0 : switch (flag)
71 : {
72 : case ASSUAN_NO_WAITPID:
73 0 : ctx->flags.no_waitpid = value;
74 0 : break;
75 :
76 : case ASSUAN_CONFIDENTIAL:
77 0 : ctx->flags.confidential = value;
78 0 : break;
79 :
80 : case ASSUAN_NO_FIXSIGNALS:
81 0 : ctx->flags.no_fixsignals = value;
82 0 : break;
83 :
84 : case ASSUAN_CONVEY_COMMENTS:
85 0 : ctx->flags.convey_comments = value;
86 0 : break;
87 :
88 : case ASSUAN_NO_LOGGING:
89 0 : ctx->flags.no_logging = value;
90 0 : break;
91 :
92 : case ASSUAN_FORCE_CLOSE:
93 0 : ctx->flags.force_close = 1;
94 0 : break;
95 : }
96 : }
97 :
98 :
99 : /* Return the VALUE of FLAG in context CTX. */
100 : int
101 0 : assuan_get_flag (assuan_context_t ctx, assuan_flag_t flag)
102 : {
103 0 : int res = 0;
104 0 : TRACE_BEG1 (ctx, ASSUAN_LOG_CTX, "assuan_get_flag", ctx,
105 : "flag=%i", flag);
106 :
107 0 : if (! ctx)
108 0 : return 0;
109 :
110 0 : switch (flag)
111 : {
112 : case ASSUAN_NO_WAITPID:
113 0 : res = ctx->flags.no_waitpid;
114 0 : break;
115 :
116 : case ASSUAN_CONFIDENTIAL:
117 0 : res = ctx->flags.confidential;
118 0 : break;
119 :
120 : case ASSUAN_NO_FIXSIGNALS:
121 0 : res = ctx->flags.no_fixsignals;
122 0 : break;
123 :
124 : case ASSUAN_CONVEY_COMMENTS:
125 0 : res = ctx->flags.convey_comments;
126 0 : break;
127 :
128 : case ASSUAN_NO_LOGGING:
129 0 : res = ctx->flags.no_logging;
130 0 : break;
131 :
132 : case ASSUAN_FORCE_CLOSE:
133 0 : res = ctx->flags.force_close;
134 0 : break;
135 : }
136 :
137 0 : return TRACE_SUC1 ("flag_value=%i", res);
138 : }
139 :
140 :
141 : /* Same as assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 1). */
142 : void
143 0 : assuan_begin_confidential (assuan_context_t ctx)
144 : {
145 0 : assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 1);
146 0 : }
147 :
148 :
149 : /* Same as assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 0). */
150 : void
151 0 : assuan_end_confidential (assuan_context_t ctx)
152 : {
153 0 : assuan_set_flag (ctx, ASSUAN_CONFIDENTIAL, 0);
154 0 : }
155 :
156 :
157 : /* Set the system callbacks. */
158 : void
159 0 : assuan_ctx_set_system_hooks (assuan_context_t ctx,
160 : assuan_system_hooks_t system_hooks)
161 : {
162 0 : TRACE2 (ctx, ASSUAN_LOG_CTX, "assuan_set_system_hooks", ctx,
163 : "system_hooks=%p (version %i)", system_hooks,
164 : system_hooks->version);
165 :
166 0 : _assuan_system_hooks_copy (&ctx->system, system_hooks);
167 0 : }
168 :
169 :
170 : /* Set the IO monitor function. */
171 0 : void assuan_set_io_monitor (assuan_context_t ctx,
172 : assuan_io_monitor_t io_monitor, void *hook_data)
173 : {
174 0 : TRACE2 (ctx, ASSUAN_LOG_CTX, "assuan_set_io_monitor", ctx,
175 : "io_monitor=%p,hook_data=%p", io_monitor, hook_data);
176 :
177 0 : if (! ctx)
178 0 : return;
179 :
180 0 : ctx->io_monitor = io_monitor;
181 0 : ctx->io_monitor_data = hook_data;
182 : }
183 :
184 :
185 : /* Store the error in the context so that the error sending function
186 : can take out a descriptive text. Inside the assuan code, use the
187 : macro set_error instead of this function. */
188 : gpg_error_t
189 0 : assuan_set_error (assuan_context_t ctx, gpg_error_t err, const char *text)
190 : {
191 0 : TRACE4 (ctx, ASSUAN_LOG_CTX, "assuan_set_error", ctx,
192 : "err=%i (%s,%s),text=%s", err, gpg_strsource (err),
193 : gpg_strerror (err), text?text:"(none)");
194 :
195 0 : ctx->err_no = err;
196 0 : ctx->err_str = text;
197 0 : return err;
198 : }
199 :
200 :
201 : /* Return the PID of the peer or ASSUAN_INVALID_PID if not known.
202 : This function works in some situations where assuan_get_ucred
203 : fails. */
204 : pid_t
205 0 : assuan_get_pid (assuan_context_t ctx)
206 : {
207 0 : TRACE1 (ctx, ASSUAN_LOG_CTX, "assuan_get_pid", ctx,
208 : "pid=%i", ctx ? ctx->pid : -1);
209 :
210 0 : return (ctx && ctx->pid) ? ctx->pid : ASSUAN_INVALID_PID;
211 : }
212 :
213 :
214 : /* Return user credentials. For getting the pid of the peer the
215 : assuan_get_pid is usually better suited. */
216 : gpg_error_t
217 0 : assuan_get_peercred (assuan_context_t ctx, assuan_peercred_t *peercred)
218 : {
219 0 : TRACE (ctx, ASSUAN_LOG_CTX, "assuan_get_peercred", ctx);
220 :
221 0 : if (!ctx)
222 0 : return _assuan_error (ctx, GPG_ERR_ASS_INV_VALUE);
223 0 : if (!ctx->peercred_valid)
224 0 : return _assuan_error (ctx, GPG_ERR_ASS_GENERAL);
225 :
226 0 : *peercred = &ctx->peercred;
227 :
228 0 : return 0;
229 : }
|