17c478bd9Sstevel@tonic-gate /* 2*740638c8Sbw * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 37c478bd9Sstevel@tonic-gate * Use is subject to license terms. 47c478bd9Sstevel@tonic-gate */ 57c478bd9Sstevel@tonic-gate 67c478bd9Sstevel@tonic-gate /* Copyright(c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 77c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 87c478bd9Sstevel@tonic-gate 97c478bd9Sstevel@tonic-gate /* 107c478bd9Sstevel@tonic-gate * Copyright (c) 1983 The Regents of the University of California. 117c478bd9Sstevel@tonic-gate * All rights reserved. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms are permitted 147c478bd9Sstevel@tonic-gate * provided that the above copyright notice and this paragraph are 157c478bd9Sstevel@tonic-gate * duplicated in all such forms and that any documentation, 167c478bd9Sstevel@tonic-gate * advertising materials, and other materials related to such 177c478bd9Sstevel@tonic-gate * distribution and use acknowledge that the software was developed 187c478bd9Sstevel@tonic-gate * by the University of California, Berkeley. The name of the 197c478bd9Sstevel@tonic-gate * University may not be used to endorse or promote products derived 207c478bd9Sstevel@tonic-gate * from this software without specific prior written permission. 217c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 227c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 237c478bd9Sstevel@tonic-gate * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * remote login server: 307c478bd9Sstevel@tonic-gate * remuser\0 317c478bd9Sstevel@tonic-gate * locuser\0 327c478bd9Sstevel@tonic-gate * terminal info\0 337c478bd9Sstevel@tonic-gate * data 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #include <time.h> 377c478bd9Sstevel@tonic-gate #include <sys/types.h> 387c478bd9Sstevel@tonic-gate #include <sys/stat.h> 397c478bd9Sstevel@tonic-gate #include <sys/socket.h> 407c478bd9Sstevel@tonic-gate #include <sys/wait.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <netinet/in.h> 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate #include <errno.h> 457c478bd9Sstevel@tonic-gate #include <signal.h> 467c478bd9Sstevel@tonic-gate #include <fcntl.h> 477c478bd9Sstevel@tonic-gate #include <stdio.h> 487c478bd9Sstevel@tonic-gate #include <netdb.h> 497c478bd9Sstevel@tonic-gate #include <syslog.h> 507c478bd9Sstevel@tonic-gate #include <string.h> 517c478bd9Sstevel@tonic-gate #include <unistd.h> 527c478bd9Sstevel@tonic-gate #include <stdlib.h> 537c478bd9Sstevel@tonic-gate #include <alloca.h> 547c478bd9Sstevel@tonic-gate #include <stropts.h> 557c478bd9Sstevel@tonic-gate #include <sac.h> /* for SC_WILDC */ 567c478bd9Sstevel@tonic-gate #include <utmpx.h> 577c478bd9Sstevel@tonic-gate #include <sys/filio.h> 587c478bd9Sstevel@tonic-gate #include <sys/logindmux.h> 597c478bd9Sstevel@tonic-gate #include <sys/rlioctl.h> 607c478bd9Sstevel@tonic-gate #include <sys/termios.h> 617c478bd9Sstevel@tonic-gate #include <sys/tihdr.h> 627c478bd9Sstevel@tonic-gate #include <arpa/inet.h> 637c478bd9Sstevel@tonic-gate #include <security/pam_appl.h> 647c478bd9Sstevel@tonic-gate #include <strings.h> 657c478bd9Sstevel@tonic-gate #include <com_err.h> 667c478bd9Sstevel@tonic-gate #include <k5-int.h> 677c478bd9Sstevel@tonic-gate #include <kcmd.h> 687c478bd9Sstevel@tonic-gate #include <krb5_repository.h> 697c478bd9Sstevel@tonic-gate #include <sys/cryptmod.h> 707c478bd9Sstevel@tonic-gate #include <bsm/adt.h> 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #define KRB5_RECVAUTH_V5 5 737c478bd9Sstevel@tonic-gate #define UT_NAMESIZE sizeof (((struct utmpx *)0)->ut_name) 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate static char lusername[UT_NAMESIZE+1]; 767c478bd9Sstevel@tonic-gate static char rusername[UT_NAMESIZE+1]; 777c478bd9Sstevel@tonic-gate static char *krusername = NULL; 787c478bd9Sstevel@tonic-gate static char term[64]; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate static krb5_ccache ccache = NULL; 817c478bd9Sstevel@tonic-gate static krb5_keyblock *session_key = NULL; 827c478bd9Sstevel@tonic-gate static int chksum_flag = 0; 837c478bd9Sstevel@tonic-gate static int use_auth = 0; 847c478bd9Sstevel@tonic-gate static enum kcmd_proto kcmd_protocol; 857c478bd9Sstevel@tonic-gate #ifdef ALLOW_KCMD_V2 867c478bd9Sstevel@tonic-gate static krb5_data encr_iv = { NULL, 0 }; 877c478bd9Sstevel@tonic-gate static krb5_data decr_iv = { NULL, 0 }; 887c478bd9Sstevel@tonic-gate #endif /* ALLOW_KCMD_V2 */ 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate #define CHKSUM_REQUIRED 0x01 917c478bd9Sstevel@tonic-gate #define CHKSUM_IGNORED 0x02 927c478bd9Sstevel@tonic-gate #define VALID_CHKSUM(x) ((x) == 0 || (x) == CHKSUM_REQUIRED ||\ 937c478bd9Sstevel@tonic-gate (x) == CHKSUM_IGNORED) 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate #define PWD_IF_FAIL 0x01 967c478bd9Sstevel@tonic-gate #define PWD_REQUIRED 0x02 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate #define AUTH_NONE 0x00 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate #define ARGSTR "k5exEXciM:s:S:D:" 1017c478bd9Sstevel@tonic-gate #define DEFAULT_TOS 16 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate #define KRB5_PROG_NAME "krlogin" 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate #define SECURE_MSG "This rlogin session is using encryption " \ 1067c478bd9Sstevel@tonic-gate "for all data transmissions.\r\n" 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate #define KRB_V5_SENDAUTH_VERS "KRB5_SENDAUTH_V1.0" 1097c478bd9Sstevel@tonic-gate #define KRB5_RECVAUTH_V5 5 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate static krb5_error_code krb5_compat_recvauth(krb5_context context, 1127c478bd9Sstevel@tonic-gate krb5_auth_context *auth_context, 1137c478bd9Sstevel@tonic-gate krb5_pointer fdp, 1147c478bd9Sstevel@tonic-gate krb5_principal server, 1157c478bd9Sstevel@tonic-gate krb5_int32 flags, 1167c478bd9Sstevel@tonic-gate krb5_keytab keytab, 1177c478bd9Sstevel@tonic-gate krb5_ticket **ticket, 1187c478bd9Sstevel@tonic-gate krb5_int32 *auth_sys, 1197c478bd9Sstevel@tonic-gate krb5_data *version); 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate static void do_krb_login(int, char *, char *, krb5_context, int, krb5_keytab); 1227c478bd9Sstevel@tonic-gate static int configure_stream(int, krb5_keyblock *, int, krb5_data *, uint_t); 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate extern krb5_error_code krb5_read_message(krb5_context, krb5_pointer, 1257c478bd9Sstevel@tonic-gate krb5_data *); 1267c478bd9Sstevel@tonic-gate extern krb5_error_code krb5_net_read(krb5_context, int, char *, int); 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate #define LOGIN_PROGRAM "/bin/login" 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate #define DEFAULT_PROG_NAME "rlogin" 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate static const char *pam_prog_name = DEFAULT_PROG_NAME; 1337c478bd9Sstevel@tonic-gate static void rmut(void); 1347c478bd9Sstevel@tonic-gate static void doit(int, struct sockaddr_storage *, krb5_context, int, 1357c478bd9Sstevel@tonic-gate krb5_keytab); 1367c478bd9Sstevel@tonic-gate static void protocol(int, int, int); 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate static int readstream(int, char *, int); 1397c478bd9Sstevel@tonic-gate static void fatal(int, const char *); 1407c478bd9Sstevel@tonic-gate static void fatalperror(int, const char *); 1417c478bd9Sstevel@tonic-gate static int send_oob(int fd, void *ptr, size_t count); 1427c478bd9Sstevel@tonic-gate static int removemod(int f, char *modname); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate static int 1457c478bd9Sstevel@tonic-gate issock(int fd) 1467c478bd9Sstevel@tonic-gate { 1477c478bd9Sstevel@tonic-gate struct stat stats; 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate if (fstat(fd, &stats) == -1) 1507c478bd9Sstevel@tonic-gate return (0); 1517c478bd9Sstevel@tonic-gate return (S_ISSOCK(stats.st_mode)); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* 1557c478bd9Sstevel@tonic-gate * audit_rlogin_settid stores the terminal id while it is still 1567c478bd9Sstevel@tonic-gate * available. Subsequent calls to adt_load_hostname() return 1577c478bd9Sstevel@tonic-gate * the id which is stored here. 1587c478bd9Sstevel@tonic-gate */ 1597c478bd9Sstevel@tonic-gate static int 1607c478bd9Sstevel@tonic-gate audit_rlogin_settid(int fd) { 1617c478bd9Sstevel@tonic-gate adt_session_data_t *ah; 1627c478bd9Sstevel@tonic-gate adt_termid_t *termid; 1637c478bd9Sstevel@tonic-gate int rc; 1647c478bd9Sstevel@tonic-gate 1657c478bd9Sstevel@tonic-gate if ((rc = adt_start_session(&ah, NULL, 0)) == 0) { 1667c478bd9Sstevel@tonic-gate if ((rc = adt_load_termid(fd, &termid)) == 0) { 1677c478bd9Sstevel@tonic-gate if ((rc = adt_set_user(ah, ADT_NO_AUDIT, 1687c478bd9Sstevel@tonic-gate ADT_NO_AUDIT, 0, ADT_NO_AUDIT, 1697c478bd9Sstevel@tonic-gate termid, ADT_SETTID)) == 0) 1707c478bd9Sstevel@tonic-gate (void) adt_set_proc(ah); 1717c478bd9Sstevel@tonic-gate free(termid); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate (void) adt_end_session(ah); 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate return (rc); 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate /* ARGSUSED */ 180*740638c8Sbw int 1817c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1827c478bd9Sstevel@tonic-gate { 1837c478bd9Sstevel@tonic-gate int on = 1; 1847c478bd9Sstevel@tonic-gate socklen_t fromlen; 1857c478bd9Sstevel@tonic-gate struct sockaddr_storage from; 1867c478bd9Sstevel@tonic-gate int fd = -1; 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate extern char *optarg; 1897c478bd9Sstevel@tonic-gate char c; 1907c478bd9Sstevel@tonic-gate int tos = -1; 1917c478bd9Sstevel@tonic-gate krb5_context krb_context; 1927c478bd9Sstevel@tonic-gate krb5_keytab keytab = NULL; 1937c478bd9Sstevel@tonic-gate krb5_error_code status; 1947c478bd9Sstevel@tonic-gate char *realm = NULL; 1957c478bd9Sstevel@tonic-gate char *keytab_file = NULL; 1967c478bd9Sstevel@tonic-gate int encr_flag = 0; 1977c478bd9Sstevel@tonic-gate struct sockaddr_storage ouraddr; 1987c478bd9Sstevel@tonic-gate socklen_t ourlen; 1997c478bd9Sstevel@tonic-gate #ifdef DEBUG 2007c478bd9Sstevel@tonic-gate int debug_port = 0; 2017c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 2027c478bd9Sstevel@tonic-gate openlog("rlogind", LOG_PID | LOG_ODELAY, LOG_DAEMON); 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, ARGSTR)) != -1) { 2057c478bd9Sstevel@tonic-gate switch (c) { 2067c478bd9Sstevel@tonic-gate case 'k': 2077c478bd9Sstevel@tonic-gate case '5': 2087c478bd9Sstevel@tonic-gate use_auth = KRB5_RECVAUTH_V5; 2097c478bd9Sstevel@tonic-gate break; 2107c478bd9Sstevel@tonic-gate case 'e': 2117c478bd9Sstevel@tonic-gate case 'E': 2127c478bd9Sstevel@tonic-gate case 'x': 2137c478bd9Sstevel@tonic-gate case 'X': 2147c478bd9Sstevel@tonic-gate encr_flag = 1; 2157c478bd9Sstevel@tonic-gate break; 2167c478bd9Sstevel@tonic-gate case 'M': 2177c478bd9Sstevel@tonic-gate realm = (char *)strdup(optarg); 2187c478bd9Sstevel@tonic-gate break; 2197c478bd9Sstevel@tonic-gate case 'S': 2207c478bd9Sstevel@tonic-gate keytab_file = (char *)strdup(optarg); 2217c478bd9Sstevel@tonic-gate break; 2227c478bd9Sstevel@tonic-gate case 'c': 2237c478bd9Sstevel@tonic-gate chksum_flag |= CHKSUM_REQUIRED; 2247c478bd9Sstevel@tonic-gate break; 2257c478bd9Sstevel@tonic-gate case 'i': 2267c478bd9Sstevel@tonic-gate chksum_flag |= CHKSUM_IGNORED; 2277c478bd9Sstevel@tonic-gate break; 2287c478bd9Sstevel@tonic-gate case 's': 2297c478bd9Sstevel@tonic-gate if (optarg == NULL || (tos = atoi(optarg)) < 0 || 2307c478bd9Sstevel@tonic-gate tos > 255) { 2317c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "%s: illegal tos value: " 2327c478bd9Sstevel@tonic-gate "%s\n", argv[0], optarg); 2337c478bd9Sstevel@tonic-gate } else { 2347c478bd9Sstevel@tonic-gate if (tos < 0) 2357c478bd9Sstevel@tonic-gate tos = DEFAULT_TOS; 2367c478bd9Sstevel@tonic-gate } 2377c478bd9Sstevel@tonic-gate break; 2387c478bd9Sstevel@tonic-gate #ifdef DEBUG 2397c478bd9Sstevel@tonic-gate case 'D': 2407c478bd9Sstevel@tonic-gate debug_port = atoi(optarg); 2417c478bd9Sstevel@tonic-gate break; 2427c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 2437c478bd9Sstevel@tonic-gate default: 2447c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Unrecognized command line option " 2457c478bd9Sstevel@tonic-gate "(%s), exiting", argv[optind]); 2467c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate if (use_auth == KRB5_RECVAUTH_V5) { 2507c478bd9Sstevel@tonic-gate status = krb5_init_context(&krb_context); 2517c478bd9Sstevel@tonic-gate if (status) { 2527c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Error initializing krb5: %s", 2537c478bd9Sstevel@tonic-gate error_message(status)); 2547c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 2557c478bd9Sstevel@tonic-gate } 2567c478bd9Sstevel@tonic-gate if (realm != NULL) 2577c478bd9Sstevel@tonic-gate krb5_set_default_realm(krb_context, realm); 2587c478bd9Sstevel@tonic-gate if (keytab_file != NULL) { 2597c478bd9Sstevel@tonic-gate if ((status = krb5_kt_resolve(krb_context, 2607c478bd9Sstevel@tonic-gate keytab_file, 2617c478bd9Sstevel@tonic-gate &keytab))) { 2627c478bd9Sstevel@tonic-gate com_err(argv[0], 2637c478bd9Sstevel@tonic-gate status, 2647c478bd9Sstevel@tonic-gate "while resolving srvtab file %s", 2657c478bd9Sstevel@tonic-gate keytab_file); 2667c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate } 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate #ifdef DEBUG 2727c478bd9Sstevel@tonic-gate if (debug_port) { 2737c478bd9Sstevel@tonic-gate int s; 2747c478bd9Sstevel@tonic-gate struct sockaddr_in sin; 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate if ((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { 2777c478bd9Sstevel@tonic-gate fatalperror(STDERR_FILENO, "Error in socket"); 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate (void) memset((char *)&sin, 0, sizeof (sin)); 2817c478bd9Sstevel@tonic-gate sin.sin_family = AF_INET; 2827c478bd9Sstevel@tonic-gate sin.sin_port = htons(debug_port); 2837c478bd9Sstevel@tonic-gate sin.sin_addr.s_addr = INADDR_ANY; 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, 2867c478bd9Sstevel@tonic-gate (char *)&on, sizeof (on)); 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate if ((bind(s, (struct sockaddr *)&sin, sizeof (sin))) < 0) { 2897c478bd9Sstevel@tonic-gate fatalperror(STDERR_FILENO, "bind error"); 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate if ((listen(s, 5)) < 0) { 2937c478bd9Sstevel@tonic-gate fatalperror(STDERR_FILENO, "listen error"); 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate fromlen = sizeof (from); 2977c478bd9Sstevel@tonic-gate if ((fd = accept(s, (struct sockaddr *)&from, &fromlen)) < 0) { 2987c478bd9Sstevel@tonic-gate fatalperror(STDERR_FILENO, "accept error"); 2997c478bd9Sstevel@tonic-gate } 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate (void) close(s); 3027c478bd9Sstevel@tonic-gate } else 3037c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 3047c478bd9Sstevel@tonic-gate { 3057c478bd9Sstevel@tonic-gate if (!issock(STDIN_FILENO)) 3067c478bd9Sstevel@tonic-gate fatal(STDIN_FILENO, 3077c478bd9Sstevel@tonic-gate "stdin is not a socket file descriptor"); 3087c478bd9Sstevel@tonic-gate fd = STDIN_FILENO; 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate fromlen = sizeof (from); 3127c478bd9Sstevel@tonic-gate if (getpeername(fd, (struct sockaddr *)&from, &fromlen) < 0) 3137c478bd9Sstevel@tonic-gate fatalperror(STDERR_FILENO, "getpeername"); 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate if (audit_rlogin_settid(fd)) /* set terminal ID */ 3167c478bd9Sstevel@tonic-gate fatalperror(STDERR_FILENO, "audit"); 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, 3197c478bd9Sstevel@tonic-gate sizeof (on)) < 0) 3207c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, "setsockopt(SO_KEEPALIVE): %m"); 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate if (!VALID_CHKSUM(chksum_flag)) { 3237c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Configuration error: mutually exclusive " 3247c478bd9Sstevel@tonic-gate "options specified (-c and -i)"); 3257c478bd9Sstevel@tonic-gate fatal(fd, "Checksums are required and ignored (-c and -i);" 3267c478bd9Sstevel@tonic-gate "these options are mutually exclusive - check " 3277c478bd9Sstevel@tonic-gate "the documentation."); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate ourlen = sizeof (ouraddr); 3307c478bd9Sstevel@tonic-gate if (getsockname(fd, (struct sockaddr *)&ouraddr, &ourlen) == -1) { 3317c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "getsockname error: %m"); 3327c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate if (tos != -1 && 3367c478bd9Sstevel@tonic-gate ouraddr.ss_family != AF_INET6 && 3377c478bd9Sstevel@tonic-gate setsockopt(fd, IPPROTO_IP, IP_TOS, (char *)&tos, 3387c478bd9Sstevel@tonic-gate sizeof (tos)) < 0 && 3397c478bd9Sstevel@tonic-gate errno != ENOPROTOOPT) { 3407c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "setsockopt(IP_TOS %d): %m", tos); 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate doit(fd, &from, krb_context, encr_flag, keytab); 343*740638c8Sbw return (0); 3447c478bd9Sstevel@tonic-gate } 3457c478bd9Sstevel@tonic-gate 3467c478bd9Sstevel@tonic-gate static void cleanup(int); 3477c478bd9Sstevel@tonic-gate static int nsize = 0; /* bytes read prior to pushing rlmod */ 3487c478bd9Sstevel@tonic-gate static char *rlbuf; /* buffer where nbytes are read to */ 3497c478bd9Sstevel@tonic-gate static char *line; 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate static struct winsize win = { 0, 0, 0, 0 }; 3527c478bd9Sstevel@tonic-gate static pid_t pid; 3537c478bd9Sstevel@tonic-gate static char hostname[MAXHOSTNAMELEN + 1]; 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate static void 3567c478bd9Sstevel@tonic-gate getstr(int f, char *buf, int cnt, char *err) 3577c478bd9Sstevel@tonic-gate { 3587c478bd9Sstevel@tonic-gate char c; 3597c478bd9Sstevel@tonic-gate do { 3607c478bd9Sstevel@tonic-gate if (read(f, &c, 1) != 1 || (--cnt < 0)) { 3617c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Error reading \'%s\' field", err); 3627c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate *buf++ = c; 3657c478bd9Sstevel@tonic-gate } while (c != '\0'); 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate static krb5_error_code 3697c478bd9Sstevel@tonic-gate recvauth(int f, 3707c478bd9Sstevel@tonic-gate krb5_context krb_context, 3717c478bd9Sstevel@tonic-gate unsigned int *valid_checksum, 3727c478bd9Sstevel@tonic-gate krb5_ticket **ticket, 3737c478bd9Sstevel@tonic-gate int *auth_type, 3747c478bd9Sstevel@tonic-gate krb5_principal *client, 3757c478bd9Sstevel@tonic-gate int encr_flag, 3767c478bd9Sstevel@tonic-gate krb5_keytab keytab) 3777c478bd9Sstevel@tonic-gate { 3787c478bd9Sstevel@tonic-gate krb5_error_code status = 0; 3797c478bd9Sstevel@tonic-gate krb5_auth_context auth_context = NULL; 3807c478bd9Sstevel@tonic-gate krb5_rcache rcache; 3817c478bd9Sstevel@tonic-gate krb5_authenticator *authenticator; 3827c478bd9Sstevel@tonic-gate krb5_data inbuf; 3837c478bd9Sstevel@tonic-gate krb5_data auth_version; 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate *valid_checksum = 0; 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate if ((status = krb5_auth_con_init(krb_context, &auth_context))) 3887c478bd9Sstevel@tonic-gate return (status); 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate /* Only need remote address for rd_cred() to verify client */ 3917c478bd9Sstevel@tonic-gate if ((status = krb5_auth_con_genaddrs(krb_context, auth_context, f, 3927c478bd9Sstevel@tonic-gate KRB5_AUTH_CONTEXT_GENERATE_REMOTE_FULL_ADDR))) 3937c478bd9Sstevel@tonic-gate return (status); 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate status = krb5_auth_con_getrcache(krb_context, auth_context, &rcache); 3967c478bd9Sstevel@tonic-gate if (status) 3977c478bd9Sstevel@tonic-gate return (status); 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate if (!rcache) { 4007c478bd9Sstevel@tonic-gate krb5_principal server; 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate status = krb5_sname_to_principal(krb_context, 0, 0, 4037c478bd9Sstevel@tonic-gate KRB5_NT_SRV_HST, &server); 4047c478bd9Sstevel@tonic-gate if (status) 4057c478bd9Sstevel@tonic-gate return (status); 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate status = krb5_get_server_rcache(krb_context, 4087c478bd9Sstevel@tonic-gate krb5_princ_component(krb_context, server, 0), 4097c478bd9Sstevel@tonic-gate &rcache); 4107c478bd9Sstevel@tonic-gate krb5_free_principal(krb_context, server); 4117c478bd9Sstevel@tonic-gate if (status) 4127c478bd9Sstevel@tonic-gate return (status); 4137c478bd9Sstevel@tonic-gate 4147c478bd9Sstevel@tonic-gate status = krb5_auth_con_setrcache(krb_context, auth_context, 4157c478bd9Sstevel@tonic-gate rcache); 4167c478bd9Sstevel@tonic-gate if (status) 4177c478bd9Sstevel@tonic-gate return (status); 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate if ((status = krb5_compat_recvauth(krb_context, 4207c478bd9Sstevel@tonic-gate &auth_context, 4217c478bd9Sstevel@tonic-gate &f, 4227c478bd9Sstevel@tonic-gate NULL, /* Specify daemon principal */ 4237c478bd9Sstevel@tonic-gate 0, /* no flags */ 4247c478bd9Sstevel@tonic-gate keytab, /* NULL to use v5srvtab */ 4257c478bd9Sstevel@tonic-gate ticket, /* return ticket */ 4267c478bd9Sstevel@tonic-gate auth_type, /* authentication system */ 4277c478bd9Sstevel@tonic-gate &auth_version))) { 4287c478bd9Sstevel@tonic-gate if (*auth_type == KRB5_RECVAUTH_V5) { 4297c478bd9Sstevel@tonic-gate /* 4307c478bd9Sstevel@tonic-gate * clean up before exiting 4317c478bd9Sstevel@tonic-gate */ 4327c478bd9Sstevel@tonic-gate getstr(f, rusername, sizeof (rusername), "remuser"); 4337c478bd9Sstevel@tonic-gate getstr(f, lusername, sizeof (lusername), "locuser"); 4347c478bd9Sstevel@tonic-gate getstr(f, term, sizeof (term), "Terminal type"); 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate return (status); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate getstr(f, lusername, sizeof (lusername), "locuser"); 4407c478bd9Sstevel@tonic-gate getstr(f, term, sizeof (term), "Terminal type"); 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate kcmd_protocol = KCMD_UNKNOWN_PROTOCOL; 4437c478bd9Sstevel@tonic-gate if (auth_version.length != 9 || auth_version.data == NULL) { 4447c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Bad application protocol version length in " 4457c478bd9Sstevel@tonic-gate "KRB5 exchange, exiting"); 4467c478bd9Sstevel@tonic-gate fatal(f, "Bad application version length, exiting."); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate /* 4497c478bd9Sstevel@tonic-gate * Determine which Kerberos CMD protocol was used. 4507c478bd9Sstevel@tonic-gate */ 4517c478bd9Sstevel@tonic-gate if (strncmp(auth_version.data, "KCMDV0.1", 9) == 0) { 4527c478bd9Sstevel@tonic-gate kcmd_protocol = KCMD_OLD_PROTOCOL; 4537c478bd9Sstevel@tonic-gate } else if (strncmp(auth_version.data, "KCMDV0.2", 9) == 0) { 4547c478bd9Sstevel@tonic-gate kcmd_protocol = KCMD_NEW_PROTOCOL; 4557c478bd9Sstevel@tonic-gate } else { 4567c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Unrecognized KCMD protocol (%s), exiting", 4577c478bd9Sstevel@tonic-gate (char *)auth_version.data); 4587c478bd9Sstevel@tonic-gate fatal(f, "Unrecognized KCMD protocol, exiting"); 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate 4617c478bd9Sstevel@tonic-gate if ((*auth_type == KRB5_RECVAUTH_V5) && chksum_flag && 4627c478bd9Sstevel@tonic-gate kcmd_protocol == KCMD_OLD_PROTOCOL) { 4637c478bd9Sstevel@tonic-gate if ((status = krb5_auth_con_getauthenticator(krb_context, 4647c478bd9Sstevel@tonic-gate auth_context, 4657c478bd9Sstevel@tonic-gate &authenticator))) 4667c478bd9Sstevel@tonic-gate return (status); 4677c478bd9Sstevel@tonic-gate if (authenticator->checksum) { 4687c478bd9Sstevel@tonic-gate struct sockaddr_storage adr; 4697c478bd9Sstevel@tonic-gate int adr_length = sizeof (adr); 4707c478bd9Sstevel@tonic-gate int buflen; 4717c478bd9Sstevel@tonic-gate krb5_data input; 4727c478bd9Sstevel@tonic-gate krb5_keyblock key; 4737c478bd9Sstevel@tonic-gate char *chksumbuf; 4747c478bd9Sstevel@tonic-gate 4757c478bd9Sstevel@tonic-gate /* 4767c478bd9Sstevel@tonic-gate * Define the lenght of the chksum buffer. 4777c478bd9Sstevel@tonic-gate * chksum string = "[portnum]:termstr:username" 4787c478bd9Sstevel@tonic-gate * The extra 32 is to hold a integer string for 4797c478bd9Sstevel@tonic-gate * the portnumber. 4807c478bd9Sstevel@tonic-gate */ 4817c478bd9Sstevel@tonic-gate buflen = strlen(term) + strlen(lusername) + 32; 4827c478bd9Sstevel@tonic-gate chksumbuf = (char *)malloc(buflen); 4837c478bd9Sstevel@tonic-gate if (chksumbuf == 0) { 4847c478bd9Sstevel@tonic-gate krb5_free_authenticator(krb_context, 4857c478bd9Sstevel@tonic-gate authenticator); 4867c478bd9Sstevel@tonic-gate fatal(f, "Out of memory error"); 4877c478bd9Sstevel@tonic-gate } 4887c478bd9Sstevel@tonic-gate 4897c478bd9Sstevel@tonic-gate if (getsockname(f, (struct sockaddr *)&adr, 4907c478bd9Sstevel@tonic-gate &adr_length) != 0) { 4917c478bd9Sstevel@tonic-gate krb5_free_authenticator(krb_context, 4927c478bd9Sstevel@tonic-gate authenticator); 4937c478bd9Sstevel@tonic-gate fatal(f, "getsockname error"); 4947c478bd9Sstevel@tonic-gate } 4957c478bd9Sstevel@tonic-gate 4967c478bd9Sstevel@tonic-gate (void) snprintf(chksumbuf, buflen, 4977c478bd9Sstevel@tonic-gate "%u:%s%s", 4987c478bd9Sstevel@tonic-gate ntohs(SOCK_PORT(adr)), 4997c478bd9Sstevel@tonic-gate term, lusername); 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate input.data = chksumbuf; 5027c478bd9Sstevel@tonic-gate input.length = strlen(chksumbuf); 5037c478bd9Sstevel@tonic-gate key.contents = (*ticket)->enc_part2->session->contents; 5047c478bd9Sstevel@tonic-gate key.length = (*ticket)->enc_part2->session->length; 5057c478bd9Sstevel@tonic-gate status = krb5_c_verify_checksum(krb_context, 5067c478bd9Sstevel@tonic-gate &key, 0, 5077c478bd9Sstevel@tonic-gate &input, 5087c478bd9Sstevel@tonic-gate authenticator->checksum, 5097c478bd9Sstevel@tonic-gate valid_checksum); 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate if (status == 0 && *valid_checksum == 0) 5127c478bd9Sstevel@tonic-gate status = KRB5KRB_AP_ERR_BAD_INTEGRITY; 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate if (chksumbuf) 5157c478bd9Sstevel@tonic-gate krb5_xfree(chksumbuf); 5167c478bd9Sstevel@tonic-gate if (status) { 5177c478bd9Sstevel@tonic-gate krb5_free_authenticator(krb_context, 5187c478bd9Sstevel@tonic-gate authenticator); 5197c478bd9Sstevel@tonic-gate return (status); 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate } 5227c478bd9Sstevel@tonic-gate krb5_free_authenticator(krb_context, authenticator); 5237c478bd9Sstevel@tonic-gate } 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate if ((status = krb5_copy_principal(krb_context, 5267c478bd9Sstevel@tonic-gate (*ticket)->enc_part2->client, 5277c478bd9Sstevel@tonic-gate client))) 5287c478bd9Sstevel@tonic-gate return (status); 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate /* Get the Unix username of the remote user */ 5317c478bd9Sstevel@tonic-gate getstr(f, rusername, sizeof (rusername), "remuser"); 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate /* Get the Kerberos principal name string of the remote user */ 5347c478bd9Sstevel@tonic-gate if ((status = krb5_unparse_name(krb_context, *client, &krusername))) 5357c478bd9Sstevel@tonic-gate return (status); 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate #ifdef DEBUG 5387c478bd9Sstevel@tonic-gate syslog(LOG_DEBUG | LOG_AUTH, "rlogind: got krb5 credentials for %s", 5397c478bd9Sstevel@tonic-gate (krusername != NULL ? krusername : "<unknown>")); 5407c478bd9Sstevel@tonic-gate #endif 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate if (encr_flag) { 5437c478bd9Sstevel@tonic-gate status = krb5_auth_con_getremotesubkey(krb_context, 5447c478bd9Sstevel@tonic-gate auth_context, 5457c478bd9Sstevel@tonic-gate &session_key); 5467c478bd9Sstevel@tonic-gate if (status) { 5477c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Error getting KRB5 session " 5487c478bd9Sstevel@tonic-gate "subkey, exiting"); 5497c478bd9Sstevel@tonic-gate fatal(f, "Error getting KRB5 session subkey, exiting"); 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate /* 5527c478bd9Sstevel@tonic-gate * The "new" protocol requires that a subkey be sent. 5537c478bd9Sstevel@tonic-gate */ 5547c478bd9Sstevel@tonic-gate if (session_key == NULL && 5557c478bd9Sstevel@tonic-gate kcmd_protocol == KCMD_NEW_PROTOCOL) { 5567c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "No KRB5 session subkey sent, exiting"); 5577c478bd9Sstevel@tonic-gate fatal(f, "No KRB5 session subkey sent, exiting"); 5587c478bd9Sstevel@tonic-gate } 5597c478bd9Sstevel@tonic-gate /* 5607c478bd9Sstevel@tonic-gate * The "old" protocol does not permit an authenticator subkey. 5617c478bd9Sstevel@tonic-gate * The key is taken from the ticket instead (see below). 5627c478bd9Sstevel@tonic-gate */ 5637c478bd9Sstevel@tonic-gate if (session_key != NULL && 5647c478bd9Sstevel@tonic-gate kcmd_protocol == KCMD_OLD_PROTOCOL) { 5657c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "KRB5 session subkey not permitted " 5667c478bd9Sstevel@tonic-gate "with old KCMD protocol, exiting"); 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate fatal(f, "KRB5 session subkey not permitted " 5697c478bd9Sstevel@tonic-gate "with old KCMD protocol, exiting"); 5707c478bd9Sstevel@tonic-gate } 5717c478bd9Sstevel@tonic-gate /* 5727c478bd9Sstevel@tonic-gate * If no key at this point, use the session key from 5737c478bd9Sstevel@tonic-gate * the ticket. 5747c478bd9Sstevel@tonic-gate */ 5757c478bd9Sstevel@tonic-gate if (session_key == NULL) { 5767c478bd9Sstevel@tonic-gate /* 5777c478bd9Sstevel@tonic-gate * Save the session key so we can configure the crypto 5787c478bd9Sstevel@tonic-gate * module later. 5797c478bd9Sstevel@tonic-gate */ 5807c478bd9Sstevel@tonic-gate status = krb5_copy_keyblock(krb_context, 5817c478bd9Sstevel@tonic-gate (*ticket)->enc_part2->session, 5827c478bd9Sstevel@tonic-gate &session_key); 5837c478bd9Sstevel@tonic-gate if (status) { 5847c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "krb5_copy_keyblock failed"); 5857c478bd9Sstevel@tonic-gate fatal(f, "krb5_copy_keyblock failed"); 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate /* 5897c478bd9Sstevel@tonic-gate * If session key still cannot be found, we must 5907c478bd9Sstevel@tonic-gate * exit because encryption is required here 5917c478bd9Sstevel@tonic-gate * when encr_flag (-x) is set. 5927c478bd9Sstevel@tonic-gate */ 5937c478bd9Sstevel@tonic-gate if (session_key == NULL) { 5947c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Could not find an encryption key," 5957c478bd9Sstevel@tonic-gate "exiting"); 5967c478bd9Sstevel@tonic-gate fatal(f, "Encryption required but key not found, " 5977c478bd9Sstevel@tonic-gate "exiting"); 5987c478bd9Sstevel@tonic-gate } 5997c478bd9Sstevel@tonic-gate } 6007c478bd9Sstevel@tonic-gate /* 6017c478bd9Sstevel@tonic-gate * Use krb5_read_message to read the principal stuff. 6027c478bd9Sstevel@tonic-gate */ 6037c478bd9Sstevel@tonic-gate if ((status = krb5_read_message(krb_context, (krb5_pointer)&f, 6047c478bd9Sstevel@tonic-gate &inbuf))) 6057c478bd9Sstevel@tonic-gate fatal(f, "Error reading krb5 message"); 6067c478bd9Sstevel@tonic-gate 6077c478bd9Sstevel@tonic-gate if ((inbuf.length) && /* Forwarding being done, read creds */ 6087c478bd9Sstevel@tonic-gate (status = rd_and_store_for_creds(krb_context, auth_context, 6097c478bd9Sstevel@tonic-gate &inbuf, *ticket, lusername, 6107c478bd9Sstevel@tonic-gate &ccache))) { 6117c478bd9Sstevel@tonic-gate if (rcache) 6127c478bd9Sstevel@tonic-gate (void) krb5_rc_close(krb_context, rcache); 6137c478bd9Sstevel@tonic-gate fatal(f, "Can't get forwarded credentials"); 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate if (rcache) 6167c478bd9Sstevel@tonic-gate (void) krb5_rc_close(krb_context, rcache); 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate return (status); 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate static void 6227c478bd9Sstevel@tonic-gate do_krb_login(int f, char *host_addr, char *hostname, 6237c478bd9Sstevel@tonic-gate krb5_context krb_context, int encr_flag, 6247c478bd9Sstevel@tonic-gate krb5_keytab keytab) 6257c478bd9Sstevel@tonic-gate { 6267c478bd9Sstevel@tonic-gate krb5_error_code status; 6277c478bd9Sstevel@tonic-gate uint_t valid_checksum; 6287c478bd9Sstevel@tonic-gate krb5_ticket *ticket = NULL; 6297c478bd9Sstevel@tonic-gate int auth_sys = 0; 6307c478bd9Sstevel@tonic-gate int auth_sent = 0; 6317c478bd9Sstevel@tonic-gate krb5_principal client = NULL; 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate if (getuid()) 6347c478bd9Sstevel@tonic-gate fatal(f, "Error authorizing KRB5 connection, " 6357c478bd9Sstevel@tonic-gate "server lacks privilege"); 6367c478bd9Sstevel@tonic-gate 6377c478bd9Sstevel@tonic-gate status = recvauth(f, krb_context, &valid_checksum, &ticket, 6387c478bd9Sstevel@tonic-gate &auth_sys, &client, encr_flag, keytab); 6397c478bd9Sstevel@tonic-gate if (status) { 6407c478bd9Sstevel@tonic-gate if (ticket) 6417c478bd9Sstevel@tonic-gate krb5_free_ticket(krb_context, ticket); 6427c478bd9Sstevel@tonic-gate if (status != 255) 6437c478bd9Sstevel@tonic-gate syslog(LOG_ERR, 6447c478bd9Sstevel@tonic-gate "Authentication failed from %s(%s): %s\n", 6457c478bd9Sstevel@tonic-gate host_addr, hostname, error_message(status)); 6467c478bd9Sstevel@tonic-gate fatal(f, "Kerberos authentication failed, exiting"); 6477c478bd9Sstevel@tonic-gate } 6487c478bd9Sstevel@tonic-gate 6497c478bd9Sstevel@tonic-gate if (auth_sys != KRB5_RECVAUTH_V5) { 6507c478bd9Sstevel@tonic-gate fatal(f, "This server only supports Kerberos V5"); 6517c478bd9Sstevel@tonic-gate } else { 6527c478bd9Sstevel@tonic-gate /* 6537c478bd9Sstevel@tonic-gate * Authenticated OK, now check authorization. 6547c478bd9Sstevel@tonic-gate */ 6557c478bd9Sstevel@tonic-gate if (client && krb5_kuserok(krb_context, client, lusername)) 6567c478bd9Sstevel@tonic-gate auth_sent = KRB5_RECVAUTH_V5; 6577c478bd9Sstevel@tonic-gate } 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate if (auth_sent == KRB5_RECVAUTH_V5 && 6607c478bd9Sstevel@tonic-gate kcmd_protocol == KCMD_OLD_PROTOCOL && 6617c478bd9Sstevel@tonic-gate chksum_flag == CHKSUM_REQUIRED && !valid_checksum) { 6627c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Client did not supply required checksum, " 6637c478bd9Sstevel@tonic-gate "connection rejected."); 6647c478bd9Sstevel@tonic-gate fatal(f, "Client did not supply required checksum, " 6657c478bd9Sstevel@tonic-gate "connection rejected."); 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate 6687c478bd9Sstevel@tonic-gate if (auth_sys != auth_sent) { 6697c478bd9Sstevel@tonic-gate char *msg_fail = NULL; 6707c478bd9Sstevel@tonic-gate int msgsize = 0; 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate if (ticket) 6737c478bd9Sstevel@tonic-gate krb5_free_ticket(krb_context, ticket); 6747c478bd9Sstevel@tonic-gate 6757c478bd9Sstevel@tonic-gate if (krusername != NULL) { 6767c478bd9Sstevel@tonic-gate /* 6777c478bd9Sstevel@tonic-gate * msgsize must be enough to hold 6787c478bd9Sstevel@tonic-gate * krusername, lusername and a brief 6797c478bd9Sstevel@tonic-gate * message describing the failure. 6807c478bd9Sstevel@tonic-gate */ 6817c478bd9Sstevel@tonic-gate msgsize = strlen(krusername) + 6827c478bd9Sstevel@tonic-gate strlen(lusername) + 80; 6837c478bd9Sstevel@tonic-gate msg_fail = (char *)malloc(msgsize); 6847c478bd9Sstevel@tonic-gate } 6857c478bd9Sstevel@tonic-gate if (msg_fail == NULL) { 6867c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "User is not authorized to login to " 6877c478bd9Sstevel@tonic-gate "specified account"); 6887c478bd9Sstevel@tonic-gate 6897c478bd9Sstevel@tonic-gate fatal(f, "User is not authorized to login to " 6907c478bd9Sstevel@tonic-gate "specified account"); 6917c478bd9Sstevel@tonic-gate } 6927c478bd9Sstevel@tonic-gate if (auth_sent != 0) 6937c478bd9Sstevel@tonic-gate (void) snprintf(msg_fail, msgsize, 6947c478bd9Sstevel@tonic-gate "Access denied because of improper " 6957c478bd9Sstevel@tonic-gate "KRB5 credentials"); 6967c478bd9Sstevel@tonic-gate else 6977c478bd9Sstevel@tonic-gate (void) snprintf(msg_fail, msgsize, 6987c478bd9Sstevel@tonic-gate "User %s is not authorized to login " 6997c478bd9Sstevel@tonic-gate "to account %s", 7007c478bd9Sstevel@tonic-gate krusername, lusername); 7017c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "%s", msg_fail); 7027c478bd9Sstevel@tonic-gate fatal(f, msg_fail); 7037c478bd9Sstevel@tonic-gate } 7047c478bd9Sstevel@tonic-gate } 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate /* 7077c478bd9Sstevel@tonic-gate * stop_stream 7087c478bd9Sstevel@tonic-gate * 7097c478bd9Sstevel@tonic-gate * Utility routine to send a CRYPTIOCSTOP ioctl to the 7107c478bd9Sstevel@tonic-gate * crypto module(cryptmod). 7117c478bd9Sstevel@tonic-gate */ 7127c478bd9Sstevel@tonic-gate static void 7137c478bd9Sstevel@tonic-gate stop_stream(int fd, int dir) 7147c478bd9Sstevel@tonic-gate { 7157c478bd9Sstevel@tonic-gate struct strioctl crioc; 7167c478bd9Sstevel@tonic-gate uint32_t stopdir = dir; 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate crioc.ic_cmd = CRYPTIOCSTOP; 7197c478bd9Sstevel@tonic-gate crioc.ic_timout = -1; 7207c478bd9Sstevel@tonic-gate crioc.ic_len = sizeof (stopdir); 7217c478bd9Sstevel@tonic-gate crioc.ic_dp = (char *)&stopdir; 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &crioc)) 7247c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Error sending CRYPTIOCSTOP ioctl: %m"); 7257c478bd9Sstevel@tonic-gate } 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate /* 7287c478bd9Sstevel@tonic-gate * start_stream 7297c478bd9Sstevel@tonic-gate * 7307c478bd9Sstevel@tonic-gate * Utility routine to send a CRYPTIOCSTART ioctl to the 7317c478bd9Sstevel@tonic-gate * crypto module(cryptmod). This routine may contain optional 7327c478bd9Sstevel@tonic-gate * payload data that the cryptmod will interpret as bytes that 7337c478bd9Sstevel@tonic-gate * need to be decrypted and sent back up to the application 7347c478bd9Sstevel@tonic-gate * via the data stream. 7357c478bd9Sstevel@tonic-gate */ 7367c478bd9Sstevel@tonic-gate static void 7377c478bd9Sstevel@tonic-gate start_stream(int fd, int dir) 7387c478bd9Sstevel@tonic-gate { 7397c478bd9Sstevel@tonic-gate struct strioctl crioc; 7407c478bd9Sstevel@tonic-gate uint32_t iocval; 7417c478bd9Sstevel@tonic-gate size_t datalen = 0; 7427c478bd9Sstevel@tonic-gate char *data = NULL; 7437c478bd9Sstevel@tonic-gate 7447c478bd9Sstevel@tonic-gate if (dir == CRYPT_DECRYPT) { 7457c478bd9Sstevel@tonic-gate iocval = CRYPTIOCSTARTDEC; 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate /* Look for data not yet processed */ 7487c478bd9Sstevel@tonic-gate if (ioctl(fd, I_NREAD, &datalen) < 0) { 7497c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "I_NREAD returned error %m"); 7507c478bd9Sstevel@tonic-gate datalen = 0; 7517c478bd9Sstevel@tonic-gate } else { 7527c478bd9Sstevel@tonic-gate if (datalen > 0) { 7537c478bd9Sstevel@tonic-gate data = malloc(datalen); 7547c478bd9Sstevel@tonic-gate if (data != NULL) { 7557c478bd9Sstevel@tonic-gate int nbytes = read(fd, data, datalen); 7567c478bd9Sstevel@tonic-gate datalen = nbytes; 7577c478bd9Sstevel@tonic-gate } else { 7587c478bd9Sstevel@tonic-gate syslog(LOG_ERR, 7597c478bd9Sstevel@tonic-gate "malloc error (%d bytes)", 7607c478bd9Sstevel@tonic-gate datalen); 7617c478bd9Sstevel@tonic-gate datalen = 0; 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate } else { 7647c478bd9Sstevel@tonic-gate datalen = 0; 7657c478bd9Sstevel@tonic-gate } 7667c478bd9Sstevel@tonic-gate } 7677c478bd9Sstevel@tonic-gate } else { 7687c478bd9Sstevel@tonic-gate iocval = CRYPTIOCSTARTENC; 7697c478bd9Sstevel@tonic-gate } 7707c478bd9Sstevel@tonic-gate 7717c478bd9Sstevel@tonic-gate crioc.ic_cmd = iocval; 7727c478bd9Sstevel@tonic-gate crioc.ic_timout = -1; 7737c478bd9Sstevel@tonic-gate crioc.ic_len = datalen; 7747c478bd9Sstevel@tonic-gate crioc.ic_dp = data; 7757c478bd9Sstevel@tonic-gate 7767c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &crioc)) 7777c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Error sending CRYPTIOCSTART ioctl: %m"); 7787c478bd9Sstevel@tonic-gate 7797c478bd9Sstevel@tonic-gate if (data != NULL) 7807c478bd9Sstevel@tonic-gate free(data); 7817c478bd9Sstevel@tonic-gate } 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate static int 7847c478bd9Sstevel@tonic-gate configure_stream(int fd, krb5_keyblock *skey, int dir, krb5_data *ivec, 7857c478bd9Sstevel@tonic-gate uint_t iv_usage) 7867c478bd9Sstevel@tonic-gate { 7877c478bd9Sstevel@tonic-gate struct cr_info_t setup_info; 7887c478bd9Sstevel@tonic-gate struct strioctl crioc; 7897c478bd9Sstevel@tonic-gate int retval = 0; 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate switch (skey->enctype) { 7927c478bd9Sstevel@tonic-gate case ENCTYPE_DES_CBC_CRC: 7937c478bd9Sstevel@tonic-gate setup_info.crypto_method = CRYPT_METHOD_DES_CBC_CRC; 7947c478bd9Sstevel@tonic-gate break; 7957c478bd9Sstevel@tonic-gate case ENCTYPE_DES_CBC_MD5: 7967c478bd9Sstevel@tonic-gate setup_info.crypto_method = CRYPT_METHOD_DES_CBC_MD5; 7977c478bd9Sstevel@tonic-gate break; 7987c478bd9Sstevel@tonic-gate case ENCTYPE_DES_CBC_RAW: 7997c478bd9Sstevel@tonic-gate setup_info.crypto_method = CRYPT_METHOD_DES_CBC_NULL; 8007c478bd9Sstevel@tonic-gate break; 8017c478bd9Sstevel@tonic-gate case ENCTYPE_DES3_CBC_SHA1: 8027c478bd9Sstevel@tonic-gate setup_info.crypto_method = CRYPT_METHOD_DES3_CBC_SHA1; 8037c478bd9Sstevel@tonic-gate break; 8047c478bd9Sstevel@tonic-gate case ENCTYPE_ARCFOUR_HMAC: 8057c478bd9Sstevel@tonic-gate setup_info.crypto_method = CRYPT_METHOD_ARCFOUR_HMAC_MD5; 8067c478bd9Sstevel@tonic-gate break; 8077c478bd9Sstevel@tonic-gate case ENCTYPE_ARCFOUR_HMAC_EXP: 8087c478bd9Sstevel@tonic-gate setup_info.crypto_method = CRYPT_METHOD_ARCFOUR_HMAC_MD5_EXP; 8097c478bd9Sstevel@tonic-gate break; 8107c478bd9Sstevel@tonic-gate case ENCTYPE_AES128_CTS_HMAC_SHA1_96: 8117c478bd9Sstevel@tonic-gate setup_info.crypto_method = CRYPT_METHOD_AES128; 8127c478bd9Sstevel@tonic-gate break; 8137c478bd9Sstevel@tonic-gate case ENCTYPE_AES256_CTS_HMAC_SHA1_96: 8147c478bd9Sstevel@tonic-gate setup_info.crypto_method = CRYPT_METHOD_AES256; 8157c478bd9Sstevel@tonic-gate break; 8167c478bd9Sstevel@tonic-gate default: 8177c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Enctype in kerberos session key " 8187c478bd9Sstevel@tonic-gate "is not supported by crypto module(%d)", 8197c478bd9Sstevel@tonic-gate skey->enctype); 8207c478bd9Sstevel@tonic-gate return (-1); 8217c478bd9Sstevel@tonic-gate } 8227c478bd9Sstevel@tonic-gate if (ivec == NULL || ivec->length == 0) { 8237c478bd9Sstevel@tonic-gate (void) memset(&setup_info.ivec, 0, sizeof (setup_info.ivec)); 8247c478bd9Sstevel@tonic-gate 8257c478bd9Sstevel@tonic-gate if (skey->enctype != ENCTYPE_ARCFOUR_HMAC && 8267c478bd9Sstevel@tonic-gate skey->enctype != ENCTYPE_ARCFOUR_HMAC_EXP) 8277c478bd9Sstevel@tonic-gate /* Kerberos IVs are 8 bytes long for DES keys */ 8287c478bd9Sstevel@tonic-gate setup_info.iveclen = KRB5_MIT_DES_KEYSIZE; 8297c478bd9Sstevel@tonic-gate else 8307c478bd9Sstevel@tonic-gate setup_info.iveclen = 0; 8317c478bd9Sstevel@tonic-gate } else { 8327c478bd9Sstevel@tonic-gate (void) memcpy(&setup_info.ivec, ivec->data, ivec->length); 8337c478bd9Sstevel@tonic-gate setup_info.iveclen = ivec->length; 8347c478bd9Sstevel@tonic-gate } 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate setup_info.ivec_usage = iv_usage; 8377c478bd9Sstevel@tonic-gate (void) memcpy(&setup_info.key, skey->contents, skey->length); 8387c478bd9Sstevel@tonic-gate 8397c478bd9Sstevel@tonic-gate setup_info.keylen = skey->length; 8407c478bd9Sstevel@tonic-gate setup_info.direction_mask = dir; 8417c478bd9Sstevel@tonic-gate /* 8427c478bd9Sstevel@tonic-gate * R* commands get special handling by crypto module - 8437c478bd9Sstevel@tonic-gate * 4 byte length field is used before each encrypted block 8447c478bd9Sstevel@tonic-gate * of data. 8457c478bd9Sstevel@tonic-gate */ 8467c478bd9Sstevel@tonic-gate setup_info.option_mask = (kcmd_protocol == KCMD_OLD_PROTOCOL ? 8477c478bd9Sstevel@tonic-gate CRYPTOPT_RCMD_MODE_V1 : 8487c478bd9Sstevel@tonic-gate CRYPTOPT_RCMD_MODE_V2); 8497c478bd9Sstevel@tonic-gate 8507c478bd9Sstevel@tonic-gate crioc.ic_cmd = CRYPTIOCSETUP; 8517c478bd9Sstevel@tonic-gate crioc.ic_timout = -1; 8527c478bd9Sstevel@tonic-gate crioc.ic_len = sizeof (setup_info); 8537c478bd9Sstevel@tonic-gate crioc.ic_dp = (char *)&setup_info; 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &crioc)) { 8567c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Error sending CRYPTIOCSETUP ioctl: %m"); 8577c478bd9Sstevel@tonic-gate retval = -1; 8587c478bd9Sstevel@tonic-gate } 8597c478bd9Sstevel@tonic-gate return (retval); 8607c478bd9Sstevel@tonic-gate } 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate static krb5_error_code 8637c478bd9Sstevel@tonic-gate krb5_compat_recvauth(krb5_context context, 8647c478bd9Sstevel@tonic-gate krb5_auth_context *auth_context, 8657c478bd9Sstevel@tonic-gate krb5_pointer fdp, /* IN */ 8667c478bd9Sstevel@tonic-gate krb5_principal server, /* IN */ 8677c478bd9Sstevel@tonic-gate krb5_int32 flags, /* IN */ 8687c478bd9Sstevel@tonic-gate krb5_keytab keytab, /* IN */ 8697c478bd9Sstevel@tonic-gate krb5_ticket **ticket, /* OUT */ 8707c478bd9Sstevel@tonic-gate krb5_int32 *auth_sys, /* OUT */ 8717c478bd9Sstevel@tonic-gate krb5_data *version) /* OUT */ 8727c478bd9Sstevel@tonic-gate { 8737c478bd9Sstevel@tonic-gate krb5_int32 vlen; 8747c478bd9Sstevel@tonic-gate char *buf; 8757c478bd9Sstevel@tonic-gate int len, length; 8767c478bd9Sstevel@tonic-gate krb5_int32 retval; 8777c478bd9Sstevel@tonic-gate int fd = *((int *)fdp); 8787c478bd9Sstevel@tonic-gate 8797c478bd9Sstevel@tonic-gate if ((retval = krb5_net_read(context, fd, (char *)&vlen, 4)) != 4) 8807c478bd9Sstevel@tonic-gate return ((retval < 0) ? errno : ECONNABORTED); 8817c478bd9Sstevel@tonic-gate 8827c478bd9Sstevel@tonic-gate /* 8837c478bd9Sstevel@tonic-gate * Assume that we're talking to a V5 recvauth; read in the 8847c478bd9Sstevel@tonic-gate * the version string, and make sure it matches. 8857c478bd9Sstevel@tonic-gate */ 8867c478bd9Sstevel@tonic-gate len = (int)ntohl(vlen); 8877c478bd9Sstevel@tonic-gate 8887c478bd9Sstevel@tonic-gate if (len < 0 || len > 255) 8897c478bd9Sstevel@tonic-gate return (KRB5_SENDAUTH_BADAUTHVERS); 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate buf = malloc(len); 8927c478bd9Sstevel@tonic-gate if (buf == NULL) 8937c478bd9Sstevel@tonic-gate return (ENOMEM); 8947c478bd9Sstevel@tonic-gate 8957c478bd9Sstevel@tonic-gate length = krb5_net_read(context, fd, buf, len); 8967c478bd9Sstevel@tonic-gate if (len != length) { 8977c478bd9Sstevel@tonic-gate krb5_xfree(buf); 8987c478bd9Sstevel@tonic-gate return ((len < 0) ? errno : ECONNABORTED); 8997c478bd9Sstevel@tonic-gate } 9007c478bd9Sstevel@tonic-gate 9017c478bd9Sstevel@tonic-gate if (strcmp(buf, KRB_V5_SENDAUTH_VERS) != 0) { 9027c478bd9Sstevel@tonic-gate krb5_xfree(buf); 9037c478bd9Sstevel@tonic-gate return (KRB5_SENDAUTH_BADAUTHVERS); 9047c478bd9Sstevel@tonic-gate } 9057c478bd9Sstevel@tonic-gate krb5_xfree(buf); 9067c478bd9Sstevel@tonic-gate 9077c478bd9Sstevel@tonic-gate *auth_sys = KRB5_RECVAUTH_V5; 9087c478bd9Sstevel@tonic-gate 9097c478bd9Sstevel@tonic-gate retval = krb5_recvauth_version(context, auth_context, fdp, 9107c478bd9Sstevel@tonic-gate server, flags | KRB5_RECVAUTH_SKIP_VERSION, 9117c478bd9Sstevel@tonic-gate keytab, ticket, version); 9127c478bd9Sstevel@tonic-gate 9137c478bd9Sstevel@tonic-gate return (retval); 9147c478bd9Sstevel@tonic-gate } 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate static void 9187c478bd9Sstevel@tonic-gate doit(int f, 9197c478bd9Sstevel@tonic-gate struct sockaddr_storage *fromp, 9207c478bd9Sstevel@tonic-gate krb5_context krb_context, 9217c478bd9Sstevel@tonic-gate int encr_flag, 9227c478bd9Sstevel@tonic-gate krb5_keytab keytab) 9237c478bd9Sstevel@tonic-gate { 9247c478bd9Sstevel@tonic-gate int p, t, on = 1; 9257c478bd9Sstevel@tonic-gate char c; 9267c478bd9Sstevel@tonic-gate char abuf[INET6_ADDRSTRLEN]; 9277c478bd9Sstevel@tonic-gate struct sockaddr_in *sin; 9287c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 9297c478bd9Sstevel@tonic-gate int fromplen; 9307c478bd9Sstevel@tonic-gate in_port_t port; 9317c478bd9Sstevel@tonic-gate struct termios tp; 9327c478bd9Sstevel@tonic-gate boolean_t bad_port; 9337c478bd9Sstevel@tonic-gate boolean_t no_name; 9347c478bd9Sstevel@tonic-gate char rhost_addra[INET6_ADDRSTRLEN]; 9357c478bd9Sstevel@tonic-gate 9367c478bd9Sstevel@tonic-gate if (!(rlbuf = malloc(BUFSIZ))) { 9377c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "rlbuf malloc failed\n"); 9387c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 9397c478bd9Sstevel@tonic-gate } 9407c478bd9Sstevel@tonic-gate (void) alarm(60); 9417c478bd9Sstevel@tonic-gate if (read(f, &c, 1) != 1 || c != 0) { 9427c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "failed to receive protocol zero byte\n"); 9437c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 9447c478bd9Sstevel@tonic-gate } 9457c478bd9Sstevel@tonic-gate (void) alarm(0); 9467c478bd9Sstevel@tonic-gate if (fromp->ss_family == AF_INET) { 9477c478bd9Sstevel@tonic-gate sin = (struct sockaddr_in *)fromp; 9487c478bd9Sstevel@tonic-gate port = sin->sin_port = ntohs((ushort_t)sin->sin_port); 9497c478bd9Sstevel@tonic-gate fromplen = sizeof (struct sockaddr_in); 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate if (!inet_ntop(AF_INET, &sin->sin_addr, 9527c478bd9Sstevel@tonic-gate rhost_addra, sizeof (rhost_addra))) 9537c478bd9Sstevel@tonic-gate goto badconversion; 9547c478bd9Sstevel@tonic-gate } else if (fromp->ss_family == AF_INET6) { 9557c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)fromp; 9567c478bd9Sstevel@tonic-gate port = sin6->sin6_port = ntohs((ushort_t)sin6->sin6_port); 9577c478bd9Sstevel@tonic-gate fromplen = sizeof (struct sockaddr_in6); 9587c478bd9Sstevel@tonic-gate 9597c478bd9Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 9607c478bd9Sstevel@tonic-gate struct in_addr ipv4_addr; 9617c478bd9Sstevel@tonic-gate 9627c478bd9Sstevel@tonic-gate IN6_V4MAPPED_TO_INADDR(&sin6->sin6_addr, 9637c478bd9Sstevel@tonic-gate &ipv4_addr); 9647c478bd9Sstevel@tonic-gate if (!inet_ntop(AF_INET, &ipv4_addr, rhost_addra, 9657c478bd9Sstevel@tonic-gate sizeof (rhost_addra))) 9667c478bd9Sstevel@tonic-gate goto badconversion; 9677c478bd9Sstevel@tonic-gate } else { 9687c478bd9Sstevel@tonic-gate if (!inet_ntop(AF_INET6, &sin6->sin6_addr, 9697c478bd9Sstevel@tonic-gate rhost_addra, sizeof (rhost_addra))) 9707c478bd9Sstevel@tonic-gate goto badconversion; 9717c478bd9Sstevel@tonic-gate } 9727c478bd9Sstevel@tonic-gate } else { 9737c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "unknown address family %d\n", 9747c478bd9Sstevel@tonic-gate fromp->ss_family); 9757c478bd9Sstevel@tonic-gate fatal(f, "Permission denied"); 9767c478bd9Sstevel@tonic-gate } 9777c478bd9Sstevel@tonic-gate 9787c478bd9Sstevel@tonic-gate /* 9797c478bd9Sstevel@tonic-gate * Allow connections only from the "ephemeral" reserved 9807c478bd9Sstevel@tonic-gate * ports(ports 512 - 1023) by checking the remote port 9817c478bd9Sstevel@tonic-gate * because other utilities(e.g. in.ftpd) can be used to 9827c478bd9Sstevel@tonic-gate * allow a unprivileged user to originate a connection 9837c478bd9Sstevel@tonic-gate * from a privileged port and provide untrustworthy 9847c478bd9Sstevel@tonic-gate * authentication. 9857c478bd9Sstevel@tonic-gate */ 9867c478bd9Sstevel@tonic-gate bad_port = (use_auth != KRB5_RECVAUTH_V5 && 9877c478bd9Sstevel@tonic-gate (port >= (in_port_t)IPPORT_RESERVED) || 9887c478bd9Sstevel@tonic-gate (port < (in_port_t)(IPPORT_RESERVED/2))); 9897c478bd9Sstevel@tonic-gate no_name = getnameinfo((const struct sockaddr *) fromp, 9907c478bd9Sstevel@tonic-gate fromplen, hostname, sizeof (hostname), 9917c478bd9Sstevel@tonic-gate NULL, 0, 0) != 0; 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate if (no_name || bad_port) { 9947c478bd9Sstevel@tonic-gate (void) strlcpy(abuf, rhost_addra, sizeof (abuf)); 9957c478bd9Sstevel@tonic-gate /* If no host name, use IP address for name later on. */ 9967c478bd9Sstevel@tonic-gate if (no_name) 9977c478bd9Sstevel@tonic-gate (void) strlcpy(hostname, abuf, sizeof (hostname)); 9987c478bd9Sstevel@tonic-gate } 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate if (bad_port) { 10017c478bd9Sstevel@tonic-gate if (no_name) 10027c478bd9Sstevel@tonic-gate syslog(LOG_NOTICE, 10037c478bd9Sstevel@tonic-gate "connection from %s - bad port\n", 10047c478bd9Sstevel@tonic-gate abuf); 10057c478bd9Sstevel@tonic-gate else 10067c478bd9Sstevel@tonic-gate syslog(LOG_NOTICE, 10077c478bd9Sstevel@tonic-gate "connection from %s(%s) - bad port\n", 10087c478bd9Sstevel@tonic-gate hostname, abuf); 10097c478bd9Sstevel@tonic-gate fatal(f, "Permission denied"); 10107c478bd9Sstevel@tonic-gate } 10117c478bd9Sstevel@tonic-gate 10127c478bd9Sstevel@tonic-gate if (use_auth == KRB5_RECVAUTH_V5) { 10137c478bd9Sstevel@tonic-gate do_krb_login(f, rhost_addra, hostname, 10147c478bd9Sstevel@tonic-gate krb_context, encr_flag, keytab); 10157c478bd9Sstevel@tonic-gate if (krusername != NULL && strlen(krusername)) { 10167c478bd9Sstevel@tonic-gate /* 10177c478bd9Sstevel@tonic-gate * Kerberos Authentication succeeded, 10187c478bd9Sstevel@tonic-gate * so set the proper program name to use 10197c478bd9Sstevel@tonic-gate * with pam (important during 'cleanup' 10207c478bd9Sstevel@tonic-gate * routine later). 10217c478bd9Sstevel@tonic-gate */ 10227c478bd9Sstevel@tonic-gate pam_prog_name = KRB5_PROG_NAME; 10237c478bd9Sstevel@tonic-gate } 10247c478bd9Sstevel@tonic-gate } 10257c478bd9Sstevel@tonic-gate 10267c478bd9Sstevel@tonic-gate if (write(f, "", 1) != 1) { 10277c478bd9Sstevel@tonic-gate syslog(LOG_NOTICE, 10287c478bd9Sstevel@tonic-gate "send of the zero byte(to %s) failed:" 10297c478bd9Sstevel@tonic-gate " cannot start data transfer mode\n", 10307c478bd9Sstevel@tonic-gate (no_name ? abuf : hostname)); 10317c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 10327c478bd9Sstevel@tonic-gate } 10337c478bd9Sstevel@tonic-gate if ((p = open("/dev/ptmx", O_RDWR)) == -1) 10347c478bd9Sstevel@tonic-gate fatalperror(f, "cannot open /dev/ptmx"); 10357c478bd9Sstevel@tonic-gate if (grantpt(p) == -1) 10367c478bd9Sstevel@tonic-gate fatal(f, "could not grant slave pty"); 10377c478bd9Sstevel@tonic-gate if (unlockpt(p) == -1) 10387c478bd9Sstevel@tonic-gate fatal(f, "could not unlock slave pty"); 10397c478bd9Sstevel@tonic-gate if ((line = ptsname(p)) == NULL) 10407c478bd9Sstevel@tonic-gate fatal(f, "could not enable slave pty"); 10417c478bd9Sstevel@tonic-gate if ((t = open(line, O_RDWR)) == -1) 10427c478bd9Sstevel@tonic-gate fatal(f, "could not open slave pty"); 10437c478bd9Sstevel@tonic-gate if (ioctl(t, I_PUSH, "ptem") == -1) 10447c478bd9Sstevel@tonic-gate fatalperror(f, "ioctl I_PUSH ptem"); 10457c478bd9Sstevel@tonic-gate if (ioctl(t, I_PUSH, "ldterm") == -1) 10467c478bd9Sstevel@tonic-gate fatalperror(f, "ioctl I_PUSH ldterm"); 10477c478bd9Sstevel@tonic-gate if (ioctl(t, I_PUSH, "ttcompat") == -1) 10487c478bd9Sstevel@tonic-gate fatalperror(f, "ioctl I_PUSH ttcompat"); 10497c478bd9Sstevel@tonic-gate /* 10507c478bd9Sstevel@tonic-gate * POP the sockmod and push the rlmod module. 10517c478bd9Sstevel@tonic-gate * 10527c478bd9Sstevel@tonic-gate * Note that sockmod has to be removed since readstream assumes 10537c478bd9Sstevel@tonic-gate * a "raw" TPI endpoint(e.g. it uses getmsg). 10547c478bd9Sstevel@tonic-gate */ 10557c478bd9Sstevel@tonic-gate if (removemod(f, "sockmod") < 0) 10567c478bd9Sstevel@tonic-gate fatalperror(f, "couldn't remove sockmod"); 10577c478bd9Sstevel@tonic-gate 10587c478bd9Sstevel@tonic-gate if (encr_flag) { 10597c478bd9Sstevel@tonic-gate if (ioctl(f, I_PUSH, "cryptmod") < 0) 10607c478bd9Sstevel@tonic-gate fatalperror(f, "ioctl I_PUSH rlmod"); 10617c478bd9Sstevel@tonic-gate 10627c478bd9Sstevel@tonic-gate } 10637c478bd9Sstevel@tonic-gate 10647c478bd9Sstevel@tonic-gate if (ioctl(f, I_PUSH, "rlmod") < 0) 10657c478bd9Sstevel@tonic-gate fatalperror(f, "ioctl I_PUSH rlmod"); 10667c478bd9Sstevel@tonic-gate 10677c478bd9Sstevel@tonic-gate if (encr_flag) { 10687c478bd9Sstevel@tonic-gate /* 10697c478bd9Sstevel@tonic-gate * Make sure rlmod will pass unrecognized IOCTLs to cryptmod 10707c478bd9Sstevel@tonic-gate */ 10717c478bd9Sstevel@tonic-gate uchar_t passthru = 1; 10727c478bd9Sstevel@tonic-gate struct strioctl rlmodctl; 10737c478bd9Sstevel@tonic-gate 10747c478bd9Sstevel@tonic-gate rlmodctl.ic_cmd = CRYPTPASSTHRU; 10757c478bd9Sstevel@tonic-gate rlmodctl.ic_timout = -1; 10767c478bd9Sstevel@tonic-gate rlmodctl.ic_len = sizeof (uchar_t); 10777c478bd9Sstevel@tonic-gate rlmodctl.ic_dp = (char *)&passthru; 10787c478bd9Sstevel@tonic-gate 10797c478bd9Sstevel@tonic-gate if (ioctl(f, I_STR, &rlmodctl) < 0) 10807c478bd9Sstevel@tonic-gate fatal(f, "ioctl CRYPTPASSTHRU failed\n"); 10817c478bd9Sstevel@tonic-gate } 10827c478bd9Sstevel@tonic-gate 10837c478bd9Sstevel@tonic-gate /* 10847c478bd9Sstevel@tonic-gate * readstream will do a getmsg till it receives 10857c478bd9Sstevel@tonic-gate * M_PROTO type T_DATA_REQ from rloginmodopen() 10867c478bd9Sstevel@tonic-gate * indicating all data on the stream prior to pushing rlmod has 10877c478bd9Sstevel@tonic-gate * been drained at the stream head. 10887c478bd9Sstevel@tonic-gate */ 10897c478bd9Sstevel@tonic-gate if ((nsize = readstream(f, rlbuf, BUFSIZ)) < 0) 10907c478bd9Sstevel@tonic-gate fatalperror(f, "readstream failed"); 10917c478bd9Sstevel@tonic-gate /* 10927c478bd9Sstevel@tonic-gate * Make sure the pty doesn't modify the strings passed 10937c478bd9Sstevel@tonic-gate * to login as part of the "rlogin protocol." The login 10947c478bd9Sstevel@tonic-gate * program should set these flags to apropriate values 10957c478bd9Sstevel@tonic-gate * after it has read the strings. 10967c478bd9Sstevel@tonic-gate */ 10977c478bd9Sstevel@tonic-gate if (ioctl(t, TCGETS, &tp) == -1) 10987c478bd9Sstevel@tonic-gate fatalperror(f, "ioctl TCGETS"); 10997c478bd9Sstevel@tonic-gate tp.c_lflag &= ~(ECHO|ICANON); 11007c478bd9Sstevel@tonic-gate tp.c_oflag &= ~(XTABS|OCRNL); 11017c478bd9Sstevel@tonic-gate tp.c_iflag &= ~(IGNPAR|ICRNL); 11027c478bd9Sstevel@tonic-gate if (ioctl(t, TCSETS, &tp) == -1) 11037c478bd9Sstevel@tonic-gate fatalperror(f, "ioctl TCSETS"); 11047c478bd9Sstevel@tonic-gate 11057c478bd9Sstevel@tonic-gate /* 11067c478bd9Sstevel@tonic-gate * System V ptys allow the TIOC{SG}WINSZ ioctl to be 11077c478bd9Sstevel@tonic-gate * issued on the master side of the pty. Luckily, that's 11087c478bd9Sstevel@tonic-gate * the only tty ioctl we need to do do, so we can close the 11097c478bd9Sstevel@tonic-gate * slave side in the parent process after the fork. 11107c478bd9Sstevel@tonic-gate */ 11117c478bd9Sstevel@tonic-gate (void) ioctl(p, TIOCSWINSZ, &win); 11127c478bd9Sstevel@tonic-gate 11137c478bd9Sstevel@tonic-gate pid = fork(); 11147c478bd9Sstevel@tonic-gate if (pid < 0) 11157c478bd9Sstevel@tonic-gate fatalperror(f, "fork"); 11167c478bd9Sstevel@tonic-gate if (pid == 0) { 11177c478bd9Sstevel@tonic-gate int tt; 11187c478bd9Sstevel@tonic-gate struct utmpx ut; 11197c478bd9Sstevel@tonic-gate 11207c478bd9Sstevel@tonic-gate /* System V login expects a utmp entry to already be there */ 11217c478bd9Sstevel@tonic-gate (void) memset(&ut, 0, sizeof (ut)); 11227c478bd9Sstevel@tonic-gate (void) strncpy(ut.ut_user, ".rlogin", sizeof (ut.ut_user)); 11237c478bd9Sstevel@tonic-gate (void) strncpy(ut.ut_line, line, sizeof (ut.ut_line)); 11247c478bd9Sstevel@tonic-gate ut.ut_pid = getpid(); 11257c478bd9Sstevel@tonic-gate ut.ut_id[0] = 'r'; 11267c478bd9Sstevel@tonic-gate ut.ut_id[1] = (char)SC_WILDC; 11277c478bd9Sstevel@tonic-gate ut.ut_id[2] = (char)SC_WILDC; 11287c478bd9Sstevel@tonic-gate ut.ut_id[3] = (char)SC_WILDC; 11297c478bd9Sstevel@tonic-gate ut.ut_type = LOGIN_PROCESS; 11307c478bd9Sstevel@tonic-gate ut.ut_exit.e_termination = 0; 11317c478bd9Sstevel@tonic-gate ut.ut_exit.e_exit = 0; 11327c478bd9Sstevel@tonic-gate (void) time(&ut.ut_tv.tv_sec); 11337c478bd9Sstevel@tonic-gate if (makeutx(&ut) == NULL) 11347c478bd9Sstevel@tonic-gate syslog(LOG_INFO, "in.rlogind:\tmakeutx failed"); 11357c478bd9Sstevel@tonic-gate 11367c478bd9Sstevel@tonic-gate /* controlling tty */ 11377c478bd9Sstevel@tonic-gate if (setsid() == -1) 11387c478bd9Sstevel@tonic-gate fatalperror(f, "setsid"); 11397c478bd9Sstevel@tonic-gate if ((tt = open(line, O_RDWR)) == -1) 11407c478bd9Sstevel@tonic-gate fatalperror(f, "could not re-open slave pty"); 11417c478bd9Sstevel@tonic-gate 11427c478bd9Sstevel@tonic-gate if (close(p) == -1) 11437c478bd9Sstevel@tonic-gate fatalperror(f, "error closing pty master"); 11447c478bd9Sstevel@tonic-gate if (close(t) == -1) 11457c478bd9Sstevel@tonic-gate fatalperror(f, "error closing pty slave" 11467c478bd9Sstevel@tonic-gate " opened before session established"); 11477c478bd9Sstevel@tonic-gate /* 11487c478bd9Sstevel@tonic-gate * If this fails we may or may not be able to output an 11497c478bd9Sstevel@tonic-gate * error message. 11507c478bd9Sstevel@tonic-gate */ 11517c478bd9Sstevel@tonic-gate if (close(f) == -1) 11527c478bd9Sstevel@tonic-gate fatalperror(f, "error closing deamon stdout"); 11537c478bd9Sstevel@tonic-gate if (dup2(tt, STDIN_FILENO) == -1 || 11547c478bd9Sstevel@tonic-gate dup2(tt, STDOUT_FILENO) == -1 || 11557c478bd9Sstevel@tonic-gate dup2(tt, STDERR_FILENO) == -1) 11567c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); /* Disaster! No stderr! */ 11577c478bd9Sstevel@tonic-gate 11587c478bd9Sstevel@tonic-gate (void) close(tt); 11597c478bd9Sstevel@tonic-gate 11607c478bd9Sstevel@tonic-gate if (use_auth == KRB5_RECVAUTH_V5 && 11617c478bd9Sstevel@tonic-gate krusername != NULL && strlen(krusername)) { 11627c478bd9Sstevel@tonic-gate (void) execl(LOGIN_PROGRAM, "login", 11637c478bd9Sstevel@tonic-gate "-d", line, 11647c478bd9Sstevel@tonic-gate "-r", hostname, 11657c478bd9Sstevel@tonic-gate "-u", krusername, /* KRB5 principal name */ 11667c478bd9Sstevel@tonic-gate "-s", pam_prog_name, 11677c478bd9Sstevel@tonic-gate "-t", term, /* Remote Terminal */ 11687c478bd9Sstevel@tonic-gate "-U", rusername, /* Remote User */ 11697c478bd9Sstevel@tonic-gate "-R", KRB5_REPOSITORY_NAME, 11707c478bd9Sstevel@tonic-gate lusername, /* local user */ 11717c478bd9Sstevel@tonic-gate NULL); 11727c478bd9Sstevel@tonic-gate } else { 11737c478bd9Sstevel@tonic-gate (void) execl(LOGIN_PROGRAM, "login", 11747c478bd9Sstevel@tonic-gate "-d", line, 11757c478bd9Sstevel@tonic-gate "-r", hostname, 11767c478bd9Sstevel@tonic-gate NULL); 11777c478bd9Sstevel@tonic-gate } 11787c478bd9Sstevel@tonic-gate 11797c478bd9Sstevel@tonic-gate fatalperror(STDERR_FILENO, "/bin/login"); 11807c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 11817c478bd9Sstevel@tonic-gate } 11827c478bd9Sstevel@tonic-gate (void) close(t); 11837c478bd9Sstevel@tonic-gate (void) ioctl(f, FIONBIO, &on); 11847c478bd9Sstevel@tonic-gate (void) ioctl(p, FIONBIO, &on); 11857c478bd9Sstevel@tonic-gate 11867c478bd9Sstevel@tonic-gate /* 11877c478bd9Sstevel@tonic-gate * Must ignore SIGTTOU, otherwise we'll stop 11887c478bd9Sstevel@tonic-gate * when we try and set slave pty's window shape 11897c478bd9Sstevel@tonic-gate * (our controlling tty is the master pty). 11907c478bd9Sstevel@tonic-gate * Likewise, we don't want any of the tty-generated 11917c478bd9Sstevel@tonic-gate * signals from chars passing through. 11927c478bd9Sstevel@tonic-gate */ 11937c478bd9Sstevel@tonic-gate (void) sigset(SIGTSTP, SIG_IGN); 11947c478bd9Sstevel@tonic-gate (void) sigset(SIGINT, SIG_IGN); 11957c478bd9Sstevel@tonic-gate (void) sigset(SIGQUIT, SIG_IGN); 11967c478bd9Sstevel@tonic-gate (void) sigset(SIGTTOU, SIG_IGN); 11977c478bd9Sstevel@tonic-gate (void) sigset(SIGTTIN, SIG_IGN); 11987c478bd9Sstevel@tonic-gate (void) sigset(SIGCHLD, cleanup); 11997c478bd9Sstevel@tonic-gate (void) setpgrp(); 12007c478bd9Sstevel@tonic-gate 12017c478bd9Sstevel@tonic-gate if (encr_flag) { 12027c478bd9Sstevel@tonic-gate krb5_data ivec, *ivptr; 12037c478bd9Sstevel@tonic-gate uint_t ivec_usage; 12047c478bd9Sstevel@tonic-gate stop_stream(f, CRYPT_ENCRYPT|CRYPT_DECRYPT); 12057c478bd9Sstevel@tonic-gate 12067c478bd9Sstevel@tonic-gate /* 12077c478bd9Sstevel@tonic-gate * Configure the STREAMS crypto module. For now, 12087c478bd9Sstevel@tonic-gate * don't use any IV parameter. KCMDV0.2 support 12097c478bd9Sstevel@tonic-gate * will require the use of Initialization Vectors 12107c478bd9Sstevel@tonic-gate * for both encrypt and decrypt modes. 12117c478bd9Sstevel@tonic-gate */ 12127c478bd9Sstevel@tonic-gate if (kcmd_protocol == KCMD_OLD_PROTOCOL) { 12137c478bd9Sstevel@tonic-gate if (session_key->enctype == ENCTYPE_DES_CBC_CRC) { 12147c478bd9Sstevel@tonic-gate /* 12157c478bd9Sstevel@tonic-gate * This is gross but necessary for MIT compat. 12167c478bd9Sstevel@tonic-gate */ 12177c478bd9Sstevel@tonic-gate ivec.length = session_key->length; 12187c478bd9Sstevel@tonic-gate ivec.data = (char *)session_key->contents; 12197c478bd9Sstevel@tonic-gate ivec_usage = IVEC_REUSE; 12207c478bd9Sstevel@tonic-gate ivptr = &ivec; 12217c478bd9Sstevel@tonic-gate } else { 12227c478bd9Sstevel@tonic-gate ivptr = NULL; /* defaults to all 0's */ 12237c478bd9Sstevel@tonic-gate ivec_usage = IVEC_NEVER; 12247c478bd9Sstevel@tonic-gate } 12257c478bd9Sstevel@tonic-gate /* 12267c478bd9Sstevel@tonic-gate * configure both sides of stream together 12277c478bd9Sstevel@tonic-gate * since they share the same IV. 12287c478bd9Sstevel@tonic-gate * This is what makes the OLD KCMD protocol 12297c478bd9Sstevel@tonic-gate * less secure than the newer one - Bad ivecs. 12307c478bd9Sstevel@tonic-gate */ 12317c478bd9Sstevel@tonic-gate if (configure_stream(f, session_key, 12327c478bd9Sstevel@tonic-gate CRYPT_ENCRYPT|CRYPT_DECRYPT, 12337c478bd9Sstevel@tonic-gate ivptr, ivec_usage) != 0) 12347c478bd9Sstevel@tonic-gate fatal(f, "Cannot initialize encryption -" 12357c478bd9Sstevel@tonic-gate " exiting.\n"); 12367c478bd9Sstevel@tonic-gate } else { 12377c478bd9Sstevel@tonic-gate size_t blocksize; 12387c478bd9Sstevel@tonic-gate if (session_key->enctype == ENCTYPE_ARCFOUR_HMAC || 12397c478bd9Sstevel@tonic-gate session_key->enctype == ENCTYPE_ARCFOUR_HMAC_EXP) { 12407c478bd9Sstevel@tonic-gate if (configure_stream(f, session_key, 12417c478bd9Sstevel@tonic-gate CRYPT_ENCRYPT|CRYPT_DECRYPT, 12427c478bd9Sstevel@tonic-gate NULL, IVEC_NEVER) != 0) 12437c478bd9Sstevel@tonic-gate fatal(f, 12447c478bd9Sstevel@tonic-gate "Cannot initialize encryption -" 12457c478bd9Sstevel@tonic-gate " exiting.\n"); 12467c478bd9Sstevel@tonic-gate goto startcrypto; 12477c478bd9Sstevel@tonic-gate } 12487c478bd9Sstevel@tonic-gate if (krb5_c_block_size(krb_context, 12497c478bd9Sstevel@tonic-gate session_key->enctype, 12507c478bd9Sstevel@tonic-gate &blocksize)) { 12517c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "Cannot determine blocksize " 12527c478bd9Sstevel@tonic-gate "for encryption type %d", 12537c478bd9Sstevel@tonic-gate session_key->enctype); 12547c478bd9Sstevel@tonic-gate fatal(f, "Cannot determine blocksize " 12557c478bd9Sstevel@tonic-gate "for encryption - exiting.\n"); 12567c478bd9Sstevel@tonic-gate } 12577c478bd9Sstevel@tonic-gate ivec.data = (char *)malloc(blocksize); 12587c478bd9Sstevel@tonic-gate ivec.length = blocksize; 12597c478bd9Sstevel@tonic-gate if (ivec.data == NULL) 12607c478bd9Sstevel@tonic-gate fatal(f, "memory error - exiting\n"); 12617c478bd9Sstevel@tonic-gate /* 12627c478bd9Sstevel@tonic-gate * Following MIT convention - 12637c478bd9Sstevel@tonic-gate * encrypt IV = 0x01 x blocksize 12647c478bd9Sstevel@tonic-gate * decrypt IV = 0x00 x blocksize 12657c478bd9Sstevel@tonic-gate * ivec_usage = IVEC_ONETIME 12667c478bd9Sstevel@tonic-gate * 12677c478bd9Sstevel@tonic-gate * configure_stream separately for encrypt and 12687c478bd9Sstevel@tonic-gate * decrypt because there are 2 different IVs. 12697c478bd9Sstevel@tonic-gate * 12707c478bd9Sstevel@tonic-gate * AES uses 0's for IV. 12717c478bd9Sstevel@tonic-gate */ 12727c478bd9Sstevel@tonic-gate if (session_key->enctype == 12737c478bd9Sstevel@tonic-gate ENCTYPE_AES128_CTS_HMAC_SHA1_96 || 12747c478bd9Sstevel@tonic-gate session_key->enctype == 12757c478bd9Sstevel@tonic-gate ENCTYPE_AES256_CTS_HMAC_SHA1_96) 12767c478bd9Sstevel@tonic-gate (void) memset(ivec.data, 0x00, blocksize); 12777c478bd9Sstevel@tonic-gate else 12787c478bd9Sstevel@tonic-gate (void) memset(ivec.data, 0x01, blocksize); 12797c478bd9Sstevel@tonic-gate if (configure_stream(f, session_key, CRYPT_ENCRYPT, 12807c478bd9Sstevel@tonic-gate &ivec, IVEC_ONETIME) != 0) 12817c478bd9Sstevel@tonic-gate fatal(f, "Cannot initialize encryption -" 12827c478bd9Sstevel@tonic-gate " exiting.\n"); 12837c478bd9Sstevel@tonic-gate (void) memset(ivec.data, 0x00, blocksize); 12847c478bd9Sstevel@tonic-gate if (configure_stream(f, session_key, CRYPT_DECRYPT, 12857c478bd9Sstevel@tonic-gate &ivec, IVEC_ONETIME) != 0) 12867c478bd9Sstevel@tonic-gate fatal(f, "Cannot initialize encryption -" 12877c478bd9Sstevel@tonic-gate " exiting.\n"); 12887c478bd9Sstevel@tonic-gate 12897c478bd9Sstevel@tonic-gate (void) free(ivec.data); 12907c478bd9Sstevel@tonic-gate } 12917c478bd9Sstevel@tonic-gate startcrypto: 12927c478bd9Sstevel@tonic-gate start_stream(f, CRYPT_ENCRYPT); 12937c478bd9Sstevel@tonic-gate start_stream(f, CRYPT_DECRYPT); 12947c478bd9Sstevel@tonic-gate } 12957c478bd9Sstevel@tonic-gate protocol(f, p, encr_flag); 12967c478bd9Sstevel@tonic-gate cleanup(0); 12977c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 12987c478bd9Sstevel@tonic-gate 12997c478bd9Sstevel@tonic-gate badconversion: 13007c478bd9Sstevel@tonic-gate fatalperror(f, "address conversion"); 13017c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 13027c478bd9Sstevel@tonic-gate } 13037c478bd9Sstevel@tonic-gate 13047c478bd9Sstevel@tonic-gate /* 13057c478bd9Sstevel@tonic-gate * rlogin "protocol" machine. 13067c478bd9Sstevel@tonic-gate */ 13077c478bd9Sstevel@tonic-gate static void 13087c478bd9Sstevel@tonic-gate protocol(int f, int p, int encr_flag) 13097c478bd9Sstevel@tonic-gate { 13107c478bd9Sstevel@tonic-gate struct stat buf; 13117c478bd9Sstevel@tonic-gate struct protocol_arg rloginp; 13127c478bd9Sstevel@tonic-gate struct strioctl rloginmod; 13137c478bd9Sstevel@tonic-gate int ptmfd; /* fd of logindmux coneected to ptmx */ 13147c478bd9Sstevel@tonic-gate int netfd; /* fd of logindmux connected to netf */ 13157c478bd9Sstevel@tonic-gate static uchar_t oobdata[] = {TIOCPKT_WINDOW}; 13167c478bd9Sstevel@tonic-gate 13177c478bd9Sstevel@tonic-gate /* indicate new rlogin */ 13187c478bd9Sstevel@tonic-gate if (send_oob(f, oobdata, 1) < 0) 13197c478bd9Sstevel@tonic-gate fatalperror(f, "send_oob"); 13207c478bd9Sstevel@tonic-gate /* 13217c478bd9Sstevel@tonic-gate * We cannot send the SECURE_MSG until after the 13227c478bd9Sstevel@tonic-gate * client has been signaled with the oobdata (above). 13237c478bd9Sstevel@tonic-gate */ 13247c478bd9Sstevel@tonic-gate if (encr_flag) { 13257c478bd9Sstevel@tonic-gate if (write(f, SECURE_MSG, strlen(SECURE_MSG)) < 0) 13267c478bd9Sstevel@tonic-gate fatalperror(f, "Error writing SECURE message"); 13277c478bd9Sstevel@tonic-gate } 13287c478bd9Sstevel@tonic-gate 13297c478bd9Sstevel@tonic-gate /* 13307c478bd9Sstevel@tonic-gate * Open logindmux driver and link netf and ptmx 13317c478bd9Sstevel@tonic-gate * underneath logindmux. 13327c478bd9Sstevel@tonic-gate */ 13337c478bd9Sstevel@tonic-gate if ((ptmfd = open("/dev/logindmux", O_RDWR)) == -1) 13347c478bd9Sstevel@tonic-gate fatalperror(f, "open /dev/logindmux"); 13357c478bd9Sstevel@tonic-gate 13367c478bd9Sstevel@tonic-gate if ((netfd = open("/dev/logindmux", O_RDWR)) == -1) 13377c478bd9Sstevel@tonic-gate fatalperror(f, "open /dev/logindmux"); 13387c478bd9Sstevel@tonic-gate 13397c478bd9Sstevel@tonic-gate if (ioctl(ptmfd, I_LINK, p) < 0) 13407c478bd9Sstevel@tonic-gate fatal(f, "ioctl I_LINK of /dev/ptmx failed\n"); 13417c478bd9Sstevel@tonic-gate 13427c478bd9Sstevel@tonic-gate if (ioctl(netfd, I_LINK, f) < 0) 13437c478bd9Sstevel@tonic-gate fatal(f, "ioctl I_LINK of tcp connection failed\n"); 13447c478bd9Sstevel@tonic-gate 13457c478bd9Sstevel@tonic-gate /* 13467c478bd9Sstevel@tonic-gate * Figure out the device number of the ptm's mux fd, and pass that 13477c478bd9Sstevel@tonic-gate * to the net's mux. 13487c478bd9Sstevel@tonic-gate */ 13497c478bd9Sstevel@tonic-gate if (fstat(ptmfd, &buf) < 0) 13507c478bd9Sstevel@tonic-gate fatalperror(f, "cannot determine device number" 13517c478bd9Sstevel@tonic-gate " of pty side of /dev/logindmux"); 13527c478bd9Sstevel@tonic-gate rloginp.dev = buf.st_rdev; 13537c478bd9Sstevel@tonic-gate rloginp.flag = 0; 13547c478bd9Sstevel@tonic-gate 13557c478bd9Sstevel@tonic-gate rloginmod.ic_cmd = LOGDMX_IOC_QEXCHANGE; 13567c478bd9Sstevel@tonic-gate rloginmod.ic_timout = -1; 13577c478bd9Sstevel@tonic-gate rloginmod.ic_len = sizeof (struct protocol_arg); 13587c478bd9Sstevel@tonic-gate rloginmod.ic_dp = (char *)&rloginp; 13597c478bd9Sstevel@tonic-gate 13607c478bd9Sstevel@tonic-gate if (ioctl(netfd, I_STR, &rloginmod) < 0) 13617c478bd9Sstevel@tonic-gate fatal(netfd, "ioctl LOGDMX_IOC_QEXCHANGE of netfd failed\n"); 13627c478bd9Sstevel@tonic-gate 13637c478bd9Sstevel@tonic-gate /* 13647c478bd9Sstevel@tonic-gate * Figure out the device number of the net's mux fd, and pass that 13657c478bd9Sstevel@tonic-gate * to the ptm's mux. 13667c478bd9Sstevel@tonic-gate */ 13677c478bd9Sstevel@tonic-gate if (fstat(netfd, &buf)) 13687c478bd9Sstevel@tonic-gate fatalperror(f, "cannot determine device number" 13697c478bd9Sstevel@tonic-gate " of network side of /dev/logindmux"); 13707c478bd9Sstevel@tonic-gate rloginp.dev = buf.st_rdev; 13717c478bd9Sstevel@tonic-gate rloginp.flag = 1; 13727c478bd9Sstevel@tonic-gate 13737c478bd9Sstevel@tonic-gate rloginmod.ic_cmd = LOGDMX_IOC_QEXCHANGE; 13747c478bd9Sstevel@tonic-gate rloginmod.ic_timout = -1; 13757c478bd9Sstevel@tonic-gate rloginmod.ic_len = sizeof (struct protocol_arg); 13767c478bd9Sstevel@tonic-gate rloginmod.ic_dp = (char *)&rloginp; 13777c478bd9Sstevel@tonic-gate 13787c478bd9Sstevel@tonic-gate if (ioctl(ptmfd, I_STR, &rloginmod) < 0) 13797c478bd9Sstevel@tonic-gate fatal(netfd, "ioctl LOGDMXZ_IOC_QEXCHANGE of ptmfd failed\n"); 13807c478bd9Sstevel@tonic-gate /* 13817c478bd9Sstevel@tonic-gate * Send an ioctl type RL_IOC_ENABLE to reenable the 13827c478bd9Sstevel@tonic-gate * message queue and reinsert the data read from streamhead 13837c478bd9Sstevel@tonic-gate * at the time of pushing rloginmod module. 13847c478bd9Sstevel@tonic-gate * We need to send this ioctl even if no data was read earlier 13857c478bd9Sstevel@tonic-gate * since we need to reenable the message queue of rloginmod module. 13867c478bd9Sstevel@tonic-gate */ 13877c478bd9Sstevel@tonic-gate rloginmod.ic_cmd = RL_IOC_ENABLE; 13887c478bd9Sstevel@tonic-gate rloginmod.ic_timout = -1; 13897c478bd9Sstevel@tonic-gate if (nsize) { 13907c478bd9Sstevel@tonic-gate rloginmod.ic_len = nsize; 13917c478bd9Sstevel@tonic-gate rloginmod.ic_dp = rlbuf; 13927c478bd9Sstevel@tonic-gate } else { 13937c478bd9Sstevel@tonic-gate rloginmod.ic_len = 0; 13947c478bd9Sstevel@tonic-gate rloginmod.ic_dp = NULL; 13957c478bd9Sstevel@tonic-gate } 13967c478bd9Sstevel@tonic-gate 13977c478bd9Sstevel@tonic-gate if (ioctl(netfd, I_STR, &rloginmod) < 0) 13987c478bd9Sstevel@tonic-gate fatal(netfd, "ioctl RL_IOC_ENABLE of netfd failed\n"); 13997c478bd9Sstevel@tonic-gate 14007c478bd9Sstevel@tonic-gate /* 14017c478bd9Sstevel@tonic-gate * User level daemon now pauses till the shell exits. 14027c478bd9Sstevel@tonic-gate */ 14037c478bd9Sstevel@tonic-gate (void) pause(); 14047c478bd9Sstevel@tonic-gate } 14057c478bd9Sstevel@tonic-gate 14067c478bd9Sstevel@tonic-gate /* This is a signal handler, hence the dummy argument */ 14077c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 14087c478bd9Sstevel@tonic-gate static void 14097c478bd9Sstevel@tonic-gate cleanup(int dummy) 14107c478bd9Sstevel@tonic-gate { 14117c478bd9Sstevel@tonic-gate rmut(); 14127c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 14137c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 14147c478bd9Sstevel@tonic-gate } 14157c478bd9Sstevel@tonic-gate 14167c478bd9Sstevel@tonic-gate /* 14177c478bd9Sstevel@tonic-gate * TPI style replacement for socket send() primitive, so we don't require 14187c478bd9Sstevel@tonic-gate * sockmod to be on the stream. 14197c478bd9Sstevel@tonic-gate */ 14207c478bd9Sstevel@tonic-gate static int 14217c478bd9Sstevel@tonic-gate send_oob(int fd, void *ptr, size_t count) 14227c478bd9Sstevel@tonic-gate { 14237c478bd9Sstevel@tonic-gate struct T_exdata_req exd_req; 14247c478bd9Sstevel@tonic-gate struct strbuf hdr, dat; 14257c478bd9Sstevel@tonic-gate int ret; 14267c478bd9Sstevel@tonic-gate 14277c478bd9Sstevel@tonic-gate exd_req.PRIM_type = T_EXDATA_REQ; 14287c478bd9Sstevel@tonic-gate exd_req.MORE_flag = 0; 14297c478bd9Sstevel@tonic-gate 14307c478bd9Sstevel@tonic-gate hdr.buf = (char *)&exd_req; 14317c478bd9Sstevel@tonic-gate hdr.len = sizeof (exd_req); 14327c478bd9Sstevel@tonic-gate 14337c478bd9Sstevel@tonic-gate dat.buf = ptr; 14347c478bd9Sstevel@tonic-gate dat.len = count; 14357c478bd9Sstevel@tonic-gate 14367c478bd9Sstevel@tonic-gate ret = putmsg(fd, &hdr, &dat, 0); 14377c478bd9Sstevel@tonic-gate if (ret == 0) 14387c478bd9Sstevel@tonic-gate ret = count; 14397c478bd9Sstevel@tonic-gate return (ret); 14407c478bd9Sstevel@tonic-gate } 14417c478bd9Sstevel@tonic-gate 14427c478bd9Sstevel@tonic-gate static void 14437c478bd9Sstevel@tonic-gate fatal(int fd, const char *msg) 14447c478bd9Sstevel@tonic-gate { 14457c478bd9Sstevel@tonic-gate char *bufp; 14467c478bd9Sstevel@tonic-gate size_t len = strlen(msg) + 16; /* enough for our wrapper */ 14477c478bd9Sstevel@tonic-gate 14487c478bd9Sstevel@tonic-gate bufp = alloca(len); 14497c478bd9Sstevel@tonic-gate /* ASCII 001 is the error indicator */ 14507c478bd9Sstevel@tonic-gate len = snprintf(bufp, len, "\01rlogind: %s.\r\n", msg); 14517c478bd9Sstevel@tonic-gate (void) write(fd, bufp, len); 14527c478bd9Sstevel@tonic-gate exit(EXIT_FAILURE); 14537c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 14547c478bd9Sstevel@tonic-gate } 14557c478bd9Sstevel@tonic-gate 14567c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 14577c478bd9Sstevel@tonic-gate static void 14587c478bd9Sstevel@tonic-gate fatalperror(int fd, const char *msg) 14597c478bd9Sstevel@tonic-gate { 14607c478bd9Sstevel@tonic-gate char *bufp; 14617c478bd9Sstevel@tonic-gate const char *errstr; 14627c478bd9Sstevel@tonic-gate int save_errno = errno; 14637c478bd9Sstevel@tonic-gate size_t len = strlen(msg); 14647c478bd9Sstevel@tonic-gate 14657c478bd9Sstevel@tonic-gate if ((errstr = strerror(save_errno))) { 14667c478bd9Sstevel@tonic-gate len += strlen(errstr) + 3; /* 3 for ": " and \0 below */ 14677c478bd9Sstevel@tonic-gate bufp = alloca(len); 14687c478bd9Sstevel@tonic-gate (void) snprintf(bufp, len, "%s: %s", msg, errstr); 14697c478bd9Sstevel@tonic-gate } else { 14707c478bd9Sstevel@tonic-gate const char fmt[] = "%s: Error %d"; 14717c478bd9Sstevel@tonic-gate 14727c478bd9Sstevel@tonic-gate /* -4 for %s & %d. "*8/3" is bytes->decimal, pessimistically */ 14737c478bd9Sstevel@tonic-gate len += sizeof (fmt) -4 + (sizeof (save_errno) *8 /3); 14747c478bd9Sstevel@tonic-gate bufp = alloca(len); 14757c478bd9Sstevel@tonic-gate (void) snprintf(bufp, len, fmt, msg, save_errno); 14767c478bd9Sstevel@tonic-gate } 14777c478bd9Sstevel@tonic-gate fatal(fd, bufp); 14787c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 14797c478bd9Sstevel@tonic-gate } 14807c478bd9Sstevel@tonic-gate 14817c478bd9Sstevel@tonic-gate static void 14827c478bd9Sstevel@tonic-gate rmut(void) 14837c478bd9Sstevel@tonic-gate { 14847c478bd9Sstevel@tonic-gate pam_handle_t *pamh; 14857c478bd9Sstevel@tonic-gate struct utmpx *up; 14867c478bd9Sstevel@tonic-gate char user[sizeof (up->ut_user) + 1]; 14877c478bd9Sstevel@tonic-gate char ttyn[sizeof (up->ut_line) + 1]; 14887c478bd9Sstevel@tonic-gate char rhost[sizeof (up->ut_host) + 1]; 14897c478bd9Sstevel@tonic-gate 14907c478bd9Sstevel@tonic-gate /* while cleaning up dont allow disruption */ 14917c478bd9Sstevel@tonic-gate (void) sigset(SIGCHLD, SIG_IGN); 14927c478bd9Sstevel@tonic-gate 14937c478bd9Sstevel@tonic-gate setutxent(); 14947c478bd9Sstevel@tonic-gate while (up = getutxent()) { 14957c478bd9Sstevel@tonic-gate if (up->ut_pid == pid) { 14967c478bd9Sstevel@tonic-gate if (up->ut_type == DEAD_PROCESS) 14977c478bd9Sstevel@tonic-gate break; /* Cleaned up elsewhere. */ 14987c478bd9Sstevel@tonic-gate 14997c478bd9Sstevel@tonic-gate /* 15007c478bd9Sstevel@tonic-gate * call pam_close_session if login changed 15017c478bd9Sstevel@tonic-gate * the utmpx user entry from type LOGIN_PROCESS 15027c478bd9Sstevel@tonic-gate * to type USER_PROCESS, which happens 15037c478bd9Sstevel@tonic-gate * after pam_open_session is called. 15047c478bd9Sstevel@tonic-gate */ 15057c478bd9Sstevel@tonic-gate if (up->ut_type == USER_PROCESS) { 15067c478bd9Sstevel@tonic-gate (void) strlcpy(user, up->ut_user, 15077c478bd9Sstevel@tonic-gate sizeof (user)); 15087c478bd9Sstevel@tonic-gate (void) strlcpy(ttyn, up->ut_line, 15097c478bd9Sstevel@tonic-gate sizeof (ttyn)); 15107c478bd9Sstevel@tonic-gate (void) strlcpy(rhost, up->ut_host, 15117c478bd9Sstevel@tonic-gate sizeof (rhost)); 15127c478bd9Sstevel@tonic-gate 15137c478bd9Sstevel@tonic-gate /* 15147c478bd9Sstevel@tonic-gate * Use the same pam_prog_name that 15157c478bd9Sstevel@tonic-gate * 'login' used. 15167c478bd9Sstevel@tonic-gate */ 15177c478bd9Sstevel@tonic-gate if ((pam_start(pam_prog_name, user, NULL, 15187c478bd9Sstevel@tonic-gate &pamh)) 15197c478bd9Sstevel@tonic-gate == PAM_SUCCESS) { 15207c478bd9Sstevel@tonic-gate (void) pam_set_item(pamh, PAM_TTY, 15217c478bd9Sstevel@tonic-gate ttyn); 15227c478bd9Sstevel@tonic-gate (void) pam_set_item(pamh, PAM_RHOST, 15237c478bd9Sstevel@tonic-gate rhost); 15247c478bd9Sstevel@tonic-gate (void) pam_close_session(pamh, 0); 15257c478bd9Sstevel@tonic-gate (void) pam_end(pamh, PAM_SUCCESS); 15267c478bd9Sstevel@tonic-gate } 15277c478bd9Sstevel@tonic-gate } 15287c478bd9Sstevel@tonic-gate 15297c478bd9Sstevel@tonic-gate up->ut_type = DEAD_PROCESS; 15307c478bd9Sstevel@tonic-gate up->ut_exit.e_termination = WTERMSIG(0); 15317c478bd9Sstevel@tonic-gate up->ut_exit.e_exit = WEXITSTATUS(0); 15327c478bd9Sstevel@tonic-gate (void) time(&up->ut_tv.tv_sec); 15337c478bd9Sstevel@tonic-gate 15347c478bd9Sstevel@tonic-gate if (modutx(up) == NULL) { 15357c478bd9Sstevel@tonic-gate /* 15367c478bd9Sstevel@tonic-gate * Since modutx failed we'll 15377c478bd9Sstevel@tonic-gate * write out the new entry 15387c478bd9Sstevel@tonic-gate * ourselves. 15397c478bd9Sstevel@tonic-gate */ 15407c478bd9Sstevel@tonic-gate (void) pututxline(up); 15417c478bd9Sstevel@tonic-gate updwtmpx("wtmpx", up); 15427c478bd9Sstevel@tonic-gate } 15437c478bd9Sstevel@tonic-gate break; 15447c478bd9Sstevel@tonic-gate } 15457c478bd9Sstevel@tonic-gate } 15467c478bd9Sstevel@tonic-gate 15477c478bd9Sstevel@tonic-gate endutxent(); 15487c478bd9Sstevel@tonic-gate 15497c478bd9Sstevel@tonic-gate (void) sigset(SIGCHLD, cleanup); 15507c478bd9Sstevel@tonic-gate } 15517c478bd9Sstevel@tonic-gate 15527c478bd9Sstevel@tonic-gate static int 15537c478bd9Sstevel@tonic-gate readstream(int fd, char *buf, int size) 15547c478bd9Sstevel@tonic-gate { 15557c478bd9Sstevel@tonic-gate struct strbuf ctlbuf, datbuf; 15567c478bd9Sstevel@tonic-gate union T_primitives tpi; 15577c478bd9Sstevel@tonic-gate int nbytes = 0; 15587c478bd9Sstevel@tonic-gate int ret = 0; 15597c478bd9Sstevel@tonic-gate int flags = 0; 15607c478bd9Sstevel@tonic-gate int bufsize = size; 15617c478bd9Sstevel@tonic-gate int nread; 15627c478bd9Sstevel@tonic-gate 15637c478bd9Sstevel@tonic-gate (void) memset(&ctlbuf, 0, sizeof (ctlbuf)); 15647c478bd9Sstevel@tonic-gate (void) memset(&datbuf, 0, sizeof (datbuf)); 15657c478bd9Sstevel@tonic-gate 15667c478bd9Sstevel@tonic-gate ctlbuf.buf = (char *)&tpi; 15677c478bd9Sstevel@tonic-gate ctlbuf.maxlen = sizeof (tpi); 15687c478bd9Sstevel@tonic-gate datbuf.buf = buf; 15697c478bd9Sstevel@tonic-gate datbuf.maxlen = size; 15707c478bd9Sstevel@tonic-gate 15717c478bd9Sstevel@tonic-gate for (;;) { 15727c478bd9Sstevel@tonic-gate if (ioctl(fd, I_NREAD, &nread) < 0) { 15737c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "I_NREAD returned error %m"); 15747c478bd9Sstevel@tonic-gate return (-1); 15757c478bd9Sstevel@tonic-gate } 15767c478bd9Sstevel@tonic-gate if (nread + nbytes > bufsize) { 15777c478bd9Sstevel@tonic-gate buf = (char *)realloc(buf, (unsigned)(bufsize + nread)); 15787c478bd9Sstevel@tonic-gate if (buf == NULL) { 15797c478bd9Sstevel@tonic-gate syslog(LOG_WARNING, 15807c478bd9Sstevel@tonic-gate "buffer allocation failed\n"); 15817c478bd9Sstevel@tonic-gate return (-1); 15827c478bd9Sstevel@tonic-gate } 15837c478bd9Sstevel@tonic-gate bufsize += nread; 15847c478bd9Sstevel@tonic-gate rlbuf = buf; 15857c478bd9Sstevel@tonic-gate datbuf.buf = buf + nbytes; 15867c478bd9Sstevel@tonic-gate } 15877c478bd9Sstevel@tonic-gate datbuf.maxlen = bufsize - nbytes; 15887c478bd9Sstevel@tonic-gate ret = getmsg(fd, &ctlbuf, &datbuf, &flags); 15897c478bd9Sstevel@tonic-gate if (ret < 0) { 15907c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "getmsg failed error %m"); 15917c478bd9Sstevel@tonic-gate return (-1); 15927c478bd9Sstevel@tonic-gate } 15937c478bd9Sstevel@tonic-gate if ((ctlbuf.len == 0) && (datbuf.len == 0)) { 15947c478bd9Sstevel@tonic-gate /* 15957c478bd9Sstevel@tonic-gate * getmsg() returned no data - this indicates 15967c478bd9Sstevel@tonic-gate * that the connection is closing down. 15977c478bd9Sstevel@tonic-gate */ 15987c478bd9Sstevel@tonic-gate cleanup(0); 15997c478bd9Sstevel@tonic-gate } 16007c478bd9Sstevel@tonic-gate if (ctlbuf.len <= 0) { 16017c478bd9Sstevel@tonic-gate nbytes += datbuf.len; 16027c478bd9Sstevel@tonic-gate datbuf.buf += datbuf.len; 16037c478bd9Sstevel@tonic-gate continue; 16047c478bd9Sstevel@tonic-gate } 16057c478bd9Sstevel@tonic-gate if (tpi.type == T_DATA_REQ) { 16067c478bd9Sstevel@tonic-gate return (nbytes); 16077c478bd9Sstevel@tonic-gate } 16087c478bd9Sstevel@tonic-gate if ((tpi.type == T_ORDREL_IND) || (tpi.type == T_DISCON_IND)) 16097c478bd9Sstevel@tonic-gate cleanup(0); 16107c478bd9Sstevel@tonic-gate } 16117c478bd9Sstevel@tonic-gate } 16127c478bd9Sstevel@tonic-gate 16137c478bd9Sstevel@tonic-gate /* 16147c478bd9Sstevel@tonic-gate * Verify that the named module is at the top of the stream 16157c478bd9Sstevel@tonic-gate * and then pop it off. 16167c478bd9Sstevel@tonic-gate */ 16177c478bd9Sstevel@tonic-gate static int 16187c478bd9Sstevel@tonic-gate removemod(int f, char *modname) 16197c478bd9Sstevel@tonic-gate { 16207c478bd9Sstevel@tonic-gate char topmodname[BUFSIZ]; 16217c478bd9Sstevel@tonic-gate 16227c478bd9Sstevel@tonic-gate if (ioctl(f, I_LOOK, topmodname) < 0) 16237c478bd9Sstevel@tonic-gate return (-1); 16247c478bd9Sstevel@tonic-gate if (strcmp(modname, topmodname) != 0) { 16257c478bd9Sstevel@tonic-gate errno = ENXIO; 16267c478bd9Sstevel@tonic-gate return (-1); 16277c478bd9Sstevel@tonic-gate } 16287c478bd9Sstevel@tonic-gate if (ioctl(f, I_POP, 0) < 0) 16297c478bd9Sstevel@tonic-gate return (-1); 16307c478bd9Sstevel@tonic-gate return (0); 16317c478bd9Sstevel@tonic-gate } 1632