int set_runtime_element(struct static_hash_table *ptable, const char *key, int nkeylen, const char *data, int ndatalen) { struct static_hash_node *p; struct static_hash_node *newnode; int found; unsigned long hash; unsigned long index; p = runtime_lookup(ptable, key, nkeylen, &found, &hash, &index); if (found) { if (p->data) free(p->data); } else // !found { if (p->key) { newnode = (struct static_hash_node *)malloc(sizeof(struct static_hash_node)); if (!newnode) return 0; memset(newnode, 0x00, sizeof(struct static_hash_node)); while (p->link != 0) p = (struct static_hash_node *)(p->link); p->link = (size_t)newnode; ptable->pdata[index].entries++; ptable->non_root_entries++; p = newnode; } p->key = _strndup(key, nkeylen); p->hash = hash; if (!p->key) return 0; ptable->size++; } p->data = _strndup(data, ndatalen); if (!p->data) return 0; return 1; }
char * get_cpuinfo_line (char *cpuinfo, const char *tag) { char *flags; char *end; char *colon; flags = strstr(cpuinfo,tag); if (flags == NULL) return NULL; end = strchr(flags, '\n'); if (end == NULL) return NULL; colon = strchr (flags, ':'); if (colon == NULL) return NULL; colon++; if(colon >= end) return NULL; return _strndup (colon, end-colon); }
char * get_tag_value (char *s, const char *tag) { char *flags; char *end; char *colon; flags = strstr(s,tag); if (flags == NULL) return NULL; end = strchr(flags, '\n'); if (end == NULL) return NULL; colon = strchr (flags, ':'); if (colon == NULL) return NULL; colon++; if(colon >= end) return NULL; return _strndup (colon, end-colon); }
static void chap_send_failure(struct chap_auth_data_t *ad) { struct chap_failure_t msg = { .hdr.proto = htons(PPP_CHAP), .hdr.code = CHAP_FAILURE, .hdr.id = ad->id, .hdr.len = htons(sizeof(msg) - 1 - 2), .message = MSG_FAILURE, }; if (conf_ppp_verbose) log_ppp_info2("send [CHAP Failure id=%x \"%s\"]\n", msg.hdr.id, MSG_FAILURE); ppp_chan_send(ad->ppp, &msg, ntohs(msg.hdr.len) + 2); } static void chap_send_success(struct chap_auth_data_t *ad) { struct chap_success_t msg = { .hdr.proto = htons(PPP_CHAP), .hdr.code = CHAP_SUCCESS, .hdr.id = ad->id, .hdr.len = htons(sizeof(msg)-1-2), .message = MSG_SUCCESS, }; if (conf_ppp_verbose) log_ppp_info2("send [CHAP Success id=%x \"%s\"]\n", msg.hdr.id, MSG_SUCCESS); ppp_chan_send(ad->ppp, &msg, ntohs(msg.hdr.len) + 2); } static void chap_send_challenge(struct chap_auth_data_t *ad) { struct chap_challenge_t msg = { .hdr.proto = htons(PPP_CHAP), .hdr.code = CHAP_CHALLENGE, .hdr.id = ++ad->id, .hdr.len = htons(sizeof(msg) - 2), .val_size = VALUE_SIZE, }; read(urandom_fd, ad->val, VALUE_SIZE); memcpy(msg.val, ad->val, VALUE_SIZE); if (conf_ppp_verbose) { log_ppp_info2("send [CHAP Challenge id=%x <", msg.hdr.id); print_buf(msg.val, VALUE_SIZE); log_ppp_info2(">]\n"); } ppp_chan_send(ad->ppp, &msg, ntohs(msg.hdr.len) + 2); if (conf_timeout && !ad->timeout.tpd) triton_timer_add(ad->ppp->ctrl->ctx, &ad->timeout, 0); } static void chap_recv_response(struct chap_auth_data_t *ad, struct chap_hdr_t *hdr) { MD5_CTX md5_ctx; uint8_t md5[MD5_DIGEST_LENGTH]; char *passwd; char *name; int r; struct chap_challenge_t *msg = (struct chap_challenge_t*)hdr; if (ad->timeout.tpd) triton_timer_del(&ad->timeout); if (conf_ppp_verbose) { log_ppp_info2("recv [CHAP Response id=%x <", msg->hdr.id); print_buf(msg->val, msg->val_size); log_ppp_info2(">, name=\""); print_str(msg->name, ntohs(msg->hdr.len) - sizeof(*msg) + 2); log_ppp_info2("\"]\n"); } if (msg->hdr.id != ad->id) { if (conf_ppp_verbose) log_ppp_warn("chap-md5: id mismatch\n"); return; } if (msg->val_size != VALUE_SIZE) { log_ppp_error("chap-md5: incorrect value-size (%i)\n", msg->val_size); chap_send_failure(ad); if (ad->started) ppp_terminate(ad->ppp, TERM_USER_ERROR, 0); else ppp_auth_failed(ad->ppp, NULL); return; } name = _strndup(msg->name,ntohs(msg->hdr.len) - sizeof(*msg) + 2); if (conf_any_login) { if (ppp_auth_successed(ad->ppp, name)) { chap_send_failure(ad); ppp_terminate(ad->ppp, TERM_AUTH_ERROR, 0); _free(name); return; } chap_send_success(ad); ad->started = 1; return; } r = pwdb_check(ad->ppp, name, PPP_CHAP, CHAP_MD5, ad->id, ad->val, VALUE_SIZE, msg->val); if (r == PWDB_NO_IMPL) { passwd = pwdb_get_passwd(ad->ppp,name); if (!passwd) { _free(name); if (conf_ppp_verbose) log_ppp_warn("chap-md5: user not found\n"); chap_send_failure(ad); return; } MD5_Init(&md5_ctx); MD5_Update(&md5_ctx,&msg->hdr.id,1); MD5_Update(&md5_ctx,passwd,strlen(passwd)); MD5_Update(&md5_ctx,ad->val,VALUE_SIZE); MD5_Final(md5,&md5_ctx); if (memcmp(md5,msg->val,sizeof(md5))) { if (conf_ppp_verbose) log_ppp_warn("chap-md5: challenge response mismatch\n"); chap_send_failure(ad); if (ad->started) ppp_terminate(ad->ppp, TERM_USER_ERROR, 0); else ppp_auth_failed(ad->ppp, name); _free(name); } else { if (!ad->started) { if (ppp_auth_successed(ad->ppp, name)) { chap_send_failure(ad); ppp_terminate(ad->ppp, TERM_AUTH_ERROR, 0); _free(name); } else { chap_send_success(ad); ad->started = 1; if (conf_interval) triton_timer_add(ad->ppp->ctrl->ctx, &ad->interval, 0); } } else _free(name); } _free(passwd); } else if (r == PWDB_DENIED) { chap_send_failure(ad); if (ad->started) ppp_terminate(ad->ppp, TERM_USER_ERROR, 0); else ppp_auth_failed(ad->ppp, name); _free(name); } else { if (!ad->started) { if (ppp_auth_successed(ad->ppp, name)) { chap_send_failure(ad); ppp_terminate(ad->ppp, TERM_AUTH_ERROR, 0); _free(name); } else { chap_send_success(ad); ad->started = 1; if (conf_interval) triton_timer_add(ad->ppp->ctrl->ctx, &ad->interval, 0); } } else { chap_send_success(ad); _free(name); } } } static int chap_check(uint8_t *ptr) { return *ptr == CHAP_MD5; } static int chap_restart(struct ppp_t *ppp, struct auth_data_t *auth) { struct chap_auth_data_t *d = container_of(auth, typeof(*d), auth); chap_send_challenge(d); return 0; } static struct ppp_auth_handler_t chap= { .name = "CHAP-md5", .init = auth_data_init, .free = auth_data_free, .send_conf_req = lcp_send_conf_req, .recv_conf_req = lcp_recv_conf_req, .start = chap_start, .finish = chap_finish, .check = chap_check, .restart = chap_restart, }; static void chap_recv(struct ppp_handler_t *h) { struct chap_auth_data_t *d = container_of(h, typeof(*d), h); struct chap_hdr_t *hdr = (struct chap_hdr_t *)d->ppp->buf; if (d->ppp->buf_size < sizeof(*hdr) || ntohs(hdr->len) < HDR_LEN || ntohs(hdr->len) < d->ppp->buf_size - 2) { log_ppp_warn("chap-md5: short packet received\n"); return; } if (hdr->code == CHAP_RESPONSE) chap_recv_response(d, hdr); else log_ppp_warn("chap-md5: unknown code received %x\n", hdr->code); } static void load_config(void) { const char *opt; opt = conf_get_opt("auth", "timeout"); if (opt && atoi(opt) > 0) conf_timeout = atoi(opt); opt = conf_get_opt("auth", "interval"); if (opt && atoi(opt) > 0) conf_interval = atoi(opt); opt = conf_get_opt("auth", "max-failure"); if (opt && atoi(opt) > 0) conf_max_failure = atoi(opt); opt = conf_get_opt("auth", "any-login"); if (opt) conf_any_login = atoi(opt); } static void auth_chap_md5_init() { load_config(); if (ppp_auth_register_handler(&chap)) log_emerg("chap-md5: failed to register handler\n"); triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config); } DEFINE_INIT(4, auth_chap_md5_init);
char* _strdup(const char *src) { size_t len = strlen(src); return _strndup(src, len ); }
/* attributes are of the form [key1, key2, key3=value, key4="repr"] */ static NEOERR* parse_attr(char **str, HDF_ATTR **attr) { NEOERR *err = STATUS_OK; char *s = *str; char *k, *v; int k_l, v_l; STRING buf; char c; HDF_ATTR *ha, *hal = NULL; *attr = NULL; string_init(&buf); while (*s && *s != ']') { k = s; k_l = 0; v = NULL; v_l = 0; while (*s && isalnum(*s)) s++; k_l = s-k; if (*s == '\0' || k_l == 0) { _dealloc_hdf_attr(attr); return nerr_raise(NERR_PARSE, "Misformed attribute specification: %s", *str); } SKIPWS(s); if (*s == '=') { s++; SKIPWS(s); if (*s == '"') { s++; while (*s && *s != '"') { if (*s == '\\') { if (isdigit(*(s+1))) { s++; c = *s - '0'; if (isdigit(*(s+1))) { s++; c = (c * 8) + (*s - '0'); if (isdigit(*(s+1))) { s++; c = (c * 8) + (*s - '0'); } } } else { s++; if (*s == 'n') c = '\n'; else if (*s == 't') c = '\t'; else if (*s == 'r') c = '\r'; else c = *s; } err = string_append_char(&buf, c); } else { err = string_append_char(&buf, *s); } if (err) { string_clear(&buf); _dealloc_hdf_attr(attr); return nerr_pass(err); } s++; } if (*s == '\0') { _dealloc_hdf_attr(attr); string_clear(&buf); return nerr_raise(NERR_PARSE, "Misformed attribute specification: %s", *str); } s++; v = buf.buf; v_l = buf.len; } else { v = s; while (*s && *s != ' ' && *s != ',' && *s != ']') s++; if (*s == '\0') { _dealloc_hdf_attr(attr); return nerr_raise(NERR_PARSE, "Misformed attribute specification: %s", *str); } v_l = s-v; } } else { v = "1"; } ha = (HDF_ATTR*) calloc (1, sizeof(HDF_ATTR)); if (ha == NULL) { _dealloc_hdf_attr(attr); string_clear(&buf); return nerr_raise(NERR_NOMEM, "Unable to load attributes: %s", s); } if (*attr == NULL) *attr = ha; ha->key = _strndup(k, k_l); if (v) ha->value = _strndup(v, v_l); else ha->value = strdup(""); if (ha->key == NULL || ha->value == NULL) { _dealloc_hdf_attr(attr); string_clear(&buf); return nerr_raise(NERR_NOMEM, "Unable to load attributes: %s", s); } if (hal != NULL) hal->next = ha; hal = ha; string_clear(&buf); SKIPWS(s); if (*s == ',') { s++; SKIPWS(s); } } if (*s == '\0') { _dealloc_hdf_attr(attr); return nerr_raise(NERR_PARSE, "Misformed attribute specification: %s", *str); } *str = s+1; return STATUS_OK; }
static unsigned char esl_console_complete(const char *buffer, const char *cursor, const char *lastchar) { char cmd_str[2048] = ""; unsigned char ret = CC_REDISPLAY; char *dup = _strndup(buffer, (int)(lastchar - buffer)); char *buf = dup; int sc = 0, offset = (int)(cursor - buffer), pos = (offset > 0) ? offset : 0; char *p; if (pos > 0) { *(buf + pos) = '\0'; } if ((p = strchr(buf, '\r')) || (p = strchr(buf, '\n'))) { *p = '\0'; } while (*buf == ' ') { buf++; sc++; } #ifdef HAVE_EDITLINE if (!*buf && sc) { el_deletestr(el, sc); sc = 0; } #endif p = end_of_str(buf); while(p >= buf && *p == ' ') { sc++; p--; } #ifdef HAVE_EDITLINE if (sc > 1) { el_deletestr(el, sc - 1); *(p + 2) = '\0'; } #endif if (*cursor) { snprintf(cmd_str, sizeof(cmd_str), "api console_complete c=%ld;%s\n\n", (long)pos, buf); } else { snprintf(cmd_str, sizeof(cmd_str), "api console_complete %s\n\n", buf); } esl_send_recv(global_handle, cmd_str); if (global_handle->last_sr_event && global_handle->last_sr_event->body) { char *r = global_handle->last_sr_event->body; char *w, *p1; if (r) { if ((w = strstr(r, "\n\nwrite="))) { int len = 0; *w = '\0'; w += 8; len = atoi(w); if ((p1= strchr(w, ':'))) { w = p1+ 1; } printf("%s\n\n\n", r); #ifdef HAVE_EDITLINE el_deletestr(el, len); el_insertstr(el, w); #else #ifdef _MSC_VER console_bufferInput(0, len, (char*)buffer, DELETE_REFRESH_OP); console_bufferInput(w, (int)strlen(w), (char*)buffer, 0); #endif #endif } else { printf("%s\n", r); #ifdef _MSC_VER console_bufferInput(0, 0, (char*)buffer, DELETE_REFRESH_OP); #endif } } fflush(stdout); } esl_safe_free(dup); return ret; }