Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char *
- external_auth_format_packet(struct passwd *pw, const Key *key)
- {
- int n;
- u_int len;
- u_char *blob;
- char *uu;
- if (key_is_cert(key)) {
- if (key->cert == NULL) {
- error("%s: no cert data", __func__);
- return NULL;
- }
- if (buffer_len(&key->cert->certblob) == 0) {
- error("%s: no signed certificate blob", __func__);
- return NULL;
- }
- }
- switch (key->type) {
- case KEY_RSA1:
- return NULL;
- break;
- case KEY_DSA:
- case KEY_DSA_CERT_V00:
- case KEY_DSA_CERT:
- if (key->dsa == NULL)
- return NULL;
- break;
- case KEY_RSA:
- case KEY_RSA_CERT_V00:
- case KEY_RSA_CERT:
- if (key->rsa == NULL)
- return NULL;
- break;
- default:
- return NULL;
- }
- key_to_blob(key, &blob, &len);
- uu = xmalloc(2*len);
- n = uuencode(blob, len, uu, 2*len);
- if (n > 0) {
- char *return_string = NULL;
- json_t *root = json_object();
- json_object_set_new( root, "user", json_string( pw->pw_name ) );
- json_object_set_new( root, "type", json_string( key_ssh_name(key) ) );
- json_object_set_new( root, "key", json_string( uu ) );
- return_string = json_dumps( root, 0 );
- json_decref( root );
- // /* creates buffer and serializer instance. */
- // msgpack_sbuffer* buffer = msgpack_sbuffer_new();
- // msgpack_packer* pk = msgpack_packer_new(buffer, msgpack_sbuffer_write);
- //
- // /* serializes ["user", "key type", "key"]. */
- // u_int pw_name_len = strlen(pw->pw_name);
- // u_int key_type_len = strlen(key_ssh_name(key));
- // u_int uu_len = strlen(uu);
- // msgpack_pack_array(pk, 3);
- // msgpack_pack_raw(pk, pw_name_len);
- // msgpack_pack_raw_body(pk, pw->pw_name, pw_name_len);
- // msgpack_pack_raw(pk, key_type_len);
- // msgpack_pack_raw_body(pk, key_ssh_name(key), key_type_len);
- // msgpack_pack_raw(pk, uu_len);
- // msgpack_pack_raw_body(pk, uu, uu_len);
- //
- // /* cleaning */
- // msgpack_sbuffer_free(buffer);
- // msgpack_packer_free(pk);
- free(blob);
- free(uu);
- return return_string;
- }
- free(blob);
- free(uu);
- return NULL;
- }
- #define BUFFERSIZE 1024
- #define MAXDATASIZE 256
- int external_auth_method(struct passwd *pw, Key *key) {
- int sock;
- int success = 0;
- int received_chars;
- int total_received_chars = 0;
- char tmp_buffer[MAXDATASIZE];
- char *json_packet;
- char* receiver_buffer;
- char *sock_addr_path = "/tmp/auth.sock";
- char *option_parser_file = "external_auth.mode";
- struct sockaddr_un sock_addr;
- size_t receiver_buffer_size = BUFFERSIZE;
- json_error_t json_error;
- json_t *json_root;
- DEBUG_LOG("userauth_external: DEBUG: socket_path: %s", sock_addr_path);
- if ( (sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
- error("userauth_external: Socket open failed");
- return 0;
- }
- memset(&sock_addr, 0, sizeof(sock_addr));
- sock_addr.sun_family = AF_UNIX;
- strncpy(sock_addr.sun_path, sock_addr_path, sizeof(sock_addr.sun_path)-1);
- if (connect(sock, (struct sockaddr*)&sock_addr, sizeof(sock_addr)) == -1) {
- int tmp = errno;
- error("userauth_external: Socket connect error (%s , errno %d\n)", strerror(tmp), tmp);
- return 0;
- }
- /* Formats JSON packet */
- json_packet = external_auth_format_packet(pw, key);
- DEBUG_LOG("userauth_external: DEBUG: json_packet: %s", json_packet);
- /* Writes json_packet to socket*/
- write(sock, json_packet, strlen(json_packet));
- write(sock, "\n", 1);
- DEBUG_LOG("userauth_external: DEBUG: sent json_packet to server");
- /* Receiver buffer memory allocation */
- receiver_buffer = malloc(receiver_buffer_size);
- DEBUG_LOG("userauth_external: DEBUG: receiver_buffer memory allocation");
- /* Checking receiver buffer */
- if (receiver_buffer) {
- while((received_chars = recv(sock, tmp_buffer, MAXDATASIZE, 0)) > 0) {
- DEBUG_LOG("userauth_external: DEBUG: recv has received: %s", tmp_buffer);
- /* Checking if total received chars exceds buffer size */
- if (received_chars + total_received_chars > receiver_buffer_size) {
- receiver_buffer_size += 2;
- DEBUG_LOG("userauth_external: DEBUG: multiplying receiver_buffer_size");
- char* tmp = realloc(receiver_buffer, receiver_buffer_size);
- if (tmp) {
- DEBUG_LOG("userauth_external: DEBUG: copying tmp buffer to new size receiver_buffer");
- receiver_buffer = tmp;
- } else {
- DEBUG_LOG("userauth_external: DEBUG: memory allocation failure");
- free(receiver_buffer);
- receiver_buffer = 0;
- break;
- }
- }
- /* Copying from tmp_buffer to receiver_buffer */
- memcpy(receiver_buffer + total_received_chars, tmp_buffer, received_chars);
- total_received_chars += received_chars;
- DEBUG_LOG("userauth_external: DEBUG: copying from tmp_buffer to receiver_buffer");
- }
- DEBUG_LOG("userauth_external: DEBUG: finished receiving data from socket");
- }
- /* Load json source to json parser */
- json_root = json_loads( receiver_buffer, 0, &json_error );
- if (json_root) {
- DEBUG_LOG("userauth_external: DEBUG: json parsed successfully");
- /* Getting status value */
- json_t *json_status = json_object_get( json_root, "status" );
- if (!json_is_integer(json_status)) {
- error("userauth_external: JSON ERROR: status key doesn't exists");
- return 0;
- }
- int status = (int)json_integer_value(json_status);
- DEBUG_LOG("userauth_external: DEBUG: received status code: %i", status);
- if (status == 1) {
- json_t *json_command = json_object_get( json_root, "command" );
- if (!json_is_string(json_command)) {
- error("userauth_external: JSON ERROR: command key doesn't exists");
- return 0;
- }
- const char *return_comamnd = json_string_value( json_command );
- DEBUG_LOG("userauth_external: DEBUG: received command: %s", return_comamnd);
- if (auth_parse_options(pw, return_comamnd, option_parser_file, 0) == 1) {
- success = 1;
- }
- } else if (success != 0) {
- error("userauth_external: JSON ERROR: wrong status received");
- return 0;
- } else {
- DEBUG_LOG("userauth_external: DEBUG: not authorized key" );
- }
- } else {
- error("userauth_external: ERROR: Unable to load parmaters! error: on line %d: %s\n", json_error.line, json_error.text);
- error("userauth_external: ERROR: response %s", receiver_buffer);
- return 0;
- }
- close(sock);
- return success;
- }
- ### log
- Sep 26 12:35:07 ubuntu sshd[58912]: userauth_external: DEBUG: socket_path: /tmp/auth.sock
- Sep 26 12:35:07 ubuntu sshd[58912]: userauth_external: DEBUG: json_packet: {"user": "root", "key": "AAAAB3NzaC1yc2EAAAADAQABAAABAQDwHDG/TpoUDts9CFrUWh6eoOlPcQYTLIpkF1udDaRZMsdZSZyDsWO5otqrUiQuVLFBXKa4CMiYBJIll2Ye/ZMWjbWd6mkrhE/u4Ha+DS6wyoza3mgn4ekDYxHqzk6/9kVnNtdh+sw3h0OwhkJJG/fAGzGFZhaV5bRS8f9u6pxIKTdmUfozkfe/QhYlFS9gaFnlK2rn8efK1oeNL00YBEcFLVeFdFqU9xuTOrHIQuOHxEynF/zekJ69D1MDhLZDhzq7mfy00wOnS51Q0gxZUdBiFr0ahuD/WrxKgwYunAsp1hD22+WU89vmoPtonWE7a6hTeZtz6Mb0YPfmRwz+CaR1", "type": "ssh-rsa"}
- Sep 26 12:35:07 ubuntu sshd[58912]: userauth_external: DEBUG: sent json_packet to server
- Sep 26 12:35:07 ubuntu sshd[58912]: userauth_external: DEBUG: receiver_buffer memory allocation
- Sep 26 12:35:07 ubuntu sshd[58912]: userauth_external: DEBUG: recv has received: {"status":1,"command":"command='./gitserve' "}
- Sep 26 12:35:07 ubuntu sshd[58912]: userauth_external: DEBUG: copying from tmp_buffer to receiver_buffer
- Sep 26 12:35:07 ubuntu sshd[58912]: userauth_external: DEBUG: finished receiving data from socket
- Sep 26 12:35:07 ubuntu sshd[58912]: error: userauth_external: ERROR: Unable to load parmaters! error: on line 1: end of file expected near 'rUWh'\n
- Sep 26 12:35:07 ubuntu sshd[58912]: error: userauth_external: ERROR: response {"status":1,"command":"command='./gitserve' "}rUWh6eoOlPcQYTLIpkF1udDaRZMsdZSZyDsWO5otqrUiQuVLFBXKa4CMiYBJIll2Ye/ZMWjbWd6mkrhE/u4Ha+DS6wyoza3mgn4ekDYxHqzk6/9kVnNtdh+sw3h0OwhkJJG/fAGzGFZhaV5bRS8f9u6pxIKTdmUfozkfe/QhYlFS9gaFnlK2rn8efK1oeNL00YBEcFLVeFdFqU9xuTOrHIQuOHxEynF/zekJ69D1MDhLZDhzq7mfy00wOnS51Q0gxZUdBiFr0ahuD/WrxKgwYunAsp1hD22+WU89vmoPtonWE7a6hTeZtz6Mb0YPfmRwz+CaR1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement