Annotation of embedaddon/sudo/plugins/sudoers/auth/fwtk.c, revision 1.1.1.5

1.1       misho       1: /*
1.1.1.4   misho       2:  * Copyright (c) 1999-2005, 2008, 2010-2013
1.1       misho       3:  *     Todd C. Miller <Todd.Miller@courtesan.com>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  *
                     17:  * Sponsored in part by the Defense Advanced Research Projects
                     18:  * Agency (DARPA) and Air Force Research Laboratory, Air Force
                     19:  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
                     20:  */
                     21: 
                     22: #include <config.h>
                     23: 
                     24: #include <sys/types.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: # include <string.h>
                     36: #endif /* HAVE_STRING_H */
                     37: #ifdef HAVE_STRINGS_H
                     38: # include <strings.h>
                     39: #endif /* HAVE_STRING_H */
                     40: #ifdef HAVE_UNISTD_H
                     41: # include <unistd.h>
                     42: #endif /* HAVE_UNISTD_H */
                     43: #include <pwd.h>
                     44: 
                     45: #include <auth.h>
                     46: #include <firewall.h>
                     47: 
                     48: #include "sudoers.h"
                     49: #include "sudo_auth.h"
                     50: 
                     51: int
1.1.1.2   misho      52: sudo_fwtk_init(struct passwd *pw, sudo_auth *auth)
1.1       misho      53: {
                     54:     static Cfg *confp;                 /* Configuration entry struct */
                     55:     char resp[128];                    /* Response from the server */
1.1.1.2   misho      56:     debug_decl(sudo_fwtk_init, SUDO_DEBUG_AUTH)
1.1       misho      57: 
                     58:     if ((confp = cfg_read("sudo")) == (Cfg *)-1) {
1.1.1.5 ! misho      59:        warningx(U_("unable to read fwtk config"));
1.1.1.2   misho      60:        debug_return_int(AUTH_FATAL);
1.1       misho      61:     }
                     62: 
                     63:     if (auth_open(confp)) {
1.1.1.5 ! misho      64:        warningx(U_("unable to connect to authentication server"));
1.1.1.2   misho      65:        debug_return_int(AUTH_FATAL);
1.1       misho      66:     }
                     67: 
                     68:     /* Get welcome message from auth server */
                     69:     if (auth_recv(resp, sizeof(resp))) {
1.1.1.5 ! misho      70:        warningx(U_("lost connection to authentication server"));
1.1.1.2   misho      71:        debug_return_int(AUTH_FATAL);
1.1       misho      72:     }
                     73:     if (strncmp(resp, "Authsrv ready", 13) != 0) {
1.1.1.5 ! misho      74:        warningx(U_("authentication server error:\n%s"), resp);
1.1.1.2   misho      75:        debug_return_int(AUTH_FATAL);
1.1       misho      76:     }
                     77: 
1.1.1.2   misho      78:     debug_return_int(AUTH_SUCCESS);
1.1       misho      79: }
                     80: 
                     81: int
1.1.1.2   misho      82: sudo_fwtk_verify(struct passwd *pw, char *prompt, sudo_auth *auth)
1.1       misho      83: {
                     84:     char *pass;                                /* Password from the user */
1.1.1.4   misho      85:     char buf[SUDO_CONV_REPL_MAX + 12]; /* General prupose buffer */
1.1       misho      86:     char resp[128];                    /* Response from the server */
                     87:     int error;
1.1.1.2   misho      88:     debug_decl(sudo_fwtk_verify, SUDO_DEBUG_AUTH)
1.1       misho      89: 
                     90:     /* Send username to authentication server. */
                     91:     (void) snprintf(buf, sizeof(buf), "authorize %s 'sudo'", pw->pw_name);
                     92: restart:
                     93:     if (auth_send(buf) || auth_recv(resp, sizeof(resp))) {
1.1.1.5 ! misho      94:        warningx(U_("lost connection to authentication server"));
1.1.1.2   misho      95:        debug_return_int(AUTH_FATAL);
1.1       misho      96:     }
                     97: 
                     98:     /* Get the password/response from the user. */
                     99:     if (strncmp(resp, "challenge ", 10) == 0) {
                    100:        (void) snprintf(buf, sizeof(buf), "%s\nResponse: ", &resp[10]);
                    101:        pass = auth_getpass(buf, def_passwd_timeout * 60, SUDO_CONV_PROMPT_ECHO_OFF);
                    102:        if (pass && *pass == '\0') {
                    103:            pass = auth_getpass("Response [echo on]: ",
                    104:                def_passwd_timeout * 60, SUDO_CONV_PROMPT_ECHO_ON);
                    105:        }
                    106:     } else if (strncmp(resp, "chalnecho ", 10) == 0) {
                    107:        pass = auth_getpass(&resp[10], def_passwd_timeout * 60,
                    108:            SUDO_CONV_PROMPT_ECHO_OFF);
                    109:     } else if (strncmp(resp, "password", 8) == 0) {
                    110:        pass = auth_getpass(prompt, def_passwd_timeout * 60,
                    111:            SUDO_CONV_PROMPT_ECHO_OFF);
                    112:     } else if (strncmp(resp, "display ", 8) == 0) {
                    113:        fprintf(stderr, "%s\n", &resp[8]);
                    114:        strlcpy(buf, "response dummy", sizeof(buf));
                    115:        goto restart;
                    116:     } else {
                    117:        warningx("%s", resp);
1.1.1.2   misho     118:        debug_return_int(AUTH_FATAL);
1.1       misho     119:     }
                    120:     if (!pass) {                       /* ^C or error */
1.1.1.2   misho     121:        debug_return_int(AUTH_INTR);
1.1       misho     122:     }
                    123: 
                    124:     /* Send the user's response to the server */
                    125:     (void) snprintf(buf, sizeof(buf), "response '%s'", pass);
                    126:     if (auth_send(buf) || auth_recv(resp, sizeof(resp))) {
1.1.1.5 ! misho     127:        warningx(U_("lost connection to authentication server"));
1.1       misho     128:        error = AUTH_FATAL;
                    129:        goto done;
                    130:     }
                    131: 
                    132:     if (strncmp(resp, "ok", 2) == 0) {
                    133:        error = AUTH_SUCCESS;
                    134:        goto done;
                    135:     }
                    136: 
                    137:     /* Main loop prints "Permission Denied" or insult. */
                    138:     if (strcmp(resp, "Permission Denied.") != 0)
                    139:        warningx("%s", resp);
                    140:     error = AUTH_FAILURE;
                    141: done:
1.1.1.4   misho     142:     memset_s(pass, SUDO_PASS_MAX, 0, strlen(pass));
                    143:     memset_s(buf, sizeof(buf), 0, sizeof(buf));
1.1.1.2   misho     144:     debug_return_int(error);
1.1       misho     145: }
                    146: 
                    147: int
1.1.1.2   misho     148: sudo_fwtk_cleanup(struct passwd *pw, sudo_auth *auth)
1.1       misho     149: {
1.1.1.2   misho     150:     debug_decl(sudo_fwtk_cleanup, SUDO_DEBUG_AUTH)
1.1       misho     151: 
                    152:     auth_close();
1.1.1.2   misho     153:     debug_return_int(AUTH_SUCCESS);
1.1       misho     154: }

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