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