Back to index

openldap  2.4.31
netgroup.c
Go to the documentation of this file.
00001 /* netgroup.c - netgroup lookup routines */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 
00004  *
00005  * Copyright 2008-2012 The OpenLDAP Foundation.
00006  * Portions Copyright 2008 by Howard Chu, Symas Corp.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This code references portions of the nss-ldapd package
00019  * written by Arthur de Jong. The nss-ldapd code was forked
00020  * from the nss-ldap library written by Luke Howard.
00021  */
00022 
00023 #include "nssov.h"
00024 #include <ac/ctype.h>
00025 
00026 /* ( nisSchema.2.8 NAME 'nisNetgroup' SUP top STRUCTURAL
00027  *   DESC 'Abstraction of a netgroup. May refer to other netgroups'
00028  *   MUST cn
00029  *   MAY ( nisNetgroupTriple $ memberNisNetgroup $ description ) )
00030  */
00031 
00032 /* the basic search filter for searches */
00033 static struct berval netgroup_filter = BER_BVC("(objectClass=nisNetgroup)");
00034 
00035 /* the attributes to request with searches */
00036 static struct berval netgroup_keys[] = {
00037        BER_BVC("cn"),
00038        BER_BVC("nisNetgroupTriple"),
00039        BER_BVC("memberNisNetgroup"),
00040        BER_BVNULL
00041 };
00042 
00043 NSSOV_INIT(netgroup)
00044 
00045 NSSOV_CBPRIV(netgroup,
00046        char buf[256];
00047        struct berval name;);
00048 
00049 static int write_string_stripspace_len(TFILE *fp,const char *str,int len)
00050 {
00051        int32_t tmpint32;
00052        int i,j;
00053        DEBUG_PRINT("WRITE_STRING: var="__STRING(str)" string=\"%s\"",str);
00054        if (str==NULL)
00055        {
00056               WRITE_INT32(fp,0);
00057        }
00058        else
00059        {
00060               /* skip leading spaces */
00061               for (i=0;(str[i]!='\0')&&(isspace(str[i]));i++)
00062                      /* nothing else to do */ ;
00063               /* skip trailing spaces */
00064               for (j=len;(j>i)&&(isspace(str[j-1]));j--)
00065                      /* nothing else to do */ ;
00066               /* write length of string */
00067               WRITE_INT32(fp,j-i);
00068               /* write string itself */
00069               if (j>i)
00070               {
00071                      WRITE(fp,str+i,j-i);
00072               }
00073        }
00074        /* we're done */
00075        return 0;
00076 }
00077 
00078 #define WRITE_STRING_STRIPSPACE_LEN(fp,str,len) \
00079        if (write_string_stripspace_len(fp,str,len)) \
00080               return -1;
00081 
00082 #define WRITE_STRING_STRIPSPACE(fp,str) \
00083        WRITE_STRING_STRIPSPACE_LEN(fp,str,strlen(str))
00084 
00085 static int write_netgroup_triple(TFILE *fp,const char *triple)
00086 {
00087        int32_t tmpint32;
00088        int i;
00089        int hostb,hoste,userb,usere,domainb,domaine;
00090        /* skip leading spaces */
00091        for (i=0;(triple[i]!='\0')&&(isspace(triple[i]));i++)
00092               /* nothing else to do */ ;
00093        /* we should have a bracket now */
00094        if (triple[i]!='(')
00095        {
00096               Debug(LDAP_DEBUG_ANY,"write_netgroup_triple(): entry does not begin with '(' (entry skipped)\n",0,0,0);
00097               return 0;
00098        }
00099        i++;
00100        hostb=i;
00101        /* find comma (end of host string) */
00102        for (;(triple[i]!='\0')&&(triple[i]!=',');i++)
00103               /* nothing else to do */ ;
00104        if (triple[i]!=',')
00105        {
00106               Debug(LDAP_DEBUG_ANY,"write_netgroup_triple(): missing ',' (entry skipped)\n",0,0,0);
00107               return 0;
00108        }
00109        hoste=i;
00110        i++;
00111        userb=i;
00112        /* find comma (end of user string) */
00113        for (;(triple[i]!='\0')&&(triple[i]!=',');i++)
00114               /* nothing else to do */ ;
00115        if (triple[i]!=',')
00116        {
00117               Debug(LDAP_DEBUG_ANY,"write_netgroup_triple(): missing ',' (entry skipped)\n",0,0,0);
00118               return 0;
00119        }
00120        usere=i;
00121        i++;
00122        domainb=i;
00123        /* find closing bracket (end of domain string) */
00124        for (;(triple[i]!='\0')&&(triple[i]!=')');i++)
00125               /* nothing else to do */ ;
00126        if (triple[i]!=')')
00127        {
00128               Debug(LDAP_DEBUG_ANY,"write_netgroup_triple(): missing ')' (entry skipped)\n",0,0,0);
00129               return 0;
00130        }
00131        domaine=i;
00132        i++;
00133        /* skip trailing spaces */
00134        for (;(triple[i]!='\0')&&(isspace(triple[i]));i++)
00135               /* nothing else to do */ ;
00136        /* if anything is left in the string we have a problem */
00137        if (triple[i]!='\0')
00138        {
00139               Debug(LDAP_DEBUG_ANY,"write_netgroup_triple(): string contains trailing data (entry skipped)\n",0,0,0);
00140               return 0;
00141        }
00142        /* write strings */
00143        WRITE_INT32(fp,NSLCD_RESULT_BEGIN);
00144        WRITE_INT32(fp,NSLCD_NETGROUP_TYPE_TRIPLE);
00145        WRITE_STRING_STRIPSPACE_LEN(fp,triple+hostb,hoste-hostb)
00146        WRITE_STRING_STRIPSPACE_LEN(fp,triple+userb,usere-userb)
00147        WRITE_STRING_STRIPSPACE_LEN(fp,triple+domainb,domaine-domainb)
00148        /* we're done */
00149        return 0;
00150 }
00151 
00152 static int write_netgroup(nssov_netgroup_cbp *cbp,Entry *entry)
00153 {
00154        int32_t tmpint32;
00155        int i;
00156        Attribute *a;
00157 
00158        /* get the netgroup triples and member */
00159        a = attr_find(entry->e_attrs,cbp->mi->mi_attrs[1].an_desc);
00160        if ( a ) {
00161        /* write the netgroup triples */
00162               for (i=0;i<a->a_numvals;i++)
00163               {
00164                      if (write_netgroup_triple(cbp->fp, a->a_vals[i].bv_val))
00165                             return -1;
00166               }
00167        }
00168        a = attr_find(entry->e_attrs,cbp->mi->mi_attrs[2].an_desc);
00169        if ( a ) {
00170        /* write netgroup members */
00171               for (i=0;i<a->a_numvals;i++)
00172               {
00173                      /* write the result code */
00174                      WRITE_INT32(cbp->fp,NSLCD_RESULT_BEGIN);
00175                      /* write triple indicator */
00176                      WRITE_INT32(cbp->fp,NSLCD_NETGROUP_TYPE_NETGROUP);
00177                      /* write netgroup name */
00178                      if (write_string_stripspace_len(cbp->fp,a->a_vals[i].bv_val,a->a_vals[i].bv_len))
00179                             return -1;
00180               }
00181        }
00182        /* we're done */
00183        return 0;
00184 }
00185 
00186 NSSOV_CB(netgroup)
00187 
00188 NSSOV_HANDLE(
00189        netgroup,byname,
00190        char fbuf[1024];
00191        struct berval filter = {sizeof(fbuf)};
00192        filter.bv_val = fbuf;
00193        READ_STRING(fp,cbp.buf);,
00194        cbp.name.bv_len = tmpint32;
00195        cbp.name.bv_val = cbp.buf;
00196        Debug(LDAP_DEBUG_TRACE,"nssov_netgroup_byname(%s)\n",cbp.name.bv_val,0,0);,
00197        NSLCD_ACTION_NETGROUP_BYNAME,
00198        nssov_filter_byname(cbp.mi,0,&cbp.name,&filter)
00199 )