Annotation of src/usr.bin/ssh/ssh-keysign.c, Revision 1.21
1.1 markus 1: /*
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"
1.20 stevesk 25:
26: #include <paths.h>
1.1 markus 27:
28: #include <openssl/evp.h>
1.6 markus 29: #include <openssl/rand.h>
30: #include <openssl/rsa.h>
1.1 markus 31:
32: #include "log.h"
33: #include "key.h"
1.7 markus 34: #include "ssh.h"
1.1 markus 35: #include "ssh2.h"
36: #include "misc.h"
37: #include "xmalloc.h"
38: #include "buffer.h"
39: #include "bufaux.h"
40: #include "authfile.h"
41: #include "msg.h"
1.2 markus 42: #include "canohost.h"
1.1 markus 43: #include "pathnames.h"
1.7 markus 44: #include "readconf.h"
1.17 dtucker 45: #include "uidswap.h"
1.7 markus 46:
1.12 djm 47: /* XXX readconf.c needs these */
48: uid_t original_real_uid;
1.1 markus 49:
50: static int
1.2 markus 51: valid_request(struct passwd *pw, char *host, Key **ret, u_char *data,
52: u_int datalen)
1.1 markus 53: {
54: Buffer b;
1.11 markus 55: Key *key = NULL;
1.2 markus 56: u_char *pkblob;
57: u_int blen, len;
58: char *pkalg, *p;
1.1 markus 59: int pktype, fail;
60:
61: fail = 0;
62:
63: buffer_init(&b);
64: buffer_append(&b, data, datalen);
1.4 deraadt 65:
1.3 markus 66: /* session id, currently limited to SHA1 (20 bytes) */
67: p = buffer_get_string(&b, &len);
68: if (len != 20)
69: fail++;
70: xfree(p);
71:
1.1 markus 72: if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST)
73: fail++;
74:
75: /* server user */
76: buffer_skip_string(&b);
77:
78: /* service */
79: p = buffer_get_string(&b, NULL);
80: if (strcmp("ssh-connection", p) != 0)
81: fail++;
82: xfree(p);
83:
84: /* method */
85: p = buffer_get_string(&b, NULL);
86: if (strcmp("hostbased", p) != 0)
87: fail++;
88: xfree(p);
89:
90: /* pubkey */
91: pkalg = buffer_get_string(&b, NULL);
92: pkblob = buffer_get_string(&b, &blen);
93:
94: pktype = key_type_from_name(pkalg);
95: if (pktype == KEY_UNSPEC)
96: fail++;
97: else if ((key = key_from_blob(pkblob, blen)) == NULL)
98: fail++;
99: else if (key->type != pktype)
100: fail++;
101: xfree(pkalg);
102: xfree(pkblob);
103:
1.2 markus 104: /* client host name, handle trailing dot */
105: p = buffer_get_string(&b, &len);
106: debug2("valid_request: check expect chost %s got %s", host, p);
107: if (strlen(host) != len - 1)
108: fail++;
109: else if (p[len - 1] != '.')
1.4 deraadt 110: fail++;
1.2 markus 111: else if (strncasecmp(host, p, len - 1) != 0)
1.4 deraadt 112: fail++;
1.2 markus 113: xfree(p);
1.1 markus 114:
115: /* local user */
116: p = buffer_get_string(&b, NULL);
1.2 markus 117:
1.1 markus 118: if (strcmp(pw->pw_name, p) != 0)
119: fail++;
120: xfree(p);
121:
122: /* end of message */
123: if (buffer_len(&b) != 0)
124: fail++;
1.15 markus 125: buffer_free(&b);
1.1 markus 126:
127: debug3("valid_request: fail %d", fail);
128:
129: if (fail && key != NULL)
130: key_free(key);
131: else
132: *ret = key;
133:
134: return (fail ? -1 : 0);
135: }
136:
137: int
138: main(int argc, char **argv)
139: {
140: Buffer b;
1.7 markus 141: Options options;
1.1 markus 142: Key *keys[2], *key;
143: struct passwd *pw;
1.2 markus 144: int key_fd[2], i, found, version = 2, fd;
1.1 markus 145: u_char *signature, *data;
1.2 markus 146: char *host;
1.1 markus 147: u_int slen, dlen;
1.6 markus 148: u_int32_t rnd[256];
1.19 djm 149:
150: /* Ensure that stdin and stdout are connected */
151: if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2)
152: exit(1);
153: /* Leave /dev/null fd iff it is attached to stderr */
154: if (fd > 2)
155: close(fd);
1.1 markus 156:
157: key_fd[0] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY);
158: key_fd[1] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY);
159:
1.18 dtucker 160: original_real_uid = getuid(); /* XXX readconf.c needs this */
161: if ((pw = getpwuid(original_real_uid)) == NULL)
1.17 dtucker 162: fatal("getpwuid failed");
163: pw = pwcopy(pw);
164:
165: permanently_set_uid(pw);
1.1 markus 166:
167: #ifdef DEBUG_SSH_KEYSIGN
168: log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0);
1.4 deraadt 169: #endif
1.7 markus 170:
171: /* verify that ssh-keysign is enabled by the admin */
172: initialize_options(&options);
1.16 djm 173: (void)read_config_file(_PATH_HOST_CONFIG_FILE, "", &options, 0);
1.7 markus 174: fill_default_options(&options);
1.8 markus 175: if (options.enable_ssh_keysign != 1)
176: fatal("ssh-keysign not enabled in %s",
1.7 markus 177: _PATH_HOST_CONFIG_FILE);
1.1 markus 178:
179: if (key_fd[0] == -1 && key_fd[1] == -1)
180: fatal("could not open any host key");
181:
182: SSLeay_add_all_algorithms();
1.6 markus 183: for (i = 0; i < 256; i++)
184: rnd[i] = arc4random();
185: RAND_seed(rnd, sizeof(rnd));
1.1 markus 186:
187: found = 0;
188: for (i = 0; i < 2; i++) {
189: keys[i] = NULL;
190: if (key_fd[i] == -1)
191: continue;
192: keys[i] = key_load_private_pem(key_fd[i], KEY_UNSPEC,
193: NULL, NULL);
194: close(key_fd[i]);
195: if (keys[i] != NULL)
196: found = 1;
197: }
198: if (!found)
199: fatal("no hostkey found");
200:
201: buffer_init(&b);
1.9 djm 202: if (ssh_msg_recv(STDIN_FILENO, &b) < 0)
203: fatal("ssh_msg_recv failed");
1.1 markus 204: if (buffer_get_char(&b) != version)
205: fatal("bad version");
1.2 markus 206: fd = buffer_get_int(&b);
207: if ((fd == STDIN_FILENO) || (fd == STDOUT_FILENO))
208: fatal("bad fd");
209: if ((host = get_local_name(fd)) == NULL)
210: fatal("cannot get sockname for fd");
1.4 deraadt 211:
1.1 markus 212: data = buffer_get_string(&b, &dlen);
1.2 markus 213: if (valid_request(pw, host, &key, data, dlen) < 0)
1.1 markus 214: fatal("not a valid request");
1.2 markus 215: xfree(host);
1.1 markus 216:
217: found = 0;
218: for (i = 0; i < 2; i++) {
219: if (keys[i] != NULL &&
220: key_equal(key, keys[i])) {
221: found = 1;
222: break;
223: }
224: }
225: if (!found)
226: fatal("no matching hostkey found");
227:
228: if (key_sign(keys[i], &signature, &slen, data, dlen) != 0)
229: fatal("key_sign failed");
1.5 markus 230: xfree(data);
1.4 deraadt 231:
1.1 markus 232: /* send reply */
233: buffer_clear(&b);
234: buffer_put_string(&b, signature, slen);
1.14 djm 235: if (ssh_msg_send(STDOUT_FILENO, version, &b) == -1)
236: fatal("ssh_msg_send failed");
1.1 markus 237:
238: return (0);
239: }