1*ba1276acSMatthew Dillon /* $OpenBSD: auth2-pubkey.c,v 1.120 2024/05/17 00:30:23 djm Exp $ */
218de8d7fSPeter Avalos /*
318de8d7fSPeter Avalos * Copyright (c) 2000 Markus Friedl. All rights reserved.
4ee116499SAntonio Huete Jimenez * Copyright (c) 2010 Damien Miller. All rights reserved.
518de8d7fSPeter Avalos *
618de8d7fSPeter Avalos * Redistribution and use in source and binary forms, with or without
718de8d7fSPeter Avalos * modification, are permitted provided that the following conditions
818de8d7fSPeter Avalos * are met:
918de8d7fSPeter Avalos * 1. Redistributions of source code must retain the above copyright
1018de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer.
1118de8d7fSPeter Avalos * 2. Redistributions in binary form must reproduce the above copyright
1218de8d7fSPeter Avalos * notice, this list of conditions and the following disclaimer in the
1318de8d7fSPeter Avalos * documentation and/or other materials provided with the distribution.
1418de8d7fSPeter Avalos *
1518de8d7fSPeter Avalos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1618de8d7fSPeter Avalos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1718de8d7fSPeter Avalos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1818de8d7fSPeter Avalos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1918de8d7fSPeter Avalos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2018de8d7fSPeter Avalos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2118de8d7fSPeter Avalos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2218de8d7fSPeter Avalos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2318de8d7fSPeter Avalos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2418de8d7fSPeter Avalos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2518de8d7fSPeter Avalos */
2618de8d7fSPeter Avalos
2718de8d7fSPeter Avalos #include "includes.h"
2818de8d7fSPeter Avalos
2918de8d7fSPeter Avalos #include <sys/types.h>
3018de8d7fSPeter Avalos
310cbfa66cSDaniel Fojt #include <stdlib.h>
3236e94dc5SPeter Avalos #include <errno.h>
3336e94dc5SPeter Avalos #ifdef HAVE_PATHS_H
3436e94dc5SPeter Avalos # include <paths.h>
3536e94dc5SPeter Avalos #endif
3618de8d7fSPeter Avalos #include <pwd.h>
3736e94dc5SPeter Avalos #include <signal.h>
3818de8d7fSPeter Avalos #include <stdio.h>
3918de8d7fSPeter Avalos #include <stdarg.h>
40856ea928SPeter Avalos #include <string.h>
41856ea928SPeter Avalos #include <time.h>
4218de8d7fSPeter Avalos #include <unistd.h>
43e9778795SPeter Avalos #include <limits.h>
4418de8d7fSPeter Avalos
4518de8d7fSPeter Avalos #include "xmalloc.h"
4618de8d7fSPeter Avalos #include "ssh.h"
4718de8d7fSPeter Avalos #include "ssh2.h"
4818de8d7fSPeter Avalos #include "packet.h"
4950a69bb5SSascha Wildner #include "kex.h"
50664f4763Szrj #include "sshbuf.h"
5118de8d7fSPeter Avalos #include "log.h"
5236e94dc5SPeter Avalos #include "misc.h"
5318de8d7fSPeter Avalos #include "servconf.h"
5418de8d7fSPeter Avalos #include "compat.h"
55ce74bacaSMatthew Dillon #include "sshkey.h"
5618de8d7fSPeter Avalos #include "hostfile.h"
5718de8d7fSPeter Avalos #include "auth.h"
5818de8d7fSPeter Avalos #include "pathnames.h"
5918de8d7fSPeter Avalos #include "uidswap.h"
6018de8d7fSPeter Avalos #include "auth-options.h"
6118de8d7fSPeter Avalos #include "canohost.h"
6218de8d7fSPeter Avalos #ifdef GSSAPI
6318de8d7fSPeter Avalos #include "ssh-gss.h"
6418de8d7fSPeter Avalos #endif
6518de8d7fSPeter Avalos #include "monitor_wrap.h"
66856ea928SPeter Avalos #include "authfile.h"
67856ea928SPeter Avalos #include "match.h"
68e9778795SPeter Avalos #include "ssherr.h"
69e9778795SPeter Avalos #include "channels.h" /* XXX for session.h */
70e9778795SPeter Avalos #include "session.h" /* XXX for child_set_env(); refactor? */
710cbfa66cSDaniel Fojt #include "sk-api.h"
7218de8d7fSPeter Avalos
7318de8d7fSPeter Avalos /* import */
7418de8d7fSPeter Avalos extern ServerOptions options;
75*ba1276acSMatthew Dillon extern struct authmethod_cfg methodcfg_pubkey;
7618de8d7fSPeter Avalos
77664f4763Szrj static char *
format_key(const struct sshkey * key)78664f4763Szrj format_key(const struct sshkey *key)
79664f4763Szrj {
80664f4763Szrj char *ret, *fp = sshkey_fingerprint(key,
81664f4763Szrj options.fingerprint_hash, SSH_FP_DEFAULT);
82664f4763Szrj
83664f4763Szrj xasprintf(&ret, "%s %s", sshkey_type(key), fp);
84664f4763Szrj free(fp);
85664f4763Szrj return ret;
86664f4763Szrj }
87664f4763Szrj
8818de8d7fSPeter Avalos static int
userauth_pubkey(struct ssh * ssh,const char * method)89ee116499SAntonio Huete Jimenez userauth_pubkey(struct ssh *ssh, const char *method)
9018de8d7fSPeter Avalos {
91ce74bacaSMatthew Dillon Authctxt *authctxt = ssh->authctxt;
92664f4763Szrj struct passwd *pw = authctxt->pw;
93664f4763Szrj struct sshbuf *b = NULL;
94ee116499SAntonio Huete Jimenez struct sshkey *key = NULL, *hostkey = NULL;
95664f4763Szrj char *pkalg = NULL, *userstyle = NULL, *key_s = NULL, *ca_s = NULL;
96664f4763Szrj u_char *pkblob = NULL, *sig = NULL, have_sig;
97ce74bacaSMatthew Dillon size_t blen, slen;
98ee116499SAntonio Huete Jimenez int hostbound, r, pktype;
9950a69bb5SSascha Wildner int req_presence = 0, req_verify = 0, authenticated = 0;
100664f4763Szrj struct sshauthopt *authopts = NULL;
1010cbfa66cSDaniel Fojt struct sshkey_sig_details *sig_details = NULL;
10218de8d7fSPeter Avalos
103ee116499SAntonio Huete Jimenez hostbound = strcmp(method, "publickey-hostbound-v00@openssh.com") == 0;
104ee116499SAntonio Huete Jimenez
105664f4763Szrj if ((r = sshpkt_get_u8(ssh, &have_sig)) != 0 ||
106664f4763Szrj (r = sshpkt_get_cstring(ssh, &pkalg, NULL)) != 0 ||
107ce74bacaSMatthew Dillon (r = sshpkt_get_string(ssh, &pkblob, &blen)) != 0)
108ee116499SAntonio Huete Jimenez fatal_fr(r, "parse %s packet", method);
109ee116499SAntonio Huete Jimenez
110ee116499SAntonio Huete Jimenez /* hostbound auth includes the hostkey offered at initial KEX */
111ee116499SAntonio Huete Jimenez if (hostbound) {
112ee116499SAntonio Huete Jimenez if ((r = sshpkt_getb_froms(ssh, &b)) != 0 ||
113ee116499SAntonio Huete Jimenez (r = sshkey_fromb(b, &hostkey)) != 0)
114ee116499SAntonio Huete Jimenez fatal_fr(r, "parse %s hostkey", method);
115ee116499SAntonio Huete Jimenez if (ssh->kex->initial_hostkey == NULL)
116ee116499SAntonio Huete Jimenez fatal_f("internal error: initial hostkey not recorded");
117ee116499SAntonio Huete Jimenez if (!sshkey_equal(hostkey, ssh->kex->initial_hostkey))
118ee116499SAntonio Huete Jimenez fatal_f("%s packet contained wrong host key", method);
119ee116499SAntonio Huete Jimenez sshbuf_free(b);
120ee116499SAntonio Huete Jimenez b = NULL;
121ee116499SAntonio Huete Jimenez }
122664f4763Szrj
123664f4763Szrj if (log_level_get() >= SYSLOG_LEVEL_DEBUG2) {
124664f4763Szrj char *keystring;
125664f4763Szrj struct sshbuf *pkbuf;
126664f4763Szrj
127664f4763Szrj if ((pkbuf = sshbuf_from(pkblob, blen)) == NULL)
12850a69bb5SSascha Wildner fatal_f("sshbuf_from failed");
1290cbfa66cSDaniel Fojt if ((keystring = sshbuf_dtob64_string(pkbuf, 0)) == NULL)
13050a69bb5SSascha Wildner fatal_f("sshbuf_dtob64 failed");
13150a69bb5SSascha Wildner debug2_f("%s user %s %s public key %s %s",
132664f4763Szrj authctxt->valid ? "valid" : "invalid", authctxt->user,
133664f4763Szrj have_sig ? "attempting" : "querying", pkalg, keystring);
134664f4763Szrj sshbuf_free(pkbuf);
135664f4763Szrj free(keystring);
13618de8d7fSPeter Avalos }
137664f4763Szrj
138ce74bacaSMatthew Dillon pktype = sshkey_type_from_name(pkalg);
13918de8d7fSPeter Avalos if (pktype == KEY_UNSPEC) {
14018de8d7fSPeter Avalos /* this is perfectly legal */
14150a69bb5SSascha Wildner verbose_f("unsupported public key algorithm: %s", pkalg);
14218de8d7fSPeter Avalos goto done;
14318de8d7fSPeter Avalos }
144ce74bacaSMatthew Dillon if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) {
14550a69bb5SSascha Wildner error_fr(r, "parse key");
146ce74bacaSMatthew Dillon goto done;
147ce74bacaSMatthew Dillon }
14818de8d7fSPeter Avalos if (key == NULL) {
14950a69bb5SSascha Wildner error_f("cannot decode key: %s", pkalg);
15018de8d7fSPeter Avalos goto done;
15118de8d7fSPeter Avalos }
15218de8d7fSPeter Avalos if (key->type != pktype) {
15350a69bb5SSascha Wildner error_f("type mismatch for decoded key "
15450a69bb5SSascha Wildner "(received %d, expected %d)", key->type, pktype);
15518de8d7fSPeter Avalos goto done;
15618de8d7fSPeter Avalos }
157ce74bacaSMatthew Dillon if (auth2_key_already_used(authctxt, key)) {
158ce74bacaSMatthew Dillon logit("refusing previously-used %s key", sshkey_type(key));
159e9778795SPeter Avalos goto done;
160e9778795SPeter Avalos }
16150a69bb5SSascha Wildner if (match_pattern_list(pkalg, options.pubkey_accepted_algos, 0) != 1) {
162ee116499SAntonio Huete Jimenez logit_f("signature algorithm %s not in "
163ee116499SAntonio Huete Jimenez "PubkeyAcceptedAlgorithms", pkalg);
164e9778795SPeter Avalos goto done;
165e9778795SPeter Avalos }
166664f4763Szrj if ((r = sshkey_check_cert_sigtype(key,
167664f4763Szrj options.ca_sign_algorithms)) != 0) {
16850a69bb5SSascha Wildner logit_fr(r, "certificate signature algorithm %s",
169664f4763Szrj (key->cert == NULL || key->cert->signature_type == NULL) ?
17050a69bb5SSascha Wildner "(null)" : key->cert->signature_type);
171664f4763Szrj goto done;
172664f4763Szrj }
173ee116499SAntonio Huete Jimenez if ((r = sshkey_check_rsa_length(key,
174ee116499SAntonio Huete Jimenez options.required_rsa_size)) != 0) {
175ee116499SAntonio Huete Jimenez logit_r(r, "refusing %s key", sshkey_type(key));
176ee116499SAntonio Huete Jimenez goto done;
177ee116499SAntonio Huete Jimenez }
178664f4763Szrj key_s = format_key(key);
179664f4763Szrj if (sshkey_is_cert(key))
180664f4763Szrj ca_s = format_key(key->cert->signature_key);
181e9778795SPeter Avalos
18218de8d7fSPeter Avalos if (have_sig) {
183ee116499SAntonio Huete Jimenez debug3_f("%s have %s signature for %s%s%s",
184ee116499SAntonio Huete Jimenez method, pkalg, key_s,
18550a69bb5SSascha Wildner ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
186ce74bacaSMatthew Dillon if ((r = sshpkt_get_string(ssh, &sig, &slen)) != 0 ||
187ce74bacaSMatthew Dillon (r = sshpkt_get_end(ssh)) != 0)
18850a69bb5SSascha Wildner fatal_fr(r, "parse signature packet");
189ce74bacaSMatthew Dillon if ((b = sshbuf_new()) == NULL)
19050a69bb5SSascha Wildner fatal_f("sshbuf_new failed");
191ce74bacaSMatthew Dillon if (ssh->compat & SSH_OLD_SESSIONID) {
19250a69bb5SSascha Wildner if ((r = sshbuf_putb(b, ssh->kex->session_id)) != 0)
19350a69bb5SSascha Wildner fatal_fr(r, "put old session id");
19418de8d7fSPeter Avalos } else {
19550a69bb5SSascha Wildner if ((r = sshbuf_put_stringb(b,
19650a69bb5SSascha Wildner ssh->kex->session_id)) != 0)
19750a69bb5SSascha Wildner fatal_fr(r, "put session id");
19818de8d7fSPeter Avalos }
199664f4763Szrj if (!authctxt->valid || authctxt->user == NULL) {
20050a69bb5SSascha Wildner debug2_f("disabled because of invalid user");
201664f4763Szrj goto done;
202664f4763Szrj }
20318de8d7fSPeter Avalos /* reconstruct packet */
20436e94dc5SPeter Avalos xasprintf(&userstyle, "%s%s%s", authctxt->user,
20536e94dc5SPeter Avalos authctxt->style ? ":" : "",
20636e94dc5SPeter Avalos authctxt->style ? authctxt->style : "");
207ce74bacaSMatthew Dillon if ((r = sshbuf_put_u8(b, SSH2_MSG_USERAUTH_REQUEST)) != 0 ||
208ce74bacaSMatthew Dillon (r = sshbuf_put_cstring(b, userstyle)) != 0 ||
209664f4763Szrj (r = sshbuf_put_cstring(b, authctxt->service)) != 0 ||
210ee116499SAntonio Huete Jimenez (r = sshbuf_put_cstring(b, method)) != 0 ||
211ce74bacaSMatthew Dillon (r = sshbuf_put_u8(b, have_sig)) != 0 ||
212664f4763Szrj (r = sshbuf_put_cstring(b, pkalg)) != 0 ||
213664f4763Szrj (r = sshbuf_put_string(b, pkblob, blen)) != 0)
214ee116499SAntonio Huete Jimenez fatal_fr(r, "reconstruct %s packet", method);
215ee116499SAntonio Huete Jimenez if (hostbound &&
216ee116499SAntonio Huete Jimenez (r = sshkey_puts(ssh->kex->initial_hostkey, b)) != 0)
217ee116499SAntonio Huete Jimenez fatal_fr(r, "reconstruct %s packet", method);
21818de8d7fSPeter Avalos #ifdef DEBUG_PK
219ce74bacaSMatthew Dillon sshbuf_dump(b, stderr);
22018de8d7fSPeter Avalos #endif
22118de8d7fSPeter Avalos /* test for correct signature */
22218de8d7fSPeter Avalos authenticated = 0;
223*ba1276acSMatthew Dillon if (mm_user_key_allowed(ssh, pw, key, 1, &authopts) &&
224*ba1276acSMatthew Dillon mm_sshkey_verify(key, sig, slen,
225664f4763Szrj sshbuf_ptr(b), sshbuf_len(b),
226664f4763Szrj (ssh->compat & SSH_BUG_SIGTYPE) == 0 ? pkalg : NULL,
227*ba1276acSMatthew Dillon ssh->compat, &sig_details) == 0) {
22818de8d7fSPeter Avalos authenticated = 1;
229e9778795SPeter Avalos }
2300cbfa66cSDaniel Fojt if (authenticated == 1 && sig_details != NULL) {
2310cbfa66cSDaniel Fojt auth2_record_info(authctxt, "signature count = %u",
2320cbfa66cSDaniel Fojt sig_details->sk_counter);
23350a69bb5SSascha Wildner debug_f("sk_counter = %u, sk_flags = 0x%02x",
23450a69bb5SSascha Wildner sig_details->sk_counter, sig_details->sk_flags);
2350cbfa66cSDaniel Fojt req_presence = (options.pubkey_auth_options &
2360cbfa66cSDaniel Fojt PUBKEYAUTH_TOUCH_REQUIRED) ||
2370cbfa66cSDaniel Fojt !authopts->no_require_user_presence;
2380cbfa66cSDaniel Fojt if (req_presence && (sig_details->sk_flags &
2390cbfa66cSDaniel Fojt SSH_SK_USER_PRESENCE_REQD) == 0) {
2400cbfa66cSDaniel Fojt error("public key %s signature for %s%s from "
2410cbfa66cSDaniel Fojt "%.128s port %d rejected: user presence "
2420cbfa66cSDaniel Fojt "(authenticator touch) requirement "
2430cbfa66cSDaniel Fojt "not met ", key_s,
2440cbfa66cSDaniel Fojt authctxt->valid ? "" : "invalid user ",
2450cbfa66cSDaniel Fojt authctxt->user, ssh_remote_ipaddr(ssh),
2460cbfa66cSDaniel Fojt ssh_remote_port(ssh));
2470cbfa66cSDaniel Fojt authenticated = 0;
2480cbfa66cSDaniel Fojt goto done;
2490cbfa66cSDaniel Fojt }
25050a69bb5SSascha Wildner req_verify = (options.pubkey_auth_options &
25150a69bb5SSascha Wildner PUBKEYAUTH_VERIFY_REQUIRED) ||
25250a69bb5SSascha Wildner authopts->require_verify;
25350a69bb5SSascha Wildner if (req_verify && (sig_details->sk_flags &
25450a69bb5SSascha Wildner SSH_SK_USER_VERIFICATION_REQD) == 0) {
25550a69bb5SSascha Wildner error("public key %s signature for %s%s from "
25650a69bb5SSascha Wildner "%.128s port %d rejected: user "
25750a69bb5SSascha Wildner "verification requirement not met ", key_s,
25850a69bb5SSascha Wildner authctxt->valid ? "" : "invalid user ",
25950a69bb5SSascha Wildner authctxt->user, ssh_remote_ipaddr(ssh),
26050a69bb5SSascha Wildner ssh_remote_port(ssh));
26150a69bb5SSascha Wildner authenticated = 0;
26250a69bb5SSascha Wildner goto done;
26350a69bb5SSascha Wildner }
2640cbfa66cSDaniel Fojt }
265ce74bacaSMatthew Dillon auth2_record_key(authctxt, authenticated, key);
26618de8d7fSPeter Avalos } else {
267ee116499SAntonio Huete Jimenez debug_f("%s test pkalg %s pkblob %s%s%s", method, pkalg, key_s,
26850a69bb5SSascha Wildner ca_s == NULL ? "" : " CA ", ca_s == NULL ? "" : ca_s);
269664f4763Szrj
270ce74bacaSMatthew Dillon if ((r = sshpkt_get_end(ssh)) != 0)
27150a69bb5SSascha Wildner fatal_fr(r, "parse packet");
27218de8d7fSPeter Avalos
273664f4763Szrj if (!authctxt->valid || authctxt->user == NULL) {
27450a69bb5SSascha Wildner debug2_f("disabled because of invalid user");
275664f4763Szrj goto done;
276664f4763Szrj }
27718de8d7fSPeter Avalos /* XXX fake reply and always send PK_OK ? */
27818de8d7fSPeter Avalos /*
27918de8d7fSPeter Avalos * XXX this allows testing whether a user is allowed
28018de8d7fSPeter Avalos * to login: if you happen to have a valid pubkey this
28118de8d7fSPeter Avalos * message is sent. the message is NEVER sent at all
28218de8d7fSPeter Avalos * if a user is not allowed to login. is this an
28318de8d7fSPeter Avalos * issue? -markus
28418de8d7fSPeter Avalos */
285*ba1276acSMatthew Dillon if (mm_user_key_allowed(ssh, pw, key, 0, NULL)) {
286ce74bacaSMatthew Dillon if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_PK_OK))
287ce74bacaSMatthew Dillon != 0 ||
288ce74bacaSMatthew Dillon (r = sshpkt_put_cstring(ssh, pkalg)) != 0 ||
289ce74bacaSMatthew Dillon (r = sshpkt_put_string(ssh, pkblob, blen)) != 0 ||
290664f4763Szrj (r = sshpkt_send(ssh)) != 0 ||
291664f4763Szrj (r = ssh_packet_write_wait(ssh)) != 0)
29250a69bb5SSascha Wildner fatal_fr(r, "send packet");
29318de8d7fSPeter Avalos authctxt->postponed = 1;
29418de8d7fSPeter Avalos }
29518de8d7fSPeter Avalos }
29618de8d7fSPeter Avalos done:
297664f4763Szrj if (authenticated == 1 && auth_activate_options(ssh, authopts) != 0) {
29850a69bb5SSascha Wildner debug_f("key options inconsistent with existing");
299664f4763Szrj authenticated = 0;
300664f4763Szrj }
30150a69bb5SSascha Wildner debug2_f("authenticated %d pkalg %s", authenticated, pkalg);
302664f4763Szrj
303664f4763Szrj sshbuf_free(b);
304664f4763Szrj sshauthopt_free(authopts);
305ce74bacaSMatthew Dillon sshkey_free(key);
306ee116499SAntonio Huete Jimenez sshkey_free(hostkey);
307ce74bacaSMatthew Dillon free(userstyle);
30836e94dc5SPeter Avalos free(pkalg);
30936e94dc5SPeter Avalos free(pkblob);
310664f4763Szrj free(key_s);
311664f4763Szrj free(ca_s);
312664f4763Szrj free(sig);
3130cbfa66cSDaniel Fojt sshkey_sig_details_free(sig_details);
31418de8d7fSPeter Avalos return authenticated;
31518de8d7fSPeter Avalos }
31618de8d7fSPeter Avalos
317856ea928SPeter Avalos static int
match_principals_file(struct passwd * pw,char * file,struct sshkey_cert * cert,struct sshauthopt ** authoptsp)318ee116499SAntonio Huete Jimenez match_principals_file(struct passwd *pw, char *file,
319664f4763Szrj struct sshkey_cert *cert, struct sshauthopt **authoptsp)
320e9778795SPeter Avalos {
321e9778795SPeter Avalos FILE *f;
322e9778795SPeter Avalos int success;
323e9778795SPeter Avalos
324664f4763Szrj if (authoptsp != NULL)
325664f4763Szrj *authoptsp = NULL;
326664f4763Szrj
327e9778795SPeter Avalos temporarily_use_uid(pw);
328e9778795SPeter Avalos debug("trying authorized principals file %s", file);
329e9778795SPeter Avalos if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
330e9778795SPeter Avalos restore_uid();
331e9778795SPeter Avalos return 0;
332e9778795SPeter Avalos }
333ee116499SAntonio Huete Jimenez success = auth_process_principals(f, file, cert, authoptsp);
334e9778795SPeter Avalos fclose(f);
335e9778795SPeter Avalos restore_uid();
336e9778795SPeter Avalos return success;
337e9778795SPeter Avalos }
338e9778795SPeter Avalos
339e9778795SPeter Avalos /*
340e9778795SPeter Avalos * Checks whether principal is allowed in output of command.
341e9778795SPeter Avalos * returns 1 if the principal is allowed or 0 otherwise.
342e9778795SPeter Avalos */
343e9778795SPeter Avalos static int
match_principals_command(struct passwd * user_pw,const struct sshkey * key,const char * conn_id,const char * rdomain,struct sshauthopt ** authoptsp)344*ba1276acSMatthew Dillon match_principals_command(struct passwd *user_pw, const struct sshkey *key,
345*ba1276acSMatthew Dillon const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp)
346e9778795SPeter Avalos {
347664f4763Szrj struct passwd *runas_pw = NULL;
348ce74bacaSMatthew Dillon const struct sshkey_cert *cert = key->cert;
349e9778795SPeter Avalos FILE *f = NULL;
350ce74bacaSMatthew Dillon int r, ok, found_principal = 0;
351e9778795SPeter Avalos int i, ac = 0, uid_swapped = 0;
352e9778795SPeter Avalos pid_t pid;
353e9778795SPeter Avalos char *tmp, *username = NULL, *command = NULL, **av = NULL;
354ce74bacaSMatthew Dillon char *ca_fp = NULL, *key_fp = NULL, *catext = NULL, *keytext = NULL;
3550cbfa66cSDaniel Fojt char serial_s[32], uidstr[32];
356e9778795SPeter Avalos void (*osigchld)(int);
357e9778795SPeter Avalos
358664f4763Szrj if (authoptsp != NULL)
359664f4763Szrj *authoptsp = NULL;
360e9778795SPeter Avalos if (options.authorized_principals_command == NULL)
361e9778795SPeter Avalos return 0;
362e9778795SPeter Avalos if (options.authorized_principals_command_user == NULL) {
363e9778795SPeter Avalos error("No user for AuthorizedPrincipalsCommand specified, "
364e9778795SPeter Avalos "skipping");
365e9778795SPeter Avalos return 0;
366e9778795SPeter Avalos }
367e9778795SPeter Avalos
368e9778795SPeter Avalos /*
369e9778795SPeter Avalos * NB. all returns later this function should go via "out" to
370e9778795SPeter Avalos * ensure the original SIGCHLD handler is restored properly.
371e9778795SPeter Avalos */
3720cbfa66cSDaniel Fojt osigchld = ssh_signal(SIGCHLD, SIG_DFL);
373e9778795SPeter Avalos
374e9778795SPeter Avalos /* Prepare and verify the user for the command */
375e9778795SPeter Avalos username = percent_expand(options.authorized_principals_command_user,
376e9778795SPeter Avalos "u", user_pw->pw_name, (char *)NULL);
377664f4763Szrj runas_pw = getpwnam(username);
378664f4763Szrj if (runas_pw == NULL) {
379e9778795SPeter Avalos error("AuthorizedPrincipalsCommandUser \"%s\" not found: %s",
380e9778795SPeter Avalos username, strerror(errno));
381e9778795SPeter Avalos goto out;
382e9778795SPeter Avalos }
383e9778795SPeter Avalos
384e9778795SPeter Avalos /* Turn the command into an argument vector */
38550a69bb5SSascha Wildner if (argv_split(options.authorized_principals_command,
38650a69bb5SSascha Wildner &ac, &av, 0) != 0) {
387e9778795SPeter Avalos error("AuthorizedPrincipalsCommand \"%s\" contains "
3880cbfa66cSDaniel Fojt "invalid quotes", options.authorized_principals_command);
389e9778795SPeter Avalos goto out;
390e9778795SPeter Avalos }
391e9778795SPeter Avalos if (ac == 0) {
392e9778795SPeter Avalos error("AuthorizedPrincipalsCommand \"%s\" yielded no arguments",
3930cbfa66cSDaniel Fojt options.authorized_principals_command);
394e9778795SPeter Avalos goto out;
395e9778795SPeter Avalos }
396ce74bacaSMatthew Dillon if ((ca_fp = sshkey_fingerprint(cert->signature_key,
397ce74bacaSMatthew Dillon options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
39850a69bb5SSascha Wildner error_f("sshkey_fingerprint failed");
399ce74bacaSMatthew Dillon goto out;
400ce74bacaSMatthew Dillon }
401ce74bacaSMatthew Dillon if ((key_fp = sshkey_fingerprint(key,
402ce74bacaSMatthew Dillon options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL) {
40350a69bb5SSascha Wildner error_f("sshkey_fingerprint failed");
404ce74bacaSMatthew Dillon goto out;
405ce74bacaSMatthew Dillon }
406ce74bacaSMatthew Dillon if ((r = sshkey_to_base64(cert->signature_key, &catext)) != 0) {
40750a69bb5SSascha Wildner error_fr(r, "sshkey_to_base64 failed");
408ce74bacaSMatthew Dillon goto out;
409ce74bacaSMatthew Dillon }
410ce74bacaSMatthew Dillon if ((r = sshkey_to_base64(key, &keytext)) != 0) {
41150a69bb5SSascha Wildner error_fr(r, "sshkey_to_base64 failed");
412ce74bacaSMatthew Dillon goto out;
413ce74bacaSMatthew Dillon }
414ce74bacaSMatthew Dillon snprintf(serial_s, sizeof(serial_s), "%llu",
415ce74bacaSMatthew Dillon (unsigned long long)cert->serial);
416664f4763Szrj snprintf(uidstr, sizeof(uidstr), "%llu",
417664f4763Szrj (unsigned long long)user_pw->pw_uid);
418e9778795SPeter Avalos for (i = 1; i < ac; i++) {
419e9778795SPeter Avalos tmp = percent_expand(av[i],
420*ba1276acSMatthew Dillon "C", conn_id,
421*ba1276acSMatthew Dillon "D", rdomain,
422664f4763Szrj "U", uidstr,
423e9778795SPeter Avalos "u", user_pw->pw_name,
424e9778795SPeter Avalos "h", user_pw->pw_dir,
425ce74bacaSMatthew Dillon "t", sshkey_ssh_name(key),
426ce74bacaSMatthew Dillon "T", sshkey_ssh_name(cert->signature_key),
427ce74bacaSMatthew Dillon "f", key_fp,
428ce74bacaSMatthew Dillon "F", ca_fp,
429ce74bacaSMatthew Dillon "k", keytext,
430ce74bacaSMatthew Dillon "K", catext,
431ce74bacaSMatthew Dillon "i", cert->key_id,
432ce74bacaSMatthew Dillon "s", serial_s,
433e9778795SPeter Avalos (char *)NULL);
434e9778795SPeter Avalos if (tmp == NULL)
43550a69bb5SSascha Wildner fatal_f("percent_expand failed");
436e9778795SPeter Avalos free(av[i]);
437e9778795SPeter Avalos av[i] = tmp;
438e9778795SPeter Avalos }
439e9778795SPeter Avalos /* Prepare a printable command for logs, etc. */
440ce74bacaSMatthew Dillon command = argv_assemble(ac, av);
441e9778795SPeter Avalos
44250a69bb5SSascha Wildner if ((pid = subprocess("AuthorizedPrincipalsCommand", command,
443ce74bacaSMatthew Dillon ac, av, &f,
44450a69bb5SSascha Wildner SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
44550a69bb5SSascha Wildner runas_pw, temporarily_use_uid, restore_uid)) == 0)
446e9778795SPeter Avalos goto out;
447e9778795SPeter Avalos
448e9778795SPeter Avalos uid_swapped = 1;
449664f4763Szrj temporarily_use_uid(runas_pw);
450e9778795SPeter Avalos
451ee116499SAntonio Huete Jimenez ok = auth_process_principals(f, "(command)", cert, authoptsp);
452e9778795SPeter Avalos
453ce74bacaSMatthew Dillon fclose(f);
454ce74bacaSMatthew Dillon f = NULL;
455ce74bacaSMatthew Dillon
456ce74bacaSMatthew Dillon if (exited_cleanly(pid, "AuthorizedPrincipalsCommand", command, 0) != 0)
457e9778795SPeter Avalos goto out;
458e9778795SPeter Avalos
459e9778795SPeter Avalos /* Read completed successfully */
460e9778795SPeter Avalos found_principal = ok;
461e9778795SPeter Avalos out:
462e9778795SPeter Avalos if (f != NULL)
463e9778795SPeter Avalos fclose(f);
4640cbfa66cSDaniel Fojt ssh_signal(SIGCHLD, osigchld);
465e9778795SPeter Avalos for (i = 0; i < ac; i++)
466e9778795SPeter Avalos free(av[i]);
467e9778795SPeter Avalos free(av);
468e9778795SPeter Avalos if (uid_swapped)
469e9778795SPeter Avalos restore_uid();
470e9778795SPeter Avalos free(command);
471e9778795SPeter Avalos free(username);
472ce74bacaSMatthew Dillon free(ca_fp);
473ce74bacaSMatthew Dillon free(key_fp);
474ce74bacaSMatthew Dillon free(catext);
475ce74bacaSMatthew Dillon free(keytext);
476e9778795SPeter Avalos return found_principal;
477e9778795SPeter Avalos }
478664f4763Szrj
479856ea928SPeter Avalos /* Authenticate a certificate key against TrustedUserCAKeys */
480856ea928SPeter Avalos static int
user_cert_trusted_ca(struct passwd * pw,struct sshkey * key,const char * remote_ip,const char * remote_host,const char * conn_id,const char * rdomain,struct sshauthopt ** authoptsp)481ee116499SAntonio Huete Jimenez user_cert_trusted_ca(struct passwd *pw, struct sshkey *key,
482ee116499SAntonio Huete Jimenez const char *remote_ip, const char *remote_host,
483*ba1276acSMatthew Dillon const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp)
484856ea928SPeter Avalos {
485856ea928SPeter Avalos char *ca_fp, *principals_file = NULL;
486856ea928SPeter Avalos const char *reason;
487664f4763Szrj struct sshauthopt *principals_opts = NULL, *cert_opts = NULL;
488664f4763Szrj struct sshauthopt *final_opts = NULL;
489ce74bacaSMatthew Dillon int r, ret = 0, found_principal = 0, use_authorized_principals;
490856ea928SPeter Avalos
491664f4763Szrj if (authoptsp != NULL)
492664f4763Szrj *authoptsp = NULL;
493664f4763Szrj
494ce74bacaSMatthew Dillon if (!sshkey_is_cert(key) || options.trusted_user_ca_keys == NULL)
495856ea928SPeter Avalos return 0;
496856ea928SPeter Avalos
497e9778795SPeter Avalos if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
498e9778795SPeter Avalos options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
499e9778795SPeter Avalos return 0;
500856ea928SPeter Avalos
501ce74bacaSMatthew Dillon if ((r = sshkey_in_file(key->cert->signature_key,
502ce74bacaSMatthew Dillon options.trusted_user_ca_keys, 1, 0)) != 0) {
50350a69bb5SSascha Wildner debug2_fr(r, "CA %s %s is not listed in %s",
504ce74bacaSMatthew Dillon sshkey_type(key->cert->signature_key), ca_fp,
50550a69bb5SSascha Wildner options.trusted_user_ca_keys);
506856ea928SPeter Avalos goto out;
507856ea928SPeter Avalos }
508856ea928SPeter Avalos /*
509856ea928SPeter Avalos * If AuthorizedPrincipals is in use, then compare the certificate
510856ea928SPeter Avalos * principals against the names in that file rather than matching
511856ea928SPeter Avalos * against the username.
512856ea928SPeter Avalos */
513856ea928SPeter Avalos if ((principals_file = authorized_principals_file(pw)) != NULL) {
514ee116499SAntonio Huete Jimenez if (match_principals_file(pw, principals_file,
515664f4763Szrj key->cert, &principals_opts))
516e9778795SPeter Avalos found_principal = 1;
517e9778795SPeter Avalos }
518e9778795SPeter Avalos /* Try querying command if specified */
519ee116499SAntonio Huete Jimenez if (!found_principal && match_principals_command(pw, key,
520*ba1276acSMatthew Dillon conn_id, rdomain, &principals_opts))
521e9778795SPeter Avalos found_principal = 1;
522e9778795SPeter Avalos /* If principals file or command is specified, then require a match */
523e9778795SPeter Avalos use_authorized_principals = principals_file != NULL ||
524e9778795SPeter Avalos options.authorized_principals_command != NULL;
525e9778795SPeter Avalos if (!found_principal && use_authorized_principals) {
526e9778795SPeter Avalos reason = "Certificate does not contain an authorized principal";
527664f4763Szrj goto fail_reason;
528664f4763Szrj }
529664f4763Szrj if (use_authorized_principals && principals_opts == NULL)
53050a69bb5SSascha Wildner fatal_f("internal error: missing principals_opts");
53150a69bb5SSascha Wildner if (sshkey_cert_check_authority_now(key, 0, 1, 0,
532664f4763Szrj use_authorized_principals ? NULL : pw->pw_name, &reason) != 0)
533664f4763Szrj goto fail_reason;
534664f4763Szrj
535664f4763Szrj /* Check authority from options in key and from principals file/cmd */
536664f4763Szrj if ((cert_opts = sshauthopt_from_cert(key)) == NULL) {
537664f4763Szrj reason = "Invalid certificate options";
538664f4763Szrj goto fail_reason;
539664f4763Szrj }
540ee116499SAntonio Huete Jimenez if (auth_authorise_keyopts(pw, cert_opts, 0,
541ee116499SAntonio Huete Jimenez remote_ip, remote_host, "cert") != 0) {
542664f4763Szrj reason = "Refused by certificate options";
543664f4763Szrj goto fail_reason;
544664f4763Szrj }
545664f4763Szrj if (principals_opts == NULL) {
546664f4763Szrj final_opts = cert_opts;
547664f4763Szrj cert_opts = NULL;
548664f4763Szrj } else {
549ee116499SAntonio Huete Jimenez if (auth_authorise_keyopts(pw, principals_opts, 0,
550ee116499SAntonio Huete Jimenez remote_ip, remote_host, "principals") != 0) {
551664f4763Szrj reason = "Refused by certificate principals options";
552664f4763Szrj goto fail_reason;
553664f4763Szrj }
554664f4763Szrj if ((final_opts = sshauthopt_merge(principals_opts,
555664f4763Szrj cert_opts, &reason)) == NULL) {
556856ea928SPeter Avalos fail_reason:
557856ea928SPeter Avalos error("%s", reason);
558856ea928SPeter Avalos auth_debug_add("%s", reason);
559856ea928SPeter Avalos goto out;
560856ea928SPeter Avalos }
561664f4763Szrj }
562856ea928SPeter Avalos
563664f4763Szrj /* Success */
564e9778795SPeter Avalos verbose("Accepted certificate ID \"%s\" (serial %llu) signed by "
565e9778795SPeter Avalos "%s CA %s via %s", key->cert->key_id,
566e9778795SPeter Avalos (unsigned long long)key->cert->serial,
567ce74bacaSMatthew Dillon sshkey_type(key->cert->signature_key), ca_fp,
568856ea928SPeter Avalos options.trusted_user_ca_keys);
569664f4763Szrj if (authoptsp != NULL) {
570664f4763Szrj *authoptsp = final_opts;
571664f4763Szrj final_opts = NULL;
572664f4763Szrj }
573856ea928SPeter Avalos ret = 1;
574856ea928SPeter Avalos out:
575664f4763Szrj sshauthopt_free(principals_opts);
576664f4763Szrj sshauthopt_free(cert_opts);
577664f4763Szrj sshauthopt_free(final_opts);
57836e94dc5SPeter Avalos free(principals_file);
57936e94dc5SPeter Avalos free(ca_fp);
580856ea928SPeter Avalos return ret;
581856ea928SPeter Avalos }
582856ea928SPeter Avalos
58336e94dc5SPeter Avalos /*
58436e94dc5SPeter Avalos * Checks whether key is allowed in file.
58536e94dc5SPeter Avalos * returns 1 if the key is allowed or 0 otherwise.
58636e94dc5SPeter Avalos */
58736e94dc5SPeter Avalos static int
user_key_allowed2(struct passwd * pw,struct sshkey * key,char * file,const char * remote_ip,const char * remote_host,struct sshauthopt ** authoptsp)588ee116499SAntonio Huete Jimenez user_key_allowed2(struct passwd *pw, struct sshkey *key,
589ee116499SAntonio Huete Jimenez char *file, const char *remote_ip, const char *remote_host,
590ee116499SAntonio Huete Jimenez struct sshauthopt **authoptsp)
59136e94dc5SPeter Avalos {
59236e94dc5SPeter Avalos FILE *f;
59336e94dc5SPeter Avalos int found_key = 0;
59436e94dc5SPeter Avalos
595664f4763Szrj if (authoptsp != NULL)
596664f4763Szrj *authoptsp = NULL;
597664f4763Szrj
59836e94dc5SPeter Avalos /* Temporarily use the user's uid. */
59936e94dc5SPeter Avalos temporarily_use_uid(pw);
60036e94dc5SPeter Avalos
60136e94dc5SPeter Avalos debug("trying public key file %s", file);
60236e94dc5SPeter Avalos if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
603ee116499SAntonio Huete Jimenez found_key = auth_check_authkeys_file(pw, f, file,
604ee116499SAntonio Huete Jimenez key, remote_ip, remote_host, authoptsp);
60536e94dc5SPeter Avalos fclose(f);
60636e94dc5SPeter Avalos }
60736e94dc5SPeter Avalos
60836e94dc5SPeter Avalos restore_uid();
60936e94dc5SPeter Avalos return found_key;
61036e94dc5SPeter Avalos }
61136e94dc5SPeter Avalos
61236e94dc5SPeter Avalos /*
61336e94dc5SPeter Avalos * Checks whether key is allowed in output of command.
61436e94dc5SPeter Avalos * returns 1 if the key is allowed or 0 otherwise.
61536e94dc5SPeter Avalos */
61636e94dc5SPeter Avalos static int
user_key_command_allowed2(struct passwd * user_pw,struct sshkey * key,const char * remote_ip,const char * remote_host,const char * conn_id,const char * rdomain,struct sshauthopt ** authoptsp)617ee116499SAntonio Huete Jimenez user_key_command_allowed2(struct passwd *user_pw, struct sshkey *key,
618ee116499SAntonio Huete Jimenez const char *remote_ip, const char *remote_host,
619*ba1276acSMatthew Dillon const char *conn_id, const char *rdomain, struct sshauthopt **authoptsp)
62036e94dc5SPeter Avalos {
621664f4763Szrj struct passwd *runas_pw = NULL;
622e9778795SPeter Avalos FILE *f = NULL;
623e9778795SPeter Avalos int r, ok, found_key = 0;
624e9778795SPeter Avalos int i, uid_swapped = 0, ac = 0;
62536e94dc5SPeter Avalos pid_t pid;
626e9778795SPeter Avalos char *username = NULL, *key_fp = NULL, *keytext = NULL;
627664f4763Szrj char uidstr[32], *tmp, *command = NULL, **av = NULL;
628e9778795SPeter Avalos void (*osigchld)(int);
62936e94dc5SPeter Avalos
630664f4763Szrj if (authoptsp != NULL)
631664f4763Szrj *authoptsp = NULL;
632e9778795SPeter Avalos if (options.authorized_keys_command == NULL)
63336e94dc5SPeter Avalos return 0;
63436e94dc5SPeter Avalos if (options.authorized_keys_command_user == NULL) {
63536e94dc5SPeter Avalos error("No user for AuthorizedKeysCommand specified, skipping");
63636e94dc5SPeter Avalos return 0;
63736e94dc5SPeter Avalos }
63836e94dc5SPeter Avalos
639e9778795SPeter Avalos /*
640e9778795SPeter Avalos * NB. all returns later this function should go via "out" to
641e9778795SPeter Avalos * ensure the original SIGCHLD handler is restored properly.
642e9778795SPeter Avalos */
6430cbfa66cSDaniel Fojt osigchld = ssh_signal(SIGCHLD, SIG_DFL);
644e9778795SPeter Avalos
645e9778795SPeter Avalos /* Prepare and verify the user for the command */
64636e94dc5SPeter Avalos username = percent_expand(options.authorized_keys_command_user,
64736e94dc5SPeter Avalos "u", user_pw->pw_name, (char *)NULL);
648664f4763Szrj runas_pw = getpwnam(username);
649664f4763Szrj if (runas_pw == NULL) {
65036e94dc5SPeter Avalos error("AuthorizedKeysCommandUser \"%s\" not found: %s",
65136e94dc5SPeter Avalos username, strerror(errno));
65236e94dc5SPeter Avalos goto out;
65336e94dc5SPeter Avalos }
65436e94dc5SPeter Avalos
655e9778795SPeter Avalos /* Prepare AuthorizedKeysCommand */
656e9778795SPeter Avalos if ((key_fp = sshkey_fingerprint(key, options.fingerprint_hash,
657e9778795SPeter Avalos SSH_FP_DEFAULT)) == NULL) {
65850a69bb5SSascha Wildner error_f("sshkey_fingerprint failed");
659e9778795SPeter Avalos goto out;
660e9778795SPeter Avalos }
661e9778795SPeter Avalos if ((r = sshkey_to_base64(key, &keytext)) != 0) {
66250a69bb5SSascha Wildner error_fr(r, "sshkey_to_base64 failed");
66336e94dc5SPeter Avalos goto out;
66436e94dc5SPeter Avalos }
66536e94dc5SPeter Avalos
666e9778795SPeter Avalos /* Turn the command into an argument vector */
66750a69bb5SSascha Wildner if (argv_split(options.authorized_keys_command, &ac, &av, 0) != 0) {
668e9778795SPeter Avalos error("AuthorizedKeysCommand \"%s\" contains invalid quotes",
66950a69bb5SSascha Wildner options.authorized_keys_command);
670e9778795SPeter Avalos goto out;
671e9778795SPeter Avalos }
672e9778795SPeter Avalos if (ac == 0) {
673e9778795SPeter Avalos error("AuthorizedKeysCommand \"%s\" yielded no arguments",
67450a69bb5SSascha Wildner options.authorized_keys_command);
675e9778795SPeter Avalos goto out;
676e9778795SPeter Avalos }
677664f4763Szrj snprintf(uidstr, sizeof(uidstr), "%llu",
678664f4763Szrj (unsigned long long)user_pw->pw_uid);
679e9778795SPeter Avalos for (i = 1; i < ac; i++) {
680e9778795SPeter Avalos tmp = percent_expand(av[i],
681*ba1276acSMatthew Dillon "C", conn_id,
682*ba1276acSMatthew Dillon "D", rdomain,
683664f4763Szrj "U", uidstr,
684e9778795SPeter Avalos "u", user_pw->pw_name,
685e9778795SPeter Avalos "h", user_pw->pw_dir,
686e9778795SPeter Avalos "t", sshkey_ssh_name(key),
687e9778795SPeter Avalos "f", key_fp,
688e9778795SPeter Avalos "k", keytext,
689e9778795SPeter Avalos (char *)NULL);
690e9778795SPeter Avalos if (tmp == NULL)
69150a69bb5SSascha Wildner fatal_f("percent_expand failed");
692e9778795SPeter Avalos free(av[i]);
693e9778795SPeter Avalos av[i] = tmp;
694e9778795SPeter Avalos }
695e9778795SPeter Avalos /* Prepare a printable command for logs, etc. */
696ce74bacaSMatthew Dillon command = argv_assemble(ac, av);
69736e94dc5SPeter Avalos
69836e94dc5SPeter Avalos /*
699e9778795SPeter Avalos * If AuthorizedKeysCommand was run without arguments
700e9778795SPeter Avalos * then fall back to the old behaviour of passing the
701e9778795SPeter Avalos * target username as a single argument.
70236e94dc5SPeter Avalos */
703e9778795SPeter Avalos if (ac == 1) {
704e9778795SPeter Avalos av = xreallocarray(av, ac + 2, sizeof(*av));
705e9778795SPeter Avalos av[1] = xstrdup(user_pw->pw_name);
706e9778795SPeter Avalos av[2] = NULL;
707e9778795SPeter Avalos /* Fix up command too, since it is used in log messages */
708e9778795SPeter Avalos free(command);
709e9778795SPeter Avalos xasprintf(&command, "%s %s", av[0], av[1]);
71036e94dc5SPeter Avalos }
71136e94dc5SPeter Avalos
71250a69bb5SSascha Wildner if ((pid = subprocess("AuthorizedKeysCommand", command,
713ce74bacaSMatthew Dillon ac, av, &f,
71450a69bb5SSascha Wildner SSH_SUBPROCESS_STDOUT_CAPTURE|SSH_SUBPROCESS_STDERR_DISCARD,
71550a69bb5SSascha Wildner runas_pw, temporarily_use_uid, restore_uid)) == 0)
716e9778795SPeter Avalos goto out;
71736e94dc5SPeter Avalos
718e9778795SPeter Avalos uid_swapped = 1;
719664f4763Szrj temporarily_use_uid(runas_pw);
72036e94dc5SPeter Avalos
721ee116499SAntonio Huete Jimenez ok = auth_check_authkeys_file(user_pw, f,
722ee116499SAntonio Huete Jimenez options.authorized_keys_command, key, remote_ip,
723ee116499SAntonio Huete Jimenez remote_host, authoptsp);
72436e94dc5SPeter Avalos
725ce74bacaSMatthew Dillon fclose(f);
726ce74bacaSMatthew Dillon f = NULL;
727ce74bacaSMatthew Dillon
728ce74bacaSMatthew Dillon if (exited_cleanly(pid, "AuthorizedKeysCommand", command, 0) != 0)
72936e94dc5SPeter Avalos goto out;
730e9778795SPeter Avalos
731e9778795SPeter Avalos /* Read completed successfully */
73236e94dc5SPeter Avalos found_key = ok;
73336e94dc5SPeter Avalos out:
734e9778795SPeter Avalos if (f != NULL)
735e9778795SPeter Avalos fclose(f);
7360cbfa66cSDaniel Fojt ssh_signal(SIGCHLD, osigchld);
737e9778795SPeter Avalos for (i = 0; i < ac; i++)
738e9778795SPeter Avalos free(av[i]);
739e9778795SPeter Avalos free(av);
740e9778795SPeter Avalos if (uid_swapped)
74136e94dc5SPeter Avalos restore_uid();
742e9778795SPeter Avalos free(command);
743e9778795SPeter Avalos free(username);
744e9778795SPeter Avalos free(key_fp);
745e9778795SPeter Avalos free(keytext);
74636e94dc5SPeter Avalos return found_key;
74736e94dc5SPeter Avalos }
74836e94dc5SPeter Avalos
74936e94dc5SPeter Avalos /*
75036e94dc5SPeter Avalos * Check whether key authenticates and authorises the user.
75136e94dc5SPeter Avalos */
75218de8d7fSPeter Avalos int
user_key_allowed(struct ssh * ssh,struct passwd * pw,struct sshkey * key,int auth_attempt,struct sshauthopt ** authoptsp)753664f4763Szrj user_key_allowed(struct ssh *ssh, struct passwd *pw, struct sshkey *key,
754664f4763Szrj int auth_attempt, struct sshauthopt **authoptsp)
75518de8d7fSPeter Avalos {
7560cbfa66cSDaniel Fojt u_int success = 0, i;
757*ba1276acSMatthew Dillon char *file, *conn_id;
758664f4763Szrj struct sshauthopt *opts = NULL;
759*ba1276acSMatthew Dillon const char *rdomain, *remote_ip, *remote_host;
7600cbfa66cSDaniel Fojt
761664f4763Szrj if (authoptsp != NULL)
762664f4763Szrj *authoptsp = NULL;
76318de8d7fSPeter Avalos
764856ea928SPeter Avalos if (auth_key_is_revoked(key))
765856ea928SPeter Avalos return 0;
766ce74bacaSMatthew Dillon if (sshkey_is_cert(key) &&
767ce74bacaSMatthew Dillon auth_key_is_revoked(key->cert->signature_key))
768856ea928SPeter Avalos return 0;
769856ea928SPeter Avalos
770*ba1276acSMatthew Dillon if ((rdomain = ssh_packet_rdomain_in(ssh)) == NULL)
771*ba1276acSMatthew Dillon rdomain = "";
772*ba1276acSMatthew Dillon remote_ip = ssh_remote_ipaddr(ssh);
773*ba1276acSMatthew Dillon remote_host = auth_get_canonical_hostname(ssh, options.use_dns);
774*ba1276acSMatthew Dillon xasprintf(&conn_id, "%s %d %s %d",
775*ba1276acSMatthew Dillon ssh_local_ipaddr(ssh), ssh_local_port(ssh),
776*ba1276acSMatthew Dillon remote_ip, ssh_remote_port(ssh));
777*ba1276acSMatthew Dillon
7780cbfa66cSDaniel Fojt for (i = 0; !success && i < options.num_authkeys_files; i++) {
7790cbfa66cSDaniel Fojt if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
7800cbfa66cSDaniel Fojt continue;
7810cbfa66cSDaniel Fojt file = expand_authorized_keys(
7820cbfa66cSDaniel Fojt options.authorized_keys_files[i], pw);
783ee116499SAntonio Huete Jimenez success = user_key_allowed2(pw, key, file,
784ee116499SAntonio Huete Jimenez remote_ip, remote_host, &opts);
7850cbfa66cSDaniel Fojt free(file);
7860cbfa66cSDaniel Fojt if (!success) {
7870cbfa66cSDaniel Fojt sshauthopt_free(opts);
7880cbfa66cSDaniel Fojt opts = NULL;
7890cbfa66cSDaniel Fojt }
7900cbfa66cSDaniel Fojt }
7910cbfa66cSDaniel Fojt if (success)
7920cbfa66cSDaniel Fojt goto out;
7930cbfa66cSDaniel Fojt
794ee116499SAntonio Huete Jimenez if ((success = user_cert_trusted_ca(pw, key, remote_ip, remote_host,
795*ba1276acSMatthew Dillon conn_id, rdomain, &opts)) != 0)
796664f4763Szrj goto out;
797664f4763Szrj sshauthopt_free(opts);
798664f4763Szrj opts = NULL;
799856ea928SPeter Avalos
800ee116499SAntonio Huete Jimenez if ((success = user_key_command_allowed2(pw, key, remote_ip,
801*ba1276acSMatthew Dillon remote_host, conn_id, rdomain, &opts)) != 0)
802664f4763Szrj goto out;
803664f4763Szrj sshauthopt_free(opts);
804664f4763Szrj opts = NULL;
80536e94dc5SPeter Avalos
806664f4763Szrj out:
807*ba1276acSMatthew Dillon free(conn_id);
808664f4763Szrj if (success && authoptsp != NULL) {
809664f4763Szrj *authoptsp = opts;
810664f4763Szrj opts = NULL;
811664f4763Szrj }
812664f4763Szrj sshauthopt_free(opts);
81318de8d7fSPeter Avalos return success;
81418de8d7fSPeter Avalos }
81518de8d7fSPeter Avalos
81618de8d7fSPeter Avalos Authmethod method_pubkey = {
817*ba1276acSMatthew Dillon &methodcfg_pubkey,
81818de8d7fSPeter Avalos userauth_pubkey,
81918de8d7fSPeter Avalos };
820