xref: /openbsd/usr.bin/talk/get_names.c (revision 237d0a1b)
1*237d0a1bShugh /*	$OpenBSD: get_names.c,v 1.7 2000/12/31 00:24:51 hugh Exp $	*/
2df930be7Sderaadt /*	$NetBSD: get_names.c,v 1.4 1994/12/09 02:14:16 jtc Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*
5df930be7Sderaadt  * Copyright (c) 1983, 1993
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
16df930be7Sderaadt  * 3. All advertising materials mentioning features or use of this software
17df930be7Sderaadt  *    must display the following acknowledgement:
18df930be7Sderaadt  *	This product includes software developed by the University of
19df930be7Sderaadt  *	California, Berkeley and its contributors.
20df930be7Sderaadt  * 4. Neither the name of the University nor the names of its contributors
21df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
22df930be7Sderaadt  *    without specific prior written permission.
23df930be7Sderaadt  *
24df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df930be7Sderaadt  * SUCH DAMAGE.
35df930be7Sderaadt  */
36df930be7Sderaadt 
37df930be7Sderaadt #ifndef lint
38df930be7Sderaadt #if 0
39df930be7Sderaadt static char sccsid[] = "@(#)get_names.c	8.1 (Berkeley) 6/6/93";
40df930be7Sderaadt #endif
41*237d0a1bShugh static char rcsid[] = "$OpenBSD: get_names.c,v 1.7 2000/12/31 00:24:51 hugh Exp $";
42df930be7Sderaadt #endif /* not lint */
43df930be7Sderaadt 
44df930be7Sderaadt #include "talk.h"
45b3f4390eSpjanzen #include <sys/param.h>
46b3f4390eSpjanzen #include <pwd.h>
47b3f4390eSpjanzen #include <unistd.h>
48df930be7Sderaadt 
49df930be7Sderaadt extern	CTL_MSG msg;
50df930be7Sderaadt 
51df930be7Sderaadt /*
52df930be7Sderaadt  * Determine the local and remote user, tty, and machines
53df930be7Sderaadt  */
54b3f4390eSpjanzen void
55df930be7Sderaadt get_names(argc, argv)
56df930be7Sderaadt 	int argc;
57df930be7Sderaadt 	char *argv[];
58df930be7Sderaadt {
59df930be7Sderaadt 	char hostname[MAXHOSTNAMELEN];
60df930be7Sderaadt 	char *his_name, *my_name;
61df930be7Sderaadt 	char *my_machine_name, *his_machine_name;
62b3f4390eSpjanzen 	char *his_tty;
63df930be7Sderaadt 	register char *cp;
64df930be7Sderaadt 	char *names;
65df930be7Sderaadt 
66*237d0a1bShugh 	if (argc > 1 && !strcmp(argv[1], "-H")) {
67*237d0a1bShugh 		argv[1] = argv[0];
68*237d0a1bShugh 		++argv;
69*237d0a1bShugh 		--argc;
70*237d0a1bShugh 		high_print = 1;
71*237d0a1bShugh 	}
72*237d0a1bShugh 
73b3f4390eSpjanzen 	if ((argc < 2 ) || ('@' == argv[1][0])) {
74bb73a7cdSmillert 		fprintf(stderr, "usage: talk user [ttyname]\n"
75bb73a7cdSmillert 				"       talk user@hostname [ttyname]\n");
76df930be7Sderaadt 		exit(-1);
77df930be7Sderaadt 	}
78bb73a7cdSmillert 	if (!isatty(0))
79bb73a7cdSmillert 		errx(1, "standard input must be a tty, not a pipe or a file");
8037055eb1Sjkatz 
81df930be7Sderaadt 	if ((my_name = getlogin()) == NULL) {
82df930be7Sderaadt 		struct passwd *pw;
83df930be7Sderaadt 
84bb73a7cdSmillert 		if ((pw = getpwuid(getuid())) == NULL)
85bb73a7cdSmillert 			errx(1, "you don't exist in the passwd file.");
86df930be7Sderaadt 		my_name = pw->pw_name;
87df930be7Sderaadt 	}
88df930be7Sderaadt 	gethostname(hostname, sizeof (hostname));
89df930be7Sderaadt 	my_machine_name = hostname;
90df930be7Sderaadt 	/* check for, and strip out, the machine name of the target */
91df930be7Sderaadt 	names = strdup(argv[1]);
92180acc8fSmillert 	for (cp = names; *cp && !strchr("@:!.", *cp); cp++)
93df930be7Sderaadt 		;
94df930be7Sderaadt 	if (*cp == '\0') {
95df930be7Sderaadt 		/* this is a local to local talk */
96df930be7Sderaadt 		his_name = names;
97df930be7Sderaadt 		his_machine_name = my_machine_name;
98df930be7Sderaadt 	} else {
99df930be7Sderaadt 		if (*cp++ == '@') {
100df930be7Sderaadt 			/* user@host */
101df930be7Sderaadt 			his_name = names;
102df930be7Sderaadt 			his_machine_name = cp;
103df930be7Sderaadt 		} else {
104df930be7Sderaadt 			/* host.user or host!user or host:user */
105df930be7Sderaadt 			his_name = cp;
106df930be7Sderaadt 			his_machine_name = names;
107df930be7Sderaadt 		}
108df930be7Sderaadt 		*--cp = '\0';
109df930be7Sderaadt 	}
110df930be7Sderaadt 	if (argc > 2)
111df930be7Sderaadt 		his_tty = argv[2];	/* tty name is arg 2 */
112df930be7Sderaadt 	else
113df930be7Sderaadt 		his_tty = "";
114df930be7Sderaadt 	get_addrs(my_machine_name, his_machine_name);
115df930be7Sderaadt 	/*
116df930be7Sderaadt 	 * Initialize the message template.
117df930be7Sderaadt 	 */
118df930be7Sderaadt 	msg.vers = TALK_VERSION;
119df930be7Sderaadt 	msg.addr.sa_family = htons(AF_INET);
120df930be7Sderaadt 	msg.ctl_addr.sa_family = htons(AF_INET);
121df930be7Sderaadt 	msg.id_num = htonl(0);
122df930be7Sderaadt 	strncpy(msg.l_name, my_name, NAME_SIZE);
123df930be7Sderaadt 	msg.l_name[NAME_SIZE - 1] = '\0';
124df930be7Sderaadt 	strncpy(msg.r_name, his_name, NAME_SIZE);
125df930be7Sderaadt 	msg.r_name[NAME_SIZE - 1] = '\0';
126df930be7Sderaadt 	strncpy(msg.r_tty, his_tty, TTY_SIZE);
127df930be7Sderaadt 	msg.r_tty[TTY_SIZE - 1] = '\0';
128df930be7Sderaadt }
129