string authorization(char *diadoc_login, char *diadoc_passwd) { CURL *curl; struct curl_slist *headerlist=NULL; struct curl_httppost *formpost=NULL; struct curl_httppost *lastptr=NULL; curl = curl_easy_init(); //cout << diadoc_login1 << endl << endl << endl; char auth_url_part1[] = "https://diadoc-api.kontur.ru/Authenticate?login="******""; strcat(auth_url_part2, auth_url_part1); strcat(auth_url_part2, diadoc_login); //cout << diadoc_login << endl; //cout << endl << strlen(diadoc_login) << endl << sizeof(*diadoc_login) << endl; char auth_url_part3[] = "&password="******""; strcat(auth_url_part4, auth_url_part3); strcat(auth_url_part4, diadoc_passwd); char auth_url[sizeof(auth_url_part2) + sizeof(auth_url_part4) + 40] = ""; strcat (auth_url, auth_url_part2); strcat (auth_url, auth_url_part4); if(curl) { headerlist = curl_slist_append(headerlist, "POST https://diadoc-api.kontur.ru/Authenticate HTTP/1.1"); headerlist = curl_slist_append(headerlist, "Authorization: DiadocAuth ddauth_api_client_id=comtec-e628f938-1133-449a-96d4-a105a0181e9a"); curl_easy_setopt(curl, CURLOPT_POST, true); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, ""); curl_easy_setopt(curl, CURLOPT_URL, auth_url); string execute = execute_curl(curl, headerlist); return execute; } curl_easy_cleanup(curl); }
int lookupGeoIPWebService( const int iRetGeo, DB_QCN_HOST_IPADDR& qhip, DB_QCN_GEO_IPADDR& qgip, DB_QCN_TRIGGER& qtrig, const double* dmxy, const double* dmz ) { int iReturn = iRetGeo; // "seed" our return code with the initial return code value char *strURL = NULL, *strReply = NULL; // the switch is the reply/return code from the lookup query into the qcn_geo_ipaddr table // note that we may want to set error return code (iRetGeo) to 0 if it seems that this record // will never get input, otherwise it will "nak" and retry (but if it's a bad GeoIP lookup, why bother?) switch(iRetGeo) { case ERR_DB_NOT_FOUND: // no record, need to do a maxmind/geoip web service lookup! strURL = new char[BYTESIZE_URL]; strReply = new char[BYTESIZE_CURL]; memset(strURL, 0x00, sizeof(char) * BYTESIZE_URL); memset(strReply, 0x00, sizeof(char) * BYTESIZE_CURL); sprintf(strURL, FORMAT_MAXMIND, qtrig.ipaddr); if (strlen(qtrig.ipaddr) > 6 && strlen(qtrig.ipaddr) < 16 && execute_curl(strURL, strReply, 512)) { // returned OK, now check strReply -- should be a single line of comma-delimited fields: // Returns: ISO 3166 Two-letter Country Code, Region Code, City, Latitude, Longitude, Error code // good reply: (note 4 commas/5 fields) // GB,K2,Oxford,51.750000,-1.250000 // error reply: (note 5 commas/6 fields all null except last) // ,,,,,IP_NOT_FOUND char* strComma[6] = {NULL, NULL, NULL, NULL, NULL, NULL}; int i = 0; strComma[0] = strchr(strReply, ','); // get the first comma for (i = 1; strComma[0] && i < 6; i++) { // parse out fields // search for next comma if last value wasn't NULL and haven't gone past the end of strReply if (strComma[i-1] && strlen(strReply) > (size_t)(strComma[i-1] - strReply + 1)) { strComma[i] = strchr(strComma[i-1]+1, ','); // note we skip a char to move off current comma ptr if (!strComma[i]) break; // if this is null, i.e.no comma found, may as well break } } if (i<4 || strComma[4] || strComma[5]) { // if this isn't null, or less than 4 commas found, then there was an error iReturn = 0; // ip not found, but this is a bad format web service lookup, so let's not bother retrying... log_messages.printf( SCHED_MSG_LOG::MSG_CRITICAL, "[QCN] [HOST#%d] [RESULTNAME=%s] [TIME=%lf] [2] Maxmind/GeoIP web lookup for IP %s via %s to %s failed\nReply: %s\n", qtrig.hostid, qtrig.result_name, qtrig.time_received, qtrig.ipaddr, "libcurl", strURL, strReply ); } else { // seems like a legit reply, so parse out // note we need to insert into qcn_geo_ipaddr, get insert_id(), then insert into host_ipaddr & trigger iReturn = 0; // success // initialize vars for the copies memset(&qgip.country, 0x00, sizeof(qgip.country)); memset(&qgip.region, 0x00, sizeof(qgip.region)); memset(&qgip.city, 0x00, sizeof(qgip.city)); // set location as geoip strcpy(qhip.location, "geoip"); // mark as geoip (also noted in field geoipaddr i.e points to qcn_geo_ipaddr record) qgip.time_lookup = dtime(); // returns a double of current time, # of seconds since epoch strncpy(qgip.country, strReply, strComma[0]-strReply); // first comma entry is country; note length is OK starting from strReply strncpy(qgip.region, strComma[0]+1, strComma[1]-strComma[0]-1); // next is region, note subtract 1 from length (, position) strncpy(qgip.city, strComma[1]+1, strComma[2]-strComma[1]-1); // next is city char *strTmp = new char[32]; memset(strTmp, 0x00, sizeof(char) * 32); strncpy(strTmp, strComma[2]+1, strComma[3]-strComma[2]-1); // next is latitude qgip.latitude = safe_atof(strTmp); memset(strTmp, 0x00, sizeof(char) * 32); strComma[4] = strReply + strlen(strReply); // make a fake endpoint strncpy(strTmp, strComma[3]+1, strComma[4]-strComma[3]-1); // next is longitude qgip.longitude = safe_atof(strTmp); delete [] strTmp; iReturn = qgip.insert(); if (!iReturn) { // success, get insert_id int iInsertID = qgip.db->insert_id(); if (iInsertID>0) { // now make a host record qhip.geoipaddrid = iInsertID; // mark the geoip database id used qtrig.geoipaddrid = qhip.geoipaddrid; qtrig.latitude = qgip.latitude; qtrig.longitude = qgip.longitude; qhip.latitude = qgip.latitude; qhip.longitude = qgip.longitude; qhip.levelvalue = 0; qhip.levelid = 0; qhip.alignid = 0; qtrig.levelvalue = 0; qtrig.levelid = 0; qtrig.alignid = 0; iReturn = qhip.insert(); if (!iReturn) { // success, insert trigger, if fails retcode sent below qtrig.hostipaddrid = qhip.db->insert_id(); // need the qcn_host_ipaddr id for trigger table iReturn = qtrig.insert(); if (iReturn) { log_messages.printf( SCHED_MSG_LOG::MSG_CRITICAL, "[QCN] [HOST#%d] [RESULTNAME=%s] [TIME=%lf] [2] Maxmind/GeoIP web lookup -- trigger %s insert failed\n", qtrig.hostid, qtrig.result_name, qtrig.time_received, qtrig.ipaddr ); } else { doTriggerMemoryInsert(qtrig, dmxy, dmz); log_messages.printf( SCHED_MSG_LOG::MSG_DEBUG, "[QCN] [HOST#%d] [RESULTNAME=%s] [TIME=%lf] [2] Maxmind/GeoIP web lookup -- trigger %s insert success\n", qtrig.hostid, qtrig.result_name, qtrig.time_received, qtrig.ipaddr ); } } else { iReturn = 0; // well we tried, return 0 so doesn't bother with this trigger again log_messages.printf( SCHED_MSG_LOG::MSG_CRITICAL, "[QCN] [HOST#%d] [RESULTNAME=%s] [TIME=%lf] [2] Maxmind/GeoIP web lookup -- host_ipaddr %s insert failed\n", qtrig.hostid, qtrig.result_name, qtrig.time_received, qhip.ipaddr ); } // failed qhip insert } // insertid else { // failed qgip insert id log_messages.printf( SCHED_MSG_LOG::MSG_CRITICAL, "[QCN] [HOST#%d] [RESULTNAME=%s] [TIME=%lf] [2] Maxmind/GeoIP web lookup -- invalid insert id on geo_ipaddr %s\n", qtrig.hostid, qtrig.result_name, qtrig.time_received, qhip.ipaddr ); } } else { // bad geo_ipaddr insert log_messages.printf( SCHED_MSG_LOG::MSG_CRITICAL, "[QCN] [HOST#%d] [RESULTNAME=%s] [TIME=%lf] [2] Maxmind/GeoIP web lookup -- geo_ipaddr %s insert failed\n", qtrig.hostid, qtrig.result_name, qtrig.time_received, qhip.ipaddr ); } } } else { // error in curl execution, set iReturn to non-zero so it can try again, should we insert trigger anyway? iReturn = 2; log_messages.printf( SCHED_MSG_LOG::MSG_CRITICAL, "[QCN] [HOST#%d] [RESULTNAME=%s] [TIME=%lf] [2a] Maxmind/GeoIP web lookup of IP %s via %s to %s failed\n", qtrig.hostid, qtrig.result_name, qtrig.time_received, qtrig.ipaddr, "libcurl", strURL ); } break; case 0: // record found already in geoip table, insert into host table and set lat/lng for qcn_trigger qhip.geoipaddrid = qgip.id; // mark the geoip database id used qhip.latitude = qgip.latitude; qhip.longitude = qgip.longitude; qtrig.latitude = qgip.latitude; qtrig.longitude = qgip.longitude; qhip.levelvalue = 0; qhip.levelid = 0; qhip.alignid = 0; qtrig.levelvalue = 0; qtrig.levelid = 0; qtrig.alignid = 0; iReturn = qhip.insert(); // note if the insert fails, return code will be set and returned below qtrig.geoipaddrid = qhip.geoipaddrid; if (!iReturn) { // success, insert trigger, if fails retcode sent below qtrig.hostipaddrid = qhip.db->insert_id(); iReturn = qtrig.insert(); // note if the insert fails, return code will be set and returned below } if (iReturn) { // error, print out debug info char* strErr = new char[512]; memset(strErr, 0x00, 512); qtrig.db_print(strErr); log_messages.printf( SCHED_MSG_LOG::MSG_CRITICAL, "geoip lookup trigger insert\nerrcode %d - %s\n", iReturn, strErr); memset(strErr, 0x00, 512); qhip.db_print(strErr); log_messages.printf( SCHED_MSG_LOG::MSG_CRITICAL, "qhip trigger insert\nerrcode %d - %s\n", iReturn, strErr); delete [] strErr; strErr = NULL; } else { doTriggerMemoryInsert(qtrig, dmxy, dmz); // trigger got in OK log_messages.printf( SCHED_MSG_LOG::MSG_DEBUG, "[QCN] [HOST#%d] [RESULTNAME=%s] [TIME=%lf] [3] Trigger inserted after qcn_geo_ipaddr lookup; mag=%lf at (%lf, %lf) - sync offset %f at %f!\n", qtrig.hostid, qtrig.result_name, qtrig.time_received, qtrig.magnitude, qtrig.latitude, qtrig.longitude, qtrig.sync_offset, qtrig.time_sync ); } break; default: // other database error, iReturn will be returned below log_messages.printf( SCHED_MSG_LOG::MSG_CRITICAL, "[QCN] [HOST#%d] [RESULTNAME=%s] [TIME=%lf] [3] - Database error encountered on qcn_geo_ipaddr lookup!\n", qtrig.hostid, qtrig.result_name, qtrig.time_received ); } if (strURL) delete [] strURL; // don't forget to get rid of these dynamic strings! if (strReply) delete [] strReply; return iReturn; }