blob: a96babfa5f6228f70c3dc0a0e6131e6894ff0e97 [file] [log] [blame]
Ben Lindstrom1bad2562002-06-06 19:57:33 +00001/*
2 * Copyright (c) 2002 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24#include "includes.h"
Damien Miller03e20032006-03-15 11:16:59 +110025
Damien Miller6645e7a2006-03-15 14:42:54 +110026#ifdef HAVE_PATHS_H
Damien Miller03e20032006-03-15 11:16:59 +110027#include <paths.h>
Damien Miller6645e7a2006-03-15 14:42:54 +110028#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +000029
30#include <openssl/evp.h>
Ben Lindstrom43ce2c82002-07-04 00:17:33 +000031#include <openssl/rand.h>
32#include <openssl/rsa.h>
Ben Lindstrom1bad2562002-06-06 19:57:33 +000033
34#include "log.h"
35#include "key.h"
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +000036#include "ssh.h"
Ben Lindstrom1bad2562002-06-06 19:57:33 +000037#include "ssh2.h"
38#include "misc.h"
39#include "xmalloc.h"
40#include "buffer.h"
41#include "bufaux.h"
42#include "authfile.h"
43#include "msg.h"
44#include "canohost.h"
45#include "pathnames.h"
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +000046#include "readconf.h"
Darren Tucker25f60a72004-08-15 17:23:34 +100047#include "uidswap.h"
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +000048
Damien Miller20a8f972003-05-18 20:50:30 +100049/* XXX readconf.c needs these */
50uid_t original_real_uid;
Ben Lindstrom1bad2562002-06-06 19:57:33 +000051
Ben Lindstrom35453522002-06-07 14:37:00 +000052extern char *__progname;
Ben Lindstrom35453522002-06-07 14:37:00 +000053
Ben Lindstrom1bad2562002-06-06 19:57:33 +000054static int
55valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
56 u_int datalen)
57{
58 Buffer b;
Damien Miller703ced52003-04-09 20:50:26 +100059 Key *key = NULL;
Ben Lindstrom1bad2562002-06-06 19:57:33 +000060 u_char *pkblob;
61 u_int blen, len;
62 char *pkalg, *p;
63 int pktype, fail;
64
65 fail = 0;
66
67 buffer_init(&b);
68 buffer_append(&b, data, datalen);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000069
Ben Lindstroma2071572002-06-09 20:01:48 +000070 /* session id, currently limited to SHA1 (20 bytes) */
71 p = buffer_get_string(&b, &len);
72 if (len != 20)
73 fail++;
74 xfree(p);
75
Ben Lindstrom1bad2562002-06-06 19:57:33 +000076 if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
77 fail++;
78
79 /* server user */
80 buffer_skip_string(&b);
81
82 /* service */
83 p = buffer_get_string(&b, NULL);
84 if (strcmp("ssh-connection", p) != 0)
85 fail++;
86 xfree(p);
87
88 /* method */
89 p = buffer_get_string(&b, NULL);
90 if (strcmp("hostbased", p) != 0)
91 fail++;
92 xfree(p);
93
94 /* pubkey */
95 pkalg = buffer_get_string(&b, NULL);
96 pkblob = buffer_get_string(&b, &blen);
97
98 pktype = key_type_from_name(pkalg);
99 if (pktype == KEY_UNSPEC)
100 fail++;
101 else if ((key = key_from_blob(pkblob, blen)) == NULL)
102 fail++;
103 else if (key->type != pktype)
104 fail++;
105 xfree(pkalg);
106 xfree(pkblob);
107
108 /* client host name, handle trailing dot */
109 p = buffer_get_string(&b, &len);
110 debug2("valid_request: check expect chost %s got %s", host, p);
111 if (strlen(host) != len - 1)
112 fail++;
113 else if (p[len - 1] != '.')
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000114 fail++;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000115 else if (strncasecmp(host, p, len - 1) != 0)
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000116 fail++;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000117 xfree(p);
118
119 /* local user */
120 p = buffer_get_string(&b, NULL);
121
122 if (strcmp(pw->pw_name, p) != 0)
123 fail++;
124 xfree(p);
125
126 /* end of message */
127 if (buffer_len(&b) != 0)
128 fail++;
Damien Millerfb1310e2004-01-21 11:02:50 +1100129 buffer_free(&b);
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000130
131 debug3("valid_request: fail %d", fail);
132
133 if (fail && key != NULL)
134 key_free(key);
135 else
136 *ret = key;
137
138 return (fail ? -1 : 0);
139}
140
141int
142main(int argc, char **argv)
143{
144 Buffer b;
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000145 Options options;
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000146 Key *keys[2], *key;
147 struct passwd *pw;
148 int key_fd[2], i, found, version = 2, fd;
149 u_char *signature, *data;
150 char *host;
151 u_int slen, dlen;
Ben Lindstrom43ce2c82002-07-04 00:17:33 +0000152 u_int32_t rnd[256];
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000153
Darren Tuckerce321d82005-10-03 18:11:24 +1000154 /* Ensure that stdin and stdout are connected */
155 if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
156 exit(1);
157 /* Leave /dev/null fd iff it is attached to stderr */
158 if (fd > 2)
159 close(fd);
160
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000161 key_fd[0] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
162 key_fd[1] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
163
Darren Tucker34620d62004-08-29 16:32:59 +1000164 original_real_uid = getuid(); /* XXX readconf.c needs this */
165 if ((pw = getpwuid(original_real_uid)) == NULL)
Darren Tucker25f60a72004-08-15 17:23:34 +1000166 fatal("getpwuid failed");
167 pw = pwcopy(pw);
168
169 permanently_set_uid(pw);
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000170
Ben Lindstrom5a6abda2002-06-09 19:41:48 +0000171 init_rng();
172 seed_rng();
173 arc4random_stir();
Ben Lindstromdb41d232002-06-07 03:11:38 +0000174
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000175#ifdef DEBUG_SSH_KEYSIGN
176 log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000177#endif
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000178
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000179 /* verify that ssh-keysign is enabled by the admin */
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000180 initialize_options(&options);
Damien Miller57a44762004-04-20 20:11:57 +1000181 (void)read_config_file(_PATH_HOST_CONFIG_FILE, "", &options, 0);
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000182 fill_default_options(&options);
Ben Lindstromb6df73b2002-11-09 15:52:31 +0000183 if (options.enable_ssh_keysign != 1)
184 fatal("ssh-keysign not enabled in %s",
Ben Lindstrom5d35a2f2002-07-04 00:19:40 +0000185 _PATH_HOST_CONFIG_FILE);
186
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000187 if (key_fd[0] == -1 && key_fd[1] == -1)
188 fatal("could not open any host key");
189
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000190 SSLeay_add_all_algorithms();
Ben Lindstrom43ce2c82002-07-04 00:17:33 +0000191 for (i = 0; i < 256; i++)
192 rnd[i] = arc4random();
193 RAND_seed(rnd, sizeof(rnd));
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000194
195 found = 0;
196 for (i = 0; i < 2; i++) {
197 keys[i] = NULL;
198 if (key_fd[i] == -1)
199 continue;
200 keys[i] = key_load_private_pem(key_fd[i], KEY_UNSPEC,
201 NULL, NULL);
202 close(key_fd[i]);
203 if (keys[i] != NULL)
204 found = 1;
205 }
206 if (!found)
207 fatal("no hostkey found");
208
209 buffer_init(&b);
Damien Miller901119b2002-10-04 11:10:04 +1000210 if (ssh_msg_recv(STDIN_FILENO, &b) < 0)
211 fatal("ssh_msg_recv failed");
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000212 if (buffer_get_char(&b) != version)
213 fatal("bad version");
214 fd = buffer_get_int(&b);
215 if ((fd == STDIN_FILENO) || (fd == STDOUT_FILENO))
216 fatal("bad fd");
217 if ((host = get_local_name(fd)) == NULL)
218 fatal("cannot get sockname for fd");
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000219
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000220 data = buffer_get_string(&b, &dlen);
221 if (valid_request(pw, host, &key, data, dlen) < 0)
222 fatal("not a valid request");
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000223 xfree(host);
224
225 found = 0;
226 for (i = 0; i < 2; i++) {
227 if (keys[i] != NULL &&
228 key_equal(key, keys[i])) {
229 found = 1;
230 break;
231 }
232 }
233 if (!found)
234 fatal("no matching hostkey found");
235
236 if (key_sign(keys[i], &signature, &slen, data, dlen) != 0)
237 fatal("key_sign failed");
Ben Lindstromfe275982002-06-27 00:25:07 +0000238 xfree(data);
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000239
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000240 /* send reply */
241 buffer_clear(&b);
242 buffer_put_string(&b, signature, slen);
Damien Miller51bf11f2003-11-17 21:20:47 +1100243 if (ssh_msg_send(STDOUT_FILENO, version, &b) == -1)
244 fatal("ssh_msg_send failed");
Ben Lindstrom1bad2562002-06-06 19:57:33 +0000245
246 return (0);
247}