113679Ssam #ifndef lint 2*23686Sbloom static char sccsid[] = "@(#)uucpname.c 5.4 (Berkeley) 06/23/85"; 313679Ssam #endif 413679Ssam 513679Ssam #include "uucp.h" 613679Ssam #include <sys/stat.h> 713679Ssam 8*23686Sbloom /*LINTLIBRARY*/ 9*23686Sbloom 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 23*23686Sbloom 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 { 33*23686Sbloom 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 54*23686Sbloom int i = sizeof(Myfullname); 5517844Sralph #endif VMS 5613679Ssam 57*23686Sbloom s = Myfullname; 5817844Sralph #ifdef VMS 59*23686Sbloom if(gethostname(Myfullname, &i) == -1) { 6017844Sralph #else !VMS 61*23686Sbloom 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; 77*23686Sbloom } else { 78*23686Sbloom strncpy(Myfullname, utsn.nodename, sizeof Myfullname); 79*23686Sbloom s = Myfullname; 8013679Ssam } 8113679Ssam } 8213679Ssam #endif 8313679Ssam 8413679Ssam #ifdef WHOAMI 8513679Ssam /* Use fake gethostname() routine */ 8613679Ssam if (s == NULL || *s == '\0') { 8713679Ssam 88*23686Sbloom s = Myfullname; 89*23686Sbloom 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; 10013679Ssam } 10113679Ssam #endif 10213679Ssam 10313679Ssam #ifdef UUNAME 10413679Ssam /* uucp name is stored in /etc/uucpname or /local/uucpname */ 10513679Ssam if (s == NULL || *s == '\0') { 10613679Ssam FILE *uucpf; 10713679Ssam 108*23686Sbloom s = Myfullname; 10913679Ssam if (((uucpf = fopen("/etc/uucpname", "r")) == NULL && 11013679Ssam (uucpf = fopen("/local/uucpname", "r")) == NULL) || 111*23686Sbloom fgets(Myfullname, sizeof Myfullname, uucpf) == NULL) { 11217844Sralph DEBUG(1, "uuname search", _FAILED); 11313679Ssam s = NULL; 11413679Ssam } 115*23686Sbloom if (s == Myfullname) { 116*23686Sbloom register char *p; 117*23686Sbloom p = index(s, '\n'); 118*23686Sbloom if (p) 119*23686Sbloom *p = '\0'; 120*23686Sbloom } 12113679Ssam if (uucpf != NULL) 12213679Ssam fclose(uucpf); 12313679Ssam } 12413679Ssam #endif 12513679Ssam 12613679Ssam #ifdef GETMYHNAME 12713679Ssam /* Use 3Com's getmyhname() routine */ 12813679Ssam if (s == NULL || *s == '\0') { 129*23686Sbloom if ((s = getmyhname()) == NULL) 13017844Sralph DEBUG(1, "getmyhname", _FAILED); 131*23686Sbloom else 132*23686Sbloom strncpy(Myfullname, s, sizeof Myfullname); 13313679Ssam } 13413679Ssam #endif 13513679Ssam 13613679Ssam #ifdef MYNAME 13713679Ssam if (s == NULL || *s == '\0') { 13813679Ssam s = MYNAME; 13913679Ssam } 14013679Ssam #endif 14113679Ssam 14213679Ssam if (s == NULL || *s == '\0') { 14313679Ssam /* 14413679Ssam * As a last ditch thing, we *could* search Spool 14513679Ssam * for D.<uucpname> and use that, 14613679Ssam * but that is too much trouble, isn't it? 14713679Ssam */ 14813679Ssam logent("SYSTEM NAME", "CANNOT DETERMINE"); 14913679Ssam s = "unknown"; 15013679Ssam } 15113679Ssam 15213679Ssam /* 15313679Ssam * copy uucpname back to caller-supplied buffer, 154*23686Sbloom * truncating to MAXBASENAME characters. 155*23686Sbloom * Also set up subdirectory names 156*23686Sbloom * Truncate names at '.' if found to handle cases like 157*23686Sbloom * seismo.arpa being returned by gethostname(). 158*23686Sbloom * uucp sites should not have '.' in their name anyway 15913679Ssam */ 160*23686Sbloom strncpy(name, s, MAXBASENAME); 161*23686Sbloom name[MAXBASENAME] = '\0'; 162*23686Sbloom s = index(name, '.'); 163*23686Sbloom if (s != NULL) 164*23686Sbloom *s = '\0'; 16513679Ssam DEBUG(1, "My uucpname = %s\n", name); 16613679Ssam 167*23686Sbloom sprintf(DLocal, "D.%.*s", SYSNSIZE, name); 168*23686Sbloom sprintf(DLocalX, "D.%.*sX", SYSNSIZE, name); 16913679Ssam } 17013679Ssam 17113679Ssam #ifdef WHOAMI 17213679Ssam /* 17317844Sralph * simulate the 4.2 bsd system call by reading /usr/include/whoami.h 17413679Ssam * and looking for the #define sysname 17513679Ssam */ 17613679Ssam 17713679Ssam #define HDRFILE "/usr/include/whoami.h" 17813679Ssam 17913679Ssam fakegethostname(name, len) 18013679Ssam char *name; 18113679Ssam int len; 18213679Ssam { 18313679Ssam char buf[BUFSIZ]; 18413679Ssam char bname[32]; 18513679Ssam char hname[32]; 18613679Ssam char nname[128]; 18713679Ssam register char *p, *q, *nptr; 18813679Ssam int i; 18913679Ssam register FILE *fd; 19013679Ssam 19113679Ssam fd = fopen(HDRFILE, "r"); 19213679Ssam if (fd == NULL) 19313679Ssam return(-1); 19413679Ssam 19513679Ssam hname[0] = 0; /* rti!trt: was "hostunknown" */ 19613679Ssam nname[0] = 0; 19713679Ssam nptr = nname; 19813679Ssam 19913679Ssam while (fgets(buf, sizeof buf, fd) != NULL) { /* each line in the file */ 20013679Ssam if (sscanf(buf, "#define sysname \"%[^\"]\"", bname) == 1) { 20113679Ssam strcpy(hname, bname); 20213679Ssam } else if (sscanf(buf, "#define nickname \"%[^\"]\"", bname) == 1) { 20313679Ssam strcpy(nptr, bname); 20413679Ssam nptr += strlen(bname) + 1; 20513679Ssam } else if (sscanf(buf, "#define nickname%d \"%[^\"]\"", &i, bname) == 2) { 20613679Ssam strcpy(nptr, bname); 20713679Ssam nptr += strlen(bname) + 1; 20813679Ssam } 20913679Ssam } 21013679Ssam fclose(fd); 21113679Ssam if (hname[0] == 0) 21217844Sralph return FAIL; 21313679Ssam strncpy(name, hname, len); 21413679Ssam p = nname; 21513679Ssam i = strlen(hname) + 1; 21613679Ssam q = name + i; 21713679Ssam while (i < len && (p[0] != 0 || p[1] != 0)) { 21813679Ssam *q++ = *p++; 21913679Ssam i++; 22013679Ssam } 22113679Ssam *q++ = 0; 22217844Sralph return SUCCESS; 22313679Ssam } 22413679Ssam #endif 225