1 /*
2 * Replacement for a missing pam_vsyslog.
3 *
4 * Provides close to the same functionality as the Linux PAM function
5 * pam_vsyslog for other PAM implementations. The logging prefix will not be
6 * quite as good, since we don't have access to the PAM group name.
7 *
8 * To use this replacement, the Autoconf script for the package must define
9 * MODULE_NAME to the name of the PAM module. (PACKAGE isn't used since it
10 * may use dashes where the module uses underscores.)
11 *
12 * The canonical version of this file is maintained in the rra-c-util package,
13 * which can be found at <https://www.eyrie.org/~eagle/software/rra-c-util/>.
14 *
15 * Written by Russ Allbery <eagle@eyrie.org>
16 * Copyright 2020 Russ Allbery <eagle@eyrie.org>
17 * Copyright 2010-2011
18 * The Board of Trustees of the Leland Stanford Junior University
19 *
20 * Copying and distribution of this file, with or without modification, are
21 * permitted in any medium without royalty provided the copyright notice and
22 * this notice are preserved. This file is offered as-is, without any
23 * warranty.
24 *
25 * SPDX-License-Identifier: FSFAP
26 */
27
28 #include <config.h>
29 #include <portable/pam.h>
30
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <syslog.h>
35
36 #ifndef LOG_AUTHPRIV
37 # define LOG_AUTHPRIV LOG_AUTH
38 #endif
39
40 void
pam_vsyslog(const pam_handle_t * pamh,int priority,const char * fmt,va_list args)41 pam_vsyslog(const pam_handle_t *pamh, int priority, const char *fmt,
42 va_list args)
43 {
44 char *msg = NULL;
45 const char *service = NULL;
46 int retval;
47
48 retval = pam_get_item(pamh, PAM_SERVICE, (PAM_CONST void **) &service);
49 if (retval != PAM_SUCCESS)
50 service = NULL;
51 if (vasprintf(&msg, fmt, args) < 0) {
52 syslog(LOG_CRIT | LOG_AUTHPRIV,
53 "cannot allocate memory in vasprintf: %m");
54 return;
55 }
56 /* clang-format off */
57 syslog(priority | LOG_AUTHPRIV, MODULE_NAME "%s%s%s: %s",
58 (service == NULL) ? "" : "(",
59 (service == NULL) ? "" : service,
60 (service == NULL) ? "" : ")", msg);
61 /* clang-format on */
62 free(msg);
63 }
64