aboutsummaryrefslogtreecommitdiff
path: root/src/x2go-auth-check.c
blob: 7370dec662b58bc898a35c2875f09aa3cceb2cf7 (plain)
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
/*
 * Copyright © 2012 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
 */

#include <libssh/libssh.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>


int
main (int argc, char * argv[])
{
	int verbosity = SSH_LOG_NOLOG;

	char password[512];
	if (argc != 4) {
		printf("Not enough params");
		return -1;
	}

	if (scanf("%511s", password) != 1) {
		return -1;
	}

	if (mlock(password, sizeof(password)) != 0) {
		return -1;
	}

	ssh_session auth_check_ssh_session = ssh_new();

	ssh_options_set ( auth_check_ssh_session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity );
	ssh_options_set ( auth_check_ssh_session, SSH_OPTIONS_USER, argv[2] );

	char * colonloc = strstr(argv[1], ":");
	if (colonloc != NULL) {
		/* We've got a port to deal with */
		colonloc[0] = '\0';
		colonloc++;
		long port = strtoul(colonloc, NULL, 10);

		char *array[2];
		array[0] = strtok( argv[1], ":" );
		char *hostname = array[0];

		ssh_options_set ( auth_check_ssh_session, SSH_OPTIONS_HOST, hostname );

		ssh_options_set ( auth_check_ssh_session, SSH_OPTIONS_PORT, &port );
	} else {
		ssh_options_set ( auth_check_ssh_session, SSH_OPTIONS_HOST, argv[1] );
	}

	int rc;
	rc = ssh_connect ( auth_check_ssh_session );
	if ( rc != SSH_OK ) {
		fprintf ( stderr, "Error connecting to via SSH: %s\n", ssh_get_error ( auth_check_ssh_session ) );
		ssh_free(auth_check_ssh_session);
		return -1;
	}

	rc = ssh_userauth_password ( auth_check_ssh_session, NULL, password );
	if ( rc != SSH_AUTH_SUCCESS ) {
		fprintf ( stderr, "Error connecting to via SSH: %s\n", ssh_get_error ( auth_check_ssh_session ) );
		ssh_disconnect(auth_check_ssh_session);
		ssh_free(auth_check_ssh_session);
		return -1;
	}

	memset(password, 0, sizeof(password));
	munlock(password, sizeof(password));

	return 0;
}