RCMD(3)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

๐Ÿ’… RCMD(3) โ€” a little group chat about remote-command sockets

okay so this one lives in the Library Functions Manual, and it's part of the Standard C Library (libc, -lc) ๐Ÿซถ. The whole page is about routines for returning a stream to a remote command โ€” extremely old-school network glamour, no cap.


โœจ SYNOPSIS โœจ

#include <unistd.h>

int
rcmd(char **ahost, int inport, const char *locuser, const char *remuser,
    const char *cmd, int *fd2p);

int
rresvport(int *port);

int
iruserok(u_long raddr, int superuser, const char *ruser,
    const char *luser);

int
ruserok(const char *rhost, int superuser, const char *ruser,
    const char *luser);

int
rcmd_af(char **ahost, int inport, const char *locuser,
    const char *remuser, const char *cmd, int *fd2p, int af);

int
rresvport_af(int *port, int af);

int
iruserok_sa(const void *addr, int addrlen, int superuser,
    const char *ruser, const char *luser);

๐ŸŒธ DESCRIPTION ๐ŸŒธ

So bestie, here's the vibe: rcmd() is the function the super-user uses to execute a command on a remote machine, and it does the authentication with an old-school scheme based on reserved port numbers ๐Ÿ‘‘. Then rresvport() hands you a descriptor to a socket whose address sits in the privileged port space. And ruserok() is what servers use to authenticate clients that come asking for service via rcmd() ๐Ÿ’….

All three of these functions live in the same file, and they're used by the rshd(8) server (among others) โ€” iykyk.


๐Ÿฆ‹ the rcmd() moment

rcmd() looks up the host *ahost using gethostbyname(3), and returns -1 if the host does not exist. But if it does exist? Then *ahost gets set to the standard name of the host, and a connection gets established to a server residing at the well-known Internet port inport โญ.

If the connection succeeds, you get a socket back in the Internet domain of type SOCK_STREAM, and that socket is given to the remote command as stdin and stdout ๐ŸŽ€.

Now โ€” if fd2p is non-zero, an auxiliary channel to a control process gets set up, and a descriptor for it gets placed in *fd2p. That control process returns diagnostic output from the command (unit 2) on this channel, and it will also accept bytes on this channel as being Unix signal numbers, to be forwarded to the process group of the command ๐Ÿ’€.

If fd2p is 0, then the stderr (unit 2 of the remote command) is made the same as the stdout, and no provision is made for sending arbitrary signals to the remote process โ€” although you may be able to get its attention by using out-of-band data ๐Ÿ™ˆ.

The protocol is described in detail in rshd(8). fr fr, go read it.


๐Ÿงš the rresvport() moment

rresvport() is how you obtain a socket to which an address with a Privileged Internet port is bound. This socket is suitable for use by rcmd() and several other functions โœจ.

Privileged Internet ports are those in the range 0 to 1023 ๐Ÿ”ฅ. And only the super-user is allowed to bind an address of this sort to a socket โ€” that's the whole point, bestie.


๐Ÿ’œ the iruserok() / ruserok() moment

iruserok() and ruserok() take a remote host's IP address or name, as returned by the gethostbyname(3) routines, plus two user names and a flag indicating whether the local user's name is that of the super-user.

Then, if the user is NOT the super-user, it checks the /etc/hosts.equiv file ๐Ÿ“. If that lookup is not done, or is unsuccessful, the .rhosts file in the local user's home directory is checked to see if the request for service is allowed.

โš ๏ธ Okay this part matters: if this file does not exist, is not a regular file, is owned by anyone other than the user or the super-user, or is writable by anyone other than the owner, the check automatically fails. Like, no discussion, it's over ๐Ÿ’€.

Zero is returned if the machine name is listed in the โ€œhosts.equivโ€ file, or the host and remote user name are found in the โ€œ.rhostsโ€ file; otherwise iruserok() and ruserok() return -1.

Also cute detail: if the local domain (as obtained from gethostname(3)) is the same as the remote domain, only the machine name need be specified ๐ŸŒˆ.

Security tea: iruserok() is strongly preferred for security reasons. It requires trusting the local DNS at most, while ruserok() requires trusting the entire DNS, which can be spoofed. So likeโ€ฆ choose wisely, bestie ๐Ÿ˜ญ

๐ŸŒŸ the _af and _sa girlies

The functions with an โ€œ_afโ€ or โ€œ_saโ€ suffix โ€” that's rcmd_af(), rresvport_af() and iruserok_sa() โ€” work the same as the corresponding functions without a suffix, except that they are capable of handling both IPv6 and IPv4 ports ๐Ÿ‘‘.

The โ€œ_afโ€ suffix means the function has an additional af argument, which is used to specify the address family (see below). The af argument extension is implemented for functions that have no binary address argument โ€” instead, the af argument specifies which address family is desired ๐Ÿ’….

The โ€œ_saโ€ suffix means the function has general socket address and length arguments. Since the socket address is a protocol independent data structure, IPv4 and IPv6 socket addresses can be passed as desired. The sa argument extension is implemented for functions that pass a protocol dependent binary address argument โ€” that argument needs to be replaced with a more general address structure to support multiple address families in a general way ๐Ÿฆ‹.

And the functions with neither an โ€œ_afโ€ suffix nor an โ€œ_saโ€ suffix work for IPv4 only โ€” except for ruserok(), which can handle both IPv6 and IPv4 โญ.

To switch the address family, the af argument must be filled with AF_INET, or AF_INET6. For rcmd_af(), PF_UNSPEC is also allowed โœจ.


๐ŸŽ€ ENVIRONMENT ๐ŸŽ€

  • RSH โ€” when using the rcmd() function, this variable is used as the program to run instead of rsh(1) ๐Ÿ’–

๐Ÿ’… DIAGNOSTICS ๐Ÿ’…

  • rcmd() returns a valid socket descriptor on success. It returns -1 on error and prints a diagnostic message on the standard error ๐Ÿ˜ญ
  • rresvport() returns a valid, bound socket descriptor on success. It returns -1 on error with the global value errno set according to the reason for failure. The error code EAGAIN is overloaded to mean ``All network ports in use.'' ๐Ÿ’€

๐ŸŒˆ SEE ALSO ๐ŸŒˆ


๐Ÿฆ‹ HISTORY ๐Ÿฆ‹

Most of these functions appeared in 4.2BSD ๐Ÿ‘‘

  • rresvport_af() appeared in RFC2292, and was implemented by the WIDE project for the Hydrangea IPv6 protocol stack kit ๐ŸŒธ
  • rcmd_af() appeared in draft-ietf-ipngwg-rfc2292bis-01.txt, and was implemented in the WIDE/KAME IPv6 protocol stack kit ๐Ÿ”ฅ
  • iruserok_sa() appeared in discussion on the IETF ipngwg mailing list, and was implemented in FreeBSD 4.0 โœจ

that's the whole page, bestie โ€” mwah ๐Ÿ’‹

Debian ๐ŸŽ€ March 3, 2000 โ€” yeah she's an elder icon but she still ate, periodt ๐Ÿ’…๐Ÿซถ

๐Ÿ“ฆ where this page comes from
  • freebsd 15.1: base 15.1
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms