1*bf221267Stedu /* $OpenBSD: get_names.c,v 1.19 2010/08/12 23:31:29 tedu 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 33df930be7Sderaadt #include "talk.h" 34b3f4390eSpjanzen #include <sys/param.h> 35b3f4390eSpjanzen #include <pwd.h> 3670ef01f6Sdavid #include <stdlib.h> 37b3f4390eSpjanzen #include <unistd.h> 38df930be7Sderaadt 39df930be7Sderaadt extern CTL_MSG msg; 40df930be7Sderaadt 41df930be7Sderaadt /* 42df930be7Sderaadt * Determine the local and remote user, tty, and machines 43df930be7Sderaadt */ 44b3f4390eSpjanzen void 45*bf221267Stedu get_names(int argc, char *argv[]) 46df930be7Sderaadt { 47df930be7Sderaadt char hostname[MAXHOSTNAMELEN]; 48df930be7Sderaadt char *his_name, *my_name; 49df930be7Sderaadt char *my_machine_name, *his_machine_name; 50b3f4390eSpjanzen char *his_tty; 51c0932ef1Smpech char *cp; 52df930be7Sderaadt char *names; 53df930be7Sderaadt 54237d0a1bShugh if (argc > 1 && !strcmp(argv[1], "-H")) { 55237d0a1bShugh argv[1] = argv[0]; 56237d0a1bShugh ++argv; 57237d0a1bShugh --argc; 58237d0a1bShugh high_print = 1; 59237d0a1bShugh } 60237d0a1bShugh 616cc7bb51Smillert if (argc > 1 && !strcmp(argv[1], "-s")) { 626cc7bb51Smillert argv[1] = argv[0]; 636cc7bb51Smillert ++argv; 646cc7bb51Smillert --argc; 656cc7bb51Smillert smooth_scroll = TRUE; 666cc7bb51Smillert } 676cc7bb51Smillert 68b3f4390eSpjanzen if ((argc < 2 ) || ('@' == argv[1][0])) { 69cd1ff621Ssobrado fprintf(stderr, "usage: talk [-Hs] person [ttyname]\n"); 7043ae5fd5Sderaadt exit(1); 71df930be7Sderaadt } 7216793986Smillert if (!isatty(STDIN_FILENO)) 73bb73a7cdSmillert errx(1, "standard input must be a tty, not a pipe or a file"); 7437055eb1Sjkatz 75df930be7Sderaadt if ((my_name = getlogin()) == NULL) { 76df930be7Sderaadt struct passwd *pw; 77df930be7Sderaadt 78bb73a7cdSmillert if ((pw = getpwuid(getuid())) == NULL) 79bb73a7cdSmillert errx(1, "you don't exist in the passwd file."); 80df930be7Sderaadt my_name = pw->pw_name; 81df930be7Sderaadt } 82df930be7Sderaadt gethostname(hostname, sizeof (hostname)); 83df930be7Sderaadt my_machine_name = hostname; 84df930be7Sderaadt /* check for, and strip out, the machine name of the target */ 85df930be7Sderaadt names = strdup(argv[1]); 8646f0aa9aSderaadt if (names == NULL) 8746f0aa9aSderaadt errx(1, "out of memory"); 88180acc8fSmillert for (cp = names; *cp && !strchr("@:!.", *cp); cp++) 89df930be7Sderaadt ; 90df930be7Sderaadt if (*cp == '\0') { 91df930be7Sderaadt /* this is a local to local talk */ 92df930be7Sderaadt his_name = names; 93df930be7Sderaadt his_machine_name = my_machine_name; 94df930be7Sderaadt } else { 95df930be7Sderaadt if (*cp++ == '@') { 96df930be7Sderaadt /* user@host */ 97df930be7Sderaadt his_name = names; 98df930be7Sderaadt his_machine_name = cp; 99df930be7Sderaadt } else { 100df930be7Sderaadt /* host.user or host!user or host:user */ 101df930be7Sderaadt his_name = cp; 102df930be7Sderaadt his_machine_name = names; 103df930be7Sderaadt } 104df930be7Sderaadt *--cp = '\0'; 105df930be7Sderaadt } 106df930be7Sderaadt if (argc > 2) 107df930be7Sderaadt his_tty = argv[2]; /* tty name is arg 2 */ 108df930be7Sderaadt else 109df930be7Sderaadt his_tty = ""; 110df930be7Sderaadt get_addrs(my_machine_name, his_machine_name); 111df930be7Sderaadt /* 112df930be7Sderaadt * Initialize the message template. 113df930be7Sderaadt */ 114df930be7Sderaadt msg.vers = TALK_VERSION; 115df930be7Sderaadt msg.addr.sa_family = htons(AF_INET); 116df930be7Sderaadt msg.ctl_addr.sa_family = htons(AF_INET); 117df930be7Sderaadt msg.id_num = htonl(0); 118df930be7Sderaadt strncpy(msg.l_name, my_name, NAME_SIZE); 119df930be7Sderaadt msg.l_name[NAME_SIZE - 1] = '\0'; 120df930be7Sderaadt strncpy(msg.r_name, his_name, NAME_SIZE); 121df930be7Sderaadt msg.r_name[NAME_SIZE - 1] = '\0'; 122df930be7Sderaadt strncpy(msg.r_tty, his_tty, TTY_SIZE); 123df930be7Sderaadt msg.r_tty[TTY_SIZE - 1] = '\0'; 12489f25fa2Sotto free(names); 125df930be7Sderaadt } 126