HANDLE add_contact(char *name, DWORD flags) { HANDLE hContact; // already on list? if (hContact=find_contact(name)) { if (!(flags & PALF_TEMPORARY) && DBGetContactSettingByte(hContact, "CList", "NotOnList", 1)) { DBDeleteContactSetting( hContact, "CList", "NotOnList" ); DBDeleteContactSetting( hContact, "CList", "Hidden" ); } LOG(("add_contact: Found %s", name)); return hContact; // already there, return handle } // no, so add LOG(("add_contact: Adding %s", name)); hContact=(HANDLE)CallServiceSync(MS_DB_CONTACT_ADD, 0, 0); if (hContact) { if (CallServiceSync(MS_PROTO_ADDTOCONTACT, (WPARAM)hContact,(LPARAM)SKYPE_PROTONAME)!=0) { LOG(("add_contact: Ouch! MS_PROTO_ADDTOCONTACT failed for some reason")); CallServiceSync(MS_DB_CONTACT_DELETE, (WPARAM)hContact, 0); return NULL; } if (name[0]) DBWriteContactSettingString(hContact, SKYPE_PROTONAME, SKYPE_NAME, name); if (flags & PALF_TEMPORARY ) { DBWriteContactSettingByte(hContact, "CList", "NotOnList", 1); DBWriteContactSettingByte(hContact, "CList", "Hidden", 1); } if (name[0]) { SkypeSend("GET USER %s DISPLAYNAME", name); } else {LOG(("add_contact: Info: The contact added has no name."));} } else {LOG(("add_contact: Ouch! MS_DB_CONTACT_ADD failed for some reason"));} LOG(("add_contact succeeded")); return hContact; }
/* check if user is authorized to view information about a particular host */ int is_authorized_for_host(host *hst, authdata *authinfo) { contact *temp_contact; if(hst == NULL) return FALSE; /* if we're not using authentication, fake it */ if(use_authentication == FALSE) return TRUE; /* if this user has not authenticated return error */ if(authinfo->authenticated == FALSE) return FALSE; /* if this user is authorized for all hosts, they are for this one... */ if(is_authorized_for_all_hosts(authinfo) == TRUE) return TRUE; /* find the contact */ temp_contact = find_contact(authinfo->username); /* see if this user is a contact for the host */ if(is_contact_for_host(hst, temp_contact) == TRUE) return TRUE; /* see if this user is an escalated contact for the host */ if(is_escalated_contact_for_host(hst, temp_contact) == TRUE) return TRUE; return FALSE; }
/* check is the current user is authorized to issue commands relating to a particular service */ int is_authorized_for_service_commands(service *svc, authdata *authinfo) { host *temp_host; contact *temp_contact; if(svc == NULL) return FALSE; /* if we're not using authentication, fake it */ if(use_authentication == FALSE) return TRUE; /* if this user has not authenticated return error */ if(authinfo->authenticated == FALSE) return FALSE; /* the user is authorized if they have rights to the service */ if(is_authorized_for_service(svc, authinfo) == TRUE) { /* find the host */ temp_host = find_host(svc->host_name); if(temp_host == NULL) return FALSE; /* find the contact */ temp_contact = find_contact(authinfo->username); /* reject if contact is not allowed to issue commands */ if(temp_contact && temp_contact->can_submit_commands == FALSE) return FALSE; /* see if this user is a contact for the host */ if(is_contact_for_host(temp_host, temp_contact) == TRUE) return TRUE; /* see if this user is an escalated contact for the host */ if(is_escalated_contact_for_host(temp_host, temp_contact) == TRUE) return TRUE; /* this user is a contact for the service, so they have permission... */ if(is_contact_for_service(svc, temp_contact) == TRUE) return TRUE; /* this user is an escalated contact for the service, so they have permission... */ if(is_escalated_contact_for_service(svc, temp_contact) == TRUE) return TRUE; /* this user is not a contact for the host, so they must have been given explicit permissions to all service commands */ if(authinfo->authorized_for_all_service_commands == TRUE) return TRUE; } return FALSE; }
static void cmd_leave(struct xmpp *xmpp, struct contact *u, char *s) { char *part; int len; if (s[2] && s[3]) { part = jid_partial(s + 3, &len); if (!part) return; find_contact(u, len, part); } if (!u->jid[0]) return; if (!strcmp(u->type, "groupchat")) xmpp_printf(xmpp, "<presence to='%s' type='unavailable'/>", u->jid); rm_contact(u); }
/** * kbucket_contact_insert - add a contact to a bucket * @bucket: the bucket to add a contact to * @contact: the contact to add. Data from this is copied, caller is * responsible for the passed contacts memory managment. * * Returns 0 when the contact was inserted successfuly. -1 when a failure to * insert occoured. 1 when the contact already exsisted. */ int kbucket_contact_insert(struct kademlia_bucket *bucket, struct kademlia_contact const *contact) { struct kademlia_contact *c = find_contact(bucket, contact); if (c) { list_del(c->list, bucket->contacts); } else { if (bucket->contact_ct >= bucket->contact_max) return -1; c = copy_contact(contact); if (!c) return -2; bucket->contact_ct ++; } list_add(c->list, bucket->contacts); return 0; }
/* check if user is authorized to view information about a particular service */ int is_authorized_for_service(service *svc, authdata *authinfo) { host *temp_host; contact *temp_contact; if(svc == NULL) return FALSE; /* if we're not using authentication, fake it */ if(use_authentication == FALSE) return TRUE; /* if this user has not authenticated return error */ if(authinfo->authenticated == FALSE) return FALSE; /* if this user is authorized for all services, they are for this one... */ if(is_authorized_for_all_services(authinfo) == TRUE) return TRUE; /* find the host */ temp_host = find_host(svc->host_name); if(temp_host == NULL) return FALSE; /* if this user is authorized for this host, they are for all services on it as well... */ if(is_authorized_for_host(temp_host, authinfo) == TRUE) return TRUE; /* find the contact */ temp_contact = find_contact(authinfo->username); /* see if this user is a contact for the service */ if(is_contact_for_service(svc, temp_contact) == TRUE) return TRUE; /* see if this user is an escalated contact for the service */ if(is_escalated_contact_for_service(svc, temp_contact) == TRUE) return TRUE; return FALSE; }
/* Adds contact with the name who to the groupchat given in gc Parameters: gc - Returns: -1 = Contact not found -2 = On failure >=0 = Number of added item */ static int AddChatContact(gchat_contacts *gc, char *who, TCHAR *pszRole) { int i = -2; MCONTACT hContact; CONTACTINFO ci = {0}; TCHAR *twho; LOG (("AddChatContact %s", who)); if (!(twho = make_nonutf_tchar_string((const unsigned char*)who))) return -2; if ((i=ExistsChatContact(gc, twho))>=0) return i; hContact=find_contact(who); GCDEST gcd = { SKYPE_PROTONAME, gc->szChatName, GC_EVENT_JOIN }; GCEVENT gce = { sizeof(gce), &gcd }; gce.time = (DWORD)time(NULL); gce.dwFlags = GCEF_ADDTOLOG; gce.ptszStatus = pszRole ? pszRole : _T("USER"); ci.cbSize = sizeof(ci); ci.szProto = SKYPE_PROTONAME; ci.dwFlag = CNF_DISPLAY | CNF_TCHAR; ci.hContact = hContact; if (hContact && !CallService(MS_CONTACT_GETCONTACTINFO,0,(LPARAM)&ci)) gce.ptszNick=ci.pszVal; else gce.ptszNick=twho; gce.ptszUID=twho; if (!CallService(MS_GC_EVENT, 0, (LPARAM)&gce)) { if ((gc->mJoinedContacts=(gchat_contact*)realloc(gc->mJoinedContacts, (gc->mJoinedCount+1)*sizeof(gchat_contact)))) { gc->mJoinedContacts[i=gc->mJoinedCount].hContact=hContact; _tcscpy (gc->mJoinedContacts[i].szRole, gce.ptszStatus); _tcscpy (gc->mJoinedContacts[i].who, twho); gc->mJoinedCount++; } } if (ci.pszVal) mir_free (ci.pszVal); free_nonutf_tchar_string (twho); return i; }
static int presence_hook(int x, struct xmpp *xmpp) { char *show, *s, *status, *from, *part, *type; struct contact *u; int npart; show = xml_node_find_text(x, "show", &xmpp->xml.mem); status = xml_node_find_text(x, "status", &xmpp->xml.mem); type = xml_node_find_attr(x, "type", &xmpp->xml.mem); from = xml_node_find_attr(x, "from", &xmpp->xml.mem); if (!from) return 0; show = (!show && type && !strcmp("unavailable", type)) ? type : show; show = show ? show : STR_ONLINE; status = status ? status : ""; for (s = status; *s; ++s) if (*s == '\n') *s = '\\'; part = jid_partial(from, &npart); find_contact(u, npart, part); if (type && type[0] && (!u || strcmp(u->type, "groupchat"))) print_msg(0, "", "-!- %s sends %s\n", from, type); if (!u || strcmp(u->type, "groupchat")) print_msg(0, "", "-!- %s is %s (%s)\n", from, show, status); if (u) { if (!strcmp(u->type, "groupchat")) { if (!strcasecmp(show, STR_ONLINE) || !strcasecmp(show, STR_OFFLINE)) print_msg(npart, part, "-!- %s is %s\n", from, show); } else { print_msg(npart, part, "-!- %s is %s (%s)\n", from, show, status); snprintf(u->show, sizeof(u->show), "%s", show); snprintf(u->status, sizeof(u->status), "%s", status); } } return 0; }
static struct contact * add_contact(int njid, const char *jid) { struct contact *u; find_contact(u, njid, jid); if (u) return u; u = calloc(1, sizeof(struct contact)); if (!u) return 0; snprintf(u->jid, sizeof(u->jid), "%.*s", njid, jid); u->fd = open_pipe(u->jid); u->next = contacts; u->type = "chat"; snprintf(u->show, sizeof(u->show), "%s", STR_OFFLINE); u->status[0] = 0; contacts = u; return u; }
MCONTACT add_contact(char *name, DWORD flags) { // already on list? MCONTACT hContact = find_contact(name); if (hContact) { if (!(flags & PALF_TEMPORARY) && db_get_b(hContact, "CList", "NotOnList", 1)) { db_unset(hContact, "CList", "NotOnList"); db_unset(hContact, "CList", "Hidden"); } LOG(("add_contact: Found %s", name)); return hContact; // already there, return handle } // no, so add LOG(("add_contact: Adding %s", name)); hContact = (MCONTACT)CallServiceSync(MS_DB_CONTACT_ADD, 0, 0); if (hContact) { if (CallServiceSync(MS_PROTO_ADDTOCONTACT, hContact, (LPARAM)SKYPE_PROTONAME) != 0) { LOG(("add_contact: Ouch! MS_PROTO_ADDTOCONTACT failed for some reason")); CallServiceSync(MS_DB_CONTACT_DELETE, hContact, 0); return NULL; } if (name[0]) db_set_s(hContact, SKYPE_PROTONAME, SKYPE_NAME, name); if (flags & PALF_TEMPORARY) { db_set_b(hContact, "CList", "NotOnList", 1); db_set_b(hContact, "CList", "Hidden", 1); } if (name[0]) { SkypeSend("GET USER %s DISPLAYNAME", name); } else { LOG(("add_contact: Info: The contact added has no name.")); } } else { LOG(("add_contact: Ouch! MS_DB_CONTACT_ADD failed for some reason")); } LOG(("add_contact succeeded")); return hContact; }
extern "C" void set_context_contact(void *data, ConnContext *context) { MCONTACT hContact = find_contact(context->username, context->protocol); context->app_data = (void*)hContact; }
/* get current authentication information */ int get_authentication_information(authdata *authinfo) { mmapfile *thefile; char *input = NULL; char *temp_ptr = NULL; contact *temp_contact = NULL; contactgroup *temp_contactgroup = NULL; if(authinfo == NULL) return ERROR; /* initial values... */ authinfo->authorized_for_all_hosts = FALSE; authinfo->authorized_for_all_host_commands = FALSE; authinfo->authorized_for_all_services = FALSE; authinfo->authorized_for_all_service_commands = FALSE; authinfo->authorized_for_system_information = FALSE; authinfo->authorized_for_system_commands = FALSE; authinfo->authorized_for_configuration_information = FALSE; authinfo->authorized_for_read_only = FALSE; authinfo->locale_lang_path=""; authinfo->locale_lang_user_lang=""; /* grab username from the environment... */ if(use_ssl_authentication) { /* patch by Pawl Zuzelski - 7/22/08 */ temp_ptr = getenv("SSL_CLIENT_S_DN_CN"); } else { temp_ptr = getenv("REMOTE_USER"); } if(temp_ptr == NULL) { authinfo->username = ""; authinfo->authenticated = FALSE; } else { authinfo->username = (char *)malloc(strlen(temp_ptr) + 1); if(authinfo->username == NULL) authinfo->username = ""; else strcpy(authinfo->username, temp_ptr); if(!strcmp(authinfo->username, "")) authinfo->authenticated = FALSE; else authinfo->authenticated = TRUE; } /* read in authorization override vars from config file... */ if((thefile = mmap_fopen(get_cgi_config_location())) != NULL) { while(1) { /* free memory */ free(input); /* read the next line */ if((input = mmap_fgets_multiline(thefile)) == NULL) break; strip(input); /* we don't have a username yet, so fake the authentication if we find a default username defined */ if(!strcmp(authinfo->username, "") && strstr(input, "default_user_name=") == input) { temp_ptr = strtok(input, "="); temp_ptr = strtok(NULL, ","); authinfo->username = (char *)malloc(strlen(temp_ptr) + 1); if(authinfo->username == NULL) authinfo->username = ""; else strcpy(authinfo->username, temp_ptr); if(!strcmp(authinfo->username, "")) authinfo->authenticated = FALSE; else authinfo->authenticated = TRUE; } else if(strstr(input, "authorized_for_all_hosts=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*")) authinfo->authorized_for_all_hosts = TRUE; } } else if(strstr(input, "authorized_for_all_services=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*")) authinfo->authorized_for_all_services = TRUE; } } else if(strstr(input, "authorized_for_system_information=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*")) authinfo->authorized_for_system_information = TRUE; } } else if(strstr(input, "authorized_for_configuration_information=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*")) authinfo->authorized_for_configuration_information = TRUE; } } else if(strstr(input, "authorized_for_all_host_commands=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*")) authinfo->authorized_for_all_host_commands = TRUE; } } else if(strstr(input, "authorized_for_all_service_commands=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*")) authinfo->authorized_for_all_service_commands = TRUE; } } else if(strstr(input, "authorized_for_system_commands=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*")) authinfo->authorized_for_system_commands = TRUE; } } else if(strstr(input, "authorized_for_read_only=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { if(!strcmp(temp_ptr, authinfo->username) || !strcmp(temp_ptr, "*")) authinfo->authorized_for_read_only = TRUE; } } else if((temp_contact = find_contact(authinfo->username)) != NULL) { if(strstr(input, "authorized_contactgroup_for_all_hosts=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { temp_contactgroup = find_contactgroup(temp_ptr); if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact)) authinfo->authorized_for_all_hosts = TRUE; } } else if(strstr(input, "authorized_contactgroup_for_all_services=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { temp_contactgroup = find_contactgroup(temp_ptr); if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact)) authinfo->authorized_for_all_services = TRUE; } } else if(strstr(input, "authorized_contactgroup_for_system_information=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { temp_contactgroup = find_contactgroup(temp_ptr); if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact)) authinfo->authorized_for_system_information = TRUE; } } else if(strstr(input, "authorized_contactgroup_for_configuration_information=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { temp_contactgroup = find_contactgroup(temp_ptr); if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact)) authinfo->authorized_for_configuration_information = TRUE; } } else if(strstr(input, "authorized_contactgroup_for_all_host_commands=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { temp_contactgroup = find_contactgroup(temp_ptr); if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact)) authinfo->authorized_for_all_host_commands = TRUE; } } else if(strstr(input, "authorized_contactgroup_for_all_service_commands=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { temp_contactgroup = find_contactgroup(temp_ptr); if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact)) authinfo->authorized_for_all_service_commands = TRUE; } } else if(strstr(input, "authorized_contactgroup_for_system_commands=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { temp_contactgroup = find_contactgroup(temp_ptr); if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact)) authinfo->authorized_for_system_commands = TRUE; } } else if(strstr(input, "authorized_contactgroup_for_read_only=") == input) { temp_ptr = strtok(input, "="); while((temp_ptr = strtok(NULL, ","))) { temp_contactgroup = find_contactgroup(temp_ptr); if(is_contact_member_of_contactgroup(temp_contactgroup, temp_contact)) authinfo->authorized_for_read_only = TRUE; } } }else if(strstr(input, "locale_lang_path=") == input) { temp_ptr = strtok(input, "="); temp_ptr = strtok(NULL, "="); if(temp_ptr) { authinfo->locale_lang_path = (char*) malloc(strlen(temp_ptr)+1); strcpy(authinfo->locale_lang_path, temp_ptr); }else{ authinfo->locale_lang_path=""; } }else if(strstr(input, "locale_lang_user="******","); lang += strlen(authinfo->username) + 1; authinfo->locale_lang_user_lang = (char*) malloc(strlen(lang) + 1); strcpy(authinfo->locale_lang_user_lang, lang); } } /* free memory and close the file */ free(input); mmap_fclose(thefile); } setlocale(LC_ALL,authinfo->locale_lang_user_lang); bindtextdomain("nagios-plugins",authinfo->locale_lang_path); //setlocale(LC_ALL,"zh_CN"); //bindtextdomain("nagios-plugins","/usr/local/nagios/share/locale"); textdomain("nagios-plugins"); if(authinfo->authenticated == TRUE) return OK; else return ERROR; }
void test_parsing(void) { struct external_command *ext_command = NULL; contact *created_contact = NULL; contact *fetched_contact = NULL; const char *cmdstr = "[1234567890] ADD_HOST_COMMENT;my_host;0;15;this is my comment, there are many like it but this one is mine"; registered_commands_init(20); { g_clear_error(&error); ok(NULL == command_parse(cmdstr, COMMAND_SYNTAX_NOKV, &error), "We can't parse commands when none are registered"); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_UNKNOWN_COMMAND), "The error code looks like expected"); ext_command = command_create("ADD_HOST_COMMENT", test__add_host_comment_handler, "This is a description for a command named ADD_HOST_COMMENT", NULL); command_argument_add(ext_command, "host", STRING, NULL, NULL); b_val = 0; command_argument_add(ext_command, "persistent", BOOL, &b_val, NULL); i_val = 42; command_argument_add(ext_command, "author", INTEGER, &i_val, NULL); s_val = "No comment"; command_argument_add(ext_command, "comment", STRING, s_val, NULL); command_register(ext_command, -1); g_clear_error(&error); ext_command = command_parse("[] UNKNOWN_COMMAND", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_MALFORMED_COMMAND), "Malformed command error is raised for malformed commands"); ok(NULL == ext_command, "No command returned for malformed command"); g_clear_error(&error); ext_command = command_parse("[UNKNOWN_COMMAND", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_MALFORMED_COMMAND), "Malformed command error is raised for malformed commands"); ok(NULL == ext_command, "No command returned for malformed command"); g_clear_error(&error); ext_command = command_parse("[139414354] UNKNOWN_COMMAND", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_UNKNOWN_COMMAND), "Unknown command error is raised for unknown commands"); ok(NULL == ext_command, "No command returned for unknown command"); g_clear_error(&error); ext_command = command_parse(cmdstr, COMMAND_SYNTAX_NOKV, &error); ok(error == NULL, "The command parses without error"); ok(!strcmp("my_host", command_argument_get_value(ext_command, "host")), "Host value parsed successfully"); ok(0 == *(int *)command_argument_get_value(ext_command, "persistent"), "Persistent value parsed successfully"); ok(15 == *(int *)command_argument_get_value(ext_command, "author"), "Author value parsed successfully"); ok(!strcmp("this is my comment, there are many like it but this one is mine", command_argument_get_value(ext_command, "comment")), "Comment value parsed successfully"); command_destroy(ext_command); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_HOST_COMMENT;my_host;0;15;this is my newline\n, there are many like it but this one is\n m\ni\nn\ne", COMMAND_SYNTAX_NOKV, &error); ok(error == NULL, "Command containing newlines parses without error"); ok(!strcmp("this is my newline\n, there are many like it but this one is\n m\ni\nn\ne", command_argument_get_value(ext_command, "comment")), "Comment containing newlines parsed successfully"); command_destroy(ext_command); g_clear_error(&error); ext_command = command_parse(cmdstr, COMMAND_SYNTAX_NOKV, &error); ok(0 == command_execute_handler(ext_command), "Callback exit value properly passed on"); ok(!strcmp("my_host", received_host), "Host value passed to callback"); ok(0 == *received_persistent, "Persistent value passed to callback"); ok(received_entry_time == 1234567890, "Entry time passed correctly to callback"); command_destroy(ext_command); free(received_host); free(received_persistent); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_HOST_COMMENT;;1", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_PARSE_MISSING_ARG), "Missing arguments are complained about"); ok(ext_command == NULL, "No command returned for command with missing arguments"); g_clear_error(&error); ext_command = command_parse("[15341345] ADD_HOST_COMMENT", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_PARSE_MISSING_ARG), "Missing arguments are complained about (no arguments supplied)"); ok(ext_command == NULL, "No command returned for command with missing arguments"); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_HOST_COMMENT;my_host;0;441;this is my comment, there are many like it but this one is mine;Post-semi-colon stuff", COMMAND_SYNTAX_NOKV, &error); ok(error == NULL, "Last string argument may contain semi-colons"); ok(ext_command != NULL, "A command should be returned when last string-argument has semi-colons"); command_destroy(ext_command); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_HOST_COMMENT;my_host;0;Dora the Explora';this is my comment, there are many like it but this one is mine", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_PARSE_TYPE_MISMATCH), "Type errors are complained about"); ok(ext_command == NULL, "No command returned for command with argument type errors"); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_HOST_COMMENT;my_host;1;4lyfe;this is my comment, there are many like it but this one is mine", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_PARSE_TYPE_MISMATCH), "Junk characters after integer arguments are complained about"); ok(ext_command == NULL, "No command returned for command with argument type mismatch errors"); g_clear_error(&error); ext_command = command_create("ADD_HOST_COMMENT_WITH_TIMESTAMP", test__add_host_comment_handler, "This is a description for a command named ADD_HOST_COMMENT", NULL); command_argument_add(ext_command, "host", STRING, NULL, NULL); command_argument_add(ext_command, "persistent", BOOL, NULL, NULL); i_val = 42; command_argument_add(ext_command, "author", INTEGER, &i_val, NULL); s_val = "No comment"; command_argument_add(ext_command, "comment", STRING, s_val, NULL); t_val = 0; command_argument_add(ext_command, "timestamp", TIMESTAMP, &t_val, NULL); command_register(ext_command, -1); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_HOST_COMMENT_WITH_TIMESTAMP;my_host;1;441;this is my comment, there are many like it but this one is mine;1234987650", COMMAND_SYNTAX_NOKV, &error); ok(error == NULL, "No error when parsing proper commands"); ok(1234987650 == *(time_t *)command_argument_get_value(ext_command, "timestamp"), "Timestamp value parsed successfully"); command_destroy(ext_command); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_HOST_COMMENT_WITH_TIMESTAMP;my_host;4;441;this is my comment, there are many like it but this one is mine;1234987650", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_VALIDATION_FAILURE), "Invalid BOOL value (4) is complained about"); ok(NULL == ext_command, "No command returned for command with invalid argument values"); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_HOST_COMMENT_WITH_TIMESTAMP;my_host;1;441;this is my comment, there are many like it but this one is mine;14:49", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_PARSE_TYPE_MISMATCH), "Malformed timestamp value is complained about"); ok(NULL == ext_command, "No command returned for command with argument type mismatch"); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_HOST_COMMENT_WITH_TIMESTAMP;my_host;1;441;;", COMMAND_SYNTAX_NOKV, &error); ok(error == NULL, "Missing arguments which have default values are not complained about"); ok(!strcmp("No comment", command_argument_get_value(ext_command, "comment")), "Default value is used for missing argument"); ok(t_val == *(time_t *)command_argument_get_value(ext_command, "timestamp"), "Default value is used for missing argument at end of arg string"); command_destroy(ext_command); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_HOST_COMMENT_WITH_TIMESTAMP;some_host;;441;;13485799", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_PARSE_MISSING_ARG), "Missing arguments which don't have default values are complained about"); ok(NULL == ext_command, "No command returned for command with missing argument and no default"); g_clear_error(&error); ext_command = command_create("ADD_SVC_COMMENT", test__add_service_comment_handler, "This is a description for a command named CMD_ADD_SVC_COMMENT", NULL); command_argument_add(ext_command, "service", SERVICE, NULL, NULL); command_argument_add(ext_command, "persistent", BOOL, &b_val, NULL); command_argument_add(ext_command, "author", INTEGER, &i_val, NULL); command_argument_add(ext_command, "comment", STRING, s_val, NULL); command_register(ext_command, -1); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_SVC_COMMENT;my_host;NO_SUCH_SERVICE;1;441;this is my service comment, there are many like it but this one is mine", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_VALIDATION_FAILURE), "Invalid service is complained about"); ok(NULL == ext_command, "No command returned for command with invalid service"); g_clear_error(&error); ext_command = command_create("ADD_SVC_COMMENT_2", test__add_service_comment_handler, "This is a description for a command with a custom service validator", NULL); command_argument_add(ext_command, "service", SERVICE, NULL, custom_service_validator); command_argument_add(ext_command, "persistent", BOOL, &b_val, NULL); command_argument_add(ext_command, "author", INTEGER, &i_val, NULL); command_argument_add(ext_command, "comment", STRING, s_val, NULL); command_register(ext_command, -1); g_clear_error(&error); ext_command = command_parse("[1234567890] ADD_SVC_COMMENT_2;my_host;LETS_PRETEND_THIS_EXISTS;1;441;this is my service comment, there are many like it but this one is mine", COMMAND_SYNTAX_NOKV, &error); ok(error == NULL, "Custom validator does not decline our invalid service"); command_destroy(ext_command); g_clear_error(&error); ext_command = command_create("DEL_HOST_COMMENT", test__del_host_comment_handler, "This command is used to delete a specific host comment.", NULL); command_argument_add(ext_command, "comment_id", ULONG, NULL, NULL); command_register(ext_command, -1); g_clear_error(&error); ext_command = command_parse("[1234567890] DEL_HOST_COMMENT;10;Excess argument;snurre", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_PARSE_EXCESS_ARG), "Excess arguments are complained about"); ok(ext_command == NULL, "No command returned for commands with excess arguments"); g_clear_error(&error); ext_command = command_parse("[1234567890] DEL_HOST_COMMENT;10", COMMAND_SYNTAX_NOKV, &error); ok((unsigned long) 10 == *(unsigned long *)command_argument_get_value(ext_command, "comment_id"), "ULONG argument parsed correctly"); command_destroy(ext_command); ext_command = command_create("DEL_HOST_COMMENT_2", test__del_host_comment_handler, "This command is used to delete a specific host comment.", "int=comment_id;str=string_arg"); command_register(ext_command, -1); g_clear_error(&error); ext_command = command_parse("[1234567890] DEL_HOST_COMMENT_2;10;foobar", COMMAND_SYNTAX_NOKV, &error); ok(error == NULL, "No error when parsing command created with argspec"); ok(!strcmp("foobar", command_argument_get_value(ext_command, "string_arg")), "Can parse command created with argspec (string arg)"); ok(10 == *(int *)command_argument_get_value(ext_command, "comment_id"), "Can parse command created with argspec (int arg)"); command_destroy(ext_command); g_clear_error(&error); ext_command = command_parse("[1234567890] DEL_HOST_COMMENT_2;1;", COMMAND_SYNTAX_NOKV, &error); ok (ext_command == NULL, "Missing argument at end of arg string is complained about"); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_PARSE_MISSING_ARG), "Missing argument at end of arg string raises the correct error"); g_clear_error(&error); ext_command = command_create("DISABLE_NOTIFICATIONS", test__disable_notifications_handler, "Disables host and service notifications on a program-wide basis.", NULL); command_register(ext_command, -1); ext_command = command_parse("[1234567890] DISABLE_NOTIFICATIONS", COMMAND_SYNTAX_NOKV, &error); ok(ext_command != NULL, "No problem parsing commands with no arguments (when none required)"); command_destroy(ext_command); g_clear_error(&error); ext_command = command_create("DO_THING_WITH_TIMEPERIOD", test__do_thing_with_timeperiod_handler, "Does a thing with a timeperiod", NULL); command_argument_add(ext_command, "timeperiod", TIMEPERIOD, NULL, NULL); command_register(ext_command, -1); g_clear_error(&error); ext_command = command_parse("[1234567890] DO_THING_WITH_TIMEPERIOD;24x8", COMMAND_SYNTAX_NOKV, &error); ok(ext_command == NULL, "No command returned when timeperiod arg is invalid"); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_VALIDATION_FAILURE), "Validation error raised for invalid timeperiod"); registered_timeperiod = find_timeperiod("24x7"); assert(NULL != registered_timeperiod); g_clear_error(&error); ext_command = command_parse("[1234567890] DO_THING_WITH_TIMEPERIOD;24x7", COMMAND_SYNTAX_NOKV, &error); ok(ext_command != NULL, "Command returned when timeperiod arg is not invalid"); ok(error == NULL, "Validation error not raised for valid timeperiod"); ok(registered_timeperiod == command_argument_get_value(ext_command, "timeperiod"), "The correct timeperiod is returned"); command_destroy(ext_command); /** CONTACT SETUP*/ g_clear_error(&error); ext_command = command_create("FIND_CONTACT", test__do_thing_with_contact_handler, "Does a thing with contact", NULL); command_argument_add(ext_command, "contact", CONTACT, NULL, NULL); command_register(ext_command, -1); created_contact = find_contact("nagiosadmin"); assert(NULL != created_contact); /** CONTACT TEST*/ g_clear_error(&error); ext_command = command_parse("[1234567890] FIND_CONTACT;bango", COMMAND_SYNTAX_NOKV, &error); ok(ext_command == NULL, "No command returned when contact arg is invalid"); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_VALIDATION_FAILURE), "Validation error raised for invalid contact"); /** CONTACT TEST*/ g_clear_error(&error); ext_command = command_parse("[1234567890] FIND_CONTACT;nagiosadmin", COMMAND_SYNTAX_NOKV, &error); ok(ext_command != NULL, "Command returned when contact arg is not invalid"); ok(error == NULL, "Validation error not raised for valid contact"); fetched_contact = command_argument_get_value(ext_command, "contact"); ok(created_contact->name == fetched_contact->name, "The correct contact is returned"); /** CONTACT TEARDOWN*/ command_destroy(ext_command); g_clear_error(&error); ext_command = command_parse("[1234567890] _MY_CUSTOMARILY_CUSTOM_CUSTARD_COMMAND;foo;bar;baz;33", COMMAND_SYNTAX_NOKV, &error); ok(g_error_matches(error, NM_COMMAND_ERROR, CMD_ERROR_CUSTOM_COMMAND), "Custom command reported as such"); ok(ext_command != NULL, "Raw command returned when parsing custom command"); ok(!strcmp("foo;bar;baz;33", command_raw_arguments(ext_command)), "Raw arguments properly set for custom command"); ok(command_entry_time(ext_command) == (time_t)1234567890, "Entry time set for custom command"); command_destroy(ext_command); g_clear_error(&error); } registered_commands_deinit(); }
int main(int argc, char *argv[]) { int ret = 1, istty, i, idx, delete_all = 0, updated = 0; dldev_t dev; contact_t db; contact_data_t rec; u8 *data; u16 len; /* for data security */ /* umask(S_IRWXG | S_IRWXO); */ istty = isatty(0); while((i = getopt(argc, argv, "hd:a")) != -1){ switch(i){ case 'd': dev_file = optarg; break; case 'a': delete_all = 1; break; case 'h': default: usage(); break; } } argc -= optind; argv += optind; #ifdef USB_USBHID dev.usb.file = dev_file; #endif memset(&rec, 0, sizeof(contact_data_t)); BEGIN_OPT() OPT("msg", rec.msg) OPT("type", rec.type) OPT("area", rec.area) OPT("number", rec.number) END_OPT() if(delete_all){ fprintf(stderr, "WARNING: DELETE ALL CONTACTS IN THE WATCH (Y/N)? "); if(tolower(getchar()) != 'y') exit(0); }else if(istty && (!rec.msg || !rec.type || !rec.area || !rec.number)) usage(); /* TODO: long phone number extending to the next or previous record */ if(open_dev(&dev)){ ERROR("open_dev"); goto exit; } if(start_session(&dev)){ ERROR("read_app_info"); goto exit; } /******************************************************************************/ #ifdef DEBUG for(i = 0; i < NUM_APPS; i++){ if(!dev.app[i].acd.app_idx) continue; printf("%2d: %d%d%d%d%d%d%d%d %02x %02x %04x %04x %04x %04x %04x %04x %s\n", i, dev.app[i].acd.app_idx, dev.app[i].acd.code_loc, dev.app[i].acd.db_loc, dev.app[i].acd.code_invalid, dev.app[i].acd.db_modified, dev.app[i].acd.db_invalid, dev.app[i].acd.passwd_req, dev.app[i].acd.mode_name, dev.app[i].acb.app_type, dev.app[i].acb.app_inst, dev.app[i].acb.asd_addr, dev.app[i].acb.add_addr, dev.app[i].acb.state_mgr_addr, dev.app[i].acb.refresh_addr, dev.app[i].acb.banner_addr, dev.app[i].acb.code_addr, dev.app[i].banner ); } #endif /******************************************************************************/ if((idx = find_app(&dev, "CONTACT")) < 0){ ERROR("CONTACT application not found"); goto end; } if(dump_add(&dev, idx, &data, &len)){ ERROR("dump_add"); goto end; } read_contact(&db, data); free(data); if(delete_all){ while(!del_contact(&db, 0)) updated = 1; }else{ if(!istty && !del_contact_file(&db, stdin)) updated = 1; if(rec.msg && rec.type && rec.area && rec.number && (i = find_contact(&db, &rec)) >= 0 && !del_contact(&db, i)) updated = 1; } if(updated){ create_contact(&db, &data, &len); if(load_add(&dev, idx, data)){ ERROR("load_add"); goto end; } free(data); } print_contact(&db, stdout); free_contact(&db); /******************************************************************************/ end: if(end_session(&dev)){ ERROR("end_session"); goto exit; } ret = 0; exit: close_dev(&dev); exit(ret); }
static int ServiceParseAimLink(WPARAM, LPARAM lParam) { char *arg = (char*)lParam; if (arg == NULL) return 1; /* sanity check */ /* skip leading prefix */ arg = strchr(arg, ':'); if (arg == NULL) return 1; /* parse failed */ for (++arg; *arg == '/'; ++arg); /* add user: aim:addbuddy?screenname=NICK&groupname=GROUP send message: aim:goim?screenname=NICK&message=MSG open chatroom: aim:gochat?roomname=ROOM&exchange=NUM */ /* add a contact to the list */ if (!_strnicmp(arg, "addbuddy?", 9)) { char *tok, *sn = NULL, *group = NULL; ADDCONTACTSTRUCT acs; PROTOSEARCHRESULT psr; if (*(arg += 9) == 0) return 1; /* parse failed */ tok = strtok(arg, "&"); /* first token */ while (tok != NULL) { if (!_strnicmp(tok, "screenname=", 11) && *(tok + 11) != 0) sn = Netlib_UrlDecode(tok + 11); if (!_strnicmp(tok, "groupname=", 10) && *(tok + 10) != 0) group = Netlib_UrlDecode(tok + 10); /* group is currently ignored */ tok = strtok(NULL, "&"); /* next token */ } if (sn == NULL) return 1; /* parse failed */ if (find_contact(sn) == NULL) { /* does not yet check if sn is current user */ acs.handleType = HANDLE_SEARCHRESULT; acs.szProto = AIM_PROTOCOL_NAME; acs.psr = &psr; memset(&psr, 0, sizeof(PROTOSEARCHRESULT)); psr.cbSize = sizeof(PROTOSEARCHRESULT); psr.nick.t = sn; CallService(MS_ADDCONTACT_SHOW, 0, (LPARAM)&acs); } return 0; } /* send a message to a contact */ else if (!_strnicmp(arg, "goim?", 5)) { char *tok, *sn = NULL, *msg = NULL; MCONTACT hContact; if (*(arg += 5) == 0) return 1; /* parse failed */ tok = strtok(arg, "&"); /* first token */ while (tok != NULL) { if (!_strnicmp(tok, "screenname=", 11) && *(tok + 11) != 0) sn = Netlib_UrlDecode(tok + 11); if (!_strnicmp(tok, "message=", 8) && *(tok + 8) != 0) msg = Netlib_UrlDecode(tok + 8); tok = strtok(NULL, "&"); /* next token */ } if (sn == NULL) return 1; /* parse failed */ if (ServiceExists(MS_MSG_SENDMESSAGE)) { hContact = find_contact(sn); if (hContact == NULL) { hContact = add_contact(sn); /* does not yet check if sn is current user */ if (hContact != NULL) db_set_b(hContact, MOD_KEY_CL, AIM_KEY_NL, 1); } if (hContact != NULL) CallService(MS_MSG_SENDMESSAGE, hContact, (LPARAM)msg); } return 0; } /* open a chatroom */ else if (!_strnicmp(arg, "gochat?", 7)) { char *tok, *rm = NULL; int exchange = 0; if (*(arg += 7) == 0) return 1; /* parse failed */ tok = strtok(arg, "&"); /* first token */ while (tok != NULL) { if (!_strnicmp(tok, "roomname=", 9) && *(tok + 9) != 0) rm = Netlib_UrlDecode(tok + 9); if (!_strnicmp(tok, "exchange=", 9)) exchange = atoi(Netlib_UrlDecode(tok + 9)); tok = strtok(NULL, "&"); /* next token */ } if (rm == NULL || exchange <= 0) return 1; /* parse failed */ aim_gchat_joinrequest(rm, exchange); return 0; } return 1; /* parse failed */ }