+/*
+ * given a process ID, get the value of the KRB5CCNAME environment variable
+ * in the context of that process. On error, just return NULL.
+ */
+static char *
+get_krb5_ccname(pid_t pid)
+{
+ int fd;
+ ssize_t len, left;
+
+ /*
+ * FIXME: sysconf for ARG_MAX instead? Kernel seems to be limited to a
+ * page however, so it may not matter.
+ */
+ char buf[4096];
+ char *p, *value = NULL;
+
+ buf[4095] = '\0';
+ snprintf(buf, 4095, "/proc/%d/environ", pid);
+ fd = open(buf, O_RDONLY);
+ if (fd < 0)
+ return NULL;
+
+ /* FIXME: don't assume that we get it all in the first read? */
+ len = read(fd, buf, 4096);
+ close(fd);
+ if (len < 0)
+ return NULL;
+
+ left = len;
+ p = buf;
+
+ /* can't have valid KRB5CCNAME if there are < 13 bytes left */
+ while (left > 12) {
+ if (strncmp("KRB5CCNAME=", p, 11)) {
+ p += strnlen(p, left);
+ ++p;
+ left = buf + len - p;
+ continue;
+ }
+ p += 11;
+ left -= 11;
+ value = strndup(p, left);
+ break;
+ }
+ return value;
+}
+