xref: /netbsd-src/usr.bin/talk/get_names.c (revision 0be3040a8f46fcbe7f5350d0e8564686a286ed57)
1*0be3040aSchristos /*	$NetBSD: get_names.c,v 1.16 2012/12/29 23:44:23 christos Exp $	*/
251207773Sjtc 
361f28255Scgd /*
451207773Sjtc  * Copyright (c) 1983, 1993
551207773Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
3232a9e65fSlukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3451207773Sjtc #if 0
3551207773Sjtc static char sccsid[] = "@(#)get_names.c	8.1 (Berkeley) 6/6/93";
3651207773Sjtc #endif
37*0be3040aSchristos __RCSID("$NetBSD: get_names.c,v 1.16 2012/12/29 23:44:23 christos Exp $");
3861f28255Scgd #endif /* not lint */
3961f28255Scgd 
4061f28255Scgd #include "talk.h"
41e3717c18Schristos #include <err.h>
4232a9e65fSlukem #include <sys/param.h>
4332a9e65fSlukem #include <pwd.h>
44fcd0fb11Smatt #include <stdlib.h>
4532a9e65fSlukem #include <unistd.h>
46e3717c18Schristos #include <util.h>
4761f28255Scgd 
4861f28255Scgd extern	CTL_MSG msg;
4961f28255Scgd 
5061f28255Scgd /*
5161f28255Scgd  * Determine the local and remote user, tty, and machines
5261f28255Scgd  */
5332a9e65fSlukem void
get_names(int argc,char * argv[])5468168df6Sjoerg get_names(int argc, char *argv[])
5561f28255Scgd {
562beab49aSmrg 	char hostname[MAXHOSTNAMELEN + 1];
57a405bce5Smycroft 	const char *his_name, *my_name;
58a405bce5Smycroft 	const char *my_machine_name, *his_machine_name;
59c2293a6fSlukem 	const char *his_tty;
6032a9e65fSlukem 	char *cp;
618ef0a5caSderaadt 	char *names;
6261f28255Scgd 
6361f28255Scgd 	if (argc < 2 ) {
64*0be3040aSchristos 		fprintf(stderr, "Usage: %s user [ttyname]\n", getprogname());
6514dbdf55Swiz 		exit(1);
6661f28255Scgd 	}
67*0be3040aSchristos 	if (!isatty(0))
68*0be3040aSchristos 		errx(EXIT_FAILURE, "Standard input must be a tty, "
69*0be3040aSchristos 		    "not a pipe or a file");
7061f28255Scgd 	if ((my_name = getlogin()) == NULL) {
7161f28255Scgd 		struct passwd *pw;
7261f28255Scgd 
73*0be3040aSchristos 		if ((pw = getpwuid(getuid())) == NULL)
74*0be3040aSchristos 			errx(EXIT_FAILURE, "You don't exist. Go away.");
7561f28255Scgd 		my_name = pw->pw_name;
7661f28255Scgd 	}
77e3717c18Schristos 	if ((cp = getenv("TALKHOST")) != NULL)
78e3717c18Schristos 		(void)estrlcpy(hostname, cp, sizeof(hostname));
79e3717c18Schristos 	else {
80e3717c18Schristos 		if (gethostname(hostname, sizeof(hostname)) == -1)
81e3717c18Schristos 			err(EXIT_FAILURE, "gethostname");
822beab49aSmrg 		hostname[sizeof(hostname) - 1] = '\0';
83e3717c18Schristos 	}
8461f28255Scgd 	my_machine_name = hostname;
8561f28255Scgd 	/* check for, and strip out, the machine name of the target */
868ef0a5caSderaadt 	names = strdup(argv[1]);
8732a9e65fSlukem 	for (cp = names; *cp && !strchr("@:!.", *cp); cp++)
8861f28255Scgd 		;
8961f28255Scgd 	if (*cp == '\0') {
9061f28255Scgd 		/* this is a local to local talk */
918ef0a5caSderaadt 		his_name = names;
9261f28255Scgd 		his_machine_name = my_machine_name;
9361f28255Scgd 	} else {
9461f28255Scgd 		if (*cp++ == '@') {
9561f28255Scgd 			/* user@host */
968ef0a5caSderaadt 			his_name = names;
9761f28255Scgd 			his_machine_name = cp;
9861f28255Scgd 		} else {
9961f28255Scgd 			/* host.user or host!user or host:user */
10061f28255Scgd 			his_name = cp;
1018ef0a5caSderaadt 			his_machine_name = names;
10261f28255Scgd 		}
10361f28255Scgd 		*--cp = '\0';
10461f28255Scgd 	}
10561f28255Scgd 	if (argc > 2)
10661f28255Scgd 		his_tty = argv[2];	/* tty name is arg 2 */
10761f28255Scgd 	else
10861f28255Scgd 		his_tty = "";
10961f28255Scgd 	get_addrs(my_machine_name, his_machine_name);
11061f28255Scgd 	/*
11161f28255Scgd 	 * Initialize the message template.
11261f28255Scgd 	 */
11361f28255Scgd 	msg.vers = TALK_VERSION;
11461f28255Scgd 	msg.addr.sa_family = htons(AF_INET);
11561f28255Scgd 	msg.ctl_addr.sa_family = htons(AF_INET);
11661f28255Scgd 	msg.id_num = htonl(0);
11761f28255Scgd 	strncpy(msg.l_name, my_name, NAME_SIZE);
11861f28255Scgd 	msg.l_name[NAME_SIZE - 1] = '\0';
11961f28255Scgd 	strncpy(msg.r_name, his_name, NAME_SIZE);
12061f28255Scgd 	msg.r_name[NAME_SIZE - 1] = '\0';
12161f28255Scgd 	strncpy(msg.r_tty, his_tty, TTY_SIZE);
12261f28255Scgd 	msg.r_tty[TTY_SIZE - 1] = '\0';
1237ee29c7fSginsbach 	free(names);
12461f28255Scgd }
125