login_token.c revision 1.10
1/* $OpenBSD: login_token.c,v 1.10 2013/01/13 21:21:17 fgsch Exp $ */ 2 3/*- 4 * Copyright (c) 1995, 1996 Berkeley Software Design, Inc. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Berkeley Software Design, 17 * Inc. 18 * 4. The name of Berkeley Software Design, Inc. may not be used to endorse 19 * or promote products derived from this software without specific prior 20 * written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``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 BERKELEY SOFTWARE DESIGN, INC. 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 * BSDI $From: login_token.c,v 1.2 1996/09/04 05:33:05 prb Exp $ 35 */ 36 37#include <sys/types.h> 38#include <sys/time.h> 39#include <sys/resource.h> 40 41#include <err.h> 42#include <readpassphrase.h> 43#include <signal.h> 44#include <stdio.h> 45#include <syslog.h> 46#include <stdlib.h> 47#include <string.h> 48#include <unistd.h> 49#include <login_cap.h> 50#include <bsd_auth.h> 51 52#include "token.h" 53 54int 55main(int argc, char *argv[]) 56{ 57 FILE *back = NULL; 58 char *username = 0; 59 char *instance; 60 char challenge[1024]; 61 char response[1024]; 62 char *pp = 0; 63 int c; 64 int mode = 0; 65 struct rlimit cds; 66 sigset_t blockset; 67 68 (void)setpriority(PRIO_PROCESS, 0, 0); 69 70 /* We block keyboard-generated signals during database accesses. */ 71 sigemptyset(&blockset); 72 sigaddset(&blockset, SIGINT); 73 sigaddset(&blockset, SIGQUIT); 74 sigaddset(&blockset, SIGTSTP); 75 76 openlog(NULL, LOG_ODELAY, LOG_AUTH); 77 78 cds.rlim_cur = 0; 79 cds.rlim_max = 0; 80 if (setrlimit(RLIMIT_CORE, &cds) < 0) 81 syslog(LOG_ERR, "couldn't set core dump size to 0: %m"); 82 83 (void)sigprocmask(SIG_BLOCK, &blockset, NULL); 84 if (token_init(argv[0]) < 0) { 85 syslog(LOG_ERR, "unknown token type"); 86 errx(1, "unknown token type"); 87 } 88 (void)sigprocmask(SIG_UNBLOCK, &blockset, NULL); 89 90 while ((c = getopt(argc, argv, "ds:v:")) != -1) 91 switch (c) { 92 case 'd': /* to remain undocumented */ 93 back = stdout; 94 break; 95 case 'v': 96 break; 97 case 's': /* service */ 98 if (strcmp(optarg, "login") == 0) 99 mode = 0; 100 else if (strcmp(optarg, "challenge") == 0) 101 mode = 1; 102 else if (strcmp(optarg, "response") == 0) 103 mode = 2; 104 else { 105 syslog(LOG_ERR, "%s: invalid service", optarg); 106 exit(1); 107 } 108 break; 109 default: 110 syslog(LOG_ERR, "usage error"); 111 exit(1); 112 } 113 114 switch (argc - optind) { 115 case 2: 116 case 1: 117 username = argv[optind]; 118 break; 119 default: 120 syslog(LOG_ERR, "usage error"); 121 exit(1); 122 } 123 124 125 if (back == NULL && (back = fdopen(3, "r+")) == NULL) { 126 syslog(LOG_ERR, "reopening back channel"); 127 exit(1); 128 } 129 if (mode == 2) { 130 mode = 0; 131 c = -1; 132 while (++c < sizeof(challenge) && 133 read(3, &challenge[c], 1) == 1) { 134 if (challenge[c] == '\0' && ++mode == 2) 135 break; 136 if (challenge[c] == '\0' && mode == 1) 137 pp = challenge + c + 1; 138 } 139 if (mode < 2) { 140 syslog(LOG_ERR, "protocol error on back channel"); 141 exit(1); 142 } 143 } else { 144 (void)sigprocmask(SIG_BLOCK, &blockset, NULL); 145 tokenchallenge(username, challenge, sizeof(challenge), 146 tt->proper); 147 (void)sigprocmask(SIG_UNBLOCK, &blockset, NULL); 148 if (mode == 1) { 149 fprintf(back, BI_VALUE " challenge %s\n", 150 auth_mkvalue(challenge)); 151 fprintf(back, BI_CHALLENGE "\n"); 152 exit(0); 153 } 154 155 pp = readpassphrase(challenge, response, sizeof(response), 0); 156 if (pp == NULL) 157 exit(1); 158 if (*pp == '\0') { 159 char buf[64]; 160 snprintf(buf, sizeof(buf), "%s Response [echo on]: ", 161 tt->proper); 162 pp = readpassphrase(buf, response, sizeof(response), 163 RPP_ECHO_ON); 164 if (pp == NULL) 165 exit(1); 166 } 167 } 168 169 (void)sigprocmask(SIG_BLOCK, &blockset, NULL); 170 if (tokenverify(username, challenge, pp) == 0) { 171 fprintf(back, BI_AUTH "\n"); 172 173 if ((instance = strchr(username, '.'))) { 174 *instance++ = 0; 175 if (strcmp(instance, "root") == 0) 176 fprintf(back, BI_ROOTOKAY "\n"); 177 } 178 fprintf(back, BI_SECURE "\n"); 179 exit(0); 180 } 181 182 fprintf(back, BI_REJECT "\n"); 183 exit(1); 184} 185