📄 parse3tuple.cxx
字号:
case LDAP_URL_ERR_NODN: { cpLog (LOG_ERR, "Bad LDAP URL - no DN: %s ", url); return false; } break; case LDAP_URL_ERR_BADSCOPE: { cpLog (LOG_ERR, "Bad LDAP URL - bad scope : error %s ", url); return false; } break; case LDAP_URL_ERR_MEM: { cpLog (LOG_ERR, "Bad LDAP URL: error %d ", err); return false; } break; default: { cpLog (LOG_ERR, "Bad LDAP URL: error %d ", err); return false; } } } cpLog (LOG_DEBUG, "LDAP host: %s", ldapUrl->lud_host ); cpLog (LOG_DEBUG, "LDAP port: %d", ldapUrl->lud_port ); char* host = ldapUrl->lud_host; int port = ldapUrl->lud_port; if ( port == 0 ) { port = LDAP_PORT; } LDAP *ld = NULL; ld = ldap_init(host, port); if (!ld) { assert(0); return false; } char* who = ""; // set this to the DN of the object you have a passwd for char* passwd = ""; // set this to the passwd err = ldap_simple_bind_s(ld, who, passwd); if ( err ) { cpLog (LOG_ERR, "LDAP problem in simple bind %s ", ldap_err2string(err)); return false; } cpLog (LOG_DEBUG, "LDAP dn: %s", ldapUrl->lud_dn ); cpLog (LOG_DEBUG, "LDAP scope: %d", ldapUrl->lud_scope ); cpLog (LOG_DEBUG, "LDAP filter: %s", ldapUrl->lud_filter ); if ( ldapUrl->lud_attrs ) { cpLog (LOG_DEBUG, "LDAP attr[0]: %s", ldapUrl->lud_attrs[0] ); } char *base = ldapUrl->lud_dn; int scope = LDAP_SCOPE_ONELEVEL; //ldapUrl->lud_scope ; char *filter = "objectclass=3tuple"; // ldapUrl->lud_filter; char *attrs[4]; attrs[0] = "tag"; attrs[1] = "type"; attrs[2] = "value"; attrs[3] = NULL; int attrsonly = 0 ; LDAPMessage *res; err = ldap_search_s(ld, base, scope, filter, attrs, attrsonly, &res); if ( err ) { cpLog (LOG_ERR, "LDAP problem in search %s ", ldap_err2string(err)); return false; } int count = ldap_count_entries(ld, res); cpLog (LOG_DEBUG, "LDAP search return %d entries", count ); for ( LDAPMessage* msg = ldap_first_entry(ld, res); msg != NULL; msg = ldap_next_entry(ld, msg) ) { cpLog (LOG_DEBUG, "LDAP got messg "); if ( msg ) { BerElement* ber = NULL; /* * This bit of code is good to fine all the attributes types * * char * attr = ldap_first_attribute(ld,msg,&ber); * while ( attr != NULL ) * { * cpLog (LOG_DEBUG, "LDAP attr=%s",attr); * attr=ldap_next_attribute(ld,msg,ber); * } * ber = NULL; * */ char **data; char tag [TAG_MAX_LENGTH]; char type [TYPE_MAX_LENGTH]; // get the tag ---------------------- data = ldap_get_values(ld, msg, "tag"); if ( data == NULL ) { cpLog (LOG_ERR, "LDAP in tag data %s ", ldap_err2string(ld->ld_errno)); return false; } assert( data[0] ); cpLog (LOG_DEBUG, "LDAP tag=%s", data[0] ); strncpy( tag , data[0], TAG_MAX_LENGTH ); // get the type ----------------------- data = ldap_get_values(ld, msg, "type"); if ( data == NULL ) { cpLog (LOG_ERR, "LDAP in type data %s ", ldap_err2string(ld->ld_errno)); return false; } assert( data[0] ); cpLog (LOG_DEBUG, "LDAP type=%s", data[0] ); strncpy( type , data[0], TYPE_MAX_LENGTH ); // get the value --------------------- data = ldap_get_values(ld, msg, "value"); if ( data == NULL ) { cpLog (LOG_ERR, "LDAP in value data %s ", ldap_err2string(ld->ld_errno)); return false; } assert( data[0] ); cpLog (LOG_DEBUG, "LDAP value=%s", data[0] ); // call the parse function with the 3 tuple (*parse) (tag, type, data[0]); } } err = ldap_msgfree(res); if ( err == -1 ) { cpLog (LOG_ERR, "LDAP problem in msgfree "); return false; } res = NULL; err = ldap_unbind_s(ld); if ( err ) { cpLog (LOG_ERR, "LDAP problem in unbind %s ", ldap_err2string(err)); return false; } ld = NULL; ldap_free_urldesc( ldapUrl ); ldapUrl = NULL;#else cpLog (LOG_ERR, "No support for LDAP data: %s", url); return false;#endif return true;}/* * parse3tuple() parses file fname line by line. Each line is expected * to have 3 fields, <tag>-<type>-<value>, separated by blanks or tabs. * Lines starting with '#' are comments. Empty lines are ignored. * The value field contains a number of characters, even field separators * like blanks and tabs, up to the end of the line. * User of this utility provides a call back function with 3 arguments * (pointers to the tag, type and value strings) to validate and use the * data. */boolparse3tuple (const char* fname, void (*parse) (char*, char*, char*)){ return parse3tupleFile (const_cast < char* > (fname), parse);}voidremoveTrailingBlanks( char* line ){ for ( int i = strlen( line ) - 1; i > 0; i-- ) { if ( line[i] == ' ' || line[i] == '\t' ) { line[i] = '\0'; } else { return ; } }}boolparse3tupleFile (char* fname, void (*parse) (char*, char*, char*)){ char line [TUPLE3_MAX_LENGTH]; char tag [TAG_MAX_LENGTH]; char type [TYPE_MAX_LENGTH]; int lineCnt; int lineLen = 0; int argCnt; int valuePos = 0; FILE* fd = fopen (fname , "r"); if (fd) { for (lineCnt = 1; fgets (line, TUPLE3_MAX_LENGTH, fd); lineCnt++, valuePos = 0) { lineLen = strlen(line); assert( lineLen > 0 ); line [lineLen - 1] = 0; /* remove \n */ if (lineLen > 1) { if (line [lineLen - 2] == '\r') { line [lineLen - 2] = 0; /* remove \r too */ } } removeTrailingBlanks( line ); if ((argCnt = sscanf (line, "%s%s%*[ \t]%n", tag, type, &valuePos)) == 2) { if (line[0] != '#') { if (valuePos == 0) { cpLog (LOG_WARNING, "Line %d has too few arguments: %s", lineCnt, line); } else { (*parse) (tag, type, line + valuePos); } } } else { if (argCnt == 1 && line[0] != '#') { cpLog (LOG_WARNING, "Line %d has too few arguments: %s", lineCnt, line); } } } fclose (fd); } else { cpLog (LOG_ERR, "Cannot open file: %s", fname); return false; } return true;} /* parse3tuple */intstrConst2i (const char* str, const char* strTable[], const int tableSize){ int i; for (i = 0; i < tableSize; i++) { if (strcmp (str, strTable [i]) == 0) { break; } } return i;} // strConst2i
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -