Annotation of embedaddon/sudo/src/conversation.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (c) 1999-2005, 2007-2010 Todd C. Miller <Todd.Miller@courtesan.com>
        !             3:  *
        !             4:  * Permission to use, copy, modify, and distribute this software for any
        !             5:  * purpose with or without fee is hereby granted, provided that the above
        !             6:  * copyright notice and this permission notice appear in all copies.
        !             7:  *
        !             8:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !             9:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            10:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            11:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            12:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            13:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            14:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            15:  *
        !            16:  * Sponsored in part by the Defense Advanced Research Projects
        !            17:  * Agency (DARPA) and Air Force Research Laboratory, Air Force
        !            18:  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
        !            19:  */
        !            20: 
        !            21: #include <config.h>
        !            22: 
        !            23: #include <sys/types.h>
        !            24: #include <sys/param.h>
        !            25: #include <stdio.h>
        !            26: #ifdef STDC_HEADERS
        !            27: # include <stdlib.h>
        !            28: # include <stddef.h>
        !            29: #else
        !            30: # ifdef HAVE_STDLIB_H
        !            31: #  include <stdlib.h>
        !            32: # endif
        !            33: #endif /* STDC_HEADERS */
        !            34: #ifdef HAVE_STRING_H
        !            35: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
        !            36: #  include <memory.h>
        !            37: # endif
        !            38: # include <string.h>
        !            39: #endif /* HAVE_STRING_H */
        !            40: #ifdef HAVE_STRINGS_H
        !            41: # include <strings.h>
        !            42: #endif /* HAVE_STRINGS_H */
        !            43: #ifdef HAVE_UNISTD_H
        !            44: # include <unistd.h>
        !            45: #endif /* HAVE_UNISTD_H */
        !            46: #include <errno.h>
        !            47: 
        !            48: #include "sudo.h"
        !            49: #include "sudo_plugin.h"
        !            50: 
        !            51: extern int tgetpass_flags; /* XXX */
        !            52: 
        !            53: /*
        !            54:  * Sudo conversation function.
        !            55:  */
        !            56: int
        !            57: sudo_conversation(int num_msgs, const struct sudo_conv_message msgs[],
        !            58:     struct sudo_conv_reply replies[])
        !            59: {
        !            60:     struct sudo_conv_reply *repl;
        !            61:     const struct sudo_conv_message *msg;
        !            62:     char *pass;
        !            63:     int n, flags = tgetpass_flags;
        !            64: 
        !            65:     for (n = 0; n < num_msgs; n++) {
        !            66:        msg = &msgs[n];
        !            67:        repl = &replies[n];
        !            68:        switch (msg->msg_type & 0xff) {
        !            69:            case SUDO_CONV_PROMPT_ECHO_ON:
        !            70:            case SUDO_CONV_PROMPT_MASK:
        !            71:                if (msg->msg_type == SUDO_CONV_PROMPT_ECHO_ON)
        !            72:                    SET(flags, TGP_ECHO);
        !            73:                else
        !            74:                    SET(flags, TGP_MASK);
        !            75:                /* FALLTHROUGH */
        !            76:            case SUDO_CONV_PROMPT_ECHO_OFF:
        !            77:                if (ISSET(msg->msg_type, SUDO_CONV_PROMPT_ECHO_OK))
        !            78:                    SET(flags, TGP_NOECHO_TRY);
        !            79:                /* Read the password unless interrupted. */
        !            80:                pass = tgetpass(msg->msg, msg->timeout, flags);
        !            81:                if (pass == NULL)
        !            82:                    goto err;
        !            83:                repl->reply = estrdup(pass);
        !            84:                zero_bytes(pass, strlen(pass));
        !            85:                break;
        !            86:            case SUDO_CONV_INFO_MSG:
        !            87:                if (msg->msg)
        !            88:                    (void) fputs(msg->msg, stdout);
        !            89:                break;
        !            90:            case SUDO_CONV_ERROR_MSG:
        !            91:                if (msg->msg)
        !            92:                    (void) fputs(msg->msg, stderr);
        !            93:                break;
        !            94:            default:
        !            95:                goto err;
        !            96:        }
        !            97:     }
        !            98: 
        !            99:     return 0;
        !           100: 
        !           101: err:
        !           102:     /* Zero and free allocated memory and return an error. */
        !           103:     do {
        !           104:        repl = &replies[n];
        !           105:        if (repl->reply != NULL) {
        !           106:            zero_bytes(repl->reply, strlen(repl->reply));
        !           107:            free(repl->reply);
        !           108:            repl->reply = NULL;
        !           109:        }
        !           110:     } while (n--);
        !           111: 
        !           112:     return -1;
        !           113: }
        !           114: 
        !           115: int
        !           116: _sudo_printf(int msg_type, const char *fmt, ...)
        !           117: {
        !           118:     va_list ap;
        !           119:     FILE *fp;
        !           120:     int len;
        !           121: 
        !           122:     switch (msg_type) {
        !           123:     case SUDO_CONV_INFO_MSG:
        !           124:        fp = stdout;
        !           125:        break;
        !           126:     case SUDO_CONV_ERROR_MSG:
        !           127:        fp = stderr;
        !           128:        break;
        !           129:     default:
        !           130:        errno = EINVAL;
        !           131:        return -1;
        !           132:     }
        !           133: 
        !           134:     va_start(ap, fmt);
        !           135:     len = vfprintf(fp, fmt, ap);
        !           136:     va_end(ap);
        !           137: 
        !           138:     return len;
        !           139: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>