Pals, i have this
code but the problem is that is presenting a memory leak and i don't
find where Can you help me, please? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ldap.h> #include <sqlda.h> #include <sqlcpr.h> #include <sqlca.h> static char usage[] = "usage: user [options]\n" "user options:\n" " -h host LDAP Server\n" " -p port port on LDAP Server\n" " -D binddn bind DN\n" " -w passwd bind password (for simple authentication)\n" " -v run in verbose mode (diagnostics to standard output)\n" " -b basedn base dn for search\n" " -u user user to log on the database Example: scott/tiger"; char *host, *dn, *password, *basedn, *userid; int port; BOOL verbose; void parseParameter(char *option, char *value); int main( int argc, char *argv[] ) { int i, j, rc; char id_oficina[5]; char *filter; char **values; char *cn; //LDAP LDAP *ld; LDAPMessage *searchResult, *entry; if( argc < 2 ) { printf( usage ); return ( 0 ); } for( i = 0; i < argc; i++ ) { if( *argv[ i ] == '-' ) { parseParameter( argv[ i ], argv[ i + 1 ] ); } } if((ld = ldap_init(host, port)) == NULL) { printf("Error: LDAP initialization failed\n"); } if(verbose) printf("Debug: LDAP initialization succeded\n"); if((rc = ldap_simple_bind_s(ld, dn, password)) != LDAP_SUCCESS) { printf("Error: ldap_simple_bind_s: %s\n", ldap_err2string(rc)); ldap_unbind(ld); return( 1 ); } if(verbose) printf("Debug: LDAP bind successful\n"); filter = (char *) malloc(strlen(id_oficina) + 7); //Connect to database EXEC SQL CONNECT :userid; if(verbose) printf("Debug: DB connection successful\n"); EXEC SQL DECLARE c_oficina CURSOR FOR SELECT ID_OFICINA FROM OFICINA; EXEC SQL OPEN c_oficina; EXEC SQL WHENEVER NOT FOUND DO break; for(;;) { EXEC SQL FETCH c_oficina INTO :id_oficina; if(verbose) printf("Debug: Oficina: %s\n", id_oficina); strcpy(filter, "(cn=U8"); strcat(filter, id_oficina); strcat(filter, ")"); if((rc = ldap_search_s(ld, basedn, LDAP_SCOPE_ONELEVEL, filter, NULL, 0, &searchResult)) != LDAP_SUCCESS) { if(verbose) printf("Debug: Oficina: %s, search has failed\n", id_oficina); ldap_msgfree(searchResult); }else { if((entry = ldap_first_entry(ld, searchResult)) != NULL) { if((values = ldap_get_values(ld, entry, "uniquemember")) != NULL) { for( j = 0; values[j] != NULL; j++) { if(strcmp((cn = ldap_explode_dn(values[j], 1)[0]), "PORTAL")) { if(verbose) printf("Debug: |_Employee: %s\n", cn); } } ldap_value_free(values); }else { if(verbose) printf("Debug: Oficina: %s, dosn't have any employees\n", id_oficina); ldap_value_free(values); } }else { if(verbose) printf("Debug: Oficina: %s, not exists in directory\n", id_oficina); } } ldap_msgfree(searchResult); } EXEC SQL CLOSE c_oficina; EXEC SQL COMMIT WORK RELEASE; //Free resources free(filter); ldap_unbind_s(ld); return 0; } void parseParameter(char *option, char *value) { if(strlen(option) == 2) { switch(option[1]) { case 'h': //host host = value; break; case 'p': port = atoi(value); break; case 'D': dn = value; break; case 'w': password = value; break; case 'v': verbose = 1; break; case 'b': basedn = value; break; case 'u': userid = value; break; } }else { printf("Invalid option: %s\n", option); return; } } Regards, |