1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
__int64 panSwalAuthenticate( __int64 alloc_ptr, int is_cms_auth, char *username, _BYTE *password, __int64 protocol, __int64 ip_address, __int64 accessible_vsys, __int64 auth_result, char *error_buf, unsigned int error_buf_size, __int64 client_info, __int64 extra_params, __int64 *response_xml ) { char *auth_request_xml = NULL; char *sanitized_extra = NULL; char *debug_request_xml = NULL; char role_str[32] = {0}; int socket_fd = -1; void *response_data = NULL; int response_len = 0; int result = 1; *error_buf = 0;
if (accessible_vsys) { char *vsys_buf = create_string_buffer(alloc_ptr, strlen(accessible_vsys)+1); if (!vsys_buf) { set_error(error_buf, "Could not allocate buffer for VSYS names"); goto cleanup; } if (append_string(vsys_buf, accessible_vsys)) { set_error(error_buf, "Could not copy VSYS names"); goto cleanup; } }
auth_request_xml = create_string_buffer(alloc_ptr, 1025); debug_request_xml = create_string_buffer(alloc_ptr, 1025); if (!auth_request_xml || !debug_request_xml) { set_error(error_buf, "Could not allocate string buffers"); goto cleanup; }
if (extra_params) { sanitized_extra = xmlURIEscapeStr(extra_params, "\"' ="); }
if (is_cms_auth) { get_role_string(*(int*)(auth_result+64), role_str, sizeof(role_str)); if (accessible_vsys && *accessible_vsys) { append_xml(auth_request_xml, "<auth-request username=\"%s\" cms-client=\"yes\" is-vsys-admin=\"yes\" " "ip-address=\"%s\" protocol=\"%s\" role=\"%s\" %s", username, ip_address, protocol, role_str, sanitized_extra); if (client_info) { append_xml(auth_request_xml, " client=\"%s\"", client_info); } append_xml(auth_request_xml, ">"); append_xml(auth_request_xml, "<vsys>"); char *vsys = strtok(accessible_vsys, ","); while (vsys) { append_xml(auth_request_xml, "<member>%s</member>", vsys); vsys = strtok(NULL, ","); } append_xml(auth_request_xml, "</vsys></auth-request>"); } else { append_xml(auth_request_xml, "<auth-request username=\"%s\" cms-client=\"yes\" " "ip-address=\"%s\" protocol=\"%s\" role=\"%s\" %s", username, ip_address, protocol, role_str, sanitized_extra); if (client_info) { append_xml(auth_request_xml, " client=\"%s\"", client_info); } append_xml(auth_request_xml, "></auth-request>"); } } else if (password && *password) { append_xml(auth_request_xml, "<auth-request username=\"%s\" passwd=\"%s\" " "ip-address=\"%s\" protocol=\"%s\" %s", username, password, ip_address, protocol, sanitized_extra); append_xml(debug_request_xml, "<auth-request username=\"%s\" passwd=\"****\" " "ip-address=\"%s\" protocol=\"%s\" %s", username, ip_address, protocol, sanitized_extra); if (client_info) { append_xml(auth_request_xml, " client=\"%s\"", client_info); append_xml(debug_request_xml, " client=\"%s\"", client_info); } append_xml(auth_request_xml, "></auth-request>"); append_xml(debug_request_xml, "></auth-request>"); } else { append_xml(auth_request_xml, "<auth-request username=\"%s\" " "ip-address=\"%s\" protocol=\"%s\" %s", username, ip_address, protocol, sanitized_extra); append_xml(debug_request_xml, "<auth-request username=\"%s\" " "ip-address=\"%s\" protocol=\"%s\" %s", username, ip_address, protocol, sanitized_extra); if (client_info) { append_xml(auth_request_xml, " client=\"%s\"", client_info); append_xml(debug_request_xml, " client=\"%s\"", client_info); } append_xml(auth_request_xml, "></auth-request>"); append_xml(debug_request_xml, "></auth-request>"); }
if (log_level >= 800) { log_debug("Auth request: %s", debug_request_xml); }
socket_fd = connect_to_auth_service(0x7F000001, 10000); if (socket_fd < 0) { set_connection_error(error_buf); goto cleanup; }
response_data = send_and_receive(socket_fd, auth_request_xml, strlen(auth_request_xml), &response_len); if (!response_data) { goto close_socket; }
if (response_xml) { *response_xml = create_string_buffer(alloc_ptr, response_len+1); append_string(*response_xml, response_data); }
if (log_level >= 800) { log_debug("Auth response: %s", response_data); }
xmlDocPtr doc = xmlReadMemory(response_data, response_len, "noname.xml", NULL, 0); if (!doc) { set_error(error_buf, "Error parsing XML response"); goto close_socket; }
xmlNodePtr root = xmlDocGetRootElement(doc); if (!root) { set_error(error_buf, "Could not get root element"); goto free_doc; }
parse_auth_response(alloc_ptr, root, auth_result, error_buf, error_buf_size);
if (strcmp((char*)(auth_result+132), "success") == 0) { strncpy(error_buf, (char*)(auth_result+420), error_buf_size); result = 0; }
free_doc: xmlFreeDoc(doc); close_socket: close_socket(socket_fd); cleanup: if (sanitized_extra) xmlFree(sanitized_extra); if (auth_request_xml) free_string_buffer(auth_request_xml); if (debug_request_xml) free_string_buffer(debug_request_xml); if (response_data) free(response_data); if (vsys_buf) free_string_buffer(vsys_buf);
return result; }
|