monitor.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191
  1. /* $OpenBSD: monitor.c,v 1.214 2020/08/27 01:07:09 djm Exp $ */
  2. /*
  3. * Copyright 2002 Niels Provos <provos@citi.umich.edu>
  4. * Copyright 2002 Markus Friedl <markus@openbsd.org>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "includes.h"
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <sys/wait.h>
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #include <limits.h>
  34. #ifdef HAVE_PATHS_H
  35. #include <paths.h>
  36. #endif
  37. #include <pwd.h>
  38. #include <signal.h>
  39. #ifdef HAVE_STDINT_H
  40. # include <stdint.h>
  41. #endif
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <stdarg.h>
  45. #include <stdio.h>
  46. #include <unistd.h>
  47. #ifdef HAVE_POLL_H
  48. #include <poll.h>
  49. #else
  50. # ifdef HAVE_SYS_POLL_H
  51. # include <sys/poll.h>
  52. # endif
  53. #endif
  54. #ifdef WITH_OPENSSL
  55. #include <openssl/dh.h>
  56. #endif
  57. #include "openbsd-compat/sys-tree.h"
  58. #include "openbsd-compat/sys-queue.h"
  59. #include "openbsd-compat/openssl-compat.h"
  60. #include "atomicio.h"
  61. #include "xmalloc.h"
  62. #include "ssh.h"
  63. #include "sshkey.h"
  64. #include "sshbuf.h"
  65. #include "hostfile.h"
  66. #include "auth.h"
  67. #include "cipher.h"
  68. #include "kex.h"
  69. #include "dh.h"
  70. #include "auth-pam.h"
  71. #include "packet.h"
  72. #include "auth-options.h"
  73. #include "sshpty.h"
  74. #include "channels.h"
  75. #include "session.h"
  76. #include "sshlogin.h"
  77. #include "canohost.h"
  78. #include "log.h"
  79. #include "misc.h"
  80. #include "servconf.h"
  81. #include "monitor.h"
  82. #ifdef GSSAPI
  83. #include "ssh-gss.h"
  84. #endif
  85. #include "monitor_wrap.h"
  86. #include "monitor_fdpass.h"
  87. #include "compat.h"
  88. #include "ssh2.h"
  89. #include "authfd.h"
  90. #include "audit.h"
  91. #include "match.h"
  92. #include "ssherr.h"
  93. #include "sk-api.h"
  94. #ifdef GSSAPI
  95. static Gssctxt *gsscontext = NULL;
  96. #endif
  97. /* Imports */
  98. extern ServerOptions options;
  99. extern u_int utmp_len;
  100. extern struct sshbuf *loginmsg;
  101. extern struct sshauthopt *auth_opts; /* XXX move to permanent ssh->authctxt? */
  102. extern void destroy_sensitive_data(struct ssh *, int);
  103. /* State exported from the child */
  104. static struct sshbuf *child_state;
  105. /* Functions on the monitor that answer unprivileged requests */
  106. int mm_answer_moduli(struct ssh *, int, struct sshbuf *);
  107. int mm_answer_sign(struct ssh *, int, struct sshbuf *);
  108. int mm_answer_pwnamallow(struct ssh *, int, struct sshbuf *);
  109. int mm_answer_auth2_read_banner(struct ssh *, int, struct sshbuf *);
  110. int mm_answer_authserv(struct ssh *, int, struct sshbuf *);
  111. #ifdef WITH_SELINUX
  112. int mm_answer_authrole(struct ssh *, int, struct sshbuf *);
  113. #endif
  114. int mm_answer_authpassword(struct ssh *, int, struct sshbuf *);
  115. int mm_answer_bsdauthquery(struct ssh *, int, struct sshbuf *);
  116. int mm_answer_bsdauthrespond(struct ssh *, int, struct sshbuf *);
  117. int mm_answer_skeyquery(struct ssh *, int, struct sshbuf *);
  118. int mm_answer_skeyrespond(struct ssh *, int, struct sshbuf *);
  119. int mm_answer_keyallowed(struct ssh *, int, struct sshbuf *);
  120. int mm_answer_keyverify(struct ssh *, int, struct sshbuf *);
  121. int mm_answer_pty(struct ssh *, int, struct sshbuf *);
  122. int mm_answer_pty_cleanup(struct ssh *, int, struct sshbuf *);
  123. int mm_answer_term(struct ssh *, int, struct sshbuf *);
  124. int mm_answer_rsa_keyallowed(struct ssh *, int, struct sshbuf *);
  125. int mm_answer_rsa_challenge(struct ssh *, int, struct sshbuf *);
  126. int mm_answer_rsa_response(struct ssh *, int, struct sshbuf *);
  127. int mm_answer_sesskey(struct ssh *, int, struct sshbuf *);
  128. int mm_answer_sessid(struct ssh *, int, struct sshbuf *);
  129. #ifdef USE_PAM
  130. int mm_answer_pam_start(struct ssh *, int, struct sshbuf *);
  131. int mm_answer_pam_account(struct ssh *, int, struct sshbuf *);
  132. int mm_answer_pam_init_ctx(struct ssh *, int, struct sshbuf *);
  133. int mm_answer_pam_query(struct ssh *, int, struct sshbuf *);
  134. int mm_answer_pam_respond(struct ssh *, int, struct sshbuf *);
  135. int mm_answer_pam_free_ctx(struct ssh *, int, struct sshbuf *);
  136. #endif
  137. #ifdef GSSAPI
  138. int mm_answer_gss_setup_ctx(struct ssh *, int, struct sshbuf *);
  139. int mm_answer_gss_accept_ctx(struct ssh *, int, struct sshbuf *);
  140. int mm_answer_gss_userok(struct ssh *, int, struct sshbuf *);
  141. int mm_answer_gss_checkmic(struct ssh *, int, struct sshbuf *);
  142. #endif
  143. #ifdef SSH_AUDIT_EVENTS
  144. int mm_answer_audit_event(struct ssh *, int, struct sshbuf *);
  145. int mm_answer_audit_command(struct ssh *, int, struct sshbuf *);
  146. int mm_answer_audit_end_command(struct ssh *, int, struct sshbuf *);
  147. int mm_answer_audit_unsupported_body(struct ssh *, int, struct sshbuf *);
  148. int mm_answer_audit_kex_body(struct ssh *, int, struct sshbuf *);
  149. int mm_answer_audit_session_key_free_body(struct ssh *, int, struct sshbuf *);
  150. int mm_answer_audit_server_key_free(struct ssh *, int, struct sshbuf *);
  151. #endif
  152. static Authctxt *authctxt;
  153. /* local state for key verify */
  154. static u_char *key_blob = NULL;
  155. static size_t key_bloblen = 0;
  156. static int key_blobtype = MM_NOKEY;
  157. static struct sshauthopt *key_opts = NULL;
  158. static char *hostbased_cuser = NULL;
  159. static char *hostbased_chost = NULL;
  160. static char *auth_method = "unknown";
  161. static char *auth_submethod = NULL;
  162. static u_int session_id2_len = 0;
  163. static u_char *session_id2 = NULL;
  164. static pid_t monitor_child_pid;
  165. struct mon_table {
  166. enum monitor_reqtype type;
  167. int flags;
  168. int (*f)(struct ssh *, int, struct sshbuf *);
  169. };
  170. #define MON_ISAUTH 0x0004 /* Required for Authentication */
  171. #define MON_AUTHDECIDE 0x0008 /* Decides Authentication */
  172. #define MON_ONCE 0x0010 /* Disable after calling */
  173. #define MON_ALOG 0x0020 /* Log auth attempt without authenticating */
  174. #define MON_AUTH (MON_ISAUTH|MON_AUTHDECIDE)
  175. #define MON_PERMIT 0x1000 /* Request is permitted */
  176. static int monitor_read(struct ssh *, struct monitor *, struct mon_table *,
  177. struct mon_table **);
  178. static int monitor_read_log(struct monitor *);
  179. struct mon_table mon_dispatch_proto20[] = {
  180. #ifdef WITH_OPENSSL
  181. {MONITOR_REQ_MODULI, MON_ONCE, mm_answer_moduli},
  182. #endif
  183. {MONITOR_REQ_SIGN, MON_ONCE, mm_answer_sign},
  184. {MONITOR_REQ_PWNAM, MON_ONCE, mm_answer_pwnamallow},
  185. {MONITOR_REQ_AUTHSERV, MON_ONCE, mm_answer_authserv},
  186. #ifdef WITH_SELINUX
  187. {MONITOR_REQ_AUTHROLE, MON_ONCE, mm_answer_authrole},
  188. #endif
  189. {MONITOR_REQ_AUTH2_READ_BANNER, MON_ONCE, mm_answer_auth2_read_banner},
  190. {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
  191. #ifdef USE_PAM
  192. {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
  193. {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
  194. {MONITOR_REQ_PAM_INIT_CTX, MON_ONCE, mm_answer_pam_init_ctx},
  195. {MONITOR_REQ_PAM_QUERY, 0, mm_answer_pam_query},
  196. {MONITOR_REQ_PAM_RESPOND, MON_ONCE, mm_answer_pam_respond},
  197. {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
  198. #endif
  199. #ifdef SSH_AUDIT_EVENTS
  200. {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
  201. {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
  202. {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
  203. {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
  204. {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
  205. #endif
  206. #ifdef BSD_AUTH
  207. {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
  208. {MONITOR_REQ_BSDAUTHRESPOND, MON_AUTH, mm_answer_bsdauthrespond},
  209. #endif
  210. {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
  211. {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
  212. #ifdef GSSAPI
  213. {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
  214. {MONITOR_REQ_GSSSTEP, 0, mm_answer_gss_accept_ctx},
  215. {MONITOR_REQ_GSSUSEROK, MON_ONCE|MON_AUTHDECIDE, mm_answer_gss_userok},
  216. {MONITOR_REQ_GSSCHECKMIC, MON_ONCE, mm_answer_gss_checkmic},
  217. #endif
  218. {0, 0, NULL}
  219. };
  220. struct mon_table mon_dispatch_postauth20[] = {
  221. #ifdef WITH_OPENSSL
  222. {MONITOR_REQ_MODULI, 0, mm_answer_moduli},
  223. #endif
  224. {MONITOR_REQ_SIGN, 0, mm_answer_sign},
  225. {MONITOR_REQ_PTY, 0, mm_answer_pty},
  226. {MONITOR_REQ_PTYCLEANUP, 0, mm_answer_pty_cleanup},
  227. {MONITOR_REQ_TERM, 0, mm_answer_term},
  228. #ifdef SSH_AUDIT_EVENTS
  229. {MONITOR_REQ_AUDIT_EVENT, MON_PERMIT, mm_answer_audit_event},
  230. {MONITOR_REQ_AUDIT_COMMAND, MON_PERMIT, mm_answer_audit_command},
  231. {MONITOR_REQ_AUDIT_END_COMMAND, MON_PERMIT, mm_answer_audit_end_command},
  232. {MONITOR_REQ_AUDIT_UNSUPPORTED, MON_PERMIT, mm_answer_audit_unsupported_body},
  233. {MONITOR_REQ_AUDIT_KEX, MON_PERMIT, mm_answer_audit_kex_body},
  234. {MONITOR_REQ_AUDIT_SESSION_KEY_FREE, MON_PERMIT, mm_answer_audit_session_key_free_body},
  235. {MONITOR_REQ_AUDIT_SERVER_KEY_FREE, MON_PERMIT, mm_answer_audit_server_key_free},
  236. #endif
  237. {0, 0, NULL}
  238. };
  239. struct mon_table *mon_dispatch;
  240. /* Specifies if a certain message is allowed at the moment */
  241. static void
  242. monitor_permit(struct mon_table *ent, enum monitor_reqtype type, int permit)
  243. {
  244. while (ent->f != NULL) {
  245. if (ent->type == type) {
  246. ent->flags &= ~MON_PERMIT;
  247. ent->flags |= permit ? MON_PERMIT : 0;
  248. return;
  249. }
  250. ent++;
  251. }
  252. }
  253. static void
  254. monitor_permit_authentications(int permit)
  255. {
  256. struct mon_table *ent = mon_dispatch;
  257. while (ent->f != NULL) {
  258. if (ent->flags & MON_AUTH) {
  259. ent->flags &= ~MON_PERMIT;
  260. ent->flags |= permit ? MON_PERMIT : 0;
  261. }
  262. ent++;
  263. }
  264. }
  265. void
  266. monitor_child_preauth(struct ssh *ssh, struct monitor *pmonitor)
  267. {
  268. struct mon_table *ent;
  269. int authenticated = 0, partial = 0;
  270. debug3("preauth child monitor started");
  271. if (pmonitor->m_recvfd >= 0)
  272. close(pmonitor->m_recvfd);
  273. if (pmonitor->m_log_sendfd >= 0)
  274. close(pmonitor->m_log_sendfd);
  275. pmonitor->m_log_sendfd = pmonitor->m_recvfd = -1;
  276. pmonitor->m_state = "preauth";
  277. authctxt = (Authctxt *)ssh->authctxt;
  278. memset(authctxt, 0, sizeof(*authctxt));
  279. ssh->authctxt = authctxt;
  280. authctxt->loginmsg = loginmsg;
  281. mon_dispatch = mon_dispatch_proto20;
  282. /* Permit requests for moduli and signatures */
  283. monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
  284. monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
  285. /* The first few requests do not require asynchronous access */
  286. while (!authenticated) {
  287. partial = 0;
  288. auth_method = "unknown";
  289. auth_submethod = NULL;
  290. auth2_authctxt_reset_info(authctxt);
  291. authenticated = (monitor_read(ssh, pmonitor,
  292. mon_dispatch, &ent) == 1);
  293. /* Special handling for multiple required authentications */
  294. if (options.num_auth_methods != 0) {
  295. if (authenticated &&
  296. !auth2_update_methods_lists(authctxt,
  297. auth_method, auth_submethod)) {
  298. debug3("%s: method %s: partial", __func__,
  299. auth_method);
  300. authenticated = 0;
  301. partial = 1;
  302. }
  303. }
  304. if (authenticated) {
  305. if (!(ent->flags & MON_AUTHDECIDE))
  306. fatal("%s: unexpected authentication from %d",
  307. __func__, ent->type);
  308. if (authctxt->pw->pw_uid == 0 &&
  309. !auth_root_allowed(ssh, auth_method))
  310. authenticated = 0;
  311. #ifdef USE_PAM
  312. /* PAM needs to perform account checks after auth */
  313. if (options.use_pam && authenticated) {
  314. struct sshbuf *m;
  315. if ((m = sshbuf_new()) == NULL)
  316. fatal("%s: sshbuf_new failed",
  317. __func__);
  318. mm_request_receive_expect(pmonitor->m_sendfd,
  319. MONITOR_REQ_PAM_ACCOUNT, m);
  320. authenticated = mm_answer_pam_account(
  321. ssh, pmonitor->m_sendfd, m);
  322. sshbuf_free(m);
  323. }
  324. #endif
  325. }
  326. if (ent->flags & (MON_AUTHDECIDE|MON_ALOG)) {
  327. auth_log(ssh, authenticated, partial,
  328. auth_method, auth_submethod);
  329. if (!partial && !authenticated)
  330. authctxt->failures++;
  331. if (authenticated || partial) {
  332. auth2_update_session_info(authctxt,
  333. auth_method, auth_submethod);
  334. }
  335. }
  336. }
  337. if (!authctxt->valid)
  338. fatal("%s: authenticated invalid user", __func__);
  339. if (strcmp(auth_method, "unknown") == 0)
  340. fatal("%s: authentication method name unknown", __func__);
  341. debug("%s: %s has been authenticated by privileged process",
  342. __func__, authctxt->user);
  343. ssh->authctxt = NULL;
  344. ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
  345. mm_get_keystate(ssh, pmonitor);
  346. /* Drain any buffered messages from the child */
  347. while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
  348. ;
  349. if (pmonitor->m_recvfd >= 0)
  350. close(pmonitor->m_recvfd);
  351. if (pmonitor->m_log_sendfd >= 0)
  352. close(pmonitor->m_log_sendfd);
  353. pmonitor->m_sendfd = pmonitor->m_log_recvfd = -1;
  354. }
  355. static void
  356. monitor_set_child_handler(pid_t pid)
  357. {
  358. monitor_child_pid = pid;
  359. }
  360. static void
  361. monitor_child_handler(int sig)
  362. {
  363. kill(monitor_child_pid, sig);
  364. }
  365. void
  366. monitor_child_postauth(struct ssh *ssh, struct monitor *pmonitor)
  367. {
  368. close(pmonitor->m_recvfd);
  369. pmonitor->m_recvfd = -1;
  370. pmonitor->m_state = "postauth";
  371. monitor_set_child_handler(pmonitor->m_pid);
  372. ssh_signal(SIGHUP, &monitor_child_handler);
  373. ssh_signal(SIGTERM, &monitor_child_handler);
  374. ssh_signal(SIGINT, &monitor_child_handler);
  375. #ifdef SIGXFSZ
  376. ssh_signal(SIGXFSZ, SIG_IGN);
  377. #endif
  378. mon_dispatch = mon_dispatch_postauth20;
  379. /* Permit requests for moduli and signatures */
  380. monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
  381. monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
  382. monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
  383. if (auth_opts->permit_pty_flag) {
  384. monitor_permit(mon_dispatch, MONITOR_REQ_PTY, 1);
  385. monitor_permit(mon_dispatch, MONITOR_REQ_PTYCLEANUP, 1);
  386. }
  387. for (;;)
  388. monitor_read(ssh, pmonitor, mon_dispatch, NULL);
  389. }
  390. static int
  391. monitor_read_log(struct monitor *pmonitor)
  392. {
  393. struct sshbuf *logmsg;
  394. u_int len, level;
  395. char *msg;
  396. u_char *p;
  397. int r;
  398. if ((logmsg = sshbuf_new()) == NULL)
  399. fatal("%s: sshbuf_new", __func__);
  400. /* Read length */
  401. if ((r = sshbuf_reserve(logmsg, 4, &p)) != 0)
  402. fatal("%s: reserve: %s", __func__, ssh_err(r));
  403. if (atomicio(read, pmonitor->m_log_recvfd, p, 4) != 4) {
  404. if (errno == EPIPE) {
  405. sshbuf_free(logmsg);
  406. debug("%s: child log fd closed", __func__);
  407. close(pmonitor->m_log_recvfd);
  408. pmonitor->m_log_recvfd = -1;
  409. return -1;
  410. }
  411. fatal("%s: log fd read: %s", __func__, strerror(errno));
  412. }
  413. if ((r = sshbuf_get_u32(logmsg, &len)) != 0)
  414. fatal("%s: get len: %s", __func__, ssh_err(r));
  415. if (len <= 4 || len > 8192)
  416. fatal("%s: invalid log message length %u", __func__, len);
  417. /* Read severity, message */
  418. sshbuf_reset(logmsg);
  419. if ((r = sshbuf_reserve(logmsg, len, &p)) != 0)
  420. fatal("%s: reserve: %s", __func__, ssh_err(r));
  421. if (atomicio(read, pmonitor->m_log_recvfd, p, len) != len)
  422. fatal("%s: log fd read: %s", __func__, strerror(errno));
  423. if ((r = sshbuf_get_u32(logmsg, &level)) != 0 ||
  424. (r = sshbuf_get_cstring(logmsg, &msg, NULL)) != 0)
  425. fatal("%s: decode: %s", __func__, ssh_err(r));
  426. /* Log it */
  427. if (log_level_name(level) == NULL)
  428. fatal("%s: invalid log level %u (corrupted message?)",
  429. __func__, level);
  430. do_log2(level, "%s [%s]", msg, pmonitor->m_state);
  431. sshbuf_free(logmsg);
  432. free(msg);
  433. return 0;
  434. }
  435. static int
  436. monitor_read(struct ssh *ssh, struct monitor *pmonitor, struct mon_table *ent,
  437. struct mon_table **pent)
  438. {
  439. struct sshbuf *m;
  440. int r, ret;
  441. u_char type;
  442. struct pollfd pfd[2];
  443. for (;;) {
  444. memset(&pfd, 0, sizeof(pfd));
  445. pfd[0].fd = pmonitor->m_sendfd;
  446. pfd[0].events = POLLIN;
  447. pfd[1].fd = pmonitor->m_log_recvfd;
  448. pfd[1].events = pfd[1].fd == -1 ? 0 : POLLIN;
  449. if (poll(pfd, pfd[1].fd == -1 ? 1 : 2, -1) == -1) {
  450. if (errno == EINTR || errno == EAGAIN)
  451. continue;
  452. fatal("%s: poll: %s", __func__, strerror(errno));
  453. }
  454. if (pfd[1].revents) {
  455. /*
  456. * Drain all log messages before processing next
  457. * monitor request.
  458. */
  459. monitor_read_log(pmonitor);
  460. continue;
  461. }
  462. if (pfd[0].revents)
  463. break; /* Continues below */
  464. }
  465. if ((m = sshbuf_new()) == NULL)
  466. fatal("%s: sshbuf_new", __func__);
  467. mm_request_receive(pmonitor->m_sendfd, m);
  468. if ((r = sshbuf_get_u8(m, &type)) != 0)
  469. fatal("%s: decode: %s", __func__, ssh_err(r));
  470. debug3("%s: checking request %d", __func__, type);
  471. while (ent->f != NULL) {
  472. if (ent->type == type)
  473. break;
  474. ent++;
  475. }
  476. if (ent->f != NULL) {
  477. if (!(ent->flags & MON_PERMIT))
  478. fatal("%s: unpermitted request %d", __func__,
  479. type);
  480. ret = (*ent->f)(ssh, pmonitor->m_sendfd, m);
  481. sshbuf_free(m);
  482. /* The child may use this request only once, disable it */
  483. if (ent->flags & MON_ONCE) {
  484. debug2("%s: %d used once, disabling now", __func__,
  485. type);
  486. ent->flags &= ~MON_PERMIT;
  487. }
  488. if (pent != NULL)
  489. *pent = ent;
  490. return ret;
  491. }
  492. fatal("%s: unsupported request: %d", __func__, type);
  493. /* NOTREACHED */
  494. return (-1);
  495. }
  496. /* allowed key state */
  497. static int
  498. monitor_allowed_key(const u_char *blob, u_int bloblen)
  499. {
  500. /* make sure key is allowed */
  501. if (key_blob == NULL || key_bloblen != bloblen ||
  502. timingsafe_bcmp(key_blob, blob, key_bloblen))
  503. return (0);
  504. return (1);
  505. }
  506. static void
  507. monitor_reset_key_state(void)
  508. {
  509. /* reset state */
  510. free(key_blob);
  511. free(hostbased_cuser);
  512. free(hostbased_chost);
  513. sshauthopt_free(key_opts);
  514. key_blob = NULL;
  515. key_bloblen = 0;
  516. key_blobtype = MM_NOKEY;
  517. key_opts = NULL;
  518. hostbased_cuser = NULL;
  519. hostbased_chost = NULL;
  520. }
  521. #ifdef WITH_OPENSSL
  522. int
  523. mm_answer_moduli(struct ssh *ssh, int sock, struct sshbuf *m)
  524. {
  525. DH *dh;
  526. const BIGNUM *dh_p, *dh_g;
  527. int r;
  528. u_int min, want, max;
  529. if ((r = sshbuf_get_u32(m, &min)) != 0 ||
  530. (r = sshbuf_get_u32(m, &want)) != 0 ||
  531. (r = sshbuf_get_u32(m, &max)) != 0)
  532. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  533. debug3("%s: got parameters: %d %d %d",
  534. __func__, min, want, max);
  535. /* We need to check here, too, in case the child got corrupted */
  536. if (max < min || want < min || max < want)
  537. fatal("%s: bad parameters: %d %d %d",
  538. __func__, min, want, max);
  539. sshbuf_reset(m);
  540. dh = choose_dh(min, want, max);
  541. if (dh == NULL) {
  542. if ((r = sshbuf_put_u8(m, 0)) != 0)
  543. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  544. return (0);
  545. } else {
  546. /* Send first bignum */
  547. DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
  548. if ((r = sshbuf_put_u8(m, 1)) != 0 ||
  549. (r = sshbuf_put_bignum2(m, dh_p)) != 0 ||
  550. (r = sshbuf_put_bignum2(m, dh_g)) != 0)
  551. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  552. DH_free(dh);
  553. }
  554. mm_request_send(sock, MONITOR_ANS_MODULI, m);
  555. return (0);
  556. }
  557. #endif
  558. int
  559. mm_answer_sign(struct ssh *ssh, int sock, struct sshbuf *m)
  560. {
  561. extern int auth_sock; /* XXX move to state struct? */
  562. struct sshkey *key;
  563. struct sshbuf *sigbuf = NULL;
  564. u_char *p = NULL, *signature = NULL;
  565. char *alg = NULL;
  566. size_t datlen, siglen, alglen;
  567. int r, is_proof = 0;
  568. u_int keyid, compat;
  569. const char proof_req[] = "hostkeys-prove-00@openssh.com";
  570. debug3("%s", __func__);
  571. if ((r = sshbuf_get_u32(m, &keyid)) != 0 ||
  572. (r = sshbuf_get_string(m, &p, &datlen)) != 0 ||
  573. (r = sshbuf_get_cstring(m, &alg, &alglen)) != 0 ||
  574. (r = sshbuf_get_u32(m, &compat)) != 0)
  575. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  576. if (keyid > INT_MAX)
  577. fatal("%s: invalid key ID", __func__);
  578. /*
  579. * Supported KEX types use SHA1 (20 bytes), SHA256 (32 bytes),
  580. * SHA384 (48 bytes) and SHA512 (64 bytes).
  581. *
  582. * Otherwise, verify the signature request is for a hostkey
  583. * proof.
  584. *
  585. * XXX perform similar check for KEX signature requests too?
  586. * it's not trivial, since what is signed is the hash, rather
  587. * than the full kex structure...
  588. */
  589. if (datlen != 20 && datlen != 32 && datlen != 48 && datlen != 64) {
  590. /*
  591. * Construct expected hostkey proof and compare it to what
  592. * the client sent us.
  593. */
  594. if (session_id2_len == 0) /* hostkeys is never first */
  595. fatal("%s: bad data length: %zu", __func__, datlen);
  596. if ((key = get_hostkey_public_by_index(keyid, ssh)) == NULL)
  597. fatal("%s: no hostkey for index %d", __func__, keyid);
  598. if ((sigbuf = sshbuf_new()) == NULL)
  599. fatal("%s: sshbuf_new", __func__);
  600. if ((r = sshbuf_put_cstring(sigbuf, proof_req)) != 0 ||
  601. (r = sshbuf_put_string(sigbuf, session_id2,
  602. session_id2_len)) != 0 ||
  603. (r = sshkey_puts(key, sigbuf)) != 0)
  604. fatal("%s: couldn't prepare private key "
  605. "proof buffer: %s", __func__, ssh_err(r));
  606. if (datlen != sshbuf_len(sigbuf) ||
  607. memcmp(p, sshbuf_ptr(sigbuf), sshbuf_len(sigbuf)) != 0)
  608. fatal("%s: bad data length: %zu, hostkey proof len %zu",
  609. __func__, datlen, sshbuf_len(sigbuf));
  610. sshbuf_free(sigbuf);
  611. is_proof = 1;
  612. }
  613. /* save session id, it will be passed on the first call */
  614. if (session_id2_len == 0) {
  615. session_id2_len = datlen;
  616. session_id2 = xmalloc(session_id2_len);
  617. memcpy(session_id2, p, session_id2_len);
  618. }
  619. if ((key = get_hostkey_by_index(keyid)) != NULL) {
  620. if ((r = sshkey_sign(key, &signature, &siglen, p, datlen, alg,
  621. options.sk_provider, NULL, compat)) != 0)
  622. fatal("%s: sshkey_sign failed: %s",
  623. __func__, ssh_err(r));
  624. } else if ((key = get_hostkey_public_by_index(keyid, ssh)) != NULL &&
  625. auth_sock > 0) {
  626. if ((r = ssh_agent_sign(auth_sock, key, &signature, &siglen,
  627. p, datlen, alg, compat)) != 0) {
  628. fatal("%s: ssh_agent_sign failed: %s",
  629. __func__, ssh_err(r));
  630. }
  631. } else
  632. fatal("%s: no hostkey from index %d", __func__, keyid);
  633. debug3("%s: %s signature %p(%zu)", __func__,
  634. is_proof ? "hostkey proof" : "KEX", signature, siglen);
  635. sshbuf_reset(m);
  636. if ((r = sshbuf_put_string(m, signature, siglen)) != 0)
  637. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  638. free(alg);
  639. free(p);
  640. free(signature);
  641. mm_request_send(sock, MONITOR_ANS_SIGN, m);
  642. /* Turn on permissions for getpwnam */
  643. monitor_permit(mon_dispatch, MONITOR_REQ_PWNAM, 1);
  644. return (0);
  645. }
  646. #define PUTPW(b, id) \
  647. do { \
  648. if ((r = sshbuf_put_string(b, \
  649. &pwent->id, sizeof(pwent->id))) != 0) \
  650. fatal(r, "assemble %s", #id); \
  651. } while (0)
  652. /* Retrieves the password entry and also checks if the user is permitted */
  653. int
  654. mm_answer_pwnamallow(struct ssh *ssh, int sock, struct sshbuf *m)
  655. {
  656. char *username;
  657. struct passwd *pwent;
  658. int r, allowed = 0;
  659. u_int i;
  660. debug3("%s", __func__);
  661. if (authctxt->attempt++ != 0)
  662. fatal("%s: multiple attempts for getpwnam", __func__);
  663. if ((r = sshbuf_get_cstring(m, &username, NULL)) != 0)
  664. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  665. pwent = getpwnamallow(ssh, username);
  666. authctxt->user = xstrdup(username);
  667. setproctitle("%s [priv]", pwent ? username : "unknown");
  668. free(username);
  669. sshbuf_reset(m);
  670. if (pwent == NULL) {
  671. if ((r = sshbuf_put_u8(m, 0)) != 0)
  672. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  673. authctxt->pw = fakepw();
  674. goto out;
  675. }
  676. allowed = 1;
  677. authctxt->pw = pwent;
  678. authctxt->valid = 1;
  679. /* XXX send fake class/dir/shell, etc. */
  680. if ((r = sshbuf_put_u8(m, 1)) != 0)
  681. fatal(r, "assemble ok");
  682. PUTPW(m, pw_uid);
  683. PUTPW(m, pw_gid);
  684. #ifdef HAVE_STRUCT_PASSWD_PW_CHANGE
  685. PUTPW(m, pw_change);
  686. #endif
  687. #ifdef HAVE_STRUCT_PASSWD_PW_EXPIRE
  688. PUTPW(m, pw_expire);
  689. #endif
  690. if ((r = sshbuf_put_cstring(m, pwent->pw_name)) != 0 ||
  691. (r = sshbuf_put_cstring(m, "*")) != 0 ||
  692. #ifdef HAVE_STRUCT_PASSWD_PW_GECOS
  693. (r = sshbuf_put_cstring(m, pwent->pw_gecos)) != 0 ||
  694. #endif
  695. #ifdef HAVE_STRUCT_PASSWD_PW_CLASS
  696. (r = sshbuf_put_cstring(m, pwent->pw_class)) != 0 ||
  697. #endif
  698. (r = sshbuf_put_cstring(m, pwent->pw_dir)) != 0 ||
  699. (r = sshbuf_put_cstring(m, pwent->pw_shell)) != 0)
  700. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  701. out:
  702. ssh_packet_set_log_preamble(ssh, "%suser %s",
  703. authctxt->valid ? "authenticating" : "invalid ", authctxt->user);
  704. if ((r = sshbuf_put_string(m, &options, sizeof(options))) != 0)
  705. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  706. #define M_CP_STROPT(x) do { \
  707. if (options.x != NULL) { \
  708. if ((r = sshbuf_put_cstring(m, options.x)) != 0) \
  709. fatal("%s: buffer error: %s", \
  710. __func__, ssh_err(r)); \
  711. } \
  712. } while (0)
  713. #define M_CP_STRARRAYOPT(x, nx) do { \
  714. for (i = 0; i < options.nx; i++) { \
  715. if ((r = sshbuf_put_cstring(m, options.x[i])) != 0) \
  716. fatal("%s: buffer error: %s", \
  717. __func__, ssh_err(r)); \
  718. } \
  719. } while (0)
  720. /* See comment in servconf.h */
  721. COPY_MATCH_STRING_OPTS();
  722. #undef M_CP_STROPT
  723. #undef M_CP_STRARRAYOPT
  724. /* Create valid auth method lists */
  725. if (auth2_setup_methods_lists(authctxt) != 0) {
  726. /*
  727. * The monitor will continue long enough to let the child
  728. * run to it's packet_disconnect(), but it must not allow any
  729. * authentication to succeed.
  730. */
  731. debug("%s: no valid authentication method lists", __func__);
  732. }
  733. debug3("%s: sending MONITOR_ANS_PWNAM: %d", __func__, allowed);
  734. mm_request_send(sock, MONITOR_ANS_PWNAM, m);
  735. /* Allow service/style information on the auth context */
  736. monitor_permit(mon_dispatch, MONITOR_REQ_AUTHSERV, 1);
  737. #ifdef WITH_SELINUX
  738. monitor_permit(mon_dispatch, MONITOR_REQ_AUTHROLE, 1);
  739. #endif
  740. monitor_permit(mon_dispatch, MONITOR_REQ_AUTH2_READ_BANNER, 1);
  741. #ifdef USE_PAM
  742. if (options.use_pam)
  743. monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
  744. #endif
  745. return (0);
  746. }
  747. int mm_answer_auth2_read_banner(struct ssh *ssh, int sock, struct sshbuf *m)
  748. {
  749. char *banner;
  750. int r;
  751. sshbuf_reset(m);
  752. banner = auth2_read_banner();
  753. if ((r = sshbuf_put_cstring(m, banner != NULL ? banner : "")) != 0)
  754. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  755. mm_request_send(sock, MONITOR_ANS_AUTH2_READ_BANNER, m);
  756. free(banner);
  757. return (0);
  758. }
  759. int
  760. mm_answer_authserv(struct ssh *ssh, int sock, struct sshbuf *m)
  761. {
  762. int r;
  763. monitor_permit_authentications(1);
  764. if ((r = sshbuf_get_cstring(m, &authctxt->service, NULL)) != 0 ||
  765. (r = sshbuf_get_cstring(m, &authctxt->style, NULL)) != 0)
  766. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  767. debug3("%s: service=%s, style=%s",
  768. __func__, authctxt->service, authctxt->style);
  769. if (strlen(authctxt->style) == 0) {
  770. free(authctxt->style);
  771. authctxt->style = NULL;
  772. }
  773. return (0);
  774. }
  775. /*
  776. * Check that the key type appears in the supplied pattern list, ignoring
  777. * mismatches in the signature algorithm. (Signature algorithm checks are
  778. * performed in the unprivileged authentication code).
  779. * Returns 1 on success, 0 otherwise.
  780. */
  781. static int
  782. key_base_type_match(const char *method, const struct sshkey *key,
  783. const char *list)
  784. {
  785. char *s, *l, *ol = xstrdup(list);
  786. int found = 0;
  787. l = ol;
  788. for ((s = strsep(&l, ",")); s && *s != '\0'; (s = strsep(&l, ","))) {
  789. if (sshkey_type_from_name(s) == key->type) {
  790. found = 1;
  791. break;
  792. }
  793. }
  794. if (!found) {
  795. error("%s key type %s is not in permitted list %s", method,
  796. sshkey_ssh_name(key), list);
  797. }
  798. free(ol);
  799. return found;
  800. }
  801. #ifdef WITH_SELINUX
  802. int
  803. mm_answer_authrole(struct ssh *ssh, int sock, struct sshbuf *m)
  804. {
  805. int r;
  806. monitor_permit_authentications(1);
  807. if ((r = sshbuf_get_cstring(m, &authctxt->role, NULL)) != 0)
  808. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  809. debug3("%s: role=%s", __func__, authctxt->role);
  810. if (strlen(authctxt->role) == 0) {
  811. free(authctxt->role);
  812. authctxt->role = NULL;
  813. }
  814. return (0);
  815. }
  816. #endif
  817. int
  818. mm_answer_authpassword(struct ssh *ssh, int sock, struct sshbuf *m)
  819. {
  820. static int call_count;
  821. char *passwd;
  822. int r, authenticated;
  823. size_t plen;
  824. if (!options.password_authentication)
  825. fatal("%s: password authentication not enabled", __func__);
  826. if ((r = sshbuf_get_cstring(m, &passwd, &plen)) != 0)
  827. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  828. /* Only authenticate if the context is valid */
  829. authenticated = options.password_authentication &&
  830. auth_password(ssh, passwd);
  831. freezero(passwd, plen);
  832. sshbuf_reset(m);
  833. if ((r = sshbuf_put_u32(m, authenticated)) != 0)
  834. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  835. #ifdef USE_PAM
  836. if ((r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0)
  837. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  838. #endif
  839. debug3("%s: sending result %d", __func__, authenticated);
  840. mm_request_send(sock, MONITOR_ANS_AUTHPASSWORD, m);
  841. call_count++;
  842. if (plen == 0 && call_count == 1)
  843. auth_method = "none";
  844. else
  845. auth_method = "password";
  846. /* Causes monitor loop to terminate if authenticated */
  847. return (authenticated);
  848. }
  849. #ifdef BSD_AUTH
  850. int
  851. mm_answer_bsdauthquery(struct ssh *ssh, int sock, struct sshbuf *m)
  852. {
  853. char *name, *infotxt;
  854. u_int numprompts, *echo_on, success;
  855. char **prompts;
  856. int r;
  857. if (!options.kbd_interactive_authentication)
  858. fatal("%s: kbd-int authentication not enabled", __func__);
  859. success = bsdauth_query(authctxt, &name, &infotxt, &numprompts,
  860. &prompts, &echo_on) < 0 ? 0 : 1;
  861. sshbuf_reset(m);
  862. if ((r = sshbuf_put_u32(m, success)) != 0)
  863. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  864. if (success) {
  865. if ((r = sshbuf_put_cstring(m, prompts[0])) != 0)
  866. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  867. }
  868. debug3("%s: sending challenge success: %u", __func__, success);
  869. mm_request_send(sock, MONITOR_ANS_BSDAUTHQUERY, m);
  870. if (success) {
  871. free(name);
  872. free(infotxt);
  873. free(prompts);
  874. free(echo_on);
  875. }
  876. return (0);
  877. }
  878. int
  879. mm_answer_bsdauthrespond(struct ssh *ssh, int sock, struct sshbuf *m)
  880. {
  881. char *response;
  882. int r, authok;
  883. if (!options.kbd_interactive_authentication)
  884. fatal("%s: kbd-int authentication not enabled", __func__);
  885. if (authctxt->as == NULL)
  886. fatal("%s: no bsd auth session", __func__);
  887. if ((r = sshbuf_get_cstring(m, &response, NULL)) != 0)
  888. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  889. authok = options.challenge_response_authentication &&
  890. auth_userresponse(authctxt->as, response, 0);
  891. authctxt->as = NULL;
  892. debug3("%s: <%s> = <%d>", __func__, response, authok);
  893. free(response);
  894. sshbuf_reset(m);
  895. if ((r = sshbuf_put_u32(m, authok)) != 0)
  896. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  897. debug3("%s: sending authenticated: %d", __func__, authok);
  898. mm_request_send(sock, MONITOR_ANS_BSDAUTHRESPOND, m);
  899. auth_method = "keyboard-interactive";
  900. auth_submethod = "bsdauth";
  901. return (authok != 0);
  902. }
  903. #endif
  904. #ifdef USE_PAM
  905. int
  906. mm_answer_pam_start(struct ssh *ssh, int sock, struct sshbuf *m)
  907. {
  908. if (!options.use_pam)
  909. fatal("UsePAM not set, but ended up in %s anyway", __func__);
  910. start_pam(ssh);
  911. monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
  912. if (options.kbd_interactive_authentication)
  913. monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
  914. return (0);
  915. }
  916. int
  917. mm_answer_pam_account(struct ssh *ssh, int sock, struct sshbuf *m)
  918. {
  919. u_int ret;
  920. int r;
  921. if (!options.use_pam)
  922. fatal("%s: PAM not enabled", __func__);
  923. ret = do_pam_account();
  924. if ((r = sshbuf_put_u32(m, ret)) != 0 ||
  925. (r = sshbuf_put_stringb(m, loginmsg)) != 0)
  926. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  927. mm_request_send(sock, MONITOR_ANS_PAM_ACCOUNT, m);
  928. return (ret);
  929. }
  930. static void *sshpam_ctxt, *sshpam_authok;
  931. extern KbdintDevice sshpam_device;
  932. int
  933. mm_answer_pam_init_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
  934. {
  935. u_int ok = 0;
  936. int r;
  937. debug3("%s", __func__);
  938. if (!options.kbd_interactive_authentication)
  939. fatal("%s: kbd-int authentication not enabled", __func__);
  940. if (sshpam_ctxt != NULL)
  941. fatal("%s: already called", __func__);
  942. sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
  943. sshpam_authok = NULL;
  944. sshbuf_reset(m);
  945. if (sshpam_ctxt != NULL) {
  946. monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
  947. monitor_permit(mon_dispatch, MONITOR_REQ_PAM_QUERY, 1);
  948. ok = 1;
  949. }
  950. if ((r = sshbuf_put_u32(m, ok)) != 0)
  951. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  952. mm_request_send(sock, MONITOR_ANS_PAM_INIT_CTX, m);
  953. return (0);
  954. }
  955. int
  956. mm_answer_pam_query(struct ssh *ssh, int sock, struct sshbuf *m)
  957. {
  958. char *name = NULL, *info = NULL, **prompts = NULL;
  959. u_int i, num = 0, *echo_on = 0;
  960. int r, ret;
  961. debug3("%s", __func__);
  962. sshpam_authok = NULL;
  963. if (sshpam_ctxt == NULL)
  964. fatal("%s: no context", __func__);
  965. ret = (sshpam_device.query)(sshpam_ctxt, &name, &info,
  966. &num, &prompts, &echo_on);
  967. if (ret == 0 && num == 0)
  968. sshpam_authok = sshpam_ctxt;
  969. if (num > 1 || name == NULL || info == NULL)
  970. fatal("sshpam_device.query failed");
  971. monitor_permit(mon_dispatch, MONITOR_REQ_PAM_RESPOND, 1);
  972. sshbuf_reset(m);
  973. if ((r = sshbuf_put_u32(m, ret)) != 0 ||
  974. (r = sshbuf_put_cstring(m, name)) != 0 ||
  975. (r = sshbuf_put_cstring(m, info)) != 0 ||
  976. (r = sshbuf_put_u32(m, sshpam_get_maxtries_reached())) != 0 ||
  977. (r = sshbuf_put_u32(m, num)) != 0)
  978. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  979. free(name);
  980. free(info);
  981. for (i = 0; i < num; ++i) {
  982. if ((r = sshbuf_put_cstring(m, prompts[i])) != 0 ||
  983. (r = sshbuf_put_u32(m, echo_on[i])) != 0)
  984. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  985. free(prompts[i]);
  986. }
  987. free(prompts);
  988. free(echo_on);
  989. auth_method = "keyboard-interactive";
  990. auth_submethod = "pam";
  991. mm_request_send(sock, MONITOR_ANS_PAM_QUERY, m);
  992. return (0);
  993. }
  994. int
  995. mm_answer_pam_respond(struct ssh *ssh, int sock, struct sshbuf *m)
  996. {
  997. char **resp;
  998. u_int i, num;
  999. int r, ret;
  1000. debug3("%s", __func__);
  1001. if (sshpam_ctxt == NULL)
  1002. fatal("%s: no context", __func__);
  1003. sshpam_authok = NULL;
  1004. if ((r = sshbuf_get_u32(m, &num)) != 0)
  1005. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1006. if (num > 0) {
  1007. resp = xcalloc(num, sizeof(char *));
  1008. for (i = 0; i < num; ++i) {
  1009. if ((r = sshbuf_get_cstring(m, &(resp[i]), NULL)) != 0)
  1010. fatal("%s: buffer error: %s",
  1011. __func__, ssh_err(r));
  1012. }
  1013. ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
  1014. for (i = 0; i < num; ++i)
  1015. free(resp[i]);
  1016. free(resp);
  1017. } else {
  1018. ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
  1019. }
  1020. sshbuf_reset(m);
  1021. if ((r = sshbuf_put_u32(m, ret)) != 0)
  1022. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1023. mm_request_send(sock, MONITOR_ANS_PAM_RESPOND, m);
  1024. auth_method = "keyboard-interactive";
  1025. auth_submethod = "pam";
  1026. if (ret == 0)
  1027. sshpam_authok = sshpam_ctxt;
  1028. return (0);
  1029. }
  1030. int
  1031. mm_answer_pam_free_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
  1032. {
  1033. int r = sshpam_authok != NULL && sshpam_authok == sshpam_ctxt;
  1034. debug3("%s", __func__);
  1035. if (sshpam_ctxt == NULL)
  1036. fatal("%s: no context", __func__);
  1037. (sshpam_device.free_ctx)(sshpam_ctxt);
  1038. sshpam_ctxt = sshpam_authok = NULL;
  1039. sshbuf_reset(m);
  1040. mm_request_send(sock, MONITOR_ANS_PAM_FREE_CTX, m);
  1041. /* Allow another attempt */
  1042. monitor_permit(mon_dispatch, MONITOR_REQ_PAM_INIT_CTX, 1);
  1043. auth_method = "keyboard-interactive";
  1044. auth_submethod = "pam";
  1045. return r;
  1046. }
  1047. #endif
  1048. int
  1049. mm_answer_keyallowed(struct ssh *ssh, int sock, struct sshbuf *m)
  1050. {
  1051. struct sshkey *key = NULL;
  1052. char *cuser, *chost;
  1053. u_int pubkey_auth_attempt;
  1054. enum mm_keytype type = 0;
  1055. int r, allowed = 0;
  1056. struct sshauthopt *opts = NULL;
  1057. debug3("%s entering", __func__);
  1058. if ((r = sshbuf_get_u32(m, &type)) != 0 ||
  1059. (r = sshbuf_get_cstring(m, &cuser, NULL)) != 0 ||
  1060. (r = sshbuf_get_cstring(m, &chost, NULL)) != 0 ||
  1061. (r = sshkey_froms(m, &key)) != 0 ||
  1062. (r = sshbuf_get_u32(m, &pubkey_auth_attempt)) != 0)
  1063. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1064. debug3("%s: key_from_blob: %p", __func__, key);
  1065. if (key != NULL && authctxt->valid) {
  1066. /* These should not make it past the privsep child */
  1067. if (sshkey_type_plain(key->type) == KEY_RSA &&
  1068. (ssh->compat & SSH_BUG_RSASIGMD5) != 0)
  1069. fatal("%s: passed a SSH_BUG_RSASIGMD5 key", __func__);
  1070. switch (type) {
  1071. case MM_USERKEY:
  1072. auth_method = "publickey";
  1073. if (!options.pubkey_authentication)
  1074. break;
  1075. if (auth2_key_already_used(authctxt, key))
  1076. break;
  1077. if (!key_base_type_match(auth_method, key,
  1078. options.pubkey_accepted_algos))
  1079. break;
  1080. allowed = user_key_allowed(ssh, authctxt->pw, key,
  1081. pubkey_auth_attempt, &opts);
  1082. break;
  1083. case MM_HOSTKEY:
  1084. auth_method = "hostbased";
  1085. if (!options.hostbased_authentication)
  1086. break;
  1087. if (auth2_key_already_used(authctxt, key))
  1088. break;
  1089. if (!key_base_type_match(auth_method, key,
  1090. options.hostbased_accepted_algos))
  1091. break;
  1092. allowed = hostbased_key_allowed(ssh, authctxt->pw,
  1093. cuser, chost, key);
  1094. auth2_record_info(authctxt,
  1095. "client user \"%.100s\", client host \"%.100s\"",
  1096. cuser, chost);
  1097. break;
  1098. default:
  1099. fatal("%s: unknown key type %d", __func__, type);
  1100. break;
  1101. }
  1102. }
  1103. debug3("%s: %s authentication%s: %s key is %s", __func__,
  1104. auth_method, pubkey_auth_attempt ? "" : " test",
  1105. (key == NULL || !authctxt->valid) ? "invalid" : sshkey_type(key),
  1106. allowed ? "allowed" : "not allowed");
  1107. auth2_record_key(authctxt, 0, key);
  1108. /* clear temporarily storage (used by verify) */
  1109. monitor_reset_key_state();
  1110. if (allowed) {
  1111. /* Save temporarily for comparison in verify */
  1112. if ((r = sshkey_to_blob(key, &key_blob, &key_bloblen)) != 0)
  1113. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1114. key_blobtype = type;
  1115. key_opts = opts;
  1116. hostbased_cuser = cuser;
  1117. hostbased_chost = chost;
  1118. } else {
  1119. /* Log failed attempt */
  1120. auth_log(ssh, 0, 0, auth_method, NULL);
  1121. free(cuser);
  1122. free(chost);
  1123. }
  1124. sshkey_free(key);
  1125. sshbuf_reset(m);
  1126. if ((r = sshbuf_put_u32(m, allowed)) != 0)
  1127. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1128. if (opts != NULL && (r = sshauthopt_serialise(opts, m, 1)) != 0)
  1129. fatal("%s: sshauthopt_serialise: %s", __func__, ssh_err(r));
  1130. mm_request_send(sock, MONITOR_ANS_KEYALLOWED, m);
  1131. if (!allowed)
  1132. sshauthopt_free(opts);
  1133. return (0);
  1134. }
  1135. static int
  1136. monitor_valid_userblob(struct ssh *ssh, const u_char *data, u_int datalen)
  1137. {
  1138. struct sshbuf *b;
  1139. const u_char *p;
  1140. char *userstyle, *s, *cp;
  1141. size_t len;
  1142. u_char type;
  1143. int r, fail = 0;
  1144. if ((b = sshbuf_from(data, datalen)) == NULL)
  1145. fatal("%s: sshbuf_from", __func__);
  1146. if (ssh->compat & SSH_OLD_SESSIONID) {
  1147. p = sshbuf_ptr(b);
  1148. len = sshbuf_len(b);
  1149. if ((session_id2 == NULL) ||
  1150. (len < session_id2_len) ||
  1151. (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
  1152. fail++;
  1153. if ((r = sshbuf_consume(b, session_id2_len)) != 0)
  1154. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1155. } else {
  1156. if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
  1157. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1158. if ((session_id2 == NULL) ||
  1159. (len != session_id2_len) ||
  1160. (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
  1161. fail++;
  1162. }
  1163. if ((r = sshbuf_get_u8(b, &type)) != 0)
  1164. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1165. if (type != SSH2_MSG_USERAUTH_REQUEST)
  1166. fail++;
  1167. if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
  1168. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1169. if ((s = strchr(cp, '/')) != NULL)
  1170. *s = '\0';
  1171. xasprintf(&userstyle, "%s%s%s", authctxt->user,
  1172. authctxt->style ? ":" : "",
  1173. authctxt->style ? authctxt->style : "");
  1174. if (strcmp(userstyle, cp) != 0) {
  1175. logit("wrong user name passed to monitor: "
  1176. "expected %s != %.100s", userstyle, cp);
  1177. fail++;
  1178. }
  1179. free(userstyle);
  1180. free(cp);
  1181. if ((r = sshbuf_skip_string(b)) != 0 || /* service */
  1182. (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
  1183. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1184. if (strcmp("publickey", cp) != 0)
  1185. fail++;
  1186. free(cp);
  1187. if ((r = sshbuf_get_u8(b, &type)) != 0)
  1188. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1189. if (type == 0)
  1190. fail++;
  1191. if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
  1192. (r = sshbuf_skip_string(b)) != 0) /* pkblob */
  1193. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1194. if (sshbuf_len(b) != 0)
  1195. fail++;
  1196. sshbuf_free(b);
  1197. return (fail == 0);
  1198. }
  1199. static int
  1200. monitor_valid_hostbasedblob(const u_char *data, u_int datalen,
  1201. const char *cuser, const char *chost)
  1202. {
  1203. struct sshbuf *b;
  1204. const u_char *p;
  1205. char *cp, *s, *userstyle;
  1206. size_t len;
  1207. int r, fail = 0;
  1208. u_char type;
  1209. if ((b = sshbuf_from(data, datalen)) == NULL)
  1210. fatal("%s: sshbuf_new", __func__);
  1211. if ((r = sshbuf_get_string_direct(b, &p, &len)) != 0)
  1212. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1213. if ((session_id2 == NULL) ||
  1214. (len != session_id2_len) ||
  1215. (timingsafe_bcmp(p, session_id2, session_id2_len) != 0))
  1216. fail++;
  1217. if ((r = sshbuf_get_u8(b, &type)) != 0)
  1218. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1219. if (type != SSH2_MSG_USERAUTH_REQUEST)
  1220. fail++;
  1221. if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
  1222. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1223. if ((s = strchr(p, '/')) != NULL)
  1224. *s = '\0';
  1225. xasprintf(&userstyle, "%s%s%s", authctxt->user,
  1226. authctxt->style ? ":" : "",
  1227. authctxt->style ? authctxt->style : "");
  1228. if (strcmp(userstyle, cp) != 0) {
  1229. logit("wrong user name passed to monitor: "
  1230. "expected %s != %.100s", userstyle, cp);
  1231. fail++;
  1232. }
  1233. free(userstyle);
  1234. free(cp);
  1235. if ((r = sshbuf_skip_string(b)) != 0 || /* service */
  1236. (r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
  1237. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1238. if (strcmp(cp, "hostbased") != 0)
  1239. fail++;
  1240. free(cp);
  1241. if ((r = sshbuf_skip_string(b)) != 0 || /* pkalg */
  1242. (r = sshbuf_skip_string(b)) != 0) /* pkblob */
  1243. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1244. /* verify client host, strip trailing dot if necessary */
  1245. if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
  1246. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1247. if (((len = strlen(cp)) > 0) && cp[len - 1] == '.')
  1248. cp[len - 1] = '\0';
  1249. if (strcmp(cp, chost) != 0)
  1250. fail++;
  1251. free(cp);
  1252. /* verify client user */
  1253. if ((r = sshbuf_get_cstring(b, &cp, NULL)) != 0)
  1254. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1255. if (strcmp(cp, cuser) != 0)
  1256. fail++;
  1257. free(cp);
  1258. if (sshbuf_len(b) != 0)
  1259. fail++;
  1260. sshbuf_free(b);
  1261. return (fail == 0);
  1262. }
  1263. int
  1264. mm_answer_keyverify(struct ssh *ssh, int sock, struct sshbuf *m)
  1265. {
  1266. struct sshkey *key;
  1267. const u_char *signature, *data, *blob;
  1268. char *sigalg = NULL, *fp = NULL;
  1269. size_t signaturelen, datalen, bloblen;
  1270. int r, ret, req_presence = 0, req_verify = 0, valid_data = 0;
  1271. int encoded_ret;
  1272. struct sshkey_sig_details *sig_details = NULL;
  1273. int type = 0;
  1274. if ((r = sshbuf_get_u32(m, &type)) != 0 ||
  1275. (r = sshbuf_get_string_direct(m, &blob, &bloblen)) != 0 ||
  1276. (r = sshbuf_get_string_direct(m, &signature, &signaturelen)) != 0 ||
  1277. (r = sshbuf_get_string_direct(m, &data, &datalen)) != 0 ||
  1278. (r = sshbuf_get_cstring(m, &sigalg, NULL)) != 0)
  1279. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1280. if (hostbased_cuser == NULL || hostbased_chost == NULL ||
  1281. !monitor_allowed_key(blob, bloblen))
  1282. fatal("%s: bad key, not previously allowed", __func__);
  1283. if (type != key_blobtype)
  1284. fatal("%s: bad key type", __func__);
  1285. /* Empty signature algorithm means NULL. */
  1286. if (*sigalg == '\0') {
  1287. free(sigalg);
  1288. sigalg = NULL;
  1289. }
  1290. /* XXX use sshkey_froms here; need to change key_blob, etc. */
  1291. if ((r = sshkey_from_blob(blob, bloblen, &key)) != 0)
  1292. fatal("%s: bad public key blob: %s", __func__, ssh_err(r));
  1293. switch (key_blobtype) {
  1294. case MM_USERKEY:
  1295. valid_data = monitor_valid_userblob(ssh, data, datalen);
  1296. auth_method = "publickey";
  1297. ret = user_key_verify(ssh, key, signature, signaturelen, data,
  1298. datalen, sigalg, ssh->compat, &sig_details);
  1299. break;
  1300. case MM_HOSTKEY:
  1301. valid_data = monitor_valid_hostbasedblob(data, datalen,
  1302. hostbased_cuser, hostbased_chost);
  1303. auth_method = "hostbased";
  1304. ret = hostbased_key_verify(ssh, key, signature, signaturelen, data,
  1305. datalen, sigalg, ssh->compat, &sig_details);
  1306. break;
  1307. default:
  1308. valid_data = 0;
  1309. ret = 0;
  1310. break;
  1311. }
  1312. if (!valid_data)
  1313. fatal("bad %s signature data blob",
  1314. key_blobtype == MM_USERKEY ? "userkey" :
  1315. (key_blobtype == MM_HOSTKEY ? "hostkey" : "unknown"));
  1316. if ((fp = sshkey_fingerprint(key, options.fingerprint_hash,
  1317. SSH_FP_DEFAULT)) == NULL)
  1318. fatal("%s: sshkey_fingerprint failed", __func__);
  1319. debug3("%s: %s %p signature %s%s%s", __func__, auth_method, key,
  1320. (ret == 0) ? "verified" : "unverified",
  1321. (ret != 0) ? ": " : "", (ret != 0) ? ssh_err(ret) : "");
  1322. if (ret == 0 && key_blobtype == MM_USERKEY && sig_details != NULL) {
  1323. req_presence = (options.pubkey_auth_options &
  1324. PUBKEYAUTH_TOUCH_REQUIRED) ||
  1325. !key_opts->no_require_user_presence;
  1326. if (req_presence &&
  1327. (sig_details->sk_flags & SSH_SK_USER_PRESENCE_REQD) == 0) {
  1328. error("public key %s %s signature for %s%s from %.128s "
  1329. "port %d rejected: user presence "
  1330. "(authenticator touch) requirement not met ",
  1331. sshkey_type(key), fp,
  1332. authctxt->valid ? "" : "invalid user ",
  1333. authctxt->user, ssh_remote_ipaddr(ssh),
  1334. ssh_remote_port(ssh));
  1335. ret = SSH_ERR_SIGNATURE_INVALID;
  1336. }
  1337. req_verify = (options.pubkey_auth_options &
  1338. PUBKEYAUTH_VERIFY_REQUIRED) || key_opts->require_verify;
  1339. if (req_verify &&
  1340. (sig_details->sk_flags & SSH_SK_USER_VERIFICATION_REQD) == 0) {
  1341. error("public key %s %s signature for %s%s from %.128s "
  1342. "port %d rejected: user verification requirement "
  1343. "not met ", sshkey_type(key), fp,
  1344. authctxt->valid ? "" : "invalid user ",
  1345. authctxt->user, ssh_remote_ipaddr(ssh),
  1346. ssh_remote_port(ssh));
  1347. ret = SSH_ERR_SIGNATURE_INVALID;
  1348. }
  1349. }
  1350. auth2_record_key(authctxt, ret == 0, key);
  1351. if (key_blobtype == MM_USERKEY)
  1352. auth_activate_options(ssh, key_opts);
  1353. monitor_reset_key_state();
  1354. sshbuf_reset(m);
  1355. /* encode ret != 0 as positive integer, since we're sending u32 */
  1356. encoded_ret = (ret != 0);
  1357. if ((r = sshbuf_put_u32(m, encoded_ret)) != 0 ||
  1358. (r = sshbuf_put_u8(m, sig_details != NULL)) != 0)
  1359. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1360. if (sig_details != NULL) {
  1361. if ((r = sshbuf_put_u32(m, sig_details->sk_counter)) != 0 ||
  1362. (r = sshbuf_put_u8(m, sig_details->sk_flags)) != 0)
  1363. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1364. }
  1365. sshkey_sig_details_free(sig_details);
  1366. mm_request_send(sock, MONITOR_ANS_KEYVERIFY, m);
  1367. free(sigalg);
  1368. free(fp);
  1369. sshkey_free(key);
  1370. return ret == 0;
  1371. }
  1372. static void
  1373. mm_record_login(struct ssh *ssh, Session *s, struct passwd *pw)
  1374. {
  1375. socklen_t fromlen;
  1376. struct sockaddr_storage from;
  1377. /*
  1378. * Get IP address of client. If the connection is not a socket, let
  1379. * the address be 0.0.0.0.
  1380. */
  1381. memset(&from, 0, sizeof(from));
  1382. fromlen = sizeof(from);
  1383. if (ssh_packet_connection_is_on_socket(ssh)) {
  1384. if (getpeername(ssh_packet_get_connection_in(ssh),
  1385. (struct sockaddr *)&from, &fromlen) == -1) {
  1386. debug("getpeername: %.100s", strerror(errno));
  1387. cleanup_exit(255);
  1388. }
  1389. }
  1390. /* Record that there was a login on that tty from the remote host. */
  1391. record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
  1392. session_get_remote_name_or_ip(ssh, utmp_len, options.use_dns),
  1393. (struct sockaddr *)&from, fromlen);
  1394. }
  1395. static void
  1396. mm_session_close(struct ssh *ssh, Session *s)
  1397. {
  1398. debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid);
  1399. if (s->ttyfd != -1) {
  1400. debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
  1401. session_pty_cleanup2(s);
  1402. }
  1403. #ifdef SSH_AUDIT_EVENTS
  1404. if (s->command != NULL) {
  1405. debug3("%s: command %d", __func__, s->command_handle);
  1406. session_end_command2(ssh, s);
  1407. }
  1408. #endif
  1409. session_unused(s->self);
  1410. }
  1411. int
  1412. mm_answer_pty(struct ssh *ssh, int sock, struct sshbuf *m)
  1413. {
  1414. extern struct monitor *pmonitor;
  1415. Session *s;
  1416. int r, res, fd0;
  1417. debug3("%s entering", __func__);
  1418. sshbuf_reset(m);
  1419. s = session_new();
  1420. if (s == NULL)
  1421. goto error;
  1422. s->authctxt = authctxt;
  1423. s->pw = authctxt->pw;
  1424. s->pid = pmonitor->m_pid;
  1425. res = pty_allocate(&s->ptyfd, &s->ttyfd, s->tty, sizeof(s->tty));
  1426. if (res == 0)
  1427. goto error;
  1428. pty_setowner(authctxt->pw, s->tty);
  1429. if ((r = sshbuf_put_u32(m, 1)) != 0 ||
  1430. (r = sshbuf_put_cstring(m, s->tty)) != 0)
  1431. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1432. /* We need to trick ttyslot */
  1433. if (dup2(s->ttyfd, 0) == -1)
  1434. fatal("%s: dup2", __func__);
  1435. mm_record_login(ssh, s, authctxt->pw);
  1436. /* Now we can close the file descriptor again */
  1437. close(0);
  1438. /* send messages generated by record_login */
  1439. if ((r = sshbuf_put_stringb(m, loginmsg)) != 0)
  1440. fatal("%s: put login message: %s", __func__, ssh_err(r));
  1441. sshbuf_reset(loginmsg);
  1442. mm_request_send(sock, MONITOR_ANS_PTY, m);
  1443. if (mm_send_fd(sock, s->ptyfd) == -1 ||
  1444. mm_send_fd(sock, s->ttyfd) == -1)
  1445. fatal("%s: send fds failed", __func__);
  1446. /* make sure nothing uses fd 0 */
  1447. if ((fd0 = open(_PATH_DEVNULL, O_RDONLY)) == -1)
  1448. fatal("%s: open(/dev/null): %s", __func__, strerror(errno));
  1449. if (fd0 != 0)
  1450. error("%s: fd0 %d != 0", __func__, fd0);
  1451. /* slave side of pty is not needed */
  1452. close(s->ttyfd);
  1453. s->ttyfd = s->ptyfd;
  1454. /* no need to dup() because nobody closes ptyfd */
  1455. s->ptymaster = s->ptyfd;
  1456. debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ttyfd);
  1457. return (0);
  1458. error:
  1459. if (s != NULL)
  1460. mm_session_close(ssh, s);
  1461. if ((r = sshbuf_put_u32(m, 0)) != 0)
  1462. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1463. mm_request_send(sock, MONITOR_ANS_PTY, m);
  1464. return (0);
  1465. }
  1466. int
  1467. mm_answer_pty_cleanup(struct ssh *ssh, int sock, struct sshbuf *m)
  1468. {
  1469. Session *s;
  1470. char *tty;
  1471. int r;
  1472. debug3("%s entering", __func__);
  1473. if ((r = sshbuf_get_cstring(m, &tty, NULL)) != 0)
  1474. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1475. if ((s = session_by_tty(tty)) != NULL)
  1476. mm_session_close(ssh, s);
  1477. sshbuf_reset(m);
  1478. free(tty);
  1479. return (0);
  1480. }
  1481. int
  1482. mm_answer_term(struct ssh *ssh, int sock, struct sshbuf *req)
  1483. {
  1484. extern struct monitor *pmonitor;
  1485. int res, status;
  1486. debug3("%s: tearing down sessions", __func__);
  1487. /* The child is terminating */
  1488. session_destroy_all(ssh, &mm_session_close);
  1489. #ifdef USE_PAM
  1490. if (options.use_pam)
  1491. sshpam_cleanup();
  1492. #endif
  1493. destroy_sensitive_data(ssh, 0);
  1494. while (waitpid(pmonitor->m_pid, &status, 0) == -1)
  1495. if (errno != EINTR)
  1496. exit(1);
  1497. res = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
  1498. /* Terminate process */
  1499. exit(res);
  1500. }
  1501. #ifdef SSH_AUDIT_EVENTS
  1502. /* Report that an audit event occurred */
  1503. int
  1504. mm_answer_audit_event(struct ssh *ssh, int socket, struct sshbuf *m)
  1505. {
  1506. u_int n;
  1507. ssh_audit_event_t event;
  1508. int r;
  1509. debug3("%s entering", __func__);
  1510. if ((r = sshbuf_get_u32(m, &n)) != 0)
  1511. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1512. event = (ssh_audit_event_t)n;
  1513. switch (event) {
  1514. case SSH_AUTH_FAIL_PUBKEY:
  1515. case SSH_AUTH_FAIL_HOSTBASED:
  1516. case SSH_AUTH_FAIL_GSSAPI:
  1517. case SSH_LOGIN_EXCEED_MAXTRIES:
  1518. case SSH_LOGIN_ROOT_DENIED:
  1519. case SSH_CONNECTION_CLOSE:
  1520. case SSH_INVALID_USER:
  1521. audit_event(ssh, event);
  1522. break;
  1523. default:
  1524. fatal("Audit event type %d not permitted", event);
  1525. }
  1526. return (0);
  1527. }
  1528. int
  1529. mm_answer_audit_command(struct ssh *ssh, int socket, struct sshbuf *m)
  1530. {
  1531. char *cmd;
  1532. int r;
  1533. Session *s;
  1534. debug3("%s entering", __func__);
  1535. if ((r = sshbuf_get_cstring(m, &cmd, NULL)) != 0)
  1536. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1537. /* sanity check command, if so how? */
  1538. s = session_new();
  1539. if (s == NULL)
  1540. fatal("%s: error allocating a session", __func__);
  1541. s->command = cmd;
  1542. #ifdef SSH_AUDIT_EVENTS
  1543. s->command_handle = audit_run_command(ssh, cmd);
  1544. #endif
  1545. sshbuf_reset(m);
  1546. sshbuf_put_u32(m, s->self);
  1547. mm_request_send(socket, MONITOR_ANS_AUDIT_COMMAND, m);
  1548. return (0);
  1549. }
  1550. int
  1551. mm_answer_audit_end_command(struct ssh *ssh, int socket, struct sshbuf *m)
  1552. {
  1553. int handle, r;
  1554. size_t len;
  1555. u_char *cmd = NULL;
  1556. Session *s;
  1557. debug3("%s entering", __func__);
  1558. if ((r = sshbuf_get_u32(m, &handle)) != 0 ||
  1559. (r = sshbuf_get_string(m, &cmd, &len)) != 0)
  1560. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1561. s = session_by_id(handle);
  1562. if (s == NULL || s->ttyfd != -1 || s->command == NULL ||
  1563. strcmp(s->command, cmd) != 0)
  1564. fatal("%s: invalid handle", __func__);
  1565. mm_session_close(ssh, s);
  1566. free(cmd);
  1567. return (0);
  1568. }
  1569. #endif /* SSH_AUDIT_EVENTS */
  1570. void
  1571. monitor_clear_keystate(struct ssh *ssh, struct monitor *pmonitor)
  1572. {
  1573. ssh_clear_newkeys(ssh, MODE_IN);
  1574. ssh_clear_newkeys(ssh, MODE_OUT);
  1575. sshbuf_free(child_state);
  1576. child_state = NULL;
  1577. }
  1578. void
  1579. monitor_apply_keystate(struct ssh *ssh, struct monitor *pmonitor)
  1580. {
  1581. struct kex *kex;
  1582. int r;
  1583. debug3("%s: packet_set_state", __func__);
  1584. if ((r = ssh_packet_set_state(ssh, child_state)) != 0)
  1585. fatal("%s: packet_set_state: %s", __func__, ssh_err(r));
  1586. sshbuf_free(child_state);
  1587. child_state = NULL;
  1588. if ((kex = ssh->kex) == NULL)
  1589. fatal("internal error: ssh->kex == NULL");
  1590. if (session_id2_len != sshbuf_len(ssh->kex->session_id)) {
  1591. fatal("incorrect session id length %zu (expected %u)",
  1592. sshbuf_len(ssh->kex->session_id), session_id2_len);
  1593. }
  1594. if (memcmp(sshbuf_ptr(ssh->kex->session_id), session_id2,
  1595. session_id2_len) != 0)
  1596. fatal("session ID mismatch");
  1597. /* XXX set callbacks */
  1598. #ifdef WITH_OPENSSL
  1599. kex->kex[KEX_DH_GRP1_SHA1] = kex_gen_server;
  1600. kex->kex[KEX_DH_GRP14_SHA1] = kex_gen_server;
  1601. kex->kex[KEX_DH_GRP14_SHA256] = kex_gen_server;
  1602. kex->kex[KEX_DH_GRP16_SHA512] = kex_gen_server;
  1603. kex->kex[KEX_DH_GRP18_SHA512] = kex_gen_server;
  1604. kex->kex[KEX_DH_GEX_SHA1] = kexgex_server;
  1605. kex->kex[KEX_DH_GEX_SHA256] = kexgex_server;
  1606. # ifdef OPENSSL_HAS_ECC
  1607. kex->kex[KEX_ECDH_SHA2] = kex_gen_server;
  1608. # endif
  1609. #endif /* WITH_OPENSSL */
  1610. kex->kex[KEX_C25519_SHA256] = kex_gen_server;
  1611. kex->kex[KEX_KEM_SNTRUP761X25519_SHA512] = kex_gen_server;
  1612. kex->load_host_public_key=&get_hostkey_public_by_type;
  1613. kex->load_host_private_key=&get_hostkey_private_by_type;
  1614. kex->host_key_index=&get_hostkey_index;
  1615. kex->sign = sshd_hostkey_sign;
  1616. }
  1617. /* This function requires careful sanity checking */
  1618. void
  1619. mm_get_keystate(struct ssh *ssh, struct monitor *pmonitor)
  1620. {
  1621. struct sshbuf *m;
  1622. debug3("%s: Waiting for new keys", __func__);
  1623. if ((child_state = sshbuf_new()) == NULL)
  1624. fatal("%s: sshbuf_new failed", __func__);
  1625. mm_request_receive_expect(pmonitor->m_sendfd, MONITOR_REQ_KEYEXPORT,
  1626. child_state);
  1627. debug3("%s: GOT new keys", __func__);
  1628. #ifdef SSH_AUDIT_EVENTS
  1629. m = sshbuf_new();
  1630. mm_request_receive_expect(pmonitor->m_sendfd,
  1631. MONITOR_REQ_AUDIT_SESSION_KEY_FREE, m);
  1632. mm_answer_audit_session_key_free_body(ssh, pmonitor->m_sendfd, m);
  1633. sshbuf_free(m);
  1634. #endif
  1635. /* Drain any buffered messages from the child */
  1636. while (pmonitor->m_log_recvfd >= 0 && monitor_read_log(pmonitor) == 0)
  1637. ;
  1638. }
  1639. /* XXX */
  1640. #define FD_CLOSEONEXEC(x) do { \
  1641. if (fcntl(x, F_SETFD, FD_CLOEXEC) == -1) \
  1642. fatal("fcntl(%d, F_SETFD)", x); \
  1643. } while (0)
  1644. static void
  1645. monitor_openfds(struct monitor *mon, int do_logfds)
  1646. {
  1647. int pair[2];
  1648. #ifdef SO_ZEROIZE
  1649. int on = 1;
  1650. #endif
  1651. if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
  1652. fatal("%s: socketpair: %s", __func__, strerror(errno));
  1653. #ifdef SO_ZEROIZE
  1654. if (setsockopt(pair[0], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
  1655. error("setsockopt SO_ZEROIZE(0): %.100s", strerror(errno));
  1656. if (setsockopt(pair[1], SOL_SOCKET, SO_ZEROIZE, &on, sizeof(on)) == -1)
  1657. error("setsockopt SO_ZEROIZE(1): %.100s", strerror(errno));
  1658. #endif
  1659. FD_CLOSEONEXEC(pair[0]);
  1660. FD_CLOSEONEXEC(pair[1]);
  1661. mon->m_recvfd = pair[0];
  1662. mon->m_sendfd = pair[1];
  1663. if (do_logfds) {
  1664. if (pipe(pair) == -1)
  1665. fatal("%s: pipe: %s", __func__, strerror(errno));
  1666. FD_CLOSEONEXEC(pair[0]);
  1667. FD_CLOSEONEXEC(pair[1]);
  1668. mon->m_log_recvfd = pair[0];
  1669. mon->m_log_sendfd = pair[1];
  1670. } else
  1671. mon->m_log_recvfd = mon->m_log_sendfd = -1;
  1672. }
  1673. #define MM_MEMSIZE 65536
  1674. struct monitor *
  1675. monitor_init(void)
  1676. {
  1677. struct monitor *mon;
  1678. mon = xcalloc(1, sizeof(*mon));
  1679. monitor_openfds(mon, 1);
  1680. mon->m_state = "";
  1681. return mon;
  1682. }
  1683. void
  1684. monitor_reinit(struct monitor *mon, const char *chroot_dir)
  1685. {
  1686. struct stat dev_log_stat;
  1687. char *dev_log_path;
  1688. int do_logfds = 0;
  1689. if (chroot_dir != NULL) {
  1690. xasprintf(&dev_log_path, "%s/dev/log", chroot_dir);
  1691. if (stat(dev_log_path, &dev_log_stat) != 0) {
  1692. debug("%s: /dev/log doesn't exist in %s chroot - will try to log via monitor using [postauth] suffix", __func__, chroot_dir);
  1693. do_logfds = 1;
  1694. }
  1695. free(dev_log_path);
  1696. }
  1697. monitor_openfds(mon, do_logfds);
  1698. }
  1699. #ifdef GSSAPI
  1700. int
  1701. mm_answer_gss_setup_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
  1702. {
  1703. gss_OID_desc goid;
  1704. OM_uint32 major;
  1705. size_t len;
  1706. u_char *p;
  1707. int r;
  1708. if (!options.gss_authentication)
  1709. fatal("%s: GSSAPI authentication not enabled", __func__);
  1710. if ((r = sshbuf_get_string(m, &p, &len)) != 0)
  1711. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1712. goid.elements = p;
  1713. goid.length = len;
  1714. major = ssh_gssapi_server_ctx(&gsscontext, &goid);
  1715. free(goid.elements);
  1716. sshbuf_reset(m);
  1717. if ((r = sshbuf_put_u32(m, major)) != 0)
  1718. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1719. mm_request_send(sock, MONITOR_ANS_GSSSETUP, m);
  1720. /* Now we have a context, enable the step */
  1721. monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
  1722. return (0);
  1723. }
  1724. int
  1725. mm_answer_gss_accept_ctx(struct ssh *ssh, int sock, struct sshbuf *m)
  1726. {
  1727. gss_buffer_desc in;
  1728. gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
  1729. OM_uint32 major, minor;
  1730. OM_uint32 flags = 0; /* GSI needs this */
  1731. int r;
  1732. if (!options.gss_authentication)
  1733. fatal("%s: GSSAPI authentication not enabled", __func__);
  1734. if ((r = ssh_gssapi_get_buffer_desc(m, &in)) != 0)
  1735. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1736. major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
  1737. free(in.value);
  1738. sshbuf_reset(m);
  1739. if ((r = sshbuf_put_u32(m, major)) != 0 ||
  1740. (r = sshbuf_put_string(m, out.value, out.length)) != 0 ||
  1741. (r = sshbuf_put_u32(m, flags)) != 0)
  1742. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1743. mm_request_send(sock, MONITOR_ANS_GSSSTEP, m);
  1744. gss_release_buffer(&minor, &out);
  1745. if (major == GSS_S_COMPLETE) {
  1746. monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
  1747. monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
  1748. monitor_permit(mon_dispatch, MONITOR_REQ_GSSCHECKMIC, 1);
  1749. }
  1750. return (0);
  1751. }
  1752. int
  1753. mm_answer_gss_checkmic(struct ssh *ssh, int sock, struct sshbuf *m)
  1754. {
  1755. gss_buffer_desc gssbuf, mic;
  1756. OM_uint32 ret;
  1757. int r;
  1758. if (!options.gss_authentication)
  1759. fatal("%s: GSSAPI authentication not enabled", __func__);
  1760. if ((r = ssh_gssapi_get_buffer_desc(m, &gssbuf)) != 0 ||
  1761. (r = ssh_gssapi_get_buffer_desc(m, &mic)) != 0)
  1762. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1763. ret = ssh_gssapi_checkmic(gsscontext, &gssbuf, &mic);
  1764. free(gssbuf.value);
  1765. free(mic.value);
  1766. sshbuf_reset(m);
  1767. if ((r = sshbuf_put_u32(m, ret)) != 0)
  1768. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1769. mm_request_send(sock, MONITOR_ANS_GSSCHECKMIC, m);
  1770. if (!GSS_ERROR(ret))
  1771. monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
  1772. return (0);
  1773. }
  1774. int
  1775. mm_answer_gss_userok(struct ssh *ssh, int sock, struct sshbuf *m)
  1776. {
  1777. int r, authenticated;
  1778. const char *displayname;
  1779. if (!options.gss_authentication)
  1780. fatal("%s: GSSAPI authentication not enabled", __func__);
  1781. authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
  1782. sshbuf_reset(m);
  1783. if ((r = sshbuf_put_u32(m, authenticated)) != 0)
  1784. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1785. debug3("%s: sending result %d", __func__, authenticated);
  1786. mm_request_send(sock, MONITOR_ANS_GSSUSEROK, m);
  1787. auth_method = "gssapi-with-mic";
  1788. if ((displayname = ssh_gssapi_displayname()) != NULL)
  1789. auth2_record_info(authctxt, "%s", displayname);
  1790. /* Monitor loop will terminate if authenticated */
  1791. return (authenticated);
  1792. }
  1793. #endif /* GSSAPI */
  1794. #ifdef SSH_AUDIT_EVENTS
  1795. int
  1796. mm_answer_audit_unsupported_body(struct ssh *ssh, int sock, struct sshbuf *m)
  1797. {
  1798. int what, r;
  1799. if ((r = sshbuf_get_u32(m, &what)) != 0)
  1800. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1801. audit_unsupported_body(ssh, what);
  1802. sshbuf_reset(m);
  1803. mm_request_send(sock, MONITOR_ANS_AUDIT_UNSUPPORTED, m);
  1804. return 0;
  1805. }
  1806. int
  1807. mm_answer_audit_kex_body(struct ssh *ssh, int sock, struct sshbuf *m)
  1808. {
  1809. int ctos, r;
  1810. char *cipher, *mac, *compress, *pfs;
  1811. u_int64_t tmp;
  1812. pid_t pid;
  1813. uid_t uid;
  1814. if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
  1815. (r = sshbuf_get_cstring(m, &cipher, NULL)) != 0 ||
  1816. (r = sshbuf_get_cstring(m, &mac, NULL)) != 0 ||
  1817. (r = sshbuf_get_cstring(m, &compress, NULL)) != 0 ||
  1818. (r = sshbuf_get_cstring(m, &pfs, NULL)) != 0 ||
  1819. (r = sshbuf_get_u64(m, &tmp)) != 0)
  1820. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1821. pid = (pid_t) tmp;
  1822. if ((r = sshbuf_get_u64(m, &tmp)) != 0)
  1823. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1824. uid = (pid_t) tmp;
  1825. audit_kex_body(ssh, ctos, cipher, mac, compress, pfs, pid, uid);
  1826. free(cipher);
  1827. free(mac);
  1828. free(compress);
  1829. free(pfs);
  1830. sshbuf_reset(m);
  1831. mm_request_send(sock, MONITOR_ANS_AUDIT_KEX, m);
  1832. return 0;
  1833. }
  1834. int
  1835. mm_answer_audit_session_key_free_body(struct ssh *ssh, int sock, struct sshbuf *m)
  1836. {
  1837. int ctos, r;
  1838. u_int64_t tmp;
  1839. pid_t pid;
  1840. uid_t uid;
  1841. if ((r = sshbuf_get_u32(m, &ctos)) != 0 ||
  1842. (r = sshbuf_get_u64(m, &tmp)) != 0)
  1843. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1844. pid = (pid_t) tmp;
  1845. if ((r = sshbuf_get_u64(m, &tmp)) != 0)
  1846. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1847. uid = (uid_t) tmp;
  1848. audit_session_key_free_body(ssh, ctos, pid, uid);
  1849. sshbuf_reset(m);
  1850. mm_request_send(sock, MONITOR_ANS_AUDIT_SESSION_KEY_FREE, m);
  1851. return 0;
  1852. }
  1853. int
  1854. mm_answer_audit_server_key_free(struct ssh *ssh, int sock, struct sshbuf *m)
  1855. {
  1856. size_t len, r;
  1857. char *fp;
  1858. u_int64_t tmp;
  1859. pid_t pid;
  1860. uid_t uid;
  1861. if ((r = sshbuf_get_cstring(m, &fp, &len)) != 0 ||
  1862. (r = sshbuf_get_u64(m, &tmp)) != 0)
  1863. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1864. pid = (pid_t) tmp;
  1865. if ((r = sshbuf_get_u64(m, &tmp)) != 0)
  1866. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  1867. uid = (uid_t) tmp;
  1868. audit_destroy_sensitive_data(ssh, fp, pid, uid);
  1869. free(fp);
  1870. sshbuf_reset(m);
  1871. return 0;
  1872. }
  1873. #endif /* SSH_AUDIT_EVENTS */