aboutsummaryrefslogtreecommitdiff
path: root/src/windows/gss/gss.c
blob: 2a9b93dcf6fcf273f7ca4b990774799ca1d3390c (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*+*************************************************************************
**
** GSS test	- Windows scaffolding to test the gssapi dll
**
** Given a hostname it does the equivalent of the Unix command
** ./gss-client [-port <port>] <hostname> host@<hostname> "msg"
**
***************************************************************************/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "gss.h"

#define GSS_CONNECT_NAME            102
#define GSS_OK                      100
#define GSS_CANCEL                  101

#define GSSAPI_INI	"kerberos.ini"				// Which INI file
#define INI_HOSTS	"GSSAPI Hosts"				// INI file section
#define INI_HOST	"Host"						// INI file line label

#define MAX_HOSTS 9
char hosts[MAX_HOSTS][256];
char szHost[256];			// GSSAPI Host to connect to
char szServiceName[256];		// Service to do
char szOID[256];			// OID to use	
int port = 0;				// Which port to use

static void do_gssapi_test (char *name);
static void parse_name (char *name);
static int read_hosts(void);
static void write_hosts (void);
static void	update_hosts (char *name);
static void fill_combo (HWND hDlg);

/*+*************************************************************************
**
** WinMain
**
** Sets up the Dialog that drives our program
**
***************************************************************************/
int PASCAL
WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
HANDLE hInstance, hPrevInstance;
LPSTR lpszCmdLine;
int nCmdShow;
{
	FARPROC lpfnDlgProc;
	WSADATA wsadata;
	WORD versionrequested;
	int rc;

	versionrequested = 0x0101;		/* Version 1.1 */
	rc = WSAStartup(versionrequested, &wsadata);
	if (rc) {
	    MessageBox(NULL, "Couldn't initialize Winsock library", "",
		       MB_OK | MB_ICONSTOP);
	    return FALSE;
	}
	if (versionrequested != wsadata.wVersion) {
	    WSACleanup();
	    MessageBox(NULL, "Winsock version 1.1 not available", "",
		       MB_OK | MB_ICONSTOP);
	    return FALSE;
	}
	
	lpfnDlgProc = MakeProcInstance(OpenGssapiDlg, hInstance);
	DialogBox (hInstance, "OPENGSSAPIDLG", NULL, lpfnDlgProc);
	FreeProcInstance(lpfnDlgProc);

	WSACleanup();
	return 0;
}
/*+*************************************************************************
**
** Do_gssapi_test
**
** Does the actual call to GSS-client
**
***************************************************************************/
void
do_gssapi_test (char *name) {
	int n;										// Return value
	HCURSOR hcursor;							// For the hourglass cursor

	parse_name(name);							// Get host, service and port

	hcursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
	n = gss (szHost, szServiceName, szOID, "Test Gssapi Message", port);
	SetCursor(hcursor);

	if (n)
		MessageBox (NULL, "gss failed", "", IDOK | MB_ICONINFORMATION);
}

/*+*************************************************************************
**
** OpenGssapiDlg(HWND, unsigned, WORD, LONG)
**
** Processes messages for "Open Gssapi Connection" dialog box
**
** Messages:
** 	WM_INITDIALOG - initialize dialog box
** 	WM_COMMAND    - Input received
**
***************************************************************************/
BOOL PASCAL
OpenGssapiDlg(
	HWND hDlg,
	WORD message,
	WORD wParam,
	LONG lParam)
{
	HDC hDC;									// For getting graphic info
	DWORD Ext;									// Size of dialog
	int xExt, yExt;								// Size broken apart
	char hostname[256];							// What the user typed
	switch (message) {
	case WM_INITDIALOG:
		/*
		** First center the dialog
		*/
		hDC = GetDC(hDlg);
		Ext = GetDialogBaseUnits();
		xExt = (190 *LOWORD(Ext)) /4 ;
		yExt = (72 * HIWORD(Ext)) /8 ;
		SetWindowPos(hDlg, NULL,
			(GetSystemMetrics(SM_CXSCREEN)/2)-(xExt/2),
			(GetSystemMetrics(SM_CYSCREEN)/2)-(yExt/2),
			0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
		ReleaseDC(hDlg, hDC);

		read_hosts ();							// Get the host list
		fill_combo (hDlg);						// Put into combo box

		SendMessage(hDlg, WM_SETFOCUS, NULL, NULL);
		return (TRUE);

	case WM_COMMAND:
		switch (wParam) {
		case GSS_CANCEL:						// Only way out of the dialog
		case IDCANCEL:							// From the menu
			EndDialog(hDlg, FALSE);
			break;

		case GSS_OK:
			GetDlgItemText(hDlg, GSS_CONNECT_NAME, hostname, 256);
			SendDlgItemMessage(hDlg, GSS_CONNECT_NAME, CB_SHOWDROPDOWN,
				FALSE, NULL);

			if (! *hostname) {
				MessageBox(hDlg, "You must enter a host name",
					NULL, MB_OK);
				break;
			}
			do_gssapi_test (hostname);			// Test GSSAPI
			update_hosts (hostname);			// Add it to the host list
			fill_combo (hDlg);					// Update the combo box

			//EndDialog(hDlg, TRUE);
			break;
		}
		return FALSE;
	}
	return FALSE;

}
/*+*************************************************************************
**
** Parse_name
**
** Turns NAME which the user entered into host, service and port.
** The host is up to first space, port is after that and service is made
** from host.
**
***************************************************************************/
static void
parse_name (char *name) {
	char *ptr;
	char seps[] = " ,\t";
	char tempname[256];
	
	memset( &tempname[0], '\0', 256 );
	strcpy( tempname, name);
	ptr = strtok( tempname, seps);
	if (ptr != NULL ){
	    strcpy( szHost, ptr );
	} else {
	    wsprintf( szHost, "k5test" );
	}
	if(ptr){
	    ptr = strtok( NULL, seps);
	}
	if( ptr ){
		port = atoi (ptr);
	}
	if( ptr ){
	    ptr = strtok( NULL, seps);
	}
	if( ptr ){
	    strcpy( szServiceName, ptr );
	}else{
	    wsprintf (szServiceName, "sample@%s", szHost); // Make the service name
	}
	if( ptr ){
	    ptr = strtok( NULL, seps);
	}
	if( ptr ){
	    wsprintf (szOID, "{ %s }", ptr); // Put in the OID
	    for (ptr = szOID; *ptr; ptr++)
		    if (*ptr == '.')
			    *ptr = ' ';
    } else {
	   szOID[0] = 0;
	}

}
/*+*************************************************************************
**
** Read_hosts
**
** Reads all the hosts listed in the INI file.
**
***************************************************************************/
static int
read_hosts (void) {
	int i;					/* Index */
	char buff[10];
	
	for (i = 0; MAX_HOSTS; ++i) {		/* Read this many entries */
		wsprintf (buff, INI_HOST "%d", i);
		GetPrivateProfileString(INI_HOSTS, buff, "", hosts[i], 256, GSSAPI_INI);
		if (*hosts[i] == '\0')		/* No more entries??? */
			break;
	}

	return i;
}
/*+*************************************************************************
**
** Write_hosts
**
** Writes the hosts list back to the ini file.
**
***************************************************************************/
static void
write_hosts () {
	int i;										// Index
	char buff[10];

	for (i = 0; i < MAX_HOSTS; ++i) {
		if (*hosts[i] == '\0')					// End of the list?
			break;
		wsprintf (buff, INI_HOST "%d", i);
		WritePrivateProfileString(INI_HOSTS, buff, hosts[i], GSSAPI_INI);
	}
}
/*+*************************************************************************
**
** Update_hosts
**
** Updates the host list with the new NAME the user typed.
**
***************************************************************************/
static void
update_hosts (char *name) {
	int i;										// Index

	for (i = 0; i < MAX_HOSTS-1; ++i) {			// Find it in the list
		if (! _stricmp (name, hosts[i])) 		// A match
			break;
		if (*hosts[i] == '\0')					// End of the list
			break;
	}
	memmove (hosts[1], hosts[0], i * sizeof(hosts[0])); // Move the data down
	strcpy (hosts[0], name);					// Insert this item

	write_hosts ();
}
/*+*************************************************************************
**
** Fill_combo
**
** Fills the combo box with the contents of the host list. Item 0 goes
** into the edit portion and the rest go into the libt box.
**
***************************************************************************/
static void
fill_combo (HWND hDlg) {
	int i;										// Index

	SendDlgItemMessage(hDlg, GSS_CONNECT_NAME, CB_RESETCONTENT, NULL, NULL);

	SetDlgItemText(hDlg, GSS_CONNECT_NAME, hosts[0]);
	SendDlgItemMessage(hDlg, GSS_CONNECT_NAME, CB_SETEDITSEL, NULL, NULL);

	for (i = 1; i < MAX_HOSTS; ++i) {			// Fill in the list box
		if (*hosts[i] == '\0')
			break;
		SendDlgItemMessage(hDlg, GSS_CONNECT_NAME, CB_ADDSTRING, 0,
			(LPARAM) ((LPSTR) hosts[i]));
	}
}