1 /* getauditlog.c - Retrieve the audit log.
2  * Copyright (C) 2007 g10 Code GmbH
3  *
4  * This file is part of GPGME.
5  *
6  * GPGME 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  * GPGME 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 <https://gnu.org/licenses/>.
18  * SPDX-License-Identifier: LGPL-2.1-or-later
19  */
20 
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 
25 #include "gpgme.h"
26 #include "debug.h"
27 #include "context.h"
28 #include "ops.h"
29 
30 
31 static gpgme_error_t
getauditlog_status_handler(void * priv,gpgme_status_code_t code,char * args)32 getauditlog_status_handler (void *priv, gpgme_status_code_t code, char *args)
33 {
34   (void)priv;
35   (void)code;
36   (void)args;
37   return 0;
38 }
39 
40 
41 static gpgme_error_t
getauditlog_start(gpgme_ctx_t ctx,int synchronous,gpgme_data_t output,unsigned int flags)42 getauditlog_start (gpgme_ctx_t ctx, int synchronous,
43                    gpgme_data_t output, unsigned int flags)
44 {
45   gpgme_error_t err;
46 
47   if (!output)
48     return gpg_error (GPG_ERR_INV_VALUE);
49 
50   if (!(flags & GPGME_AUDITLOG_DIAG))
51     {
52       err = _gpgme_op_reset (ctx, ((synchronous&255) | 256) );
53       if (err)
54         return err;
55     }
56 
57   _gpgme_engine_set_status_handler (ctx->engine,
58                                     getauditlog_status_handler, ctx);
59 
60   return _gpgme_engine_op_getauditlog (ctx->engine, output, flags);
61 }
62 
63 
64 
65 /* Return the auditlog for the current session.  This may be called
66    after a successful or failed operation.  If no audit log is
67    available GPG_ERR_NO_DATA is returned.  This is the asynchronous
68    variant. */
69 gpgme_error_t
gpgme_op_getauditlog_start(gpgme_ctx_t ctx,gpgme_data_t output,unsigned int flags)70 gpgme_op_getauditlog_start (gpgme_ctx_t ctx,
71                             gpgme_data_t output, unsigned int flags)
72 {
73   gpg_error_t err;
74   TRACE_BEG  (DEBUG_CTX, "gpgme_op_getauditlog_start", ctx,
75 	      "output=%p, flags=0x%x", output, flags);
76 
77   if (!ctx)
78     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
79 
80   err = getauditlog_start (ctx, 0, output, flags);
81   return TRACE_ERR (err);
82 }
83 
84 
85 /* Return the auditlog for the current session.  This may be called
86    after a successful or failed operation.  If no audit log is
87    available GPG_ERR_NO_DATA is returned.  This is the synchronous
88    variant. */
89 gpgme_error_t
gpgme_op_getauditlog(gpgme_ctx_t ctx,gpgme_data_t output,unsigned int flags)90 gpgme_op_getauditlog (gpgme_ctx_t ctx, gpgme_data_t output, unsigned int flags)
91 {
92   gpgme_error_t err;
93 
94   TRACE_BEG  (DEBUG_CTX, "gpgme_op_getauditlog", ctx,
95 	      "output=%p, flags=0x%x", output, flags);
96 
97   if (!ctx)
98     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
99 
100   err = getauditlog_start (ctx, 1, output, flags);
101   if (!err)
102     err = _gpgme_wait_one (ctx);
103   return TRACE_ERR (err);
104 }
105 
106