return NSS_STATUS_UNAVAIL if used without nscd 2.0
authorAdam Megacz <adam@megacz.com>
Wed, 9 Dec 2009 01:10:22 +0000 (17:10 -0800)
committerAdam Megacz <adam@megacz.com>
Wed, 9 Dec 2009 01:10:22 +0000 (17:10 -0800)
debian/changelog
nss_afs.c

index 02c1e1a..7136f1b 100644 (file)
@@ -1,3 +1,9 @@
+libnss-afs (2.0) unstable; urgency=low
+
+  * return NOT_FOUND if invoked outside of nscd
+
+ -- Adam Megacz <adam@megacz.com>  Tue,  8 Dec 2009 13:48:04 -0800
+
 libnss-afs (1.10) unstable; urgency=low
 
   * remember to release mutex if we fail due to empty ThisCell
index 9c9740c..de52438 100644 (file)
--- a/nss_afs.c
+++ b/nss_afs.c
@@ -116,7 +116,7 @@ enum nss_status ptsid2name(int uid, char **buffer, int *buflen) {
   idlist lid;
   namelist lnames;
 
-  init_afs();
+  if (init_afs()) return NSS_STATUS_UNAVAIL;
 
   if (uid==AFS_MAGIC_ANONYMOUS_USERID) {
     if (!cpstr("anonymous", buffer, buflen)) return NSS_STATUS_UNAVAIL;
@@ -162,7 +162,7 @@ enum nss_status ptsname2id(char *name, uid_t* uid) {
   namelist lnames;
   char uname[MAXUSERNAMELEN];
 
-  init_afs();
+  if (init_afs()) return NSS_STATUS_UNAVAIL;
   
   if (!strcmp(name,"anonymous")) {
     *uid = AFS_MAGIC_ANONYMOUS_USERID;
@@ -199,11 +199,35 @@ int init_afs() {
   int len;
   struct stat statbuf;
 
+  char buf[6];
+  int fd;
+  int pos;
+
   if (afs_initialized) {
     /* wait until /afs/@cell/ appears as a proxy for "the network is up" */
     if (stat(cell_root, &statbuf)) return -1;
     return 0;
   }
+
+  // check to make sure that we are running inside nscd
+  pos = 0;
+  fd = open("/proc/self/cmdline", O_RDONLY);
+  if (fd==-1) return -1;
+  while(1) {
+    int numread;
+    numread = read(fd, buf+pos, 1);
+    if (buf[ (pos+5)%6 ] == 'd' &&
+        buf[ (pos+4)%6 ] == 'c' &&
+        buf[ (pos+3)%6 ] == 's' &&
+        buf[ (pos+2)%6 ] == 'n' &&
+        (buf[(pos+1)%6 ] == '/' || pos==4) &&
+        (buf[(pos+0)%6 ] ==  0  || numread==-1)
+        )
+      break;
+    pos = (pos+1)%6;
+    if (numread==0) { close(fd); return -1; }
+  }
+  close(fd);
   
   if (pthread_mutex_lock(&mutex)) return -1;
   do {