summaryrefslogtreecommitdiffstats
path: root/examples/libsmbclient/get_auth_data_fn.h
blob: 9adb74511762552a31793f7e89d46e22100bde75 (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
89
90
91
92
#include <stdlib.h>

static void
get_auth_data_fn(const char * pServer,
                 const char * pShare,
                 char * pWorkgroup,
                 int maxLenWorkgroup,
                 char * pUsername,
                 int maxLenUsername,
                 char * pPassword,
                 int maxLenPassword)
{
	char            temp[128];
	char            server[256] = { '\0' };
	char            share[256] = { '\0' };
	char            workgroup[256] = { '\0' };
	char            username[256] = { '\0' };
	char            password[256] = { '\0' };
	char           *ret;

	static int krb5_set = 1;

	if (strcmp(server, pServer) == 0 &&
	    strcmp(share, pShare) == 0 &&
	    *workgroup != '\0' &&
	    *username != '\0')
	{
		strncpy(pWorkgroup, workgroup, maxLenWorkgroup - 1);
		strncpy(pUsername, username, maxLenUsername - 1);
		strncpy(pPassword, password, maxLenPassword - 1);
		return;
	}

	if (krb5_set && getenv("KRB5CCNAME")) {
		krb5_set = 0;
		return;
	}

	fprintf(stdout, "Workgroup: [%s] ", pWorkgroup);
	ret = fgets(temp, sizeof(temp), stdin);
	if (ret == NULL) {
		return;
	}

	if (temp[strlen(temp) - 1] == '\n') /* A new line? */
	{
		temp[strlen(temp) - 1] = '\0';
	}

	if (temp[0] != '\0')
	{
		strncpy(pWorkgroup, temp, maxLenWorkgroup - 1);
	}

	fprintf(stdout, "Username: [%s] ", pUsername);
	ret = fgets(temp, sizeof(temp), stdin);
	if (ret == NULL) {
		return;
	}

	if (temp[strlen(temp) - 1] == '\n') /* A new line? */
	{
		temp[strlen(temp) - 1] = '\0';
	}

	if (temp[0] != '\0')
	{
		strncpy(pUsername, temp, maxLenUsername - 1);
	}

	fprintf(stdout, "Password: ");
	ret = fgets(temp, sizeof(temp), stdin);
	if (ret == NULL) {
		return;
	}

	if (temp[strlen(temp) - 1] == '\n') /* A new line? */
	{
		temp[strlen(temp) - 1] = '\0';
	}

	if (temp[0] != '\0')
	{
		strncpy(pPassword, temp, maxLenPassword - 1);
	}

	strncpy(workgroup, pWorkgroup, sizeof(workgroup) - 1);
	strncpy(username, pUsername, sizeof(username) - 1);
	strncpy(password, pPassword, sizeof(password) - 1);

	krb5_set = 1;
}