* Copyright (c) 1988 Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
static char sccsid
[] = "@(#)system.c 4.1 (Berkeley) 12/4/88";
#endif /* defined(pyr) */
#if !defined(sun) && !defined(pyr)
#else /* !defined(sun) */
#endif /* !defined(sun) */
#include "../general/general.h"
#include "../api/api_exch.h"
#include "../general/globals.h"
* 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)
static 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 */
sock
= -1, /* Connected socket */
serversock
; /* Server (listening) socket */
static enum { DEAD
, UNCONNECTED
, CONNECTED
} state
;
storage_location
; /* Address we have */
storage_length
= 0; /* Length we have */
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
;
struct storage_descriptor sd
;
if (api_exch_intype(EXCH_TYPE_STORE_DESC
, sizeof sd
, (char *)&sd
) == -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",
if (api_exch_intype(EXCH_TYPE_BYTES
, storage_length
, (char *)storage
)
struct storage_descriptor sd
;
int length
= strlen(message
);
if (api_exch_outcommand(EXCH_CMD_REJECTED
) == -1) {
if (api_exch_outtype(EXCH_TYPE_STORE_DESC
, sizeof sd
, (char *)&sd
) == -1) {
if (api_exch_outtype(EXCH_TYPE_BYTES
, length
, message
) == -1) {
* Negotiate with the other side and try to do something.
* -1: Error in processing
* 0: Invalid password entered
struct storage_descriptor sd
;
if (api_exch_intype(EXCH_TYPE_STORE_DESC
, sizeof sd
, (char *)&sd
) == -1) {
if (sd
.length
> sizeof buffer
) {
doreject("(internal error) Authentication key too long");
if (api_exch_intype(EXCH_TYPE_BYTES
, sd
.length
, buffer
) == -1) {
if (strcmp(buffer
, key
) != 0) {
#if (!defined(sun)) || defined(BSD) && (BSD >= 43)
#endif /* (!defined(sun)) || defined(BSD) && (BSD >= 43) */
if ((pwent
= getpwuid((int)geteuid())) == 0) {
sprintf(promptbuf
, "Enter password for user %s:", pwent
->pw_name
);
if (api_exch_outcommand(EXCH_CMD_SEND_AUTH
) == -1) {
sd
.length
= strlen(promptbuf
);
if (api_exch_outtype(EXCH_TYPE_STORE_DESC
, sizeof sd
, (char *)&sd
)
if (api_exch_outtype(EXCH_TYPE_BYTES
, strlen(promptbuf
), promptbuf
)
sd
.length
= strlen(pwent
->pw_name
);
if (api_exch_outtype(EXCH_TYPE_STORE_DESC
, sizeof sd
, (char *)&sd
)
if (api_exch_outtype(EXCH_TYPE_BYTES
,
strlen(pwent
->pw_name
), pwent
->pw_name
) == -1) {
if (api_exch_incommand(EXCH_CMD_AUTH
) == -1) {
if (api_exch_intype(EXCH_TYPE_STORE_DESC
, sizeof sd
, (char *)&sd
)
if (sd
.length
> sizeof buffer
) {
doreject("Password entered was too long");
if (api_exch_intype(EXCH_TYPE_BYTES
, sd
.length
, buffer
) == -1) {
/* Is this the correct password? */
if (strlen(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 */
if (api_exch_outcommand(EXCH_CMD_ASSOCIATED
) == -1) {
struct storage_descriptor sd
;
fprintf(stderr
, "Internal error - attempt to free accessed storage.\n");
fprintf(stderr
, "(Encountered in file %s at line %d.)\n",
if (storage_must_send
== 0) {
if (api_exch_outcommand(EXCH_CMD_HEREIS
) == -1) {
sd
.length
= storage_length
;
sd
.location
= storage_location
;
if (api_exch_outtype(EXCH_TYPE_STORE_DESC
, sizeof sd
, (char *)&sd
) == -1) {
if (api_exch_outtype(EXCH_TYPE_BYTES
, storage_length
, (char *)storage
)
getstorage(address
, length
, copyin
)
struct storage_descriptor sd
;
"Internal error - attempt to get while storage accessed.\n");
fprintf(stderr
, "(Encountered in file %s at line %d.)\n",
if (api_exch_outcommand(EXCH_CMD_GIMME
) == -1) {
storage_location
= address
;
sd
.location
= (long)storage_location
;
sd
.length
= storage_length
;
if (api_exch_outtype(EXCH_TYPE_STORE_DESC
,
sizeof sd
, (char *)&sd
) == -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__
);
movetous(local
, es
, di
, 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__
);
} else if (length
== 0) {
getstorage(where
, length
, 1);
memcpy(local
, (char *)(storage
+((where
-storage_location
))), length
);
movetothem(es
, di
, local
, length
)
long where
= SEG_OFF_BACK(es
, di
);
if (length
> sizeof storage
) {
fprintf(stderr
, "Internal API error - movetothem() length too long.\n");
fprintf(stderr
, "(detected in file %s, line %d)\n", __FILE__
, __LINE__
);
} else if (length
== 0) {
memcpy((char *)storage
, local
, length
);
storage_location
= where
;
access_api(location
, length
, copyin
)
copyin
; /* Do we need to copy in initially? */
fprintf(stderr
, "Internal error - storage accessed twice\n");
fprintf(stderr
, "(Encountered in file %s, line %d.)\n",
} else if (length
!= 0) {
getstorage((long)location
, length
, copyin
);
unaccess_api(location
, local
, length
, copyout
)
if (storage_accessed
== 0) {
fprintf(stderr
, "Internal error - unnecessary unaccess_api call.\n");
fprintf(stderr
, "(Encountered in file %s, line %d.)\n",
storage_must_send
= copyout
; /* if needs to go back */
* Accept a connection from an API client, aborting if the child dies.
while (shell_active
&& (sock
== -1)) {
FD_SET(serversock
, &fdset
);
if ((i
= select(serversock
+1, &fdset
,
(fd_set
*)0, (fd_set
*)0, (struct timeval
*)0)) < 0) {
perror("in select waiting for API connection");
i
= accept(serversock
, (struct sockaddr
*)0, (int *)0);
perror("accepting API connection");
/* If the process has already exited, we may need to close */
if ((shell_active
== 0) && (sock
!= -1)) {
extern void setcommandmode();
setcommandmode(); /* In case child_died sneaked in */
* shell_continue() actually runs the command, and looks for API
* requests coming back in.
* We are called from the main loop in telnet.c.
pause(); /* Nothing to do */
if (api_exch_init(sock
, "server") == -1) {
while (state
== UNCONNECTED
) {
if (api_exch_incommand(EXCH_CMD_ASSOCIATE
) == -1) {
switch (i
= api_exch_nextcommand()) {
if (api_exch_intype(EXCH_TYPE_REGS
, sizeof inputRegs
,
(char *)&inputRegs
) == -1) {
} else if (api_exch_intype(EXCH_TYPE_SREGS
, sizeof inputSregs
,
(char *)&inputSregs
) == -1) {
} else if (nextstore() == -1) {
handle_api(&inputRegs
, &inputSregs
);
freestorage(); /* Send any storage back */
if (api_exch_outcommand(EXCH_CMD_REPLY
) == -1) {
} else if (api_exch_outtype(EXCH_TYPE_REGS
, sizeof inputRegs
,
(char *)&inputRegs
) == -1) {
} else if (api_exch_outtype(EXCH_TYPE_SREGS
, sizeof inputSregs
,
(char *)&inputSregs
) == -1) {
/* Done, and it all worked! */
case EXCH_CMD_DISASSOCIATE
:
"Looking for a REQUEST or DISASSOCIATE command\n");
fprintf(stderr
, "\treceived 0x%02x.\n", i
);
while ((pid
= wait3(&status
, WNOHANG
, (struct rusage
*)0)) > 0) {
extern void setconnmode();
extern void ConnectScreen();
printf("[Hit return to continue]");
(void) gets(inputbuffer
);
ConnectScreen(); /* Turn screen on (if need be) */
(void) close(serversock
);
signal(SIGCHLD
, child_died
);
* Called from telnet.c to fork a lower command.com. We
* use the spint... routines so that we can pick up
* interrupts generated by application programs.
struct sockaddr_in server
;
static char **whereAPI
= 0;
/* First, create verification file. */
keyname
= mktemp("/tmp/apiXXXXXX");
fd
= open(keyname
, O_RDWR
|O_CREAT
|O_EXCL
, IREAD
|IWRITE
);
} while ((fd
== -1) && (errno
== EEXIST
));
/* Now, get seed for random */
if (gettimeofday(&tv
, (struct timezone
*)0) == -1) {
srandom(tv
.tv_usec
); /* seed random number generator */
sprintf(key
, "%lu\n", (unsigned long) ikey
);
if (write(fd
, key
, strlen(key
)) != strlen(key
)) {
key
[strlen(key
)-1] = 0; /* Get rid of newline */
/* Next, create the socket which will be connected to */
serversock
= socket(AF_INET
, SOCK_STREAM
, 0);
perror("opening API socket");
server
.sin_family
= AF_INET
;
server
.sin_addr
.s_addr
= INADDR_ANY
;
if (bind(serversock
, (struct sockaddr
*)&server
, sizeof server
) < 0) {
perror("binding API socket");
if (getsockname(serversock
, (struct sockaddr
*)&server
, &length
) < 0) {
perror("getting API socket name");
(void) close(serversock
);
/* Get name to advertise in address list */
strcpy(sockNAME
, "API3270=");
gethostname(sockNAME
+strlen(sockNAME
), sizeof sockNAME
-strlen(sockNAME
));
if (strlen(sockNAME
) > (sizeof sockNAME
-(10+strlen(keyname
)))) {
fprintf(stderr
, "Local hostname too large; using 'localhost'.\n");
strcpy(sockNAME
, "localhost");
sprintf(sockNAME
+strlen(sockNAME
), ":%u", ntohs(server
.sin_port
));
sprintf(sockNAME
+strlen(sockNAME
), ":%s", keyname
);
if (nextenv
>= &ourENVlist
[highestof(ourENVlist
)-1]) {
fprintf(stderr
, "Too many environmental variables\n");
environ
= ourENVlist
; /* New environment */
child_died(); /* Start up signal handler */
shell_active
= 1; /* We are running down below */
if (shell_pid
= vfork()) {
(void) close(serversock
);
} else { /* New process */
for (i
= 3; i
< 30; i
++) {
if (argc
== 1) { /* Just get a shell */
cmdname
= getenv("SHELL");
execlp(cmdname
, cmdname
, 0);
perror("Exec'ing new shell...\n");
execvp(argv
[1], &argv
[1]);
perror("Exec'ing command.\n");
return shell_active
; /* Go back to main loop */