📄 remlib.c
字号:
*fd2p = accept (stdErrSocket, (struct sockaddr *)&mySin, &mySinLen); if (*fd2p == ERROR) { close (sd); close (stdErrSocket); return (ERROR); } close (stdErrSocket); } if (send (sd, localUser, strlen(localUser) + 1, 0) <= 0) { close (sd); if (fd2p != NULL) close (*fd2p); return (ERROR); } if (send (sd, remoteUser, strlen(remoteUser) + 1, 0) <= 0) { close (sd); if (fd2p != NULL) close (*fd2p); return (ERROR); } if (send (sd, cmd, strlen(cmd) + 1, 0) <= 0) { close (sd); if (fd2p != NULL) close (*fd2p); return (ERROR); } /* bsd documentation for rshd is incorrect - null byte is actually received on stdin socket */ if (recv (sd, &c, 1, 0) <= 0) { close (sd); if (fd2p != NULL) close (*fd2p); return (ERROR); } if (c != 0) { /* error will come in on stdin socket */ while (recv (sd, &c, 1, 0) == 1) { write (STD_ERR, &c, 1); if (c == '\n') break; } errnoSet (S_remLib_RSH_ERROR); close (sd); if (fd2p != NULL) close (*fd2p); return (ERROR); } return (sd); }/********************************************************************************* rresvport - open a socket with a privileged port bound to it** This routine opens a socket with a privileged port bound to it.* It is analogous to the UNIX routine rresvport().** RETURNS* A socket descriptor, or ERROR if either the socket cannot be opened or all* ports are in use.** SEE ALSO: UNIX BSD 4.3 manual entry for rresvport()*/int rresvport ( FAST int *alport /* port number to initially try */ ) { struct sockaddr_in sin; int sd; sin.sin_family = AF_INET; sin.sin_addr.s_addr = 0; sin.sin_port = htons (*alport); if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR) return (ERROR); if (bindresvport (sd, &sin) < 0) { close (sd); return (ERROR); } *alport = ntohs (sin.sin_port); return (sd); }/********************************************************************************* remCurIdGet - get the current user name and password** This routine gets the user name and password currently used for remote * host access privileges and copies them to <user> and <passwd>. Either* parameter can be initialized to NULL, and the corresponding item will not* be passed.** RETURNS: N/A** SEE ALSO: iam(), whoami()*/void remCurIdGet ( char *user, /* where to return current user name */ char *passwd /* where to return current password */ ) { if (user != NULL) strcpy (user, remUser); if (passwd != NULL) strcpy (passwd, remPasswd); }/********************************************************************************* remCurIdSet - set the remote user name and password** This routine specifies the user name that will have access privileges* on the remote machine. The user name must exist in the remote* machine's \f3/etc/passwd\fP, and if it has been assigned a password,* the password must be specified in <newPasswd>.** Either parameter can be NULL, and the corresponding item will not be set.** The maximum length of the user name and the password is MAX_IDENTITY_LEN* (defined in remLib.h).** NOTE: A more convenient version of this routine is iam(), which is intended* to be used from the shell.** RETURNS: OK, or ERROR if the name or password is too long.** SEE ALSO: iam(), whoami()*/STATUS remCurIdSet ( char *newUser, /* user name to use on remote */ char *newPasswd /* password to use on remote (NULL = none) */ ) { if (((newUser != NULL) && (strlen (newUser) > MAX_IDENTITY_LEN-1)) || ((newPasswd != NULL) && (strlen (newPasswd) > MAX_IDENTITY_LEN-1))) { errnoSet (S_remLib_IDENTITY_TOO_BIG); return (ERROR); } if (newUser == NULL) remUser[0] = EOS; else strcpy (remUser, newUser); if (newPasswd == NULL) remPasswd[0] = EOS; else strcpy (remPasswd, newPasswd); return (OK); }/********************************************************************************* iam - set the remote user name and password** This routine specifies the user name that will have access privileges* on the remote machine. The user name must exist in the remote* machine's \f3/etc/passwd\fP, and if it has been assigned a password,* the password must be specified in <newPasswd>.** Either parameter can be NULL, and the corresponding item will not be set.** The maximum length of the user name and the password is MAX_IDENTITY_LEN* (defined in remLib.h).** NOTE: This routine is a more convenient version of remCurIdSet() and is* intended to be used from the shell.** RETURNS: OK, or ERROR if the call fails.** SEE ALSO: whoami(), remCurIdGet(), remCurIdSet()*/STATUS iam ( char *newUser, /* user name to use on remote */ char *newPasswd /* password to use on remote (NULL = none) */ ) { if (remCurIdSet (newUser, newPasswd) != OK) { printErr ("User name or password too long\n"); return (ERROR); } return (OK); }/********************************************************************************* whoami - display the current remote identity** This routine displays the user name currently used for remote machine* access. The user name is set with iam() or remCurIdSet().** RETURNS: N/A** SEE ALSO: iam(), remCurIdGet(), remCurIdSet()*/void whoami (void) { char user [MAX_IDENTITY_LEN]; remCurIdGet (user, (char *) NULL); printf ("%s\n", user); }/********************************************************************************* bindresvport - bind a socket to a privileged IP port** This routine picks a port number between 600 and 1023 that is not being* used by any other programs and binds the socket passed as <sd> to that* port. Privileged IP ports (numbers between and including 0 and 1023) are* reserved for privileged programs.** RETURNS:* OK, or ERROR if the address family specified in <sin> is not supported or* the call fails.*/STATUS bindresvport ( int sd, /* socket to be bound */ FAST struct sockaddr_in *sin /* socket address -- value/result */ ) { FAST int startPort; FAST int port; struct sockaddr_in myaddr; if (sin == (struct sockaddr_in *)0) { sin = &myaddr; bzero((char *) sin, sizeof (*sin)); sin->sin_family = AF_INET; } else if (sin->sin_family != AF_INET) { errnoSet (EPFNOSUPPORT); return (ERROR); } if (ntohs (sin->sin_port) == 0) sin->sin_port = htons (remLastResvPort); port = startPort = ntohs (sin->sin_port); FOREVER { --port; if (port <= IPPORT_RESERVED - 400) port = IPPORT_RESERVED - 1; if (port == startPort) { errnoSet (S_remLib_ALL_PORTS_IN_USE); return (ERROR); } sin->sin_port = htons (port); if (bind (sd, (struct sockaddr *) sin, sizeof (*sin)) != ERROR) { remLastResvPort = port; return (OK); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -