113679Ssam #ifndef lint 2*25142Sbloom static char sccsid[] = "@(#)uucpname.c 5.5 (Berkeley) 10/09/85"; 313679Ssam #endif 413679Ssam 513679Ssam #include "uucp.h" 613679Ssam #include <sys/stat.h> 713679Ssam 823686Sbloom /*LINTLIBRARY*/ 923686Sbloom 1013679Ssam #ifdef GETMYHNAME 1113679Ssam #include <UNET/unetio.h> 1213679Ssam #endif 1313679Ssam 1413679Ssam #ifdef UNAME 1513679Ssam /* Use USG uname() library routine */ 1613679Ssam #include <sys/utsname.h> 1713679Ssam #endif 1813679Ssam 1913679Ssam #ifdef CCWHOAMI 2013679Ssam /* Compile in 'sysname' as found in standard(!) include file */ 2113679Ssam #include <whoami.h> 2213679Ssam #endif 2323686Sbloom char Myfullname[64]; 2413679Ssam 2517844Sralph /* 2613679Ssam * uucpname(name) get the uucp name 2713679Ssam * 2813679Ssam * return code - none 2913679Ssam */ 3013679Ssam uucpname(name) 3113679Ssam register char *name; 3213679Ssam { 3323686Sbloom register char *s; 3413679Ssam 3517844Sralph /* 3613679Ssam * Since some UNIX systems do not honor the set-user-id bit 3713679Ssam * when the invoking user is root, we must change the uid here. 3813679Ssam * So uucp files are created with the correct owner. 3913679Ssam */ 4013679Ssam if (geteuid() == 0 && getuid() == 0) { 4113679Ssam struct stat stbuf; 4213679Ssam stbuf.st_uid = 0; /* In case the stat fails */ 4313679Ssam stbuf.st_gid = 0; 4413679Ssam stat(UUCICO, &stbuf); /* Assume uucico is correctly owned */ 4513679Ssam setgid(stbuf.st_gid); 4613679Ssam setuid(stbuf.st_uid); 4713679Ssam } 4813679Ssam 4913679Ssam s = NULL; /* system name unknown, so far */ 5013679Ssam 5117844Sralph #ifdef GETHOSTNAME 5213679Ssam if (s == NULL || *s == '\0') { 5317844Sralph #ifdef VMS 5423686Sbloom int i = sizeof(Myfullname); 5517844Sralph #endif VMS 5613679Ssam 5723686Sbloom s = Myfullname; 5817844Sralph #ifdef VMS 5923686Sbloom if(gethostname(Myfullname, &i) == -1) { 6017844Sralph #else !VMS 6123686Sbloom if(gethostname(Myfullname, sizeof(Myfullname)) == -1) { 6217844Sralph #endif !VMS 6317844Sralph DEBUG(1, "gethostname", _FAILED); 6413679Ssam s = NULL; 6513679Ssam } 6613679Ssam } 6717844Sralph #endif GETHOSTNAME 6813679Ssam 6913679Ssam #ifdef UNAME 7013679Ssam /* Use USG library routine */ 7113679Ssam if (s == NULL || *s == '\0') { 7213679Ssam struct utsname utsn; 7313679Ssam 7413679Ssam if (uname(&utsn) == -1) { 7517844Sralph DEBUG(1, "uname", _FAILED); 7613679Ssam s = NULL; 7723686Sbloom } else { 7823686Sbloom strncpy(Myfullname, utsn.nodename, sizeof Myfullname); 7923686Sbloom s = Myfullname; 8013679Ssam } 8113679Ssam } 8213679Ssam #endif 8313679Ssam 8413679Ssam #ifdef WHOAMI 8513679Ssam /* Use fake gethostname() routine */ 8613679Ssam if (s == NULL || *s == '\0') { 8713679Ssam 8823686Sbloom s = Myfullname; 8923686Sbloom if (fakegethostname(Myfullname, sizeof(Myfullname)) == -1) { 9017844Sralph DEBUG(1, "whoami search", _FAILED); 9113679Ssam s = NULL; 9213679Ssam } 9313679Ssam } 9413679Ssam #endif 9513679Ssam 9613679Ssam #ifdef CCWHOAMI 9713679Ssam /* compile sysname right into uucp */ 9813679Ssam if (s == NULL || *s == '\0') { 9913679Ssam s = sysname; 100*25142Sbloom strncpy(Myfullname, s, sizeof Myfullname); 10113679Ssam } 10213679Ssam #endif 10313679Ssam 10413679Ssam #ifdef UUNAME 10513679Ssam /* uucp name is stored in /etc/uucpname or /local/uucpname */ 10613679Ssam if (s == NULL || *s == '\0') { 10713679Ssam FILE *uucpf; 10813679Ssam 10923686Sbloom s = Myfullname; 11013679Ssam if (((uucpf = fopen("/etc/uucpname", "r")) == NULL && 11113679Ssam (uucpf = fopen("/local/uucpname", "r")) == NULL) || 11223686Sbloom fgets(Myfullname, sizeof Myfullname, uucpf) == NULL) { 11317844Sralph DEBUG(1, "uuname search", _FAILED); 11413679Ssam s = NULL; 11513679Ssam } 11623686Sbloom if (s == Myfullname) { 11723686Sbloom register char *p; 11823686Sbloom p = index(s, '\n'); 11923686Sbloom if (p) 12023686Sbloom *p = '\0'; 12123686Sbloom } 12213679Ssam if (uucpf != NULL) 12313679Ssam fclose(uucpf); 12413679Ssam } 12513679Ssam #endif 12613679Ssam 12713679Ssam #ifdef GETMYHNAME 12813679Ssam /* Use 3Com's getmyhname() routine */ 12913679Ssam if (s == NULL || *s == '\0') { 13023686Sbloom if ((s = getmyhname()) == NULL) 13117844Sralph DEBUG(1, "getmyhname", _FAILED); 13223686Sbloom else 13323686Sbloom strncpy(Myfullname, s, sizeof Myfullname); 13413679Ssam } 13513679Ssam #endif 13613679Ssam 13713679Ssam #ifdef MYNAME 13813679Ssam if (s == NULL || *s == '\0') { 13913679Ssam s = MYNAME; 140*25142Sbloom strncpy(Myfullname, s, sizeof Myfullname); 14113679Ssam } 14213679Ssam #endif 14313679Ssam 14413679Ssam if (s == NULL || *s == '\0') { 14513679Ssam /* 14613679Ssam * As a last ditch thing, we *could* search Spool 14713679Ssam * for D.<uucpname> and use that, 14813679Ssam * but that is too much trouble, isn't it? 14913679Ssam */ 15013679Ssam logent("SYSTEM NAME", "CANNOT DETERMINE"); 151*25142Sbloom strcpy(Myfullname, "unknown"); 15213679Ssam } 15313679Ssam 15413679Ssam /* 15513679Ssam * copy uucpname back to caller-supplied buffer, 15623686Sbloom * truncating to MAXBASENAME characters. 15723686Sbloom * Also set up subdirectory names 15823686Sbloom * Truncate names at '.' if found to handle cases like 159*25142Sbloom * seismo.css.gov being returned by gethostname(). 16023686Sbloom * uucp sites should not have '.' in their name anyway 16113679Ssam */ 162*25142Sbloom s = index(Myfullname, '.'); 16323686Sbloom if (s != NULL) 16423686Sbloom *s = '\0'; 165*25142Sbloom strncpy(name, Myfullname, MAXBASENAME); 166*25142Sbloom name[MAXBASENAME] = '\0'; 16713679Ssam DEBUG(1, "My uucpname = %s\n", name); 16813679Ssam 16923686Sbloom sprintf(DLocal, "D.%.*s", SYSNSIZE, name); 17023686Sbloom sprintf(DLocalX, "D.%.*sX", SYSNSIZE, name); 17113679Ssam } 17213679Ssam 17313679Ssam #ifdef WHOAMI 17413679Ssam /* 17517844Sralph * simulate the 4.2 bsd system call by reading /usr/include/whoami.h 17613679Ssam * and looking for the #define sysname 17713679Ssam */ 17813679Ssam 17913679Ssam #define HDRFILE "/usr/include/whoami.h" 18013679Ssam 18113679Ssam fakegethostname(name, len) 18213679Ssam char *name; 18313679Ssam int len; 18413679Ssam { 18513679Ssam char buf[BUFSIZ]; 18613679Ssam char bname[32]; 18713679Ssam char hname[32]; 18813679Ssam char nname[128]; 18913679Ssam register char *p, *q, *nptr; 19013679Ssam int i; 19113679Ssam register FILE *fd; 19213679Ssam 19313679Ssam fd = fopen(HDRFILE, "r"); 19413679Ssam if (fd == NULL) 19513679Ssam return(-1); 19613679Ssam 197*25142Sbloom hname[0] = 0; 19813679Ssam nname[0] = 0; 19913679Ssam nptr = nname; 20013679Ssam 20113679Ssam while (fgets(buf, sizeof buf, fd) != NULL) { /* each line in the file */ 20213679Ssam if (sscanf(buf, "#define sysname \"%[^\"]\"", bname) == 1) { 20313679Ssam strcpy(hname, bname); 20413679Ssam } else if (sscanf(buf, "#define nickname \"%[^\"]\"", bname) == 1) { 20513679Ssam strcpy(nptr, bname); 20613679Ssam nptr += strlen(bname) + 1; 20713679Ssam } else if (sscanf(buf, "#define nickname%d \"%[^\"]\"", &i, bname) == 2) { 20813679Ssam strcpy(nptr, bname); 20913679Ssam nptr += strlen(bname) + 1; 21013679Ssam } 21113679Ssam } 21213679Ssam fclose(fd); 21313679Ssam if (hname[0] == 0) 21417844Sralph return FAIL; 21513679Ssam strncpy(name, hname, len); 21613679Ssam p = nname; 21713679Ssam i = strlen(hname) + 1; 21813679Ssam q = name + i; 21913679Ssam while (i < len && (p[0] != 0 || p[1] != 0)) { 22013679Ssam *q++ = *p++; 22113679Ssam i++; 22213679Ssam } 22313679Ssam *q++ = 0; 22417844Sralph return SUCCESS; 22513679Ssam } 22613679Ssam #endif 227