/*1* SPDX-License-Identifier: ISC2*3* Copyright (c) 1999-2005, 2007, 2010-2012, 2014-20164* Todd C. Miller <[email protected]>5* Copyright (c) 2002 Michael Stroucken <[email protected]>6*7* Permission to use, copy, modify, and distribute this software for any8* purpose with or without fee is hereby granted, provided that the above9* copyright notice and this permission notice appear in all copies.10*11* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES12* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF13* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR14* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES15* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN16* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF17* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.18*19* Sponsored in part by the Defense Advanced Research Projects20* Agency (DARPA) and Air Force Research Laboratory, Air Force21* Materiel Command, USAF, under agreement number F39502-99-1-0512.22*/2324#include <config.h>2526#ifdef HAVE_SECURID2728#include <sys/types.h>29#include <stdio.h>30#include <stdlib.h>31#include <string.h>32#include <unistd.h>33#include <pwd.h>3435/* Needed for SecurID v5.0 Authentication on UNIX */36#define UNIX 137#include <acexport.h>38#include <sdacmvls.h>3940#include <sudoers.h>41#include "sudo_auth.h"4243/*44* securid_init - Initialises communications with ACE server45* Arguments in:46* pw - UNUSED47* auth - sudo authentication structure48*49* Results out:50* auth - auth->data contains pointer to new SecurID handle51* return code - Fatal if initialization unsuccessful, otherwise52* success.53*/54int55sudo_securid_init(const struct sudoers_context *ctx, struct passwd *pw,56sudo_auth *auth)57{58static SDI_HANDLE sd_dat; /* SecurID handle */59debug_decl(sudo_securid_init, SUDOERS_DEBUG_AUTH);6061/* Only initialize once. */62if (auth->data != NULL)63debug_return_int(AUTH_SUCCESS);6465if (IS_NONINTERACTIVE(auth))66debug_return_int(AUTH_NONINTERACTIVE);6768/* Start communications */69if (AceInitialize() == SD_FALSE) {70sudo_warnx("%s", U_("failed to initialise the ACE API library"));71debug_return_int(AUTH_ERROR);72}7374auth->data = (void *) &sd_dat; /* For method-specific data */7576debug_return_int(AUTH_SUCCESS);77}7879/*80* securid_setup - Initialises a SecurID transaction and locks out other81* ACE servers82*83* Arguments in:84* pw - struct passwd for username85* promptp - UNUSED86* auth - sudo authentication structure for SecurID handle87*88* Results out:89* return code - Success if transaction started correctly, fatal90* otherwise91*/92int93sudo_securid_setup(const struct sudoers_context *ctx, struct passwd *pw,94char **promptp, sudo_auth *auth)95{96SDI_HANDLE *sd = (SDI_HANDLE *) auth->data;97int retval;98debug_decl(sudo_securid_setup, SUDOERS_DEBUG_AUTH);99100/* Re-initialize SecurID every time. */101if (SD_Init(sd) != ACM_OK) {102sudo_warnx("%s", U_("unable to contact the SecurID server"));103debug_return_int(AUTH_ERROR);104}105106/* Lock new PIN code */107retval = SD_Lock(*sd, pw->pw_name);108109switch (retval) {110case ACM_OK:111sudo_warnx("%s", U_("User ID locked for SecurID Authentication"));112debug_return_int(AUTH_SUCCESS);113114case ACE_UNDEFINED_USERNAME:115sudo_warnx("%s", U_("invalid username length for SecurID"));116debug_return_int(AUTH_ERROR);117118case ACE_ERR_INVALID_HANDLE:119sudo_warnx("%s", U_("invalid Authentication Handle for SecurID"));120debug_return_int(AUTH_ERROR);121122case ACM_ACCESS_DENIED:123sudo_warnx("%s", U_("SecurID communication failed"));124debug_return_int(AUTH_ERROR);125126default:127sudo_warnx("%s", U_("unknown SecurID error"));128debug_return_int(AUTH_ERROR);129}130}131132/*133* securid_verify - Authenticates user and handles ACE responses134*135* Arguments in:136* pw - struct passwd for username137* prompt - UNUSED138* auth - sudo authentication structure for SecurID handle139*140* Results out:141* return code - Success on successful authentication, failure on142* incorrect authentication, fatal on errors143*/144int145sudo_securid_verify(const struct sudoers_context *ctx, struct passwd *pw,146const char *promp, sudo_auth *auth, struct sudo_conv_callback *callback)147{148SDI_HANDLE *sd = (SDI_HANDLE *) auth->data;149char *pass;150int ret;151debug_decl(sudo_securid_verify, SUDOERS_DEBUG_AUTH);152153pass = auth_getpass("Enter your PASSCODE: ", SUDO_CONV_PROMPT_ECHO_OFF,154callback);155156/* Have ACE verify password */157switch (SD_Check(*sd, pass, pw->pw_name)) {158case ACM_OK:159ret = AUTH_SUCESS;160break;161162case ACE_UNDEFINED_PASSCODE:163sudo_warnx("%s", U_("invalid passcode length for SecurID"));164ret = AUTH_ERROR;165break;166167case ACE_UNDEFINED_USERNAME:168sudo_warnx("%s", U_("invalid username length for SecurID"));169ret = AUTH_ERROR;170break;171172case ACE_ERR_INVALID_HANDLE:173sudo_warnx("%s", U_("invalid Authentication Handle for SecurID"));174ret = AUTH_ERROR;175break;176177case ACM_ACCESS_DENIED:178ret = AUTH_FAILURE;179break;180181case ACM_NEXT_CODE_REQUIRED:182/* Sometimes (when current token close to expire?)183ACE challenges for the next token displayed184(entered without the PIN) */185if (pass != NULL)186freezero(pass, strlen(pass));187pass = auth_getpass("\188!!! ATTENTION !!!\n\189Wait for the token code to change, \n\190then enter the new token code.\n", \191SUDO_CONV_PROMPT_ECHO_OFF, callback);192193if (SD_Next(*sd, pass) == ACM_OK) {194ret = AUTH_SUCCESS;195break;196}197198ret = AUTH_FAILURE;199break;200201case ACM_NEW_PIN_REQUIRED:202/*203* This user's SecurID has not been activated yet,204* or the pin has been reset205*/206/* XXX - Is setting up a new PIN within sudo's scope? */207SD_Pin(*sd, "");208sudo_printf(SUDO_CONV_ERROR_MSG|SUDO_CONV_PREFER_TTY,209"Your SecurID access has not yet been set up.\n");210sudo_printf(SUDO_CONV_ERROR_MSG|SUDO_CONV_PREFER_TTY,211"Please set up a PIN before you try to authenticate.\n");212ret = AUTH_ERROR;213break;214215default:216sudo_warnx("%s", U_("unknown SecurID error"));217ret = AUTH_ERROR;218break;219}220221/* Free resources */222SD_Close(*sd);223224if (pass != NULL)225freezero(pass, strlen(pass));226227/* Return stored state to calling process */228debug_return_int(ret);229}230231#endif /* HAVE_SECURID */232233234