| 1 | /* conv.c - PAM conversation abstraction for Poldi. |
| 2 | Copyright (C) 2004, 2005, 2007, 2008 g10 Code GmbH |
| 3 | |
| 4 | This file is part of Poldi. |
| 5 | |
| 6 | Poldi is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | Poldi 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 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <config.h> |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <errno.h> |
| 26 | #include <stdarg.h> |
| 27 | |
| 28 | #include <gpg-error.h> |
| 29 | |
| 30 | #define PAM_SM_AUTH |
| 31 | #include <security/pam_modules.h> |
| 32 | |
| 33 | #include "util/util.h" |
| 34 | #include "conv.h" |
| 35 | |
| 36 | |
| 37 | |
| 38 | struct conv_s |
| 39 | { |
| 40 | const struct pam_conv *pam_conv; |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | |
| 45 | /* Create a new PAM conversation object based in PAM_CONV and store it |
| 46 | in *CONV. Returns proper error code. */ |
| 47 | gpg_error_t |
| 48 | conv_create (conv_t *conv, const struct pam_conv *pam_conv) |
| 49 | { |
| 50 | conv_t conv_new; |
| 51 | gpg_error_t err; |
| 52 | |
| 53 | err = 0; |
| 54 | |
| 55 | conv_new = malloc (sizeof (*conv_new)); |
| 56 | if (!conv_new) |
| 57 | { |
| 58 | err = gpg_error_from_syserror (); |
| 59 | goto out; |
| 60 | } |
| 61 | |
| 62 | conv_new->pam_conv = pam_conv; |
| 63 | *conv = conv_new; |
| 64 | |
| 65 | out: |
| 66 | |
| 67 | return err; |
| 68 | } |
| 69 | |
| 70 | /* Destroy the conv object CONV. */ |
| 71 | void |
| 72 | conv_destroy (conv_t conv) |
| 73 | { |
| 74 | if (conv) |
| 75 | free (conv); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | /* This function queries the PAM user for input through the |
| 81 | conversation function CONV; TEXT will be displayed as prompt, the |
| 82 | user's response will be stored in *RESPONSE. Returns proper error |
| 83 | code. */ |
| 84 | static gpg_error_t |
| 85 | ask_user (const struct pam_conv *conv, int secret, |
| 86 | const char *text, char **response) |
| 87 | { |
| 88 | struct pam_message messages[1] = { { 0, text } }; |
| 89 | const struct pam_message *pmessages[1] = { &messages[0] }; |
| 90 | struct pam_response *responses = NULL; |
| 91 | char *response_new; |
| 92 | gpg_error_t err; |
| 93 | int ret; |
| 94 | |
| 95 | if (secret) |
| 96 | messages[0].msg_style = PAM_PROMPT_ECHO_OFF; |
| 97 | else |
| 98 | messages[0].msg_style = PAM_PROMPT_ECHO_ON; |
| 99 | |
| 100 | response_new = NULL; |
| 101 | |
| 102 | ret = (*conv->conv) (sizeof (messages) / (sizeof (*messages)), pmessages, |
| 103 | &responses, conv->appdata_ptr); |
| 104 | if (ret != PAM_SUCCESS) |
| 105 | { |
| 106 | err = gpg_error (GPG_ERR_INTERNAL); |
| 107 | goto out; |
| 108 | } |
| 109 | |
| 110 | if (response) |
| 111 | { |
| 112 | response_new = strdup (responses[0].resp); |
| 113 | if (! response_new) |
| 114 | { |
| 115 | err = gpg_error_from_errno (errno); |
| 116 | goto out; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | err = 0; |
| 121 | if (response) |
| 122 | *response = response_new; |
| 123 | |
| 124 | out: |
| 125 | |
| 126 | return err; |
| 127 | } |
| 128 | |
| 129 | /* This function queries the PAM user for input through the |
| 130 | conversation function CONV; TEXT will be displayed as prompt, the |
| 131 | user's response will be stored in *RESPONSE. Returns proper error |
| 132 | code. */ |
| 133 | static gpg_error_t |
| 134 | tell_user (const struct pam_conv *conv, const char *msg) |
| 135 | { |
| 136 | struct pam_message messages[1] = { { PAM_TEXT_INFO, NULL } }; |
| 137 | const struct pam_message *pmessages[1] = { &messages[0] }; |
| 138 | struct pam_response *responses = NULL; |
| 139 | gpg_error_t err; |
| 140 | int ret; |
| 141 | |
| 142 | messages[0].msg = msg; |
| 143 | |
| 144 | ret = (*conv->conv) (sizeof (messages) / (sizeof (*messages)), pmessages, |
| 145 | &responses, conv->appdata_ptr); |
| 146 | if (ret != PAM_SUCCESS) |
| 147 | { |
| 148 | err = gpg_error (GPG_ERR_INTERNAL); |
| 149 | goto out; |
| 150 | } |
| 151 | |
| 152 | err = 0; |
| 153 | |
| 154 | out: |
| 155 | |
| 156 | return err; |
| 157 | } |
| 158 | |
| 159 | gpg_error_t |
| 160 | conv_tell (conv_t conv, const char *fmt, ...) |
| 161 | { |
| 162 | gpg_error_t err = 0; |
| 163 | char *msg = NULL; |
| 164 | va_list ap; |
| 165 | int ret; |
| 166 | |
| 167 | va_start (ap, fmt); |
| 168 | |
| 169 | ret = vasprintf (&msg, fmt, ap); |
| 170 | if (ret < 0) |
| 171 | { |
| 172 | err = gpg_error_from_errno (errno); |
| 173 | goto out; |
| 174 | } |
| 175 | |
| 176 | err = tell_user (conv->pam_conv, msg); |
| 177 | |
| 178 | out: |
| 179 | |
| 180 | va_end (ap); |
| 181 | free (msg); |
| 182 | |
| 183 | return err; |
| 184 | } |
| 185 | |
| 186 | gpg_error_t |
| 187 | conv_ask (conv_t conv, int ask_secret, |
| 188 | char **response, const char *fmt, ...) |
| 189 | { |
| 190 | gpg_error_t err = 0; |
| 191 | char *msg = NULL; |
| 192 | va_list ap; |
| 193 | int ret; |
| 194 | |
| 195 | va_start (ap, fmt); |
| 196 | |
| 197 | ret = vasprintf (&msg, fmt, ap); |
| 198 | if (ret < 0) |
| 199 | { |
| 200 | err = gpg_error_from_errno (errno); |
| 201 | goto out; |
| 202 | } |
| 203 | |
| 204 | err = ask_user (conv->pam_conv, ask_secret, msg, response); |
| 205 | |
| 206 | out: |
| 207 | |
| 208 | va_end (ap); |
| 209 | free (msg); |
| 210 | |
| 211 | return err; |
| 212 | } |
| 213 | |
| 214 | /* END */ |