source: ldm/trunk/fuentes/src/plugins/rdesktop/rdesktop.c @ 516

Last change on this file since 516 was 516, checked in by mabarracus, 5 years ago

Copy trusty code

File size: 4.0 KB
Line 
1#include <config.h>
2#include <ctype.h>
3#include <glib.h>
4#include <libintl.h>
5#include <locale.h>
6#include <pthread.h>
7#include <pty.h>
8#include <signal.h>
9#include <stdarg.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <sys/ioctl.h>
14#include <utmp.h>
15
16#include "../../ldmutils.h"
17#include "../../ldmgreetercomm.h"
18#include "../../logging.h"
19#include "../../plugin.h"
20#include "rdesktop.h"
21
22LdmBackend *descriptor;
23RdpInfo *rdpinfo;
24
25void __attribute__((constructor)) initialize() {
26    descriptor = (LdmBackend*) malloc(sizeof(LdmBackend));
27    bzero(descriptor, sizeof(LdmBackend));
28
29    descriptor->name = "rdesktop";
30    descriptor->description = "rdesktop plugin";
31    descriptor->init_cb = init_rdesktop;
32    descriptor->auth_cb = auth_rdesktop;
33    descriptor->start_cb = start_rdesktop;
34    descriptor->clean_cb = close_rdesktop;
35    ldm_init_plugin(descriptor);
36}
37
38/*
39 * init_rdesktop
40 *  Callback function for initialization
41 */
42void init_rdesktop() {
43    rdpinfo = (RdpInfo*) malloc(sizeof(RdpInfo));
44    bzero(rdpinfo, sizeof(RdpInfo));
45
46    /* Get ENV value */
47    rdpinfo->rdpoptions = g_strdup(getenv("RDP_OPTIONS"));
48    rdpinfo->server = g_strdup(getenv("RDP_SERVER"));
49}
50
51/*
52 * start_rdesktop
53 *  Callback function for starting rdesktop session
54 */
55void start_rdesktop() {
56    gboolean error = FALSE;
57
58    /* Variable validation */
59    if(!rdpinfo->username) {
60        log_entry("rdesktop",3,"no username");
61        error = TRUE;
62    }
63
64    if(!rdpinfo->password) {
65        log_entry("rdesktop",3,"no password");
66        error = TRUE;
67    }
68
69    if(!rdpinfo->server) {
70        log_entry("rdesktop",3,"no server");
71        error = TRUE;
72    }
73
74    if(!rdpinfo->domain) {
75        log_entry("rdesktop",3,"no domain");
76        error = TRUE;
77    }
78
79    if(error) {
80        die("rdesktop","missing mandatory information");
81    }
82
83    /* Greeter not needed anymore */
84    close_greeter();
85
86    log_entry("rdesktop",6,"starting rdesktop session to '%s' as '%s'", rdpinfo->server, rdpinfo->username);
87    rdesktop_session();
88    log_entry("rdesktop",6,"closing rdesktop session");
89}
90
91/*
92 * _get_domain
93 */
94void _get_domain() {
95    gchar *cmd = "value domain\n";
96
97    rdpinfo->domain = ask_value_greeter(cmd);
98}
99
100/*
101 * auth_rdesktop
102 *  Callback function for authentication
103 */
104void auth_rdesktop() {
105    gchar *cmd;
106
107    /* Separator for domains : '|' */
108    gchar *domains = getenv("RDP_DOMAIN");
109    cmd = g_strconcat("pref choice;domain;Domain;Select Domai_n ...;session;", domains, "\n", NULL);
110    if (domains) {
111        if(ask_greeter(cmd))
112            die("rdesktop", "%s from greeter failed", cmd);
113    } else {
114        log_entry("rdesktop",7,"RDP_DOMAIN isn't defined, rdesktop will connect on default domain");
115    }
116
117    /* Ask for UserID */
118    get_userid(&(rdpinfo->username));
119
120    /* If user clicks on guest button above, this has changed  */
121    get_passwd(&(rdpinfo->password));
122
123    /* Get hostname */
124    if(!rdpinfo->server)
125        get_host(&(rdpinfo->server));
126
127    /* Get Domain (rdesktop plugin specific) */
128    _get_domain();
129
130    /* Get Language */
131    get_language(&(rdpinfo->lang));
132
133    g_free(cmd);
134}
135
136/*
137 * close_rdesktop
138 *  Callback function for closing the plugins
139 */
140void close_rdesktop() {
141    log_entry("rdesktop",7,"closing rdesktop session");
142    free(rdpinfo);
143}
144
145/*
146 * rdesktop_session
147 *  Start a rdesktop session to server
148 */
149void rdesktop_session() {
150    gchar *cmd;
151
152    cmd = g_strjoin(" ", "rdesktop", "-f",
153            "-u", rdpinfo->username,
154            "-p", rdpinfo->password, NULL);
155
156    /* Only append the domain if it's set */
157    if (g_strcmp0(rdpinfo->domain,"None") != 0) {
158        cmd = g_strjoin(" ", cmd,
159            "-d", rdpinfo->domain, NULL);
160    }
161
162    /* If we have custom options, append them */
163    if (rdpinfo->rdpoptions) {
164        cmd = g_strjoin(" ", cmd, rdpinfo->rdpoptions, NULL);
165    }
166
167    cmd = g_strjoin(" ", cmd, rdpinfo->server, NULL);
168
169    /* Spawning rdesktop session */
170    rdpinfo->rdppid = ldm_spawn(cmd, NULL, NULL, NULL);
171    ldm_wait(rdpinfo->rdppid);
172
173    g_free(cmd);
174}
Note: See TracBrowser for help on using the repository browser.