static char * icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r) { char *ret = NOT_FOUND; char *type = NULL; char *local = NULL; char *pngfile = NULL; struct command *cmd; int r; r = guestfs_exists (g, CIRROS_LOGO); if (r == -1) { ret = NULL; /* a real error */ goto out; } if (r == 0) goto out; /* Check the file type and geometry. */ type = guestfs_file (g, CIRROS_LOGO); if (!type) goto out; if (!STRPREFIX (type, "ASCII text")) goto out; local = guestfs___download_to_tmp (g, fs, CIRROS_LOGO, "icon", 1024); if (!local) goto out; /* Use pbmtext to render it. */ pngfile = safe_asprintf (g, "%s/cirros.png", g->tmpdir); cmd = guestfs___new_command (g); guestfs___cmd_add_string_unquoted (cmd, PBMTEXT " < "); guestfs___cmd_add_string_quoted (cmd, local); guestfs___cmd_add_string_unquoted (cmd, " | " PNMTOPNG " > "); guestfs___cmd_add_string_quoted (cmd, pngfile); r = guestfs___cmd_run (cmd); guestfs___cmd_close (cmd); if (r == -1) goto out; if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) goto out; /* Read it into memory. */ if (read_whole_file (g, pngfile, &ret, size_r) == -1) { ret = NULL; goto out; } out: free (pngfile); free (local); free (type); return ret; }
/** * @brief redirect_to_splashpage * @param connection * @param client * @param host * @param url * @return */ static int redirect_to_splashpage(struct MHD_Connection *connection, t_client *client, const char *host, const char *url) { char *originurl = NULL; char *query = ""; get_query(connection, &query); safe_asprintf(&originurl, "http://%s%s%s%s", host, url, strlen(query) ? "?" : "" , query); return encode_and_redirect_to_splashpage(connection, originurl); }
void http_callback_status(httpd *webserver, request *r) { char * status = NULL; char * prestatus = NULL; status = get_status_text(); safe_asprintf(&prestatus, "<pre>\n%s\n</pre>", status); http_nodogsplash_serve_info(r, "Nodogsplash Status",prestatus); free(status); free(prestatus); }
void http_callback_logout(httpd *webserver, request *r) { t_client *client; s_config *config = config_get_config(); LOCK_CLIENT_LIST(); client = client_list_find_by_ip(r->clientAddr); /* Send logout to auth server if client is logged in */ if (client != NULL) { t_authresponse authresponse; char *ip = strdup(client->ip); char *mac = strdup(client->mac); char *token = strdup(client->token); unsigned long long incoming = client->counters.incoming; unsigned long long outgoing = client->counters.outgoing; debug(LOG_INFO, "Got manual logout from client ip %s, mac %s, token %s", client->ip, client->mac, client->token); fw_deny(client->ip, client->mac, client->fw_connection_state); client_list_delete(client); /* Unlock client list here since auth_server_request may take a while */ UNLOCK_CLIENT_LIST(); /* Advertise the logout if we have an auth server */ if (config->auth_servers != NULL) { auth_server_request(&authresponse, REQUEST_TYPE_LOGOUT, ip, mac, token, incoming, outgoing); } free(ip); free(mac); free(token); } else { /* Do nothing if the client is not in the client list (i.e. not logged in) */ debug(LOG_INFO, "Got manual logout from client %s, but client not in list", r->clientAddr); UNLOCK_CLIENT_LIST(); } if (config->auth_servers != NULL) { /* Re-direct them to auth server */ char *urlFragment = NULL; t_auth_serv *auth_server = get_auth_server(); safe_asprintf(&urlFragment, "%smessage=%s", auth_server->authserv_msg_script_path_fragment, GATEWAY_MESSAGE_ACCOUNT_LOGGED_OUT ); http_send_redirect_to_auth(r, urlFragment, "Redirect to logout message"); free(urlFragment); } }
/** @brief Sends a redirect to the web browser * @param r The request * @param url The url to redirect to * @param text The text to include in the redirect header and the manual redirect link title. NULL is acceptable */ void http_send_redirect(request * r, const char *url, const char *text) { char *message = NULL; char *header = NULL; char *response = NULL; /* Re-direct them to auth server */ debug(LOG_DEBUG, "Redirecting client browser to %s", url); safe_asprintf(&header, "Location: %s", url); // liudf 20160104; change 302 to 307 safe_asprintf(&response, "307 %s\r\n", text ? text : "Redirecting"); httpdSetResponse(r, response); httpdAddHeader(r, header); free(response); free(header); safe_asprintf(&message, "Please <a href='%s'>click here</a>.", url); send_http_page(r, text ? text : "Redirection to message", message); _httpd_closeSocket(r); free(message); }
static int collect_query_string(void *cls, enum MHD_ValueKind kind, const char *key, const char * value) { /* what happens when '?=foo' supplied? */ struct collect_query *collect_query = cls; if (key && !value) { collect_query->elements[collect_query->i] = safe_strdup(key); } else if(key && value) { safe_asprintf(&(collect_query->elements[collect_query->i]), "%s=%s", key, value); } collect_query->i++; return MHD_YES; }
void http_nodogsplash_redirect(request *r, const char url[]) { char *header; httpdSetResponse(r, "302 Found"); safe_asprintf(&header, "Location: %s",url); httpdAddHeader(r, header); httpdPrintf(r, "<html><head></head><body><a href='%s'>Click here to continue to<br>%s</a></body></html>",url,url); free(header); }
void http_nodogsplash_redirect(request *r, const char url[]) { char *header; debug(LOG_DEBUG,"Redirect client %s to %s",r->clientAddr,url); httpdSetResponse(r, "302 Found\n"); safe_asprintf(&header, "Location: http://%s",url); httpdAddHeader(r, header); httpdPrintf(r, "<html><head></head><body><a href='%s'>Click here to continue to<br>%s</a></body></html>",url,url); free(header); }
static int read_osinfo_db_directory (guestfs_h *g, const char *directory) { DIR *dir; int r; dir = opendir (directory); if (!dir) { debug (g, "osinfo: %s: %s", directory, strerror (errno)); return 0; /* This is not an error: RHBZ#948324. */ } for (;;) { struct dirent *d; errno = 0; d = readdir (dir); if (!d) break; if (STRSUFFIX (d->d_name, ".xml")) { CLEANUP_FREE char *pathname = NULL; pathname = safe_asprintf (g, "%s/%s", directory, d->d_name); r = read_osinfo_db_xml (g, pathname); if (r == -1) goto error; } } /* Check for failure in readdir. */ if (errno != 0) { perrorf (g, "readdir: %s", directory); goto error; } /* Close the directory handle. */ r = closedir (dir); dir = NULL; if (r == -1) { perrorf (g, "closedir: %s", directory); goto error; } return 1; error: if (dir) closedir (dir); return -1; }
/** * Return the location of firmware needed to boot the appliance. This * is aarch64 only currently, since that's the only architecture where * UEFI is mandatory (and that only for RHEL). * * C<*code> is initialized with the path to the read-only UEFI code * file. C<*vars> is initialized with the path to a copy of the UEFI * vars file (which is cleaned up automatically on exit). * * If C<*code> == C<*vars> == C<NULL> then no UEFI firmware is * available. * * C<*code> and C<*vars> should be freed by the caller. * * If the function returns C<-1> then there was a real error which * should cause appliance building to fail (no UEFI firmware is not an * error). * * See also F<v2v/utils.ml>:find_uefi_firmware */ int guestfs_int_get_uefi (guestfs_h *g, char **code, char **vars, int *flags) { #ifdef __aarch64__ size_t i; for (i = 0; guestfs_int_uefi_aarch64_firmware[i].code != NULL; ++i) { const char *codefile = guestfs_int_uefi_aarch64_firmware[i].code; const char *code_debug_file = guestfs_int_uefi_aarch64_firmware[i].code_debug; const char *varsfile = guestfs_int_uefi_aarch64_firmware[i].vars; if (access (codefile, R_OK) == 0 && access (varsfile, R_OK) == 0) { CLEANUP_CMD_CLOSE struct command *copycmd = guestfs_int_new_command (g); char *varst; int r; /* Make a copy of NVRAM variables file. You can't just map it * into the address space read-only as that triggers a different * path inside UEFI. */ varst = safe_asprintf (g, "%s/vars.fd.%d", g->tmpdir, ++g->unique); guestfs_int_cmd_add_arg (copycmd, "cp"); guestfs_int_cmd_add_arg (copycmd, varsfile); guestfs_int_cmd_add_arg (copycmd, varst); r = guestfs_int_cmd_run (copycmd); if (r == -1 || !WIFEXITED (r) || WEXITSTATUS (r) != 0) { free (varst); return -1; } /* If debugging is enabled and we can find the code file with * debugging enabled, use that instead. */ if (g->verbose && access (code_debug_file, R_OK) == 0) codefile = code_debug_file; /* Caller frees. */ *code = safe_strdup (g, codefile); *vars = varst; *flags = guestfs_int_uefi_aarch64_firmware[i].flags; return 0; } } #endif /* Not found. */ *code = *vars = NULL; *flags = 0; return 0; }
/* Windows Registry HKLM\SYSTEM\MountedDevices uses a blob of data * to store partitions. This blob is described here: * http://www.goodells.net/multiboot/partsigs.shtml * The following function maps this blob to a libguestfs partition * name, if possible. */ static char * map_registry_disk_blob (guestfs_h *g, const void *blob) { CLEANUP_FREE_STRING_LIST char **devices = NULL; CLEANUP_FREE_PARTITION_LIST struct guestfs_partition_list *partitions = NULL; size_t i, j, len; uint64_t part_offset; /* First 4 bytes are the disk ID. Search all devices to find the * disk with this disk ID. */ devices = guestfs_list_devices (g); if (devices == NULL) return NULL; for (i = 0; devices[i] != NULL; ++i) { /* Read the disk ID. */ CLEANUP_FREE char *diskid = guestfs_pread_device (g, devices[i], 4, 0x01b8, &len); if (diskid == NULL) continue; if (len < 4) continue; if (memcmp (diskid, blob, 4) == 0) /* found it */ goto found_disk; } return NULL; found_disk: /* Next 8 bytes are the offset of the partition in bytes(!) given as * a 64 bit little endian number. Luckily it's easy to get the * partition byte offset from guestfs_part_list. */ memcpy (&part_offset, (char *) blob + 4, sizeof (part_offset)); part_offset = le64toh (part_offset); partitions = guestfs_part_list (g, devices[i]); if (partitions == NULL) return NULL; for (j = 0; j < partitions->len; ++j) { if (partitions->val[j].part_start == part_offset) /* found it */ goto found_partition; } return NULL; found_partition: /* Construct the full device name. */ return safe_asprintf (g, "%s%d", devices[i], partitions->val[j].part_num); }
static int run_qemu_img_info (guestfs_h *g, const char *filename, cmd_stdout_callback fn, void *data) { char *abs_filename = NULL; char *safe_filename = NULL; struct command *cmd; int r; if (guestfs___lazy_make_tmpdir (g) == -1) return -1; safe_filename = safe_asprintf (g, "%s/format.%d", g->tmpdir, ++g->unique); /* 'filename' must be an absolute path so we can link to it. */ abs_filename = realpath (filename, NULL); if (abs_filename == NULL) { perrorf (g, "realpath"); goto error; } if (symlink (abs_filename, safe_filename) == -1) { perrorf (g, "symlink"); goto error; } cmd = guestfs___new_command (g); guestfs___cmd_add_arg (cmd, "qemu-img"); guestfs___cmd_add_arg (cmd, "info"); guestfs___cmd_add_arg (cmd, safe_filename); guestfs___cmd_set_stdout_callback (cmd, fn, data, 0); r = guestfs___cmd_run (cmd); guestfs___cmd_close (cmd); if (r == -1) goto error; if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) { error (g, _("qemu-img: %s: child process failed"), filename); goto error; } free (safe_filename); free (abs_filename); return 0; error: free (safe_filename); free (abs_filename); return -1; }
/* The g->tmpdir (per-handle temporary directory) is not created when * the handle is created. Instead we create it lazily before the * first time it is used, or during launch. */ int guestfs_int_lazy_make_tmpdir (guestfs_h *g) { if (!g->tmpdir) { CLEANUP_FREE char *tmpdir = guestfs_get_tmpdir (g); g->tmpdir = safe_asprintf (g, "%s/libguestfsXXXXXX", tmpdir); if (mkdtemp (g->tmpdir) == NULL) { perrorf (g, _("%s: cannot create temporary directory"), g->tmpdir); free (g->tmpdir); g->tmpdir = NULL; return -1; } } return 0; }
static char * icon_cirros (guestfs_h *g, struct inspect_fs *fs, size_t *size_r) { char *ret; CLEANUP_FREE char *type = NULL; CLEANUP_FREE char *local = NULL; CLEANUP_FREE char *pngfile = NULL; CLEANUP_CMD_CLOSE struct command *cmd = guestfs___new_command (g); int r; r = guestfs_is_file_opts (g, CIRROS_LOGO, GUESTFS_IS_FILE_OPTS_FOLLOWSYMLINKS, 1, -1); if (r == -1) return NULL; /* a real error */ if (r == 0) return NOT_FOUND; /* Check the file type and geometry. */ type = guestfs_file (g, CIRROS_LOGO); if (!type) return NOT_FOUND; if (!STRPREFIX (type, "ASCII text")) return NOT_FOUND; local = guestfs___download_to_tmp (g, fs, CIRROS_LOGO, "icon", 1024); if (!local) return NOT_FOUND; /* Use pbmtext to render it. */ pngfile = safe_asprintf (g, "%s/cirros.png", g->tmpdir); guestfs___cmd_add_string_unquoted (cmd, PBMTEXT " < "); guestfs___cmd_add_string_quoted (cmd, local); guestfs___cmd_add_string_unquoted (cmd, " | " PNMTOPNG " > "); guestfs___cmd_add_string_quoted (cmd, pngfile); r = guestfs___cmd_run (cmd); if (r == -1) return NOT_FOUND; if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) return NOT_FOUND; /* Read it into memory. */ if (read_whole_file (g, pngfile, &ret, size_r) == -1) return NULL; return ret; }
/* Malloc's */ char * format_time(unsigned long int secs) { unsigned int days, hours, minutes, seconds; char * str; days = secs / (24 * 60 * 60); secs -= days * (24 * 60 * 60); hours = secs / (60 * 60); secs -= hours * (60 * 60); minutes = secs / 60; secs -= minutes * 60; seconds = secs; safe_asprintf(&str,"%ud %uh %um %us", days, hours, minutes, seconds); return str; }
static char * create_cow_overlay_direct (guestfs_h *g, void *datav, struct drive *drv) { char *overlay = NULL; CLEANUP_FREE char *backing_drive = NULL; CLEANUP_CMD_CLOSE struct command *cmd = guestfs___new_command (g); int r; backing_drive = guestfs___drive_source_qemu_param (g, &drv->src); if (!backing_drive) goto error; if (guestfs___lazy_make_tmpdir (g) == -1) goto error; overlay = safe_asprintf (g, "%s/overlay%d", g->tmpdir, ++g->unique); guestfs___cmd_add_arg (cmd, "qemu-img"); guestfs___cmd_add_arg (cmd, "create"); guestfs___cmd_add_arg (cmd, "-f"); guestfs___cmd_add_arg (cmd, "qcow2"); guestfs___cmd_add_arg (cmd, "-b"); guestfs___cmd_add_arg (cmd, backing_drive); if (drv->src.format) { guestfs___cmd_add_arg (cmd, "-o"); guestfs___cmd_add_arg_format (cmd, "backing_fmt=%s", drv->src.format); } guestfs___cmd_add_arg (cmd, overlay); r = guestfs___cmd_run (cmd); if (r == -1) goto error; if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) { guestfs___external_command_failed (g, r, "qemu-img create", backing_drive); goto error; } /* Caller sets g->overlay in the handle to this, and then manages * the memory. */ return overlay; error: free (overlay); return NULL; }
/** * Create the supermin appliance directory under cachedir, if it does * not exist. * * Sanity-check that the permissions on the cachedir are safe, in case * it has been pre-created maliciously or tampered with. * * Returns the directory name which the caller must free. */ char * guestfs_int_lazy_make_supermin_appliance_dir (guestfs_h *g) { CLEANUP_FREE char *tmpdir = guestfs_get_cachedir (g); char *ret = NULL; struct stat statbuf; uid_t uid = geteuid (); ret = safe_asprintf (g, "%s/.guestfs-%ju", tmpdir, (uintmax_t) uid); ignore_value (mkdir (ret, 0755)); ignore_value (chmod (ret, 0755)); /* RHBZ#921292 */ /* See if the cache directory exists and passes some simple checks * to make sure it has not been tampered with. */ if (lstat (ret, &statbuf) == -1) { perrorf (g, _("stat: %s"), ret); free (ret); return NULL; } if (statbuf.st_uid != uid) { error (g, _("security: cached appliance %s is not owned by UID %ju"), ret, (uintmax_t) uid); free (ret); return NULL; } if (!S_ISDIR (statbuf.st_mode)) { error (g, _("security: cached appliance %s is not a directory (mode %o)"), ret, statbuf.st_mode); free (ret); return NULL; } if ((statbuf.st_mode & 0022) != 0) { error (g, _("security: cached appliance %s is writable by group or other (mode %o)"), ret, statbuf.st_mode); free (ret); return NULL; } /* "Touch" the directory. */ ignore_value (utimes (ret, NULL)); return ret; }
/** * @brief redirect_to_splashpage * @param connection * @param client * @param host * @param url * @return */ static int redirect_to_splashpage(struct MHD_Connection *connection, t_client *client, const char *host, const char *url) { char *originurl = NULL; char *query = NULL; int ret = 0; get_query(connection, &query); if (!query) { /* no mem */ return send_error(connection, 503); } safe_asprintf(&originurl, "http://%s%s%s%s", host, url, strlen(query) ? "?" : "" , query); ret = encode_and_redirect_to_splashpage(connection, originurl); free(originurl); free(query); return ret; }
static int check_windows_arch (guestfs_h *g, struct inspect_fs *fs) { CLEANUP_FREE char *cmd_exe = safe_asprintf (g, "%s/system32/cmd.exe", fs->windows_systemroot); /* Should exist because of previous check above in get_windows_systemroot. */ CLEANUP_FREE char *cmd_exe_path = guestfs_case_sensitive_path (g, cmd_exe); if (!cmd_exe_path) return -1; char *arch = guestfs_file_architecture (g, cmd_exe_path); if (!arch) return -1; fs->arch = arch; /* freed by guestfs_int_free_inspect_info */ return 0; }
char * print_latency(mtime_t latency) { #define ONE_MSEC (1000ULL) #define ONE_SEC (1000 * ONE_MSEC) #define ONE_MIN ( 60 * ONE_SEC) #define ONE_HOUR ( 60 * ONE_MIN) #define ONE_DAY ( 24 * ONE_HOUR) #define ONE_YEAR ( 365 * ONE_DAY) if (latency > ONE_YEAR) { uint64 year = latency / ONE_YEAR; uint64 day = (latency % ONE_YEAR) / ONE_DAY; return safe_asprintf("%llu year%s %llu day%s", year, year > 1 ? "s" : "", day, day > 1 ? "s" : ""); } else if (latency > ONE_DAY) { uint64 day = latency / ONE_DAY; uint64 hour = (latency % ONE_DAY) / ONE_HOUR; return safe_asprintf("%llu day%s %llu hour%s", day, day > 1 ? "s" : "", hour, hour > 1 ? "s" : ""); } else if (latency > ONE_HOUR) { uint64 hour = latency / ONE_HOUR; return safe_asprintf("%llu hour%s %llu min", hour, hour > 1 ? "s" : "", (latency % ONE_HOUR) / ONE_MIN); } else if (latency > ONE_MIN) { return safe_asprintf("%llu min %.1f sec", latency / ONE_MIN, (latency % ONE_MIN) / (ONE_SEC * 1.0)); } else if (latency > ONE_SEC) { return safe_asprintf("%.1f sec", latency / (ONE_SEC * 1.0)); } else if (latency > ONE_MSEC) { return safe_asprintf("%.1f msec", latency / (ONE_MSEC * 1.0)); } else { return safe_asprintf("%llu usec", latency); } #undef ONE_MSEC #undef ONE_SEC #undef ONE_MIN #undef ONE_HOUR #undef ONE_DAY #undef ONE_WEEK }
/** @internal * @brief Insert $ID$ with the gateway's id in a string. * * This function can replace the input string with a new one. It assumes * the input string is dynamically allocted and can be free()ed safely. * * This function must be called with the CONFIG_LOCK held. */ static void iptables_insert_gateway_id(char **input) { char *token; const s_config *config; char *buffer; if (strstr(*input, "$ID$") == NULL) return; while ((token = strstr(*input, "$ID$")) != NULL) /* This string may look odd but it's standard POSIX and ISO C */ memcpy(token, "%1$s", 4); config = config_get_config(); safe_asprintf(&buffer, *input, config->gw_interface); free(*input); *input = buffer; }
// add by lijg, 2013-05-30, Find keyword @mention in firewall rule table = @table and chain = @chain // return : 0 - not found, 1 - found it . int iptables_fw_find_mention( const char * table, const char * chain, const char * mention ) { FILE *p = NULL; char *command = NULL; char line[MAX_BUF]; char *victim = safe_strdup(mention); int found = 0; // 1.1 @victim="WiFiDog_br-lan_Trusted" iptables_insert_gateway_id(&victim); debug(LOG_DEBUG, "Attempting to find all mention of %s from %s.%s", victim, table, chain); safe_asprintf(&command, "iptables -t %s -L %s -n --line-numbers -v", table, chain); iptables_insert_gateway_id(&command); //1.2 执行命令 iptables -t mangle -L PREROUTING -n --line-numbers -v, 逐行输出链中规则 if ((p = popen(command, "r"))) { /* Skip first 2 lines */ while (!feof(p) && fgetc(p) != '\n'); while (!feof(p) && fgetc(p) != '\n'); /* Loop over entries */ while (fgets(line, sizeof(line), p)) { // 匹配链中每条规则 /* Look for victim */ if (strstr(line, victim)) { // 根据客户端IP地址进行匹配 found = 1; break; } } pclose(p); } free(command); free(victim); return (found); }
/** @brief Convenience function to redirect the web browser to the auth server * @param r The request * @param urlFragment The end of the auth server URL to redirect to (the part after path) * @param text The text to include in the redirect header ant the mnual redirect title */ void http_send_redirect_to_auth(request * r, const char *urlFragment, const char *text) { char *protocol = NULL; int port = 80; t_auth_serv *auth_server = get_auth_server(); if (auth_server->authserv_use_ssl) { protocol = "https"; port = auth_server->authserv_ssl_port; } else { protocol = "http"; port = auth_server->authserv_http_port; } char *url = NULL; safe_asprintf(&url, "%s://%s:%d%s%s", protocol, auth_server->authserv_hostname, port, auth_server->authserv_path, urlFragment); http_send_redirect(r, url, text); free(url); }
void http_callback_status(httpd * webserver, request * r) { const s_config *config = config_get_config(); char *status = NULL; char *buf; if (config->httpdusername && (strcmp(config->httpdusername, r->request.authUser) || strcmp(config->httpdpassword, r->request.authPassword))) { debug(LOG_INFO, "Status page requested, forcing authentication"); httpdForceAuthenticate(r, config->httpdrealm); return; } status = get_status_text(); safe_asprintf(&buf, "<pre>%s</pre>", status); send_http_page(r, "WiFiDog Status", buf); free(buf); free(status); }
//>>> liudf added 20160104 static char * _get_full_url(const char *redir_url) { char *protocol = NULL; int port = 80; t_auth_serv *auth_server = get_auth_server(); if (auth_server->authserv_use_ssl) { protocol = "https"; port = auth_server->authserv_ssl_port; } else { protocol = "http"; port = auth_server->authserv_http_port; } char *url = NULL; safe_asprintf(&url, "%s://%s:%d%s%s", protocol, auth_server->authserv_hostname, port, auth_server->authserv_path, redir_url); return url; }
/** * @brief encode originurl and redirect the client to the splash page * @param connection * @param client * @param originurl * @return */ static int encode_and_redirect_to_splashpage(struct MHD_Connection *connection, const char *originurl) { char *splashpageurl = NULL; char encoded[2048]; int ret; s_config *config = config_get_config(); memset(encoded, 0, sizeof(encoded)); if (uh_urlencode(encoded, 2048, originurl, strlen(originurl)) == -1) { debug(LOG_WARNING, "could not encode url"); } safe_asprintf(&splashpageurl, "http://%s:%u%s?redir=%s", config->gw_address , config->gw_port, "/splash.html", encoded); debug(LOG_DEBUG, "originurl: %s", originurl); debug(LOG_DEBUG, "splashpageurl: %s", splashpageurl); ret = send_redirect_temp(connection, splashpageurl); free(splashpageurl); return ret; }
int send_redirect_temp(struct MHD_Connection *connection, const char *url) { struct MHD_Response *response; int ret; char *redirect; const char *redirect_body = "<html><head></head><body><a href='%s'>Click here to continue to<br>%s</a></body></html>"; safe_asprintf(&redirect, redirect_body, url, url); response = MHD_create_response_from_buffer(strlen(redirect), redirect, MHD_RESPMEM_MUST_FREE); if (!response) return send_error(connection, 503); // MHD_set_response_options(response, MHD_RF_HTTP_VERSION_1_0_ONLY, MHD_RO_END); MHD_add_response_header(response, "Location", url); MHD_add_response_header(response, "Connection", "close"); ret = MHD_queue_response(connection, MHD_HTTP_TEMPORARY_REDIRECT, response); MHD_destroy_response(response); return ret; }
/** Respond to attempted access from a preauthenticated client. * Add the client to the client list and serves the splash page. */ void http_nodogsplash_first_contact(request *r) { debug(LOG_DEBUG,"[%s]first contact",r->clientAddr); t_client *client; t_auth_target *authtarget; s_config *config; char *redir, *origurl; /* only allow GET requests */ if (r->request.method != HTTP_GET) { http_nodogsplash_405(r); return; } config = config_get_config(); client = http_nodogsplash_add_client(r); /* http_nodogsplash_add_client() should log and return null on error */ if(!client) return; /* We just assume protocol http; after all we caught the client by redirecting port 80 tcp packets */ safe_asprintf(&origurl,"%s%s%s%s", r->request.host,r->request.path, r->request.query[0]?"?":"",r->request.query); /* Create redirect URL for this contact as appropriate */ redir = http_nodogsplash_make_redir(origurl); debug(LOG_DEBUG,"[%s] redirected to:%s",r->clientAddr,redir); /* Create authtarget with all needed info */ authtarget = http_nodogsplash_make_authtarget(client->token,redir); free(origurl); http_nodogsplash_serve_splash(r,authtarget,client,NULL); http_nodogsplash_free_authtarget(authtarget); }
int guestfs_int_get_uefi (guestfs_h *g, char **code, char **vars) { size_t i; for (i = 0; uefi_firmware[i] != NULL; i += 2) { const char *codefile = uefi_firmware[i]; const char *varsfile = uefi_firmware[i+1]; if (access (codefile, R_OK) == 0 && access (varsfile, R_OK) == 0) { CLEANUP_CMD_CLOSE struct command *copycmd = guestfs_int_new_command (g); char *varst; int r; /* Make a copy of NVRAM variables file. You can't just map it * into the address space read-only as that triggers a different * path inside UEFI. */ varst = safe_asprintf (g, "%s/vars.fd.%d", g->tmpdir, ++g->unique); guestfs_int_cmd_add_arg (copycmd, "cp"); guestfs_int_cmd_add_arg (copycmd, varsfile); guestfs_int_cmd_add_arg (copycmd, varst); r = guestfs_int_cmd_run (copycmd); if (r == -1 || !WIFEXITED (r) || WEXITSTATUS (r) != 0) { free (varst); return -1; } /* Caller frees. */ *code = safe_strdup (g, codefile); *vars = varst; return 0; } } /* Not found. */ *code = *vars = NULL; return 0; }
void http_send_redirect_to_noauth(char *host, request *r, char *text) //add by liuhj { char *protocol = NULL; int port = 80; t_auth_serv *auth_server = get_auth_server(); if (auth_server == NULL) { protocol = "http"; } else{ if (auth_server->authserv_use_ssl) { protocol = "https"; port = auth_server->authserv_ssl_port; } else { protocol = "http"; port = auth_server->authserv_http_port; } } char *url = NULL; safe_asprintf(&url, "%s://%s", protocol, host); http_send_redirect(r, url, text); free(url); }