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 "argv.h"
21 #include "client.h"
22 #include "common/clipboard.h"
23 #include "kubernetes.h"
24 #include "settings.h"
25 #include "user.h"
26
27 #include <guacamole/argv.h>
28 #include <guacamole/client.h>
29 #include <libwebsockets.h>
30
31 #include <langinfo.h>
32 #include <locale.h>
33 #include <pthread.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 guac_client* guac_kubernetes_lws_current_client = NULL;
38
39 /**
40 * Logging callback invoked by libwebsockets to log a single line of logging
41 * output. As libwebsockets messages are all generally low-level, the log
42 * level provided by libwebsockets is ignored here, with all messages logged
43 * instead at guacd's debug level.
44 *
45 * @param level
46 * The libwebsockets log level associated with the log message. This value
47 * is ignored by this implementation of the logging callback.
48 *
49 * @param line
50 * The line of logging output to log.
51 */
guac_kubernetes_log(int level,const char * line)52 static void guac_kubernetes_log(int level, const char* line) {
53
54 char buffer[1024];
55
56 /* Drop log message if there's nowhere to log yet */
57 if (guac_kubernetes_lws_current_client == NULL)
58 return;
59
60 /* Trim length of line to fit buffer (plus null terminator) */
61 int length = strlen(line);
62 if (length > sizeof(buffer) - 1)
63 length = sizeof(buffer) - 1;
64
65 /* Copy as much of the received line as will fit in the buffer */
66 memcpy(buffer, line, length);
67
68 /* If the line ends with a newline character, trim the character */
69 if (length > 0 && buffer[length - 1] == '\n')
70 length--;
71
72 /* Null-terminate the trimmed string */
73 buffer[length] = '\0';
74
75 /* Log using guacd's own log facilities */
76 guac_client_log(guac_kubernetes_lws_current_client, GUAC_LOG_DEBUG,
77 "libwebsockets: %s", buffer);
78
79 }
80
guac_client_init(guac_client * client)81 int guac_client_init(guac_client* client) {
82
83 /* Ensure reference to main guac_client remains available in all
84 * libwebsockets contexts */
85 guac_kubernetes_lws_current_client = client;
86
87 /* Redirect libwebsockets logging */
88 lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE | LLL_INFO,
89 guac_kubernetes_log);
90
91 /* Set client args */
92 client->args = GUAC_KUBERNETES_CLIENT_ARGS;
93
94 /* Allocate client instance data */
95 guac_kubernetes_client* kubernetes_client = calloc(1, sizeof(guac_kubernetes_client));
96 client->data = kubernetes_client;
97
98 /* Init clipboard */
99 kubernetes_client->clipboard = guac_common_clipboard_alloc(GUAC_KUBERNETES_CLIPBOARD_MAX_LENGTH);
100
101 /* Set handlers */
102 client->join_handler = guac_kubernetes_user_join_handler;
103 client->free_handler = guac_kubernetes_client_free_handler;
104
105 /* Register handlers for argument values that may be sent after the handshake */
106 guac_argv_register(GUAC_KUBERNETES_ARGV_COLOR_SCHEME, guac_kubernetes_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
107 guac_argv_register(GUAC_KUBERNETES_ARGV_FONT_NAME, guac_kubernetes_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
108 guac_argv_register(GUAC_KUBERNETES_ARGV_FONT_SIZE, guac_kubernetes_argv_callback, NULL, GUAC_ARGV_OPTION_ECHO);
109
110 /* Set locale and warn if not UTF-8 */
111 setlocale(LC_CTYPE, "");
112 if (strcmp(nl_langinfo(CODESET), "UTF-8") != 0) {
113 guac_client_log(client, GUAC_LOG_INFO,
114 "Current locale does not use UTF-8. Some characters may "
115 "not render correctly.");
116 }
117
118 /* Success */
119 return 0;
120
121 }
122
guac_kubernetes_client_free_handler(guac_client * client)123 int guac_kubernetes_client_free_handler(guac_client* client) {
124
125 guac_kubernetes_client* kubernetes_client =
126 (guac_kubernetes_client*) client->data;
127
128 /* Wait client thread to terminate */
129 pthread_join(kubernetes_client->client_thread, NULL);
130
131 /* Free settings */
132 if (kubernetes_client->settings != NULL)
133 guac_kubernetes_settings_free(kubernetes_client->settings);
134
135 guac_common_clipboard_free(kubernetes_client->clipboard);
136 free(kubernetes_client);
137 return 0;
138
139 }
140
141