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