zsh-users
 help / color / mirror / code / Atom feed
From: "E. Jay Berkenbilt" <ejb@ql.org>
To: schaefer@candle.brasslantern.com
Cc: zsh-users@sunsite.dk
Subject: Re: Differrent prompt for remote machines
Date: Sat, 31 Mar 2001 22:53:57 -0500	[thread overview]
Message-ID: <200104010353.f313rvu04868@soup.in.ql.org> (raw)
In-Reply-To: <1010331191449.ZM8000@candle.brasslantern.com> (schaefer@candle.brasslantern.com)

[-- Attachment #1: Type: text/plain, Size: 639 bytes --]

I determine whether I'm remote using this C program I wrote.  I've
tested it only linux.  It uses the fact that most well-behaved remote
login daemons make sure that your tty's utmp entry contains the host
from which you logged in if you are logged in remotely.  This program
prints "local" if you're local, "remote" if you remote, and a few
other things if it can't tell for various reasons.  I've found it to
accurately detect remote logins with ssh, telnet, and (Kerberos)
rlogin and to accurately detect local logins with or without X.  If
you remote login back to your local host (for some reason) it still
considers you to be local.

[-- Attachment #2: utmp_checkremote.c --]
[-- Type: application/octet-stream, Size: 2333 bytes --]

/*
 * $Id: utmp_checkremote.c,v 1.1 2000/09/05 21:41:27 ejb Exp $
 * $Source: /home/ejb/source/util/login/RCS/utmp_checkremote.c,v $
 * $Author: ejb $
 *
 */

#ifndef NO_RCS_HDRS
/* Define a static function and call it.  No warnings this way. */
static void rcsid(char *s)
{rcsid("@(#)$Id: utmp_checkremote.c,v 1.1 2000/09/05 21:41:27 ejb Exp $");s=s;}
#endif /* ! NO_RCS_HDRS */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <utmp.h>
#include <netdb.h>
#include <errno.h>

#define UTL_NOTTY "notty"
#define UTL_NOTFOUND "notfound"
#define UTL_LOCAL "local"
#define UTL_REMOTE "remote"
#define UTL_UNKNOWN "unknown"

static char* whoami = 0;

static void deal_with_host(char* host)
{
    char* result = UTL_UNKNOWN;
    if (strlen(host) == 0)
    {
	result = UTL_LOCAL;
    }
    else
    {
	char thishost[UT_HOSTSIZE + 1];
	char otherhost[UT_HOSTSIZE + 1];
	struct hostent* hp = 0;

	thishost[UT_HOSTSIZE] = '\0';
	otherhost[UT_HOSTSIZE] = '\0';
	if (gethostname(thishost, UT_HOSTSIZE) == -1)
	{
	    fprintf(stderr, "%s: gethostname failed: %s\n",
		    whoami, strerror(errno));
	    exit(2);
	}
	if ((hp = gethostbyname(thishost)) != 0)
	{
	    strncpy(thishost, hp->h_name, UT_HOSTSIZE);
	    if ((hp = gethostbyname(host)) != 0)
	    {
		strncpy(otherhost, hp->h_name, UT_HOSTSIZE);
		if (strcmp(thishost, otherhost) == 0)
		{
		    result = UTL_LOCAL;
		}
		else
		{
		    result = UTL_REMOTE;
		}
	    }
	}
    }

    printf("%s\n", result);
}

int main(int argc, char* argv[])
{
    struct utmp *uptr = 0;
    char* tty = 0;

    if ((whoami = strrchr(argv[0], '/')) == NULL)
    {
	whoami = argv[0];
    }
    else
    {
	++whoami;
    }

    tty = ttyname(0);
    if (tty == 0)
    {
	printf("%s\n", UTL_NOTTY);
	exit(0);
    }
    else if (strncmp(tty, "/dev/", 5) != 0)
    {
	fprintf(stderr,
		"%s: ttyname() returned something that didn't start with /dev!",
		whoami);
	exit(2);
    }
    tty += 5;

    setutent();
    while ((uptr = getutent())!=NULL) {
	if (!uptr->ut_name[0])
	    continue;
#ifdef USER_PROCESS
	if (uptr->ut_type != USER_PROCESS) continue;
#endif
	if (uptr->ut_host && uptr->ut_line &&
	    (strcmp(uptr->ut_line, tty) == 0))
	{
	    deal_with_host(uptr->ut_host);
	    exit(0);
	}
    }
    endutent();

    printf("%s\n", UTL_NOTFOUND);
    return 0;
}

  reply	other threads:[~2001-04-01  3:54 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-30  9:42 Dominik Vogt
2001-03-30  9:59 ` Andrej Borsenkow
2001-03-30 10:48   ` Dominik Vogt
2001-03-30 15:55     ` Bart Schaefer
2001-03-31 11:32 ` Zefram
2001-03-31 18:26   ` Stephane Bortzmeyer
2001-03-31 19:14   ` Bart Schaefer
2001-04-01  3:53     ` E. Jay Berkenbilt [this message]
2001-04-01  4:41       ` Bart Schaefer
2001-03-31 18:26 ` Stephane Bortzmeyer
2001-04-04 17:21   ` Dominik Vogt
2001-04-02 20:46 ` Matt Pharr

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200104010353.f313rvu04868@soup.in.ql.org \
    --to=ejb@ql.org \
    --cc=schaefer@candle.brasslantern.com \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).