📄 system.c
字号:
/*- * Copyright (c) 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#ifndef lintstatic char sccsid[] = "@(#)system.c 8.1 (Berkeley) 6/6/93";#endif /* not lint */#include <sys/types.h>#if defined(pyr)#define fd_set fdset_t#endif /* defined(pyr) *//* * Wouldn't it be nice if these REALLY were in <sys/inode.h>? Or, * equivalently, if <sys/inode.h> REALLY existed? */#define IREAD 00400#define IWRITE 00200#include <sys/file.h>#include <sys/time.h>#include <sys/socket.h>#include <netinet/in.h>#include <sys/wait.h>#include <errno.h>extern int errno;#include <netdb.h>#include <signal.h>#include <stdio.h>#include <string.h>#include <pwd.h>#include "../general/general.h"#include "../ctlr/api.h"#include "../api/api_exch.h"#include "../general/globals.h"#ifndef FD_SETSIZE/* * The following is defined just in case someone should want to run * this telnet on a 4.2 system. * */#define FD_SET(n, p) ((p)->fds_bits[0] |= (1<<(n)))#define FD_CLR(n, p) ((p)->fds_bits[0] &= ~(1<<(n)))#define FD_ISSET(n, p) ((p)->fds_bits[0] & (1<<(n)))#define FD_ZERO(p) ((p)->fds_bits[0] = 0)#endifstatic int shell_pid = 0;static char key[50]; /* Actual key */static char *keyname; /* Name of file with key in it */static char *ourENVlist[200]; /* Lots of room */static int sock = -1, /* Connected socket */ serversock; /* Server (listening) socket */static enum { DEAD, UNCONNECTED, CONNECTED } state;static long storage_location; /* Address we have */static short storage_length = 0; /* Length we have */static int storage_must_send = 0, /* Storage belongs on other side of wire */ storage_accessed = 0; /* The storage is accessed (so leave alone)! */static long storage[1000];static union REGS inputRegs;static struct SREGS inputSregs;extern int apitrace;static voidkill_connection(){ state = UNCONNECTED; if (sock != -1) { (void) close(sock); sock = -1; }}static intnextstore(){ struct storage_descriptor sd; if (api_exch_intype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) { storage_length = 0; return -1; } storage_length = sd.length; storage_location = sd.location; if (storage_length > sizeof storage) { fprintf(stderr, "API client tried to send too much storage (%d).\n", storage_length); storage_length = 0; return -1; } if (api_exch_intype(EXCH_TYPE_BYTES, storage_length, (char *)storage) == -1) { storage_length = 0; return -1; } return 0;}static intdoreject(message)char *message;{ struct storage_descriptor sd; int length = strlen(message); if (api_exch_outcommand(EXCH_CMD_REJECTED) == -1) { return -1; } sd.length = length; if (api_exch_outtype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) { return -1; } if (api_exch_outtype(EXCH_TYPE_BYTES, length, message) == -1) { return -1; } return 0;}/* * doassociate() * * Negotiate with the other side and try to do something. * * Returns: * * -1: Error in processing * 0: Invalid password entered * 1: Association OK */static intdoassociate(){ struct passwd *pwent; char promptbuf[100], buffer[200]; struct storage_descriptor sd; extern char *crypt(); if (api_exch_intype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) { return -1; } sd.length = sd.length; if (sd.length > sizeof buffer) { doreject("(internal error) Authentication key too long"); return -1; } if (api_exch_intype(EXCH_TYPE_BYTES, sd.length, buffer) == -1) { return -1; } buffer[sd.length] = 0; if (strcmp(buffer, key) != 0) {#if (!defined(sun)) || defined(BSD) && (BSD >= 43) extern uid_t geteuid();#endif /* (!defined(sun)) || defined(BSD) && (BSD >= 43) */ if ((pwent = getpwuid((int)geteuid())) == 0) { return -1; } sprintf(promptbuf, "Enter password for user %s:", pwent->pw_name); if (api_exch_outcommand(EXCH_CMD_SEND_AUTH) == -1) { return -1; } sd.length = strlen(promptbuf); if (api_exch_outtype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) { return -1; } if (api_exch_outtype(EXCH_TYPE_BYTES, strlen(promptbuf), promptbuf) == -1) { return -1; } sd.length = strlen(pwent->pw_name); if (api_exch_outtype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) { return -1; } if (api_exch_outtype(EXCH_TYPE_BYTES, strlen(pwent->pw_name), pwent->pw_name) == -1) { return -1; } if (api_exch_incommand(EXCH_CMD_AUTH) == -1) { return -1; } if (api_exch_intype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) { return -1; } sd.length = sd.length; if (sd.length > sizeof buffer) { doreject("Password entered was too long"); return -1; } if (api_exch_intype(EXCH_TYPE_BYTES, sd.length, buffer) == -1) { return -1; } buffer[sd.length] = 0; /* Is this the correct password? */ if (strlen(pwent->pw_name)) { char *ptr; int i; ptr = pwent->pw_name; i = 0; while (i < sd.length) { buffer[i++] ^= *ptr++; if (*ptr == 0) { ptr = pwent->pw_name; } } } if (strcmp(crypt(buffer, pwent->pw_passwd), pwent->pw_passwd) != 0) { doreject("Invalid password"); sleep(10); /* Don't let us do too many of these */ return 0; } } if (api_exch_outcommand(EXCH_CMD_ASSOCIATED) == -1) { return -1; } else { return 1; }}voidfreestorage(){ struct storage_descriptor sd; if (storage_accessed) { fprintf(stderr, "Internal error - attempt to free accessed storage.\n"); fprintf(stderr, "(Encountered in file %s at line %d.)\n", __FILE__, __LINE__); quit(); } if (storage_must_send == 0) { return; } storage_must_send = 0; if (api_exch_outcommand(EXCH_CMD_HEREIS) == -1) { kill_connection(); return; } sd.length = storage_length; sd.location = storage_location; if (api_exch_outtype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) { kill_connection(); return; } if (api_exch_outtype(EXCH_TYPE_BYTES, storage_length, (char *)storage) == -1) { kill_connection(); return; }}static intgetstorage(address, length, copyin)long address;int length, copyin;{ struct storage_descriptor sd; freestorage(); if (storage_accessed) { fprintf(stderr, "Internal error - attempt to get while storage accessed.\n"); fprintf(stderr, "(Encountered in file %s at line %d.)\n", __FILE__, __LINE__); quit(); } storage_must_send = 0; if (api_exch_outcommand(EXCH_CMD_GIMME) == -1) { kill_connection(); return -1; } storage_location = address; storage_length = length; if (copyin) { sd.location = (long)storage_location; sd.length = storage_length; if (api_exch_outtype(EXCH_TYPE_STORE_DESC, sizeof sd, (char *)&sd) == -1) { kill_connection(); return -1; } if (api_exch_incommand(EXCH_CMD_HEREIS) == -1) { fprintf(stderr, "Bad data from other side.\n"); fprintf(stderr, "(Encountered at %s, %d.)\n", __FILE__, __LINE__); return -1; } if (nextstore() == -1) { kill_connection(); return -1; } } return 0;}/*ARGSUSED*/voidmovetous(local, es, di, length)char *local;unsigned int es, di;int length;{ long where = SEG_OFF_BACK(es, di); if (length > sizeof storage) { fprintf(stderr, "Internal API error - movetous() length too long.\n"); fprintf(stderr, "(detected in file %s, line %d)\n", __FILE__, __LINE__); quit(); } else if (length == 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -