Initial import.
[utils.git] / krb5mapcc.c
diff --git a/krb5mapcc.c b/krb5mapcc.c
new file mode 100644 (file)
index 0000000..0cc3418
--- /dev/null
@@ -0,0 +1,95 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <ctype.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <string.h>
+
+int isnumeric(char *str)
+{
+    while(*str) {
+       if(!isdigit(*str))
+           return(0);
+       str++;
+    }
+    return(1);
+}
+
+char *findcc(int fd)
+{
+    int ret, dlen;
+    static char retbuf[1024];
+    char readbuf[1024];
+    char *p, *p2;
+    
+    dlen = 0;
+    p = readbuf;
+    do {
+       if(dlen == 1024)
+           dlen = 0;
+       ret = read(fd, readbuf + dlen, sizeof(readbuf) - dlen);
+       if(ret < 0) {
+           perror("read environ");
+           return(NULL);
+       }
+       dlen += ret;
+       
+       while((p = memchr(readbuf, 0, dlen)) != NULL) {
+           if((p2 = strchr(readbuf, '=')) != NULL) {
+               *(p2++) = 0;
+               if(!strcasecmp(readbuf, "KRB5CCNAME") && !strncasecmp(p2, "FILE:", 5)) {
+                   if(strlen(p2 + 5) >= sizeof(retbuf)) {
+                       fprintf(stderr, "really long ccname!\n");
+                       return(NULL);
+                   }
+                   strcpy(retbuf, p2 + 5);
+                   return(retbuf);
+               }
+           }
+           p++;
+           memmove(readbuf, p, dlen -= (p - readbuf));
+       }
+    } while(ret != 0);
+    return(NULL);
+}
+
+int main(int argc, char **argv)
+{
+    int fd;
+    DIR *proc;
+    struct dirent *de;
+    char envbuf[1024];
+    char *p, *cc;
+    
+    if((proc = opendir("/proc")) == NULL) {
+       perror("/proc");
+       exit(1);
+    }
+    while((de = readdir(proc)) != NULL) {
+       if(!isnumeric(de->d_name))
+           continue;
+       if(strlen(de->d_name) > 30)
+           continue;
+       
+       p = envbuf;
+       strcpy(p, "/proc/");
+       p += 6;
+       strcpy(p, de->d_name);
+       p += strlen(de->d_name);
+       *(p++) = '/';
+       strcpy(p, "environ");
+       
+       if((fd = open(envbuf, O_RDONLY)) < 0)
+           continue;
+       cc = findcc(fd);
+       close(fd);
+       
+       if(cc != NULL) {
+           printf("%s %s\n", de->d_name, cc);
+       }
+    }
+    closedir(proc);
+    return(0);
+}