source: openpam/trunk/lib/openpam_ttyconv.c @ 35

Last change on this file since 35 was 35, checked in by Dag-Erling Smørgrav, 19 years ago

According to bde, it's OK to omit parentheses when taking the size
of an object rather than a type.

Sponsored by: DARPA, NAI Labs

  • Property svn:keywords set to Id LastChangedRevision HeadURL LastChangedDate LastChangedBy
File size: 3.7 KB
Line 
1/*-
2 * Copyright (c) 2002 Networks Associates Technologies, Inc.
3 * All rights reserved.
4 *
5 * This software was developed for the FreeBSD Project by ThinkSec AS and
6 * NAI Labs, the Security Research Division of Network Associates, Inc.
7 * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
8 * DARPA CHATS research program.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote
19 *    products derived from this software without specific prior written
20 *    permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $Id: openpam_ttyconv.c 35 2002-02-06 13:25:36Z des $
35 */
36
37#include <sys/types.h>
38
39#include <ctype.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <termios.h>
44
45#include <security/pam_appl.h>
46#include <security/openpam.h>
47
48/*
49 * Simple tty-based conversation function.
50 */
51
52int
53openpam_ttyconv(int n,
54         const struct pam_message **msg,
55         struct pam_response **resp,
56         void *data)
57{
58        char buf[PAM_MAX_RESP_SIZE];
59        struct termios tattr;
60        tcflag_t lflag;
61        int fd, err, i;
62        size_t len;
63
64        data = data;
65        if (n <= 0 || n > PAM_MAX_NUM_MSG)
66                return (PAM_CONV_ERR);
67        if ((*resp = calloc(n, sizeof **resp)) == NULL)
68                return (PAM_BUF_ERR);
69        fd = fileno(stdin);
70        for (i = 0; i < n; ++i) {
71                resp[i]->resp_retcode = 0;
72                resp[i]->resp = NULL;
73                switch (msg[i]->msg_style) {
74                case PAM_PROMPT_ECHO_OFF:
75                case PAM_PROMPT_ECHO_ON:
76                        if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) {
77                                if (tcgetattr(fd, &tattr) != 0) {
78                                        openpam_log(PAM_LOG_ERROR,
79                                            "tcgetattr(): %m");
80                                        err = PAM_CONV_ERR;
81                                        goto fail;
82                                }
83                                lflag = tattr.c_lflag;
84                                tattr.c_lflag &= ~ECHO;
85                                if (tcsetattr(fd, TCSAFLUSH, &tattr) != 0) {
86                                        openpam_log(PAM_LOG_ERROR,
87                                            "tcsetattr(): %m");
88                                        err = PAM_CONV_ERR;
89                                        goto fail;
90                                }
91                        }
92                        fputs(msg[i]->msg, stderr);
93                        buf[0] = '\0';
94                        fgets(buf, sizeof buf, stdin);
95                        if (msg[i]->msg_style == PAM_PROMPT_ECHO_OFF) {
96                                tattr.c_lflag = lflag;
97                                (void)tcsetattr(fd, TCSANOW, &tattr);
98                                fputs("\n", stderr);
99                        }
100                        if (ferror(stdin)) {
101                                err = PAM_CONV_ERR;
102                                goto fail;
103                        }
104                        for (len = strlen(buf); len > 0; --len)
105                                if (!isspace(buf[len - 1]))
106                                        break;
107                        buf[len] = '\0';
108                        if ((resp[i]->resp = strdup(buf)) == NULL) {
109                                err = PAM_BUF_ERR;
110                                goto fail;
111                        }
112                        break;
113                case PAM_ERROR_MSG:
114                        fputs(msg[i]->msg, stderr);
115                        break;
116                case PAM_TEXT_INFO:
117                        fputs(msg[i]->msg, stdout);
118                        break;
119                default:
120                        err = PAM_BUF_ERR;
121                        goto fail;
122                }
123        }
124        return (PAM_SUCCESS);
125 fail:
126        while (i)
127                free(resp[--i]);
128        free(*resp);
129        *resp = NULL;
130        return (err);
131}
Note: See TracBrowser for help on using the repository browser.