static int cal_log_filepath(char **out, const struct log_t *log, const char *srcname, const char *desdir) { const char *filename; int need_timestamp = 0; int hours; char timebuf[UPTIME_SIZE]; if (!out || !log || !desdir) return -1; if (is_ac_filefmt(log->path)) filename = srcname; else filename = log->name; if (!filename) return -1; if (!strcmp(log->type, "cmd") || log->lines) need_timestamp = 1; if (need_timestamp) { if (get_uptime_string(timebuf, &hours) == -1) return -1; return asprintf(out, "%s/%s_%s", desdir, filename, timebuf); } return asprintf(out, "%s/%s", desdir, filename); }
static void resp_status_page(void) { char uptime_str[32]; char time_str[32]; get_time_string(time_str, sizeof(time_str)); get_uptime_string(uptime_str, sizeof(uptime_str)); puts("Content-type: text/html\n"); puts("<!DOCTYPE html>"); puts("<head>"); puts(" <meta charset=\"utf-8\">"); puts(" <meta http-equiv=\"Refresh\" content=\"10\">"); puts("<title>"); printf("%s Status Page", hostname); puts("</title>"); puts("</head>"); puts("<body>"); puts("<p>Server: OK</p>"); printf("<p>%s</p>\n", time_str); printf("<p>Uptime: %s</p>", uptime_str); puts("</body>"); puts("</html>"); }
static void resp_uptime(void) { char uptime_str[32]; get_uptime_string(uptime_str, sizeof(uptime_str)); puts("Content-type: text/html\n"); printf("%s", uptime_str); }
/* Pipe the splash page from the splash page file, * or redirect to remote authenticator as required. */ void http_nodogsplash_serve_splash(request *r, t_auth_target *authtarget, t_client *client, const char error_msg[]) { char *tmpstr; char line [MAX_BUF]; char *splashfilename; FILE *fd; s_config *config; config = config_get_config(); if(config->remote_auth_action) { /* Redirect to remote auth server instead of serving local splash page */ http_nodogsplash_redirect_remote_auth(r, authtarget); return; } /* Set variables; these can be interpolated in the splash page text. */ if (error_msg) httpdAddVariable(r,"error_msg", error_msg); else httpdAddVariable(r,"error_msg", ""); httpdAddVariable(r,"gatewayname",config->gw_name); httpdAddVariable(r,"tok",authtarget->token); httpdAddVariable(r,"redir",authtarget->redir); httpdAddVariable(r,"authaction",authtarget->authaction); httpdAddVariable(r,"denyaction",authtarget->denyaction); httpdAddVariable(r,"authtarget",authtarget->authtarget); httpdAddVariable(r,"clientip",client->ip); httpdAddVariable(r,"clientmac",client->mac); httpdAddVariable(r,"gatewaymac",config->gw_mac); safe_asprintf(&tmpstr, "%d", get_client_list_length()); httpdAddVariable(r,"nclients",tmpstr); free(tmpstr); safe_asprintf(&tmpstr, "%d", config->maxclients); httpdAddVariable(r,"maxclients",tmpstr); free(tmpstr); tmpstr = get_uptime_string(); httpdAddVariable(r,"uptime",tmpstr); free(tmpstr); /* We need to have imagesdir and pagesdir appear in the page as absolute paths, so they work no matter what the initial user request URL was */ safe_asprintf(&tmpstr, "/%s", config->imagesdir); httpdAddVariable(r,"imagesdir",tmpstr); free(tmpstr); safe_asprintf(&tmpstr, "/%s", config->pagesdir); httpdAddVariable(r,"pagesdir",tmpstr); free(tmpstr); /* Pipe the splash page from its file */ safe_asprintf(&splashfilename, "%s/%s", config->webroot, config->splashpage ); debug(LOG_INFO,"Serving splash page %s to %s", splashfilename,r->clientAddr); if (!(fd = fopen(splashfilename, "r"))) { debug(LOG_ERR, "Could not open splash page file '%s'", splashfilename); http_nodogsplash_serve_info(r, "Nodogsplash Error", "Failed to open splash page"); } else { while (fgets(line, MAX_BUF, fd)) { httpdOutput(r,line); } fclose(fd); } free(splashfilename); }
/** * @brief show_splashpage is called when the client clicked on Ok as well when the client doesn't know us yet. * @param connection * @param client * @return */ static int show_splashpage(struct MHD_Connection *connection, t_client *client) { struct MHD_Response *response; struct templater templor; s_config *config = config_get_config(); int ret = -1; char filename[PATH_MAX]; const char *mimetype; int size = 0, bytes = 0; int splashpage_fd; char *splashpage_result; char *splashpage_tmpl; snprintf(filename, PATH_MAX, "%s/%s",config->webroot ,config->splashpage); splashpage_fd = open(filename, O_RDONLY); if (splashpage_fd < 0) return send_error(connection, 404); mimetype = lookup_mimetype(filename); /* input size */ size = lseek(splashpage_fd, 0, SEEK_END); lseek(splashpage_fd, 0, SEEK_SET); /* we TMPLVAR_SIZE for template variables */ splashpage_tmpl = calloc(1, size); splashpage_result = calloc(1, size + TMPLVAR_SIZE); while (bytes < size) { ret = read(splashpage_fd, splashpage_tmpl+bytes, size-bytes); if (ret < 0) { free(splashpage_result); free(splashpage_tmpl); close(splashpage_fd); return send_error(connection, 503); } bytes += ret; } char *uptime = get_uptime_string(); char *nclients = NULL; char *maxclients = NULL; char *denyaction = NULL; char *authaction = NULL; char *authtarget = NULL; const char *redirect_url = NULL; char redirect_url_encoded[2048]; char *imagesdir = NULL; char *pagesdir = NULL; memset(redirect_url_encoded, 0, sizeof(redirect_url_encoded)); redirect_url = get_redirect_url(connection); if (redirect_url) { uh_urlencode(redirect_url_encoded, sizeof(redirect_url_encoded), redirect_url, strlen(redirect_url)); } safe_asprintf(&nclients, "%d", get_client_list_length()); safe_asprintf(&maxclients, "%d", config->maxclients); safe_asprintf(&denyaction, "http://%s:%d/%s/", config->gw_address, config->gw_port, config->denydir); safe_asprintf(&authaction, "http://%s:%d/%s/", config->gw_address, config->gw_port, config->authdir); safe_asprintf(&authtarget, "http://%s:%d/%s/?token=%s&redir=%s", config->gw_address, config->gw_port, config->authdir, client->token, redirect_url_encoded); safe_asprintf(&authaction, "http://%s:%d/%s/", config->gw_address, config->gw_port, config->authdir); safe_asprintf(&pagesdir, "/%s", config->pagesdir); safe_asprintf(&imagesdir, "/%s", config->imagesdir); tmpl_init_templor(&templor); tmpl_set_variable(&templor, "authaction", authaction); tmpl_set_variable(&templor, "authtarget", authtarget); tmpl_set_variable(&templor, "clientip", client->ip); tmpl_set_variable(&templor, "clientmac", client->mac); // tmpl_set_variable(&templor, "content", VERSION); tmpl_set_variable(&templor, "denyaction", denyaction); tmpl_set_variable(&templor, "error_msg", ""); tmpl_set_variable(&templor, "gatewaymac", config->gw_mac); tmpl_set_variable(&templor, "gatewayname", config->gw_name); tmpl_set_variable(&templor, "imagesdir", imagesdir); tmpl_set_variable(&templor, "pagesdir", pagesdir); tmpl_set_variable(&templor, "maxclients", maxclients); tmpl_set_variable(&templor, "nclients", nclients); tmpl_set_variable(&templor, "redir", redirect_url); tmpl_set_variable(&templor, "tok", client->token); tmpl_set_variable(&templor, "uptime", uptime); tmpl_set_variable(&templor, "version", VERSION); tmpl_parse(&templor, splashpage_result, size + TMPLVAR_SIZE, splashpage_tmpl, size); free(authaction); free(denyaction); free(maxclients); free(nclients); free(uptime); free(splashpage_tmpl); free(imagesdir); response = MHD_create_response_from_buffer(strlen(splashpage_result), (void *)splashpage_result, MHD_RESPMEM_MUST_FREE); if (!response) { close(splashpage_fd); return send_error(connection, 503); } MHD_add_response_header(response, "Content-Type", mimetype); ret = MHD_queue_response(connection, MHD_HTTP_OK, response); MHD_destroy_response(response); close(splashpage_fd); return ret; }
/** * Create a crashfile with given params. * * @param dir Where to generate crashfile. * @param event Event name. * @param hashkey Event id. * @param type Subtype of this event. * @param data* String obtained by get_data. */ void generate_crashfile(const char *dir, const char *event, size_t elen, const char *hashkey, size_t hlen, const char *type, size_t tlen, const char *data0, size_t d0len, const char *data1, size_t d1len, const char *data2, size_t d2len) { char *buf; char *path; char *tail; char datetime[LONG_TIME_SIZE]; char uptime[UPTIME_SIZE]; int hours; const int fmtsize = 128; size_t ltlen; int n; int filesize; if (!dir || !event || !elen || !hashkey || !hlen || !type || !tlen) return; if (d0len > 0 && !data0) return; if (d1len > 0 && !data1) return; if (d2len > 0 && !data2) return; ltlen = get_current_time_long(datetime); if (!ltlen) return; n = get_uptime_string(uptime, &hours); if (n < 0) return; filesize = fmtsize + ltlen + n + elen + hlen + tlen + d0len + d1len + d2len + strnlen(guuid, UUID_SIZE) + strnlen(gbuildversion, BUILD_VERSION_SIZE); buf = malloc(filesize); if (buf == NULL) { LOGE("out of memory\n"); return; } tail = cf_line(buf, "EVENT=", 6, event, elen); tail = cf_line(tail, "ID=", 3, hashkey, hlen); tail = cf_line(tail, "DEVICEID=", 9, guuid, strnlen(guuid, UUID_SIZE)); tail = cf_line(tail, "DATE=", 5, datetime, ltlen); tail = cf_line(tail, "UPTIME=", 7, uptime, n); tail = cf_line(tail, "BUILD=", 6, gbuildversion, strnlen(gbuildversion, BUILD_VERSION_SIZE)); tail = cf_line(tail, "TYPE=", 5, type, tlen); if (d0len) tail = cf_line(tail, "DATA0=", 6, data0, d0len); if (d1len) tail = cf_line(tail, "DATA1=", 6, data1, d1len); if (d2len) tail = cf_line(tail, "DATA2=", 6, data2, d2len); tail = mempcpy(tail, "_END\n", 5); *tail = '\0'; if (asprintf(&path, "%s/crashfile", dir) == -1) { LOGE("out of memory\n"); free(buf); return; } if (overwrite_file(path, buf) != 0) LOGE("failed to new crashfile (%s), error (%s)\n", path, strerror(errno)); free(buf); free(path); }