include-afsconfig-before-param-h-20010712
[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 RCSID("$Header$");
14
15 #include <stdio.h>
16 #include <security/pam_appl.h>
17 #include "afs_pam_msg.h"
18 #include "afs_message.h"
19 #include <stdarg.h>
20
21
22 int pam_afs_printf(struct pam_conv *pam_convp, int error, int fmt_msgid, ...)
23 {
24     va_list args;
25     char buf[PAM_MAX_MSG_SIZE];
26     char *fmt_msg = NULL;
27     int freeit;
28     struct pam_message mesg;
29     struct pam_message *mesgp = &mesg;
30     struct pam_response *resp = NULL;
31     int errcode;
32
33     if (pam_convp == NULL || pam_convp->conv == NULL)
34         return PAM_CONV_ERR;
35
36     fmt_msg = pam_afs_message(fmt_msgid, &freeit);
37     va_start(args, fmt_msgid);
38     vsprintf(buf, fmt_msg, args);
39     va_end(args);
40     if (freeit) free(fmt_msg);
41
42     mesg.msg_style = error ? PAM_ERROR_MSG : PAM_TEXT_INFO;
43     mesg.msg = buf;
44     errcode = (*(pam_convp->conv))(1, &mesgp, &resp, pam_convp->appdata_ptr);
45     if (resp) {
46         if (resp->resp) free(resp->resp);
47         free(resp);
48     }
49     return errcode;
50 }
51
52
53 int pam_afs_prompt(struct pam_conv *pam_convp, char **response,
54                    int echo, int fmt_msgid, ...)
55 {
56     va_list args;
57     char buf[PAM_MAX_MSG_SIZE];
58     char *fmt_msg = NULL;
59     int freeit;
60     struct pam_message mesg;
61     struct pam_message *mesgp = &mesg;
62     struct pam_response *resp = NULL;
63     int errcode;
64
65     if (pam_convp == NULL || pam_convp->conv == NULL || response == NULL)
66         return PAM_CONV_ERR;
67
68     *response = NULL;
69
70     fmt_msg = pam_afs_message(fmt_msgid, &freeit);
71     va_start(args, fmt_msgid);
72     vsprintf(buf, fmt_msg, args);
73     va_end(args);
74     if (freeit) free(fmt_msg);
75
76     mesg.msg_style = echo ? PAM_PROMPT_ECHO_ON : PAM_PROMPT_ECHO_OFF;
77     mesg.msg = buf;
78
79     errcode = (*(pam_convp->conv))(1, &mesgp, &resp, pam_convp->appdata_ptr);
80     if (resp) {
81         *response = resp->resp;
82
83         free(resp);             /* but not resp->resp */
84     }
85     return errcode;
86 }
87