Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libpam / modules / pam_userdb / conv.c
1 /*
2  * Conversation related functions
3  */
4
5 /* $Id */
6  * $FreeBSD: src/contrib/libpam/modules/pam_userdb/conv.c,v 1.1.1.1.2.2 2001/06/11 15:28:33 markm Exp $
7 /* Copyright at the end of the file */
8
9 #define _BSD_SOURCE
10
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include <security/pam_modules.h>
15 #include <security/_pam_macros.h>
16
17 #include "pam_userdb.h"
18
19 /*
20  * dummy conversation function sending exactly one prompt
21  * and expecting exactly one response from the other party
22  */
23 static int converse(pam_handle_t *pamh,
24                     struct pam_message **message,
25                     struct pam_response **response)
26 {
27     int retval;
28     const struct pam_conv *conv;
29
30     retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv ) ;
31     if (retval == PAM_SUCCESS)
32         retval = conv->conv(1, (const struct pam_message **)message,
33                             response, conv->appdata_ptr);
34         
35     return retval; /* propagate error status */
36 }
37
38
39 static char *_pam_delete(register char *xx)
40 {
41     _pam_overwrite(xx);
42     _pam_drop(xx);
43     return NULL;
44 }
45
46 /*
47  * This is a conversation function to obtain the user's password
48  */
49 int conversation(pam_handle_t *pamh)
50 {
51     struct pam_message msg[2],*pmsg[2];
52     struct pam_response *resp;
53     int retval;
54     char * token = NULL;
55     
56     pmsg[0] = &msg[0];
57     msg[0].msg_style = PAM_PROMPT_ECHO_OFF;
58     msg[0].msg = "Password: ";
59
60     /* so call the conversation expecting i responses */
61     resp = NULL;
62     retval = converse(pamh, pmsg, &resp);
63
64     if (resp != NULL) {
65         const char * item;
66         /* interpret the response */
67         if (retval == PAM_SUCCESS) {     /* a good conversation */
68             token = x_strdup(resp[0].resp);
69             if (token == NULL) {
70                 return PAM_AUTHTOK_RECOVER_ERR;
71             }
72         }
73
74         /* set the auth token */
75         retval = pam_set_item(pamh, PAM_AUTHTOK, token);
76         token = _pam_delete(token);   /* clean it up */
77         if ( (retval != PAM_SUCCESS) ||
78              (retval = pam_get_item(pamh, PAM_AUTHTOK, (const void **)&item))
79              != PAM_SUCCESS ) {
80             return retval;
81         }
82         
83         _pam_drop_reply(resp, 1);
84     } else {
85         retval = (retval == PAM_SUCCESS)
86             ? PAM_AUTHTOK_RECOVER_ERR:retval ;
87     }
88
89     return retval;
90 }
91
92 /*
93  * Copyright (c) Cristian Gafton <gafton@redhat.com>, 1999
94  *                                              All rights reserved
95  *
96  * Redistribution and use in source and binary forms, with or without
97  * modification, are permitted provided that the following conditions
98  * are met:
99  * 1. Redistributions of source code must retain the above copyright
100  *    notice, and the entire permission notice in its entirety,
101  *    including the disclaimer of warranties.
102  * 2. Redistributions in binary form must reproduce the above copyright
103  *    notice, this list of conditions and the following disclaimer in the
104  *    documentation and/or other materials provided with the distribution.
105  * 3. The name of the author may not be used to endorse or promote
106  *    products derived from this software without specific prior
107  *    written permission.
108  *
109  * ALTERNATIVELY, this product may be distributed under the terms of
110  * the GNU Public License, in which case the provisions of the GPL are
111  * required INSTEAD OF the above restrictions.  (This clause is
112  * necessary due to a potential bad interaction between the GPL and
113  * the restrictions contained in a BSD-style copyright.)
114  *
115  * THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESS OR IMPLIED
116  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
117  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
118  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
119  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
120  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
121  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
122  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
123  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
124  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
125  * OF THE POSSIBILITY OF SUCH DAMAGE.
126  */