e55461283713e171b6229493bb2bb6dd0d94999e
[openafs.git] / src / pam / afs_pam_msg.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <security/pam_appl.h>
18 #include "afs_pam_msg.h"
19 #include "afs_message.h"
20 #include <stdarg.h>
21
22
23 int
24 pam_afs_printf(struct pam_conv *pam_convp, int error, int fmt_msgid, ...)
25 {
26     va_list args;
27     char buf[PAM_MAX_MSG_SIZE];
28     char *fmt_msg = NULL;
29     int freeit;
30     struct pam_message mesg;
31     PAM_CONST struct pam_message *mesgp = &mesg;
32     struct pam_response *resp = NULL;
33     int errcode;
34
35     if (pam_convp == NULL || pam_convp->conv == NULL)
36         return PAM_CONV_ERR;
37
38     fmt_msg = pam_afs_message(fmt_msgid, &freeit);
39     va_start(args, fmt_msgid);
40     vsprintf(buf, fmt_msg, args);
41     va_end(args);
42     if (freeit)
43         free(fmt_msg);
44
45     mesg.msg_style = error ? PAM_ERROR_MSG : PAM_TEXT_INFO;
46     mesg.msg = buf;
47     errcode = (*(pam_convp->conv)) (1, &mesgp, &resp, pam_convp->appdata_ptr);
48     if (resp) {
49         if (resp->resp)
50             free(resp->resp);
51         free(resp);
52     }
53     return errcode;
54 }
55
56
57 int
58 pam_afs_prompt(struct pam_conv *pam_convp, char **response, int echo,
59                int fmt_msgid, ...)
60 {
61     va_list args;
62     char buf[PAM_MAX_MSG_SIZE];
63     char *fmt_msg = NULL;
64     int freeit;
65     struct pam_message mesg;
66     PAM_CONST struct pam_message *mesgp = &mesg;
67     struct pam_response *resp = NULL;
68     int errcode;
69
70     if (pam_convp == NULL || pam_convp->conv == NULL || response == NULL)
71         return PAM_CONV_ERR;
72
73     *response = NULL;
74
75     fmt_msg = pam_afs_message(fmt_msgid, &freeit);
76     va_start(args, fmt_msgid);
77     vsprintf(buf, fmt_msg, args);
78     va_end(args);
79     if (freeit)
80         free(fmt_msg);
81
82     mesg.msg_style = echo ? PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF;
83     mesg.msg = buf;
84
85     errcode = (*(pam_convp->conv)) (1, &mesgp, &resp, pam_convp->appdata_ptr);
86     if (resp) {
87         *response = resp->resp;
88
89         free(resp);             /* but not resp->resp */
90     }
91     return errcode;
92 }