+ if (Debug == 0)
+ return; /* Gotta be in debug to come here. */
+
+ /*
+ * If we haven't opened a file already, we can just return if it's
+ * alright to use the stderr we came in with. We can if:
+ *
+ * Role == MASTER, and Stderr is a regular file, a TTY or a pipe.
+ *
+ * Caution: Detecting when stderr is a pipe is tricky, because the 4.2
+ * man page for fstat(2) disagrees with reality, and System V leaves it
+ * undefined, which means different implementations act differently.
+ */
+ if (!auditopen && Role == MASTER) {
+ if (isatty(fileno(stderr)))
+ return;
+ else if (fstat(fileno(stderr), &stbuf) == 0) {
+#ifdef USG
+ /* Is Regular File or Fifo */
+ if ((stbuf.st_mode & 0060000) == 0)
+ return;
+#else !USG
+#ifdef BSD4_2
+ /* Is Regular File */
+ if ((stbuf.st_mode & S_IFMT) == S_IFREG ||
+ stbuf.st_mode == 0) /* Is a pipe */
+ return;
+#else !BSD4_2
+ /* Is Regular File or Pipe */
+ if ((stbuf.st_mode & S_IFMT) == S_IFREG)
+ return;
+#endif BSD4_2
+#endif USG
+ }
+ }
+
+ /*
+ * We need RMTDEBUG directory to do auditing. If the file doesn't exist,
+ * then we forget about debugging; if it exists but has improper owner-
+ * ship or modes, we gripe about it in ERRLOG.
+ */
+ if (stat(RMTDEBUG, &stbuf) != SUCCESS) {
+ Debug = 0;
+ return;
+ }
+ if ((geteuid() != stbuf.st_uid) || /* We must own it */
+ ((stbuf.st_mode & 0170700) != 040700)) { /* Directory, rwx */
+ Debug = 0;
+ assert("INVALID RMTDEBUG DIRECTORY:", RMTDEBUG, stbuf.st_mode);
+ return;
+ }
+
+ if (parm == DBG_TEMP) {
+ sprintf(buf, "%s/%d", RMTDEBUG, getpid());
+ temp = malloc(strlen (buf) + 1);
+ if (temp == CNULL) {
+ Debug = 0;
+ assert("RMTDEBUG MALLOC ERROR:", temp, errno);
+ cleanup(1);
+ }
+ strcpy(temp, buf);
+ } else
+ sprintf(buf, "%s/%s", RMTDEBUG, Rmtname);
+
+ unlink(buf);
+ if (freopen(buf, "w", stderr) != stderr) {
+ Debug = 0;
+ assert("FAILED RMTDEBUG FILE OPEN:", buf, errno);
+ cleanup(1);
+ }
+ setbuf(stderr, CNULL);
+ auditopen = 1;
+}