user.c (2525B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20#include "common-ssh/key.h" 21#include "common-ssh/user.h" 22 23#include <guacamole/mem.h> 24#include <guacamole/string.h> 25 26#include <stdlib.h> 27#include <string.h> 28 29guac_common_ssh_user* guac_common_ssh_create_user(const char* username) { 30 31 guac_common_ssh_user* user = guac_mem_alloc(sizeof(guac_common_ssh_user)); 32 33 /* Init user */ 34 user->username = guac_strdup(username); 35 user->password = NULL; 36 user->private_key = NULL; 37 38 return user; 39 40} 41 42void guac_common_ssh_destroy_user(guac_common_ssh_user* user) { 43 44 /* Free private key, if present */ 45 if (user->private_key != NULL) 46 guac_common_ssh_key_free(user->private_key); 47 48 /* Free all other data */ 49 guac_mem_free(user->password); 50 guac_mem_free(user->username); 51 guac_mem_free(user); 52 53} 54 55void guac_common_ssh_user_set_password(guac_common_ssh_user* user, 56 const char* password) { 57 58 /* Replace current password with given value */ 59 guac_mem_free(user->password); 60 user->password = guac_strdup(password); 61 62} 63 64int guac_common_ssh_user_import_key(guac_common_ssh_user* user, 65 char* private_key, char* passphrase) { 66 67 /* Free existing private key, if present */ 68 if (user->private_key != NULL) 69 guac_common_ssh_key_free(user->private_key); 70 71 /* Attempt to read key without passphrase if none given */ 72 if (passphrase == NULL) 73 user->private_key = guac_common_ssh_key_alloc(private_key, 74 strlen(private_key), ""); 75 76 /* Otherwise, use provided passphrase */ 77 else 78 user->private_key = guac_common_ssh_key_alloc(private_key, 79 strlen(private_key), passphrase); 80 81 /* Fail if key could not be read */ 82 return user->private_key == NULL; 83 84} 85