XrdSecgsiGMAPFunLDAP.cc

Go to the documentation of this file.
00001 // $Id: XrdSecgsiGMAPFunLDAP.cc 30949 2009-11-02 16:37:58Z ganis $
00002 
00003 const char *XrdSecgsiGMAPFunLDAPCVSID = "$Id: XrdSecgsiGMAPFunLDAP.cc 30949 2009-11-02 16:37:58Z ganis $";
00004 /******************************************************************************/
00005 /*                                                                            */
00006 /*             X r d S e c g s i G M A P F u n L D A P . c c                  */
00007 /*                                                                            */
00008 /* (c) 2008, G. Ganis / CERN                                                  */
00009 /*                                                                            */
00010 /******************************************************************************/
00011 
00012 /* ************************************************************************** */
00013 /*                                                                            */
00014 /* GMAP function implementation querying a LDAP database                      */
00015 /*                                                                            */
00016 /* ************************************************************************** */
00017 
00018 #include <stdio.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #include <errno.h>
00022 
00023 static char *ldapsrv    = 0;
00024 static char *searchbase = 0;
00025 static char *attribute  = 0;
00026 
00027 int XrdSecgsiGMAPInit(const char *cfg);
00028 
00029 //
00030 // Main function
00031 //
00032 extern "C"
00033 {
00034 char *XrdSecgsiGMAPFun(const char *dn, int now)
00035 {
00036    // Implementation of XrdSecgsiGMAPFun querying an LDAP server
00037    // for the distinguished name 'dn'; the unused argument is the time at
00038    // which the function is called.
00039 
00040    // Init the relevant fields (only once)
00041    if (now <= 0) {
00042       if (XrdSecgsiGMAPInit(dn) != 0)
00043          return (char *)-1;
00044       return (char *)0;
00045    }
00046 
00047    // Output
00048    char *name = 0;
00049 
00050    // Prepare the command to be executed
00051    char cmd[4096];
00052    sprintf(cmd, "ldapsearch -H %s -x -b \"%s\" \"subject=%s\" %s",
00053                  ldapsrv, searchbase, dn, attribute);
00054 
00055    // Execute the command into a pipe
00056    FILE *fp = popen(cmd, "r");
00057    if (fp) {
00058       char line[1024], att[40], uname[256];
00059       sprintf(att, "%s: ", attribute);
00060       while (fgets(line, sizeof(line), fp)) {
00061          // Look for a line starting with "uid: "
00062          if (!strncmp(line, att, strlen(att))) {
00063             sscanf(line, "%s %s", att, uname);
00064             name = strdup(uname);
00065             break;
00066          }
00067       }
00068       pclose(fp);
00069    }
00070 
00071    // Done
00072    return name;
00073 }}
00074 
00075 //
00076 // Init the relevant parameters from a dedicated config file
00077 //
00078 int XrdSecgsiGMAPInit(const char *cfg)
00079 {
00080    // Initialize the relevant parameters from the file 'cfg' or
00081    // from the one defined by XRDGSIGMAPLDAPCF.
00082    // Return 0 on success, -1 otherwise
00083 
00084    if (!cfg) cfg = getenv("XRDGSIGMAPLDAPCF");
00085    if (!cfg || strlen(cfg) <= 0) {
00086       fprintf(stderr, " +++ XrdSecgsiGMAPInit (LDAP): error: undefined config file path +++\n");
00087       return -1;
00088    }
00089 
00090    FILE *fcf = fopen(cfg, "r");
00091    if (fcf) {
00092       char l[4096], k[20], val[4096];
00093       while (fgets(l, sizeof(l), fcf)) {
00094          int len = strlen(l);
00095          if (len < 2) continue;
00096          if (l[0] == '#') continue;
00097          if (l[len-1] == '\n') l[len-1] = '\0';
00098          sscanf(l, "%s %s", k, val);
00099          if (!strcmp(k, "srv:")) {
00100             ldapsrv = strdup(val);
00101          } else if (!strcmp(k, "base:")) {
00102             searchbase = strdup(val);
00103          } else if (!strcmp(k, "attr:")) {
00104             attribute = strdup(val);
00105          } else {
00106             fprintf(stderr, "XrdSecgsiGMAPInit (LDAP): warning: unknown key: '%s' - ignoring\n", k);
00107          }
00108       }
00109       fclose(fcf);
00110    } else {
00111       fprintf(stderr, " +++ XrdSecgsiGMAPInit (LDAP): error: config file '%s'"
00112                       " could not be open (errno: %d) +++\n", cfg, errno);
00113       return -1;
00114    }
00115    // Done
00116    return 0;
00117 }

Generated on Tue Jul 5 14:46:58 2011 for ROOT_528-00b_version by  doxygen 1.5.1