xref: /openbsd/usr.bin/talk/get_names.c (revision 8d688443)
1*8d688443Smestre /*	$OpenBSD: get_names.c,v 1.21 2016/02/01 07:29:25 mestre 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.
16f75387cbSmillert  * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
18df930be7Sderaadt  *    without specific prior written permission.
19df930be7Sderaadt  *
20df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt  * SUCH DAMAGE.
31df930be7Sderaadt  */
32df930be7Sderaadt 
33*8d688443Smestre #include <sys/socket.h>
34*8d688443Smestre 
35*8d688443Smestre #include <err.h>
36*8d688443Smestre #include <limits.h>
37b3f4390eSpjanzen #include <pwd.h>
3870ef01f6Sdavid #include <stdlib.h>
39*8d688443Smestre #include <string.h>
40b3f4390eSpjanzen #include <unistd.h>
41*8d688443Smestre 
42*8d688443Smestre #include "talk.h"
43df930be7Sderaadt 
44df930be7Sderaadt extern	CTL_MSG msg;
45df930be7Sderaadt 
46df930be7Sderaadt /*
47df930be7Sderaadt  * Determine the local and remote user, tty, and machines
48df930be7Sderaadt  */
49b3f4390eSpjanzen void
get_names(int argc,char * argv[])50bf221267Stedu get_names(int argc, char *argv[])
51df930be7Sderaadt {
52b9fc9a72Sderaadt 	char hostname[HOST_NAME_MAX+1];
53df930be7Sderaadt 	char *his_name, *my_name;
54df930be7Sderaadt 	char *my_machine_name, *his_machine_name;
55b3f4390eSpjanzen 	char *his_tty;
56c0932ef1Smpech 	char *cp;
57df930be7Sderaadt 	char *names;
58df930be7Sderaadt 
59237d0a1bShugh 	if (argc > 1 && !strcmp(argv[1], "-H")) {
60237d0a1bShugh 		argv[1] = argv[0];
61237d0a1bShugh 		++argv;
62237d0a1bShugh 		--argc;
63237d0a1bShugh 		high_print = 1;
64237d0a1bShugh 	}
65237d0a1bShugh 
666cc7bb51Smillert 	if (argc > 1 && !strcmp(argv[1], "-s")) {
676cc7bb51Smillert 		argv[1] = argv[0];
686cc7bb51Smillert 		++argv;
696cc7bb51Smillert 		--argc;
706cc7bb51Smillert 		smooth_scroll = TRUE;
716cc7bb51Smillert 	}
726cc7bb51Smillert 
73b3f4390eSpjanzen 	if ((argc < 2 ) || ('@' == argv[1][0])) {
74*8d688443Smestre 		extern char *__progname;
75*8d688443Smestre 		fprintf(stderr, "usage: %s [-Hs] person [ttyname]\n", __progname);
7643ae5fd5Sderaadt 		exit(1);
77df930be7Sderaadt 	}
7816793986Smillert 	if (!isatty(STDIN_FILENO))
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]);
9246f0aa9aSderaadt 	if (names == NULL)
9346f0aa9aSderaadt 		errx(1, "out of memory");
94180acc8fSmillert 	for (cp = names; *cp && !strchr("@:!.", *cp); cp++)
95df930be7Sderaadt 		;
96df930be7Sderaadt 	if (*cp == '\0') {
97df930be7Sderaadt 		/* this is a local to local talk */
98df930be7Sderaadt 		his_name = names;
99df930be7Sderaadt 		his_machine_name = my_machine_name;
100df930be7Sderaadt 	} else {
101df930be7Sderaadt 		if (*cp++ == '@') {
102df930be7Sderaadt 			/* user@host */
103df930be7Sderaadt 			his_name = names;
104df930be7Sderaadt 			his_machine_name = cp;
105df930be7Sderaadt 		} else {
106df930be7Sderaadt 			/* host.user or host!user or host:user */
107df930be7Sderaadt 			his_name = cp;
108df930be7Sderaadt 			his_machine_name = names;
109df930be7Sderaadt 		}
110df930be7Sderaadt 		*--cp = '\0';
111df930be7Sderaadt 	}
112df930be7Sderaadt 	if (argc > 2)
113df930be7Sderaadt 		his_tty = argv[2];	/* tty name is arg 2 */
114df930be7Sderaadt 	else
115df930be7Sderaadt 		his_tty = "";
116df930be7Sderaadt 	get_addrs(my_machine_name, his_machine_name);
117df930be7Sderaadt 	/*
118df930be7Sderaadt 	 * Initialize the message template.
119df930be7Sderaadt 	 */
120df930be7Sderaadt 	msg.vers = TALK_VERSION;
121df930be7Sderaadt 	msg.addr.sa_family = htons(AF_INET);
122df930be7Sderaadt 	msg.ctl_addr.sa_family = htons(AF_INET);
123df930be7Sderaadt 	msg.id_num = htonl(0);
124df930be7Sderaadt 	strncpy(msg.l_name, my_name, NAME_SIZE);
125df930be7Sderaadt 	msg.l_name[NAME_SIZE - 1] = '\0';
126df930be7Sderaadt 	strncpy(msg.r_name, his_name, NAME_SIZE);
127df930be7Sderaadt 	msg.r_name[NAME_SIZE - 1] = '\0';
128df930be7Sderaadt 	strncpy(msg.r_tty, his_tty, TTY_SIZE);
129df930be7Sderaadt 	msg.r_tty[TTY_SIZE - 1] = '\0';
13089f25fa2Sotto 	free(names);
131df930be7Sderaadt }
132