+
+/*
+** NISPLUS_GETCANONNAME -- look up canonical name in NIS+
+*/
+
+bool
+nisplus_getcanonname(name, hbsize, statp)
+ char *name;
+ int hbsize;
+ int *statp;
+{
+ char *vp;
+ auto int vsize;
+ int buflen;
+ char buf1[MAXLINE + NIS_MAXNAMELEN];
+ static char *nis_path = NULL;
+ static char nis_path_buf[MAXLINE];
+ nis_result *result;
+ char *p;
+ int len;
+
+ if (nis_path == NULL)
+ {
+ char *ptr;
+ char pathbuf[MAXLINE];
+
+ strcpy(buf1, macvalue('m', CurEnv));
+ strcat(buf1, ".");
+ /* try the host tabe in $m */
+ if (hosts_table_ok(buf1))
+ strcpy(pathbuf, buf1);
+ else
+ strcpy(pathbuf, "$");
+
+ nis_path = nis_path_buf;
+ sprintf(nis_path, "NIS_PATH=%s", pathbuf);
+ }
+
+ if (nis_path[0] != '\0')
+ putenv(nis_path);
+ else
+ syslog(LOG_WARNING, "no NIS+ path defined");
+
+ shorten_hostname(name);
+
+ p = strchr(name, '.');
+ if (p == NULL)
+ {
+ /* single token */
+ sprintf(buf1, "[name=%s],hosts.org_dir", name);
+ }
+ else if (p[1] != '\0')
+ {
+ /* multi token -- take only first token in name buf */
+ *p = '\0';
+ sprintf(buf1, "[name=%s],hosts.org_dir.%s", name, &p[1]);
+ }
+ else
+ {
+ *statp = EX_NOHOST;
+ return FALSE;
+ }
+
+ if (tTd(38, 20))
+ printf("\nnisplus_getcanoname(%s), qbuf=%s\n%s\n",
+ name, buf1, nis_path);
+
+ result = nis_list(buf1, EXPAND_NAME|FOLLOW_LINKS|FOLLOW_PATH,
+ NULL, NULL);
+
+ /* unset NIS_PATH, just in case */
+ unsetenv("NIS_PATH");
+
+ if (result->status == NIS_SUCCESS)
+ {
+ int count;
+ char *str;
+ char *domain;
+
+ if ((count = NIS_RES_NUMOBJ(result)) != 1)
+ {
+#ifdef LOG
+ if (LogLevel > 10)
+ syslog(LOG_WARNING,
+ "nisplus_getcanonname: Lookup error, expected 1 entry, got (%d)",
+ count);
+#endif
+
+ /* ignore second entry */
+ if (tTd(38, 20))
+ printf("nisplus_getcanoname(%s), got %d entries, addtional entries ignores\n", name);
+ }
+
+ if (tTd(38, 20))
+ printf("nisplus_getcanoname(%s), found in directory \"%s\"\n",
+ name, (NIS_RES_OBJECT(result))->zo_domain);
+
+
+ vp = ((NIS_RES_OBJECT(result))->EN_col(0));
+ vsize = strlen(vp);
+ if (tTd(38, 20))
+ printf("nisplus_getcanonname(%s), found %s\n",
+ name, vp);
+ domain = macvalue('m', CurEnv);
+ if (hbsize > (vsize + ((int) strlen(domain))))
+ {
+ sprintf(name, "%s.%s", vp, domain);
+ *statp = EX_OK;
+ }
+ else
+ *statp = EX_NOHOST;
+ nis_freeresult(result);
+ return TRUE;
+ }
+ else
+ {
+ if (result->status == NIS_NOTFOUND)
+ *statp = EX_NOHOST;
+ else if (result->status == NIS_TRYAGAIN)
+ *statp = EX_TEMPFAIL;
+ else
+ {
+ *statp = EX_UNAVAILABLE;
+ }
+ }
+ if (tTd(38, 20))
+ printf("nisplus_getcanonname(%s), failed, status=%d, nsw_stat=%d\n",
+ name, result->status, *statp);
+ nis_freeresult(result);
+ return FALSE;
+}
+
+