xref: /csrg-svn/usr.bin/uucp/libuu/uucpname.c (revision 62386)
148652Sbostic /*-
2*62386Sbostic  * Copyright (c) 1985, 1993
3*62386Sbostic  *	The Regents of the University of California.  All rights reserved.
448652Sbostic  *
548652Sbostic  * %sccs.include.proprietary.c%
648652Sbostic  */
748652Sbostic 
813679Ssam #ifndef lint
9*62386Sbostic static char sccsid[] = "@(#)uucpname.c	8.1 (Berkeley) 06/06/93";
1048652Sbostic #endif /* not lint */
1113679Ssam 
1213679Ssam #include "uucp.h"
1313679Ssam #include <sys/stat.h>
1413679Ssam 
1523686Sbloom /*LINTLIBRARY*/
1623686Sbloom 
1713679Ssam #ifdef	GETMYHNAME
1813679Ssam #include <UNET/unetio.h>
1913679Ssam #endif
2013679Ssam 
2113679Ssam #ifdef	UNAME
2213679Ssam /* Use USG uname() library routine */
2313679Ssam #include <sys/utsname.h>
2413679Ssam #endif
2513679Ssam 
2613679Ssam #ifdef	CCWHOAMI
2713679Ssam /* Compile in 'sysname' as found in standard(!) include file */
2813679Ssam #include <whoami.h>
2913679Ssam #endif
3023686Sbloom char Myfullname[64];
3113679Ssam 
3217844Sralph /*
3313679Ssam  *	uucpname(name)		get the uucp name
3413679Ssam  *
3513679Ssam  *	return code - none
3613679Ssam  */
uucpname(name)3713679Ssam uucpname(name)
3813679Ssam register char *name;
3913679Ssam {
4023686Sbloom 	register char *s;
4113679Ssam 
4217844Sralph 	/*
4313679Ssam 	 * Since some UNIX systems do not honor the set-user-id bit
4413679Ssam 	 * when the invoking user is root, we must change the uid here.
4513679Ssam 	 * So uucp files are created with the correct owner.
4613679Ssam 	 */
4713679Ssam 	if (geteuid() == 0 && getuid() == 0) {
4813679Ssam 		struct stat stbuf;
4913679Ssam 		stbuf.st_uid = 0;	/* In case the stat fails */
5013679Ssam 		stbuf.st_gid = 0;
5113679Ssam 		stat(UUCICO, &stbuf);	/* Assume uucico is correctly owned */
5213679Ssam 		setgid(stbuf.st_gid);
5313679Ssam 		setuid(stbuf.st_uid);
5413679Ssam 	}
5513679Ssam 
5613679Ssam 	s = NULL;	/* system name unknown, so far */
5713679Ssam 
5817844Sralph #ifdef GETHOSTNAME
5913679Ssam 	if (s == NULL || *s == '\0') {
6017844Sralph #ifdef VMS
6123686Sbloom 		int i = sizeof(Myfullname);
6217844Sralph #endif VMS
6313679Ssam 
6423686Sbloom 		s = Myfullname;
6517844Sralph #ifdef VMS
6623686Sbloom 		if(gethostname(Myfullname, &i) == -1) {
6717844Sralph #else !VMS
6823686Sbloom 		if(gethostname(Myfullname, sizeof(Myfullname)) == -1) {
6917844Sralph #endif !VMS
7017844Sralph 			DEBUG(1, "gethostname", _FAILED);
7113679Ssam 			s = NULL;
7213679Ssam 		}
7313679Ssam 	}
7417844Sralph #endif GETHOSTNAME
7513679Ssam 
7613679Ssam #ifdef	UNAME
7713679Ssam 	/* Use USG library routine */
7813679Ssam 	if (s == NULL || *s == '\0') {
7913679Ssam 		struct utsname utsn;
8013679Ssam 
8113679Ssam 		if (uname(&utsn) == -1) {
8217844Sralph 			DEBUG(1, "uname", _FAILED);
8313679Ssam 			s = NULL;
8423686Sbloom 		} else {
8523686Sbloom 			strncpy(Myfullname, utsn.nodename, sizeof Myfullname);
8623686Sbloom 			s = Myfullname;
8713679Ssam 		}
8813679Ssam 	}
8913679Ssam #endif
9013679Ssam 
9113679Ssam #ifdef	WHOAMI
9213679Ssam 	/* Use fake gethostname() routine */
9313679Ssam 	if (s == NULL || *s == '\0') {
9413679Ssam 
9523686Sbloom 		s = Myfullname;
9623686Sbloom 		if (fakegethostname(Myfullname, sizeof(Myfullname)) == -1) {
9717844Sralph 			DEBUG(1, "whoami search", _FAILED);
9813679Ssam 			s = NULL;
9913679Ssam 		}
10013679Ssam 	}
10113679Ssam #endif
10213679Ssam 
10313679Ssam #ifdef	CCWHOAMI
10413679Ssam 	/* compile sysname right into uucp */
10513679Ssam 	if (s == NULL || *s == '\0') {
10613679Ssam 		s = sysname;
10725142Sbloom 		strncpy(Myfullname, s, sizeof Myfullname);
10813679Ssam 	}
10913679Ssam #endif
11013679Ssam 
11113679Ssam #ifdef	UUNAME
11213679Ssam 	/* uucp name is stored in /etc/uucpname or /local/uucpname */
11313679Ssam 	if (s == NULL || *s == '\0') {
11413679Ssam 		FILE *uucpf;
11513679Ssam 
11623686Sbloom 		s = Myfullname;
11713679Ssam 		if (((uucpf = fopen("/etc/uucpname", "r")) == NULL &&
11813679Ssam 		     (uucpf = fopen("/local/uucpname", "r")) == NULL) ||
11923686Sbloom 			fgets(Myfullname, sizeof Myfullname, uucpf) == NULL) {
12017844Sralph 				DEBUG(1, "uuname search", _FAILED);
12113679Ssam 				s = NULL;
12213679Ssam 		}
12323686Sbloom 		if (s == Myfullname) {
12423686Sbloom 			register char *p;
12523686Sbloom 			p = index(s, '\n');
12623686Sbloom 			if (p)
12723686Sbloom 				*p = '\0';
12823686Sbloom   		}
12913679Ssam 		if (uucpf != NULL)
13013679Ssam 			fclose(uucpf);
13113679Ssam 	}
13213679Ssam #endif
13313679Ssam 
13413679Ssam #ifdef	GETMYHNAME
13513679Ssam 	/* Use 3Com's getmyhname() routine */
13613679Ssam 	if (s == NULL || *s == '\0') {
13723686Sbloom 		if ((s = getmyhname()) == NULL)
13817844Sralph 			DEBUG(1, "getmyhname", _FAILED);
13923686Sbloom 		else
14023686Sbloom 			strncpy(Myfullname, s, sizeof Myfullname);
14113679Ssam 	}
14213679Ssam #endif
14313679Ssam 
14413679Ssam #ifdef	MYNAME
14513679Ssam 	if (s == NULL || *s == '\0') {
14613679Ssam 		s = MYNAME;
14725142Sbloom 		strncpy(Myfullname, s, sizeof Myfullname);
14813679Ssam 	}
14913679Ssam #endif
15013679Ssam 
15113679Ssam 	if (s == NULL || *s == '\0') {
15213679Ssam 		/*
15313679Ssam 		 * As a last ditch thing, we *could* search Spool
15413679Ssam 		 * for D.<uucpname> and use that,
15513679Ssam 		 * but that is too much trouble, isn't it?
15613679Ssam 		 */
15713679Ssam 		logent("SYSTEM NAME", "CANNOT DETERMINE");
15825142Sbloom 		strcpy(Myfullname, "unknown");
15913679Ssam 	}
16013679Ssam 
16113679Ssam 	/*
16213679Ssam 	 * copy uucpname back to caller-supplied buffer,
16323686Sbloom 	 * truncating to MAXBASENAME characters.
16423686Sbloom 	 * Also set up subdirectory names
16523686Sbloom 	 * Truncate names at '.' if found to handle cases like
16625142Sbloom 	 * seismo.css.gov being returned by gethostname().
16723686Sbloom 	 * uucp sites should not have '.' in their name anyway
16813679Ssam 	 */
16925142Sbloom 	s = index(Myfullname, '.');
17023686Sbloom 	if (s != NULL)
17123686Sbloom 		*s = '\0';
17225142Sbloom 	strncpy(name, Myfullname, MAXBASENAME);
17325142Sbloom 	name[MAXBASENAME] = '\0';
17413679Ssam 	DEBUG(1, "My uucpname = %s\n", name);
17513679Ssam 
17623686Sbloom 	sprintf(DLocal, "D.%.*s", SYSNSIZE, name);
17723686Sbloom 	sprintf(DLocalX, "D.%.*sX", SYSNSIZE, name);
17813679Ssam }
17913679Ssam 
18013679Ssam #ifdef	WHOAMI
18113679Ssam /*
18217844Sralph  * simulate the 4.2 bsd system call by reading /usr/include/whoami.h
18313679Ssam  * and looking for the #define sysname
18413679Ssam  */
18513679Ssam 
18613679Ssam #define	HDRFILE "/usr/include/whoami.h"
18713679Ssam 
fakegethostname(name,len)18813679Ssam fakegethostname(name, len)
18913679Ssam char *name;
19013679Ssam int len;
19113679Ssam {
19213679Ssam 	char buf[BUFSIZ];
19313679Ssam 	char bname[32];
19413679Ssam 	char hname[32];
19513679Ssam 	char nname[128];
19613679Ssam 	register char *p, *q, *nptr;
19713679Ssam 	int i;
19813679Ssam 	register FILE *fd;
19913679Ssam 
20013679Ssam 	fd = fopen(HDRFILE, "r");
20113679Ssam 	if (fd == NULL)
20213679Ssam 		return(-1);
20313679Ssam 
20425142Sbloom 	hname[0] = 0;
20513679Ssam 	nname[0] = 0;
20613679Ssam 	nptr = nname;
20713679Ssam 
20813679Ssam 	while (fgets(buf, sizeof buf, fd) != NULL) { /* each line in the file */
20913679Ssam 		if (sscanf(buf, "#define sysname \"%[^\"]\"", bname) == 1) {
21013679Ssam 			strcpy(hname, bname);
21113679Ssam 		} else if (sscanf(buf, "#define nickname \"%[^\"]\"", bname) == 1) {
21213679Ssam 			strcpy(nptr, bname);
21313679Ssam 			nptr += strlen(bname) + 1;
21413679Ssam 		} else if (sscanf(buf, "#define nickname%d \"%[^\"]\"", &i, bname) == 2) {
21513679Ssam 			strcpy(nptr, bname);
21613679Ssam 			nptr += strlen(bname) + 1;
21713679Ssam 		}
21813679Ssam 	}
21913679Ssam 	fclose(fd);
22013679Ssam 	if (hname[0] == 0)
22117844Sralph 		return FAIL;
22213679Ssam 	strncpy(name, hname, len);
22313679Ssam 	p = nname;
22413679Ssam 	i = strlen(hname) + 1;
22513679Ssam 	q = name + i;
22613679Ssam 	while (i < len && (p[0] != 0 || p[1] != 0)) {
22713679Ssam 		*q++ = *p++;
22813679Ssam 		i++;
22913679Ssam 	}
23013679Ssam 	*q++ = 0;
23117844Sralph 	return SUCCESS;
23213679Ssam }
23313679Ssam #endif
234