1.\" $NetBSD: rcmd.3,v 1.11 1997/06/05 16:40:16 mrg Exp $ 2.\" 3.\" Copyright (c) 1983, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)rcmd.3 8.1 (Berkeley) 6/4/93 35.\" 36.Dd June 4, 1993 37.Dt RCMD 3 38.Os BSD 4.2 39.Sh NAME 40.Nm rcmd , 41.Nm rresvport , 42.Nm iruserok , 43.Nm ruserok 44.Nd routines for returning a stream to a remote command 45.Sh SYNOPSIS 46.Fd #include <unistd.h> 47.Ft int 48.Fn rcmd "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" 49.Ft int 50.Fn orcmd "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" 51.Ft int 52.Fn rresvport "int *port" 53.Ft int 54.Fn iruserok "u_int32_t raddr" "int superuser" "const char *ruser" "const char *luser" 55.Ft int 56.Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser" 57.Sh DESCRIPTION 58The 59.Fn rcmd 60function is available for use by anyone to run commands on a 61remote system. It acts like the 62.Fn orcmd 63command, with the exception that it makes a call out to the 64.Xr rcmd 1 65command, or any other user-specified command, to perform the 66actual connection (thus not requiring 67that the caller be running as the super-user), and is only 68available for the 69.Dq shell/tcp 70port. 71The 72.Fn orcmd 73function 74is used by the super-user to execute a command on 75a remote machine using an authentication scheme based 76on reserved port numbers. 77The 78.Fn rresvport 79function 80returns a descriptor to a socket 81with an address in the privileged port space. 82The 83.Fn iruserok 84and 85.Fn ruserok 86functions are used by servers 87to authenticate clients requesting service with 88.Fn rcmd . 89All five functions are present in the same file and are used 90by the 91.Xr rshd 8 92server (among others). 93.Pp 94The 95.Fn rcmd 96function 97looks up the host 98.Fa *ahost 99using 100.Xr gethostbyname 3 , 101returning \-1 if the host does not exist. 102Otherwise 103.Fa *ahost 104is set to the standard name of the host 105and a connection is established to a server 106residing at the well-known Internet port 107.Fa inport . 108.Pp 109If the connection succeeds, 110a socket in the Internet domain of type 111.Dv SOCK_STREAM 112is returned to the caller, and given to the remote 113command as 114.Em stdin 115and 116.Em stdout . 117If 118.Fa fd2p 119is non-zero, then an auxiliary channel to a control 120process will be set up, and a descriptor for it will be placed 121in 122.Fa *fd2p . 123The control process will return diagnostic 124output from the command (unit 2) on this channel, and will also 125accept bytes on this channel as being 126.Tn UNIX 127signal numbers, to be 128forwarded to the process group of the command. 129If 130.Fa fd2p 131is 0, then the 132.Em stderr 133(unit 2 of the remote 134command) will be made the same as the 135.Em stdout 136and no 137provision is made for sending arbitrary signals to the remote process, 138although you may be able to get its attention by using out-of-band data. 139.Pp 140The protocol is described in detail in 141.Xr rshd 8 . 142.Pp 143The 144.Fn rresvport 145function is used to obtain a socket with a privileged 146address bound to it. This socket is suitable for use 147by 148.Fn rcmd 149and several other functions. Privileged Internet ports are those 150in the range 0 to 1023. Only the super-user 151is allowed to bind an address of this sort to a socket. 152.Pp 153The 154.Fn iruserok 155and 156.Fn ruserok 157functions take a remote host's IP address or name, respectively, 158two user names and a flag indicating whether the local user's 159name is that of the super-user. 160Then, if the user is 161.Em NOT 162the super-user, it checks the 163.Pa /etc/hosts.equiv 164file. 165If that lookup is not done, or is unsuccessful, the 166.Pa .rhosts 167in the local user's home directory is checked to see if the request for 168service is allowed. 169.Pp 170If this file does not exist, is not a regular file, is owned by anyone 171other than the user or the super-user, or is writeable by anyone other 172than the owner, the check automatically fails. 173Zero is returned if the machine name is listed in the 174.Dq Pa hosts.equiv 175file, or the host and remote user name are found in the 176.Dq Pa .rhosts 177file; otherwise 178.Fn iruserok 179and 180.Fn ruserok 181return \-1. 182If the local domain (as obtained from 183.Xr gethostname 2 ) 184is the same as the remote domain, only the machine name need be specified. 185.Pp 186If the IP address of the remote host is known, 187.Fn iruserok 188should be used in preference to 189.Fn ruserok , 190as it does not require trusting the DNS server for the remote host's domain. 191.Sh DIAGNOSTICS 192The 193.Fn rcmd 194function 195returns a valid socket descriptor on success. 196It returns \-1 on error and prints a diagnostic message on the standard error. 197.Pp 198The 199.Fn rresvport 200function 201returns a valid, bound socket descriptor on success. 202It returns \-1 on error with the global value 203.Va errno 204set according to the reason for failure. 205The error code 206.Dv EAGAIN 207is overloaded to mean ``All network ports in use.'' 208.Sh ENVIRONMENT 209.Bl -tag -width RCMD_CMDxx -compact 210.It Ev RCMD_CMD 211When using the 212.Fn rcmd 213function, this variable is used as the program to run instead of 214.Xr rcmd 1 . 215.El 216.Sh SEE ALSO 217.Xr rlogin 1 , 218.Xr rsh 1 , 219.Xr rcmd 1 , 220.Xr intro 2 , 221.Xr rexec 3 , 222.Xr rexecd 8 , 223.Xr rlogind 8 , 224.Xr rshd 8 . 225.Sh HISTORY 226The 227.Fn orcmd , 228.Fn rresvport , 229.Fn iruserok 230and 231.Fn ruserok 232functions appeared in 233.Bx 4.2 , 234where the 235.Fn orcmd 236function was called 237.Fn rcmd. 238The (newer) 239.Fn rcmd 240function appeared in 241.Nx 1.3 . 242.Sh BUGS 243As the 244.Nm 245function uses 246.Xr getpwent 3 247functions, 248passing in a previous value from one of this family of functions 249can result in unpredictable results. Do not write code like the 250following: 251.Bd -literal -offset indent 252struct passwd *pw; 253 254pw = getpwuid(getuid()); 255... 256if (rcmd(host, port, pw->pw_name, pw->pw_name, cmd, fd2p) < 0) 257 err(1, "rcmd"); 258 259.Ed 260.Pp 261When a reentrant version of 262.Xr getpwent 3 263is available, 264.Nm 265should be changed to use this instead. 266