login_token.c revision 1.4
1/* $OpenBSD: login_token.c,v 1.4 2001/12/06 05:37:04 millert 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/param.h> 39#include <sys/time.h> 40#include <sys/resource.h> 41 42#include <err.h> 43#include <readpassphrase.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(argc, argv) 56 int argc; 57 char **argv; 58{ 59 FILE *back = NULL; 60 char *class = 0; 61 char *username = 0; 62 char *instance; 63 char challenge[1024]; 64 char response[1024]; 65 char *pp = 0; 66 int c; 67 int mode = 0; 68 struct rlimit cds; 69 70 (void)setpriority(PRIO_PROCESS, 0, 0); 71 72 openlog(NULL, LOG_ODELAY, LOG_AUTH); 73 74 cds.rlim_cur = 0; 75 cds.rlim_max = 0; 76 if (setrlimit(RLIMIT_CORE, &cds) < 0) 77 syslog(LOG_ERR, "couldn't set core dump size to 0: %m"); 78 79 if (token_init(argv[0]) < 0) { 80 syslog(LOG_ERR, "unknown token type"); 81 errx(1, "unknown token type"); 82 } 83 84 while ((c = getopt(argc, argv, "ds:v:")) != -1) 85 switch(c) { 86 case 'd': /* to remain undocumented */ 87 back = stdout; 88 break; 89 case 'v': 90 break; 91 case 's': /* service */ 92 if (strcmp(optarg, "login") == 0) 93 mode = 0; 94 else if (strcmp(optarg, "challenge") == 0) 95 mode = 1; 96 else if (strcmp(optarg, "response") == 0) 97 mode = 2; 98 else { 99 syslog(LOG_ERR, "%s: invalid service", optarg); 100 exit(1); 101 } 102 break; 103 default: 104 syslog(LOG_ERR, "usage error"); 105 exit(1); 106 } 107 108 switch(argc - optind) { 109 case 2: 110 class = argv[optind + 1]; 111 case 1: 112 username = argv[optind]; 113 break; 114 default: 115 syslog(LOG_ERR, "usage error"); 116 exit(1); 117 } 118 119 120 if (back == NULL && (back = fdopen(3, "r+")) == NULL) { 121 syslog(LOG_ERR, "reopening back channel"); 122 exit(1); 123 } 124 if (mode == 2) { 125 mode = 0; 126 c = -1; 127 while (++c < sizeof(challenge) && 128 read(3, &challenge[c], 1) == 1) { 129 if (challenge[c] == '\0' && ++mode == 2) 130 break; 131 if (challenge[c] == '\0' && mode == 1) 132 pp = challenge + c + 1; 133 } 134 if (mode < 2) { 135 syslog(LOG_ERR, "protocol error on back channel"); 136 exit(1); 137 } 138 } else { 139 tokenchallenge(username, challenge, sizeof(challenge), 140 tt->proper); 141 if (mode == 1) { 142 fprintf(back, BI_VALUE " challenge %s\n", 143 auth_mkvalue(challenge)); 144 fprintf(back, BI_CHALLENGE "\n"); 145 exit(0); 146 } 147 148 pp = readpassphrase(challenge, response, sizeof(response), 0); 149 if (!pp || *pp == '\0') { 150 char buf[64]; 151 snprintf(buf, sizeof(buf), "%s Response [echo on]: ", 152 tt->proper); 153 pp = readpassphrase(buf, response, sizeof(response), 154 RPP_ECHO_ON); 155 } 156 } 157 158 if (tokenverify(username, challenge, pp) == 0) { 159 fprintf(back, BI_AUTH "\n"); 160 161 if ((instance = strchr(username, '.'))) { 162 *instance++ = 0; 163 if (strcmp(instance, "root") == 0) 164 fprintf(back, BI_ROOTOKAY "\n"); 165 } 166 fprintf(back, BI_SECURE "\n"); 167 exit(0); 168 } 169 170 fprintf(back, BI_REJECT "\n"); 171 exit(1); 172} 173