summaryrefslogtreecommitdiff
path: root/monitor.c
diff options
context:
space:
mode:
Diffstat (limited to 'monitor.c')
-rw-r--r--monitor.c362
1 files changed, 108 insertions, 254 deletions
diff --git a/monitor.c b/monitor.c
index e5656470d..bd39f7ccb 100644
--- a/monitor.c
+++ b/monitor.c
@@ -25,7 +25,7 @@
25 */ 25 */
26 26
27#include "includes.h" 27#include "includes.h"
28RCSID("$OpenBSD: monitor.c,v 1.49 2003/08/28 12:54:34 markus Exp $"); 28RCSID("$OpenBSD: monitor.c,v 1.36 2003/04/01 10:22:21 markus Exp $");
29 29
30#include <openssl/dh.h> 30#include <openssl/dh.h>
31 31
@@ -59,11 +59,6 @@ RCSID("$OpenBSD: monitor.c,v 1.49 2003/08/28 12:54:34 markus Exp $");
59#include "ssh2.h" 59#include "ssh2.h"
60#include "mpaux.h" 60#include "mpaux.h"
61 61
62#ifdef GSSAPI
63#include "ssh-gss.h"
64static Gssctxt *gsscontext = NULL;
65#endif
66
67/* Imports */ 62/* Imports */
68extern ServerOptions options; 63extern ServerOptions options;
69extern u_int utmp_len; 64extern u_int utmp_len;
@@ -98,7 +93,7 @@ struct {
98 u_int olen; 93 u_int olen;
99} child_state; 94} child_state;
100 95
101/* Functions on the monitor that answer unprivileged requests */ 96/* Functions on the montior that answer unprivileged requests */
102 97
103int mm_answer_moduli(int, Buffer *); 98int mm_answer_moduli(int, Buffer *);
104int mm_answer_sign(int, Buffer *); 99int mm_answer_sign(int, Buffer *);
@@ -123,17 +118,13 @@ int mm_answer_sessid(int, Buffer *);
123 118
124#ifdef USE_PAM 119#ifdef USE_PAM
125int mm_answer_pam_start(int, Buffer *); 120int mm_answer_pam_start(int, Buffer *);
126int mm_answer_pam_account(int, Buffer *);
127int mm_answer_pam_init_ctx(int, Buffer *);
128int mm_answer_pam_query(int, Buffer *);
129int mm_answer_pam_respond(int, Buffer *);
130int mm_answer_pam_free_ctx(int, Buffer *);
131#endif 121#endif
132 122
133#ifdef GSSAPI 123#ifdef KRB4
134int mm_answer_gss_setup_ctx(int, Buffer *); 124int mm_answer_krb4(int, Buffer *);
135int mm_answer_gss_accept_ctx(int, Buffer *); 125#endif
136int mm_answer_gss_userok(int, Buffer *); 126#ifdef KRB5
127int mm_answer_krb5(int, Buffer *);
137#endif 128#endif
138 129
139static Authctxt *authctxt; 130static Authctxt *authctxt;
@@ -146,7 +137,7 @@ static int key_blobtype = MM_NOKEY;
146static char *hostbased_cuser = NULL; 137static char *hostbased_cuser = NULL;
147static char *hostbased_chost = NULL; 138static char *hostbased_chost = NULL;
148static char *auth_method = "unknown"; 139static char *auth_method = "unknown";
149static u_int session_id2_len = 0; 140static int session_id2_len = 0;
150static u_char *session_id2 = NULL; 141static u_char *session_id2 = NULL;
151static pid_t monitor_child_pid; 142static pid_t monitor_child_pid;
152 143
@@ -173,11 +164,6 @@ struct mon_table mon_dispatch_proto20[] = {
173 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword}, 164 {MONITOR_REQ_AUTHPASSWORD, MON_AUTH, mm_answer_authpassword},
174#ifdef USE_PAM 165#ifdef USE_PAM
175 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, 166 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
176 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account},
177 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx},
178 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query},
179 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond},
180 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx},
181#endif 167#endif
182#ifdef BSD_AUTH 168#ifdef BSD_AUTH
183 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery}, 169 {MONITOR_REQ_BSDAUTHQUERY, MON_ISAUTH, mm_answer_bsdauthquery},
@@ -189,11 +175,6 @@ struct mon_table mon_dispatch_proto20[] = {
189#endif 175#endif
190 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed}, 176 {MONITOR_REQ_KEYALLOWED, MON_ISAUTH, mm_answer_keyallowed},
191 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify}, 177 {MONITOR_REQ_KEYVERIFY, MON_AUTH, mm_answer_keyverify},
192#ifdef GSSAPI
193 {MONITOR_REQ_GSSSETUP, MON_ISAUTH, mm_answer_gss_setup_ctx},
194 {MONITOR_REQ_GSSSTEP, MON_ISAUTH, mm_answer_gss_accept_ctx},
195 {MONITOR_REQ_GSSUSEROK, MON_AUTH, mm_answer_gss_userok},
196#endif
197 {0, 0, NULL} 178 {0, 0, NULL}
198}; 179};
199 180
@@ -225,11 +206,12 @@ struct mon_table mon_dispatch_proto15[] = {
225#endif 206#endif
226#ifdef USE_PAM 207#ifdef USE_PAM
227 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start}, 208 {MONITOR_REQ_PAM_START, MON_ONCE, mm_answer_pam_start},
228 {MONITOR_REQ_PAM_ACCOUNT, 0, mm_answer_pam_account}, 209#endif
229 {MONITOR_REQ_PAM_INIT_CTX, MON_ISAUTH, mm_answer_pam_init_ctx}, 210#ifdef KRB4
230 {MONITOR_REQ_PAM_QUERY, MON_ISAUTH, mm_answer_pam_query}, 211 {MONITOR_REQ_KRB4, MON_ONCE|MON_AUTH, mm_answer_krb4},
231 {MONITOR_REQ_PAM_RESPOND, MON_ISAUTH, mm_answer_pam_respond}, 212#endif
232 {MONITOR_REQ_PAM_FREE_CTX, MON_ONCE|MON_AUTHDECIDE, mm_answer_pam_free_ctx}, 213#ifdef KRB5
214 {MONITOR_REQ_KRB5, MON_ONCE|MON_AUTH, mm_answer_krb5},
233#endif 215#endif
234 {0, 0, NULL} 216 {0, 0, NULL}
235}; 217};
@@ -305,16 +287,8 @@ monitor_child_preauth(struct monitor *pmonitor)
305 !auth_root_allowed(auth_method)) 287 !auth_root_allowed(auth_method))
306 authenticated = 0; 288 authenticated = 0;
307#ifdef USE_PAM 289#ifdef USE_PAM
308 /* PAM needs to perform account checks after auth */ 290 if (!do_pam_account(authctxt->pw->pw_name, NULL))
309 if (options.use_pam) { 291 authenticated = 0;
310 Buffer m;
311
312 buffer_init(&m);
313 mm_request_receive_expect(pmonitor->m_sendfd,
314 MONITOR_REQ_PAM_ACCOUNT, &m);
315 authenticated = mm_answer_pam_account(pmonitor->m_sendfd, &m);
316 buffer_free(&m);
317 }
318#endif 292#endif
319 } 293 }
320 294
@@ -363,6 +337,7 @@ monitor_child_postauth(struct monitor *pmonitor)
363 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1); 337 monitor_permit(mon_dispatch, MONITOR_REQ_MODULI, 1);
364 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1); 338 monitor_permit(mon_dispatch, MONITOR_REQ_SIGN, 1);
365 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); 339 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
340
366 } else { 341 } else {
367 mon_dispatch = mon_dispatch_postauth15; 342 mon_dispatch = mon_dispatch_postauth15;
368 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1); 343 monitor_permit(mon_dispatch, MONITOR_REQ_TERM, 1);
@@ -598,8 +573,7 @@ mm_answer_pwnamallow(int socket, Buffer *m)
598 } 573 }
599 574
600#ifdef USE_PAM 575#ifdef USE_PAM
601 if (options.use_pam) 576 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
602 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_START, 1);
603#endif 577#endif
604 578
605 return (0); 579 return (0);
@@ -649,7 +623,7 @@ mm_answer_authpassword(int socket, Buffer *m)
649 passwd = buffer_get_string(m, &plen); 623 passwd = buffer_get_string(m, &plen);
650 /* Only authenticate if the context is valid */ 624 /* Only authenticate if the context is valid */
651 authenticated = options.password_authentication && 625 authenticated = options.password_authentication &&
652 auth_password(authctxt, passwd); 626 auth_password(authctxt, passwd) && authctxt->valid;
653 memset(passwd, 0, strlen(passwd)); 627 memset(passwd, 0, strlen(passwd));
654 xfree(passwd); 628 xfree(passwd);
655 629
@@ -782,133 +756,14 @@ mm_answer_pam_start(int socket, Buffer *m)
782{ 756{
783 char *user; 757 char *user;
784 758
785 if (!options.use_pam)
786 fatal("UsePAM not set, but ended up in %s anyway", __func__);
787
788 user = buffer_get_string(m, NULL); 759 user = buffer_get_string(m, NULL);
789 760
790 start_pam(user); 761 start_pam(user);
791 762
792 xfree(user); 763 xfree(user);
793 764
794 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_ACCOUNT, 1);
795
796 return (0);
797}
798
799int
800mm_answer_pam_account(int socket, Buffer *m)
801{
802 u_int ret;
803
804 if (!options.use_pam)
805 fatal("UsePAM not set, but ended up in %s anyway", __func__);
806
807 ret = do_pam_account();
808
809 buffer_put_int(m, ret);
810
811 mm_request_send(socket, MONITOR_ANS_PAM_ACCOUNT, m);
812
813 return (ret);
814}
815
816static void *sshpam_ctxt, *sshpam_authok;
817extern KbdintDevice sshpam_device;
818
819int
820mm_answer_pam_init_ctx(int socket, Buffer *m)
821{
822
823 debug3("%s", __func__);
824 authctxt->user = buffer_get_string(m, NULL);
825 sshpam_ctxt = (sshpam_device.init_ctx)(authctxt);
826 sshpam_authok = NULL;
827 buffer_clear(m);
828 if (sshpam_ctxt != NULL) {
829 monitor_permit(mon_dispatch, MONITOR_REQ_PAM_FREE_CTX, 1);
830 buffer_put_int(m, 1);
831 } else {
832 buffer_put_int(m, 0);
833 }
834 mm_request_send(socket, MONITOR_ANS_PAM_INIT_CTX, m);
835 return (0);
836}
837
838int
839mm_answer_pam_query(int socket, Buffer *m)
840{
841 char *name, *info, **prompts;
842 u_int num, *echo_on;
843 int i, ret;
844
845 debug3("%s", __func__);
846 sshpam_authok = NULL;
847 ret = (sshpam_device.query)(sshpam_ctxt, &name, &info, &num, &prompts, &echo_on);
848 if (ret == 0 && num == 0)
849 sshpam_authok = sshpam_ctxt;
850 if (num > 1 || name == NULL || info == NULL)
851 ret = -1;
852 buffer_clear(m);
853 buffer_put_int(m, ret);
854 buffer_put_cstring(m, name);
855 xfree(name);
856 buffer_put_cstring(m, info);
857 xfree(info);
858 buffer_put_int(m, num);
859 for (i = 0; i < num; ++i) {
860 buffer_put_cstring(m, prompts[i]);
861 xfree(prompts[i]);
862 buffer_put_int(m, echo_on[i]);
863 }
864 if (prompts != NULL)
865 xfree(prompts);
866 if (echo_on != NULL)
867 xfree(echo_on);
868 mm_request_send(socket, MONITOR_ANS_PAM_QUERY, m);
869 return (0);
870}
871
872int
873mm_answer_pam_respond(int socket, Buffer *m)
874{
875 char **resp;
876 u_int num;
877 int i, ret;
878
879 debug3("%s", __func__);
880 sshpam_authok = NULL;
881 num = buffer_get_int(m);
882 if (num > 0) {
883 resp = xmalloc(num * sizeof(char *));
884 for (i = 0; i < num; ++i)
885 resp[i] = buffer_get_string(m, NULL);
886 ret = (sshpam_device.respond)(sshpam_ctxt, num, resp);
887 for (i = 0; i < num; ++i)
888 xfree(resp[i]);
889 xfree(resp);
890 } else {
891 ret = (sshpam_device.respond)(sshpam_ctxt, num, NULL);
892 }
893 buffer_clear(m);
894 buffer_put_int(m, ret);
895 mm_request_send(socket, MONITOR_ANS_PAM_RESPOND, m);
896 auth_method = "keyboard-interactive/pam";
897 if (ret == 0)
898 sshpam_authok = sshpam_ctxt;
899 return (0); 765 return (0);
900} 766}
901
902int
903mm_answer_pam_free_ctx(int socket, Buffer *m)
904{
905
906 debug3("%s", __func__);
907 (sshpam_device.free_ctx)(sshpam_ctxt);
908 buffer_clear(m);
909 mm_request_send(socket, MONITOR_ANS_PAM_FREE_CTX, m);
910 return (sshpam_authok == sshpam_ctxt);
911}
912#endif 767#endif
913 768
914static void 769static void
@@ -1032,7 +887,7 @@ monitor_valid_userblob(u_char *data, u_int datalen)
1032 fail++; 887 fail++;
1033 p = buffer_get_string(&b, NULL); 888 p = buffer_get_string(&b, NULL);
1034 if (strcmp(authctxt->user, p) != 0) { 889 if (strcmp(authctxt->user, p) != 0) {
1035 logit("wrong user name passed to monitor: expected %s != %.100s", 890 log("wrong user name passed to monitor: expected %s != %.100s",
1036 authctxt->user, p); 891 authctxt->user, p);
1037 fail++; 892 fail++;
1038 } 893 }
@@ -1080,7 +935,7 @@ monitor_valid_hostbasedblob(u_char *data, u_int datalen, char *cuser,
1080 fail++; 935 fail++;
1081 p = buffer_get_string(&b, NULL); 936 p = buffer_get_string(&b, NULL);
1082 if (strcmp(authctxt->user, p) != 0) { 937 if (strcmp(authctxt->user, p) != 0) {
1083 logit("wrong user name passed to monitor: expected %s != %.100s", 938 log("wrong user name passed to monitor: expected %s != %.100s",
1084 authctxt->user, p); 939 authctxt->user, p);
1085 fail++; 940 fail++;
1086 } 941 }
@@ -1190,14 +1045,14 @@ mm_record_login(Session *s, struct passwd *pw)
1190 } 1045 }
1191 /* Record that there was a login on that tty from the remote host. */ 1046 /* Record that there was a login on that tty from the remote host. */
1192 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid, 1047 record_login(s->pid, s->tty, pw->pw_name, pw->pw_uid,
1193 get_remote_name_or_ip(utmp_len, options.use_dns), 1048 get_remote_name_or_ip(utmp_len, options.verify_reverse_mapping),
1194 (struct sockaddr *)&from, fromlen); 1049 (struct sockaddr *)&from, fromlen);
1195} 1050}
1196 1051
1197static void 1052static void
1198mm_session_close(Session *s) 1053mm_session_close(Session *s)
1199{ 1054{
1200 debug3("%s: session %d pid %ld", __func__, s->self, (long)s->pid); 1055 debug3("%s: session %d pid %d", __func__, s->self, s->pid);
1201 if (s->ttyfd != -1) { 1056 if (s->ttyfd != -1) {
1202 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd); 1057 debug3("%s: tty %s ptyfd %d", __func__, s->tty, s->ptyfd);
1203 fatal_remove_cleanup(session_pty_cleanup2, (void *)s); 1058 fatal_remove_cleanup(session_pty_cleanup2, (void *)s);
@@ -1461,6 +1316,89 @@ mm_answer_rsa_response(int socket, Buffer *m)
1461 return (success); 1316 return (success);
1462} 1317}
1463 1318
1319#ifdef KRB4
1320int
1321mm_answer_krb4(int socket, Buffer *m)
1322{
1323 KTEXT_ST auth, reply;
1324 char *client, *p;
1325 int success;
1326 u_int alen;
1327
1328 reply.length = auth.length = 0;
1329
1330 p = buffer_get_string(m, &alen);
1331 if (alen >= MAX_KTXT_LEN)
1332 fatal("%s: auth too large", __func__);
1333 memcpy(auth.dat, p, alen);
1334 auth.length = alen;
1335 memset(p, 0, alen);
1336 xfree(p);
1337
1338 success = options.kerberos_authentication &&
1339 authctxt->valid &&
1340 auth_krb4(authctxt, &auth, &client, &reply);
1341
1342 memset(auth.dat, 0, alen);
1343 buffer_clear(m);
1344 buffer_put_int(m, success);
1345
1346 if (success) {
1347 buffer_put_cstring(m, client);
1348 buffer_put_string(m, reply.dat, reply.length);
1349 if (client)
1350 xfree(client);
1351 if (reply.length)
1352 memset(reply.dat, 0, reply.length);
1353 }
1354
1355 debug3("%s: sending result %d", __func__, success);
1356 mm_request_send(socket, MONITOR_ANS_KRB4, m);
1357
1358 auth_method = "kerberos";
1359
1360 /* Causes monitor loop to terminate if authenticated */
1361 return (success);
1362}
1363#endif
1364
1365#ifdef KRB5
1366int
1367mm_answer_krb5(int socket, Buffer *m)
1368{
1369 krb5_data tkt, reply;
1370 char *client_user;
1371 u_int len;
1372 int success;
1373
1374 /* use temporary var to avoid size issues on 64bit arch */
1375 tkt.data = buffer_get_string(m, &len);
1376 tkt.length = len;
1377
1378 success = options.kerberos_authentication &&
1379 authctxt->valid &&
1380 auth_krb5(authctxt, &tkt, &client_user, &reply);
1381
1382 if (tkt.length)
1383 xfree(tkt.data);
1384
1385 buffer_clear(m);
1386 buffer_put_int(m, success);
1387
1388 if (success) {
1389 buffer_put_cstring(m, client_user);
1390 buffer_put_string(m, reply.data, reply.length);
1391 if (client_user)
1392 xfree(client_user);
1393 if (reply.length)
1394 xfree(reply.data);
1395 }
1396 mm_request_send(socket, MONITOR_ANS_KRB5, m);
1397
1398 return success;
1399}
1400#endif
1401
1464int 1402int
1465mm_answer_term(int socket, Buffer *req) 1403mm_answer_term(int socket, Buffer *req)
1466{ 1404{
@@ -1576,8 +1514,6 @@ mm_get_keystate(struct monitor *pmonitor)
1576 Buffer m; 1514 Buffer m;
1577 u_char *blob, *p; 1515 u_char *blob, *p;
1578 u_int bloblen, plen; 1516 u_int bloblen, plen;
1579 u_int32_t seqnr, packets;
1580 u_int64_t blocks;
1581 1517
1582 debug3("%s: Waiting for new keys", __func__); 1518 debug3("%s: Waiting for new keys", __func__);
1583 1519
@@ -1607,14 +1543,8 @@ mm_get_keystate(struct monitor *pmonitor)
1607 xfree(blob); 1543 xfree(blob);
1608 1544
1609 /* Now get sequence numbers for the packets */ 1545 /* Now get sequence numbers for the packets */
1610 seqnr = buffer_get_int(&m); 1546 packet_set_seqnr(MODE_OUT, buffer_get_int(&m));
1611 blocks = buffer_get_int64(&m); 1547 packet_set_seqnr(MODE_IN, buffer_get_int(&m));
1612 packets = buffer_get_int(&m);
1613 packet_set_state(MODE_OUT, seqnr, blocks, packets);
1614 seqnr = buffer_get_int(&m);
1615 blocks = buffer_get_int64(&m);
1616 packets = buffer_get_int(&m);
1617 packet_set_state(MODE_IN, seqnr, blocks, packets);
1618 1548
1619 skip: 1549 skip:
1620 /* Get the key context */ 1550 /* Get the key context */
@@ -1735,79 +1665,3 @@ monitor_reinit(struct monitor *mon)
1735 mon->m_recvfd = pair[0]; 1665 mon->m_recvfd = pair[0];
1736 mon->m_sendfd = pair[1]; 1666 mon->m_sendfd = pair[1];
1737} 1667}
1738
1739#ifdef GSSAPI
1740int
1741mm_answer_gss_setup_ctx(int socket, Buffer *m)
1742{
1743 gss_OID_desc oid;
1744 OM_uint32 major;
1745 u_int len;
1746
1747 oid.elements = buffer_get_string(m, &len);
1748 oid.length = len;
1749
1750 major = ssh_gssapi_server_ctx(&gsscontext, &oid);
1751
1752 xfree(oid.elements);
1753
1754 buffer_clear(m);
1755 buffer_put_int(m, major);
1756
1757 mm_request_send(socket,MONITOR_ANS_GSSSETUP, m);
1758
1759 /* Now we have a context, enable the step */
1760 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 1);
1761
1762 return (0);
1763}
1764
1765int
1766mm_answer_gss_accept_ctx(int socket, Buffer *m)
1767{
1768 gss_buffer_desc in;
1769 gss_buffer_desc out = GSS_C_EMPTY_BUFFER;
1770 OM_uint32 major,minor;
1771 OM_uint32 flags = 0; /* GSI needs this */
1772 u_int len;
1773
1774 in.value = buffer_get_string(m, &len);
1775 in.length = len;
1776 major = ssh_gssapi_accept_ctx(gsscontext, &in, &out, &flags);
1777 xfree(in.value);
1778
1779 buffer_clear(m);
1780 buffer_put_int(m, major);
1781 buffer_put_string(m, out.value, out.length);
1782 buffer_put_int(m, flags);
1783 mm_request_send(socket, MONITOR_ANS_GSSSTEP, m);
1784
1785 gss_release_buffer(&minor, &out);
1786
1787 /* Complete - now we can do signing */
1788 if (major==GSS_S_COMPLETE) {
1789 monitor_permit(mon_dispatch, MONITOR_REQ_GSSSTEP, 0);
1790 monitor_permit(mon_dispatch, MONITOR_REQ_GSSUSEROK, 1);
1791 }
1792 return (0);
1793}
1794
1795int
1796mm_answer_gss_userok(int socket, Buffer *m)
1797{
1798 int authenticated;
1799
1800 authenticated = authctxt->valid && ssh_gssapi_userok(authctxt->user);
1801
1802 buffer_clear(m);
1803 buffer_put_int(m, authenticated);
1804
1805 debug3("%s: sending result %d", __func__, authenticated);
1806 mm_request_send(socket, MONITOR_ANS_GSSUSEROK, m);
1807
1808 auth_method="gssapi";
1809
1810 /* Monitor loop will terminate if authenticated */
1811 return (authenticated);
1812}
1813#endif /* GSSAPI */