static int32_t adduser(Ceo__AddUser *in, Ceo__AddUserResponse *out, char *client) { int32_t chk_stat, status; char *prog; chk_stat = check_adduser(in, out, client); if (chk_stat) return chk_stat; if (in->type == CEO__ADD_USER__TYPE__MEMBER) { status = addmember(in, out); prog = "addmember"; } else if (in->type == CEO__ADD_USER__TYPE__CLUB_REP) { status = addmember(in, out); prog = "addclubrep"; } else if (in->type == CEO__ADD_USER__TYPE__CLUB) { status = addclub(in, out); prog = "addclub"; } else { fatal("unknown user type %d", in->type); } if (status) response_message(out, 0, "there were failures, please contact systems committee"); adduser_spam(in, out, client, prog, status); return status; }
char *get_response (int *len) { if (response.code < 100 || response.code >= 600) { return NULL; } const char *message = response_message (response.code); int length = strlen (HTTP_VERSION) + 1 + 3 + 1 + strlen (message) + 2 + 2; int i; for (i = 0; i < response.header_count; i++) { struct http_header *header = &response.headers[i]; length += strlen (header->key) + 2 + strlen (header->val) + 2; } char *result = malloc (length); char *pos = result; int wrote = snprintf (pos, length, "%s %d %s\r\n", HTTP_VERSION, response.code, message); pos += wrote; length -= wrote; for (i = 0; i < response.header_count; i++) { struct http_header *header = &response.headers[i]; wrote = snprintf (pos, length, "%s: %s\r\n", header->key, header->val); pos += wrote; length -= wrote; } *pos++ = '\r'; *pos++ = '\n'; length -= 2; if (len) { *len = (pos - result); } return result; }
void run_worker_udp_messenger(const rstd::vector<rstd::string> &client_peer_strings, const rstd::vector<rstd::string> &worker_peer_strings, const rstd::string &endpoint_info) { udp_messenger_type messenger(client_peer_strings, worker_peer_strings, endpoint_info, false); udp_messenger_type::message request_message; while (true) { if (messenger.receive(request_message)) { NP1_TEST_ASSERT(request_message.has_valid_peer()); // DON'T assume that message payload is null-terminated in production code. rstd::string request_payload = (const char *)request_message.payload_ptr(); if (request_payload == "exit") { return; } udp_messenger_type::message response_message( request_message, udp_messenger_type::message::ack_marker()); rstd::string response_payload = "x" + request_payload; response_message.append(response_payload.c_str(), response_payload.length() + 1); messenger.send(response_message); } } }
static int check_adduser(Ceo__AddUser *in, Ceo__AddUserResponse *out, char *client) { int office = check_group(client, "office"); int syscom = check_group(client, "syscom"); notice("adding uid=%s cn=%s by %s", in->username, in->realname, client); if (!office && !syscom) return response_message(out, EPERM, "%s not authorized to create users", client); if (!in->username) return response_message(out, EINVAL, "missing required argument: username"); if (!in->realname) return response_message(out, EINVAL, "missing required argument: realname"); switch (in->type) { case CEO__ADD_USER__TYPE__MEMBER: case CEO__ADD_USER__TYPE__CLUB_REP: if (!in->password) return response_message(out, EINVAL, "missing required argument: password"); break; case CEO__ADD_USER__TYPE__CLUB: if (in->password) return response_message(out, EINVAL, "club accounts cannot have passwords"); if (in->program) return response_message(out, EINVAL, "club accounts cannot have programs"); break; default: return response_message(out, EINVAL, "invalid user type: %d", in->type); } if (getpwnam(in->username) != NULL) return response_message(out, EEXIST, "user %s already exists", in->username); if (getgrnam(in->username) != NULL) return response_message(out, EEXIST, "group %s already exists", in->username); if (ceo_user_exists(in->username)) return response_message(out, EEXIST, "user %s already exists in LDAP", in->username); if (ceo_group_exists(in->username)) return response_message(out, EEXIST, "group %s already exists in LDAP", in->username); return 0; }
static int32_t addclub(Ceo__AddUser *in, Ceo__AddUserResponse *out) { char homedir[1024]; char acl[64]; int krb_stat, user_stat, group_stat, sudo_stat, home_stat, quota_stat; int id; if (snprintf(homedir, sizeof(homedir), "%s/%s", club_home, in->username) >= sizeof(homedir)) fatal("homedir overflow"); if ((id = ceo_new_uid(club_min_id, club_max_id)) <= 0) fatal("no available uids in range [%ld, %ld]", club_min_id, club_max_id); if (snprintf(acl, sizeof(acl), CLUB_ACL, id) >= sizeof(acl)) fatal("acl overflow"); if ((krb_stat = ceo_del_princ(in->username))) return response_message(out, EKERB, "unable to clear principal %s", in->username); if ((user_stat = ceo_add_user(in->username, ldap_users_base, "club", in->realname, homedir, NULL, club_shell, id, NULL))) return response_message(out, ELDAP, "unable to create ldap account %s", in->username); response_message(out, 0, "successfully created ldap account"); /* errors that occur after this point are not fatal */ if ((group_stat = ceo_add_group(in->username, ldap_groups_base, id))) response_message(out, ELDAP, "unable to create ldap group %s", in->username); else response_message(out, 0, "successfully created ldap group"); if ((sudo_stat = ceo_add_group_sudo(in->username, ldap_sudo_base))) response_message(out, ELDAP, "unable to create ldap sudoers %s", in->username); else response_message(out, 0, "successfully created ldap sudoers"); if ((home_stat = ceo_create_home(homedir, club_home_skel, id, id, acl, acl, NULL))) response_message(out, EHOME, "unable to create home directory for %s", in->username); else response_message(out, 0, "successfully created home directory"); if ((quota_stat = ceo_set_quota("csc", id))) response_message(out, EQUOTA, "unable to set quota for %s", in->username); else response_message(out, 0, "successfully set quota"); return user_stat || group_stat || sudo_stat || home_stat || quota_stat; }
static int32_t addmember(Ceo__AddUser *in, Ceo__AddUserResponse *out) { char homedir[1024]; char principal[1024]; int user_stat, group_stat, krb_stat, home_stat, quota_stat; int id; if (snprintf(principal, sizeof(principal), "%s@%s", in->username, krb5_realm) >= sizeof(principal)) fatal("principal overflow"); if (snprintf(homedir, sizeof(homedir), "%s/%s", member_home, in->username) >= sizeof(homedir)) fatal("homedir overflow"); if ((id = ceo_new_uid(member_min_id, member_max_id)) <= 0) fatal("no available uids in range [%ld, %ld]", member_min_id, member_max_id); if ((krb_stat = ceo_del_princ(in->username))) return response_message(out, EEXIST, "unable to overwrite orphaned kerberos principal %s", in->username); if ((user_stat = ceo_add_user(in->username, ldap_users_base, "member", in->realname, homedir, member_shell, id, "program", in->program, NULL))) return response_message(out, ELDAP, "unable to create ldap account %s", in->username); response_message(out, 0, "successfully created ldap account"); /* errors that occur after this point are not fatal */ if ((krb_stat = ceo_add_princ(in->username, in->password))) return response_message(out, EKERB, "unable to create kerberos principal %s", in->username); response_message(out, 0, "successfully created principal"); if ((group_stat = ceo_add_group(in->username, ldap_groups_base, id))) response_message(out, ELDAP, "unable to create ldap group %s", in->username); else response_message(out, 0, "successfully created ldap group"); if ((home_stat = ceo_create_home(homedir, member_home_skel, id, id, NULL, NULL, in->email))) response_message(out, EHOME, "unable to create home directory for %s", in->username); else response_message(out, 0, "successfully created home directory"); if ((quota_stat = ceo_set_quota("ctdalek", id))) response_message(out, EQUOTA, "unable to set quota for %s", in->username); else response_message(out, 0, "successfully set quota"); return krb_stat || user_stat || group_stat || home_stat || quota_stat; }
int main(int argc, char *argv[]) { char userpwd[USERPWD_LENGTH]; char *cache_data, *new_cache_data, *cache_ptr, *cache_file_path, *screen_name; size_t cache_size, old_cache_size; long cache_space; config *cfg; CURL *curl; CURLcode res; long response_code; memory friends_xml = {NULL, 0}; xmlDocPtr doc; xmlXPathObjectPtr xpath_result; xmlNodeSetPtr nodeset; FILE* cache_file; unsigned int i, length; /* load configuration */ cfg = parse_config(); if(!cfg) COMPLAIN_AND_EXIT("Error: Could not load or parse configuration. Make sure that the " \ "configuration file exists, is readable and contains the necessary " \ "information (see the README for more information).\n"); SNPRINTF(userpwd, USERPWD_LENGTH, "%s:%s", cfg->username, cfg->password); free(cfg); /* retrieve friends XML */ curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_memory); curl_easy_setopt(curl, CURLOPT_USERAGENT, USERAGENT_HEADER); curl_easy_setopt(curl, CURLOPT_URL, TWITTER_FRIENDS_URL); curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &friends_xml); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); curl_easy_cleanup(curl); if (!friends_xml.memory) COMPLAIN_AND_EXIT("Error: Received no data from Twitter.\n"); if (res != CURLE_OK) { free(friends_xml.memory); COMPLAIN_AND_EXIT("(Twitter) Error: %s\n", curl_easy_strerror(res)); } if (!(response_code == OK || response_code == NOT_MODIFIED)) { free(friends_xml.memory); COMPLAIN_AND_EXIT("(Twitter) Error: %s (#%lu)\n", response_message(response_code), response_code); } /* parse friends XML */ doc = xmlParseMemory(friends_xml.memory, friends_xml.size); free(friends_xml.memory); if (!doc) COMPLAIN_AND_EXIT("Error: Unable to parse Twitter's XML response.\n"); xpath_result = eval_xpath(doc, (xmlChar*) "//screen_name"); if (!xpath_result) { xmlFreeDoc(doc); xmlCleanupParser(); return 0; // no data to process - should perhaps empty cache } nodeset = xpath_result->nodesetval; cache_size = nodeset->nodeNr*15; // assuming each screen name being avg. 15 chars long cache_space = cache_size; cache_data = malloc(cache_size); cache_ptr = cache_data; /* prepare data for writing */ for (i = 0; i < nodeset->nodeNr; i++) { screen_name = (char*)xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1); length = strlen(screen_name) + 2; cache_space -= length; if (cache_space <= 0) { old_cache_size = cache_size; cache_size = MAX(cache_size*2, cache_size + length); cache_space = cache_space + (cache_size - old_cache_size); new_cache_data = realloc(cache_data, cache_size); if (!new_cache_data) { free(cache_data); xmlXPathFreeObject(xpath_result); xmlFreeDoc(doc); xmlCleanupParser(); COMPLAIN_AND_EXIT("Error: Could not allocate memory.\n"); } cache_ptr = new_cache_data + (cache_ptr - cache_data); cache_data = new_cache_data; } if (i == 0) { strncpy(cache_ptr, screen_name, length - 1); cache_ptr += length-2; } else { SNPRINTF(cache_ptr, length, " %s", screen_name); cache_ptr += length-1; } free(screen_name); } xmlXPathFreeObject(xpath_result); xmlFreeDoc(doc); xmlCleanupParser(); *cache_ptr = '\0'; /* write data to cache file */ cache_file_path = get_absolute_path(CACHE_FILENAME); cache_file = fopen(cache_file_path, "w+"); if (!cache_file) COMPLAIN_AND_EXIT("Error: Could not open cache file (%s)\n", strerror(errno)); fputs(cache_data, cache_file); fclose(cache_file); free(cache_file_path); } return 0; }