File: GetPassword.cpp

package info (click to toggle)
gcvs 1.0final-12
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,248 kB
  • ctags: 10,631
  • sloc: ansic: 71,709; cpp: 39,780; sh: 18,434; makefile: 1,915; yacc: 1,299; tcl: 1,283; perl: 910; lex: 249; csh: 185; lisp: 7
file content (377 lines) | stat: -rwxr-xr-x 8,088 bytes parent folder | download | duplicates (3)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.

** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY 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, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/*
 * Author : Alexandre Parenteau <aubonbeurre@hotmail.com> --- December 1997
 */

// GetPassword.cpp : implementation file
//

#include "stdafx.h"

#ifdef WIN32
#	include "wincvs.h"
#endif /* WIN32 */

#ifdef qMacCvsPP
#	include <UModalDialogs.h>
#	include <LEditText.h>
#	include <LStaticText.h>
#	if TARGET_RT_MAC_CFM
#		include <Keychain.h>
#	endif

#	include "MacCvsApp.h"
#	include "MacCvsConstant.h"
#	include "TextBinary.h"
#endif /* qMacCvsPP */

#if qUnix
#	include "UCvsDialogs.h"
#endif

#include "GetPassword.h"
#include "CPStr.h"

#ifdef WIN32
#	ifdef _DEBUG
#	define new DEBUG_NEW
#	undef THIS_FILE
	static char THIS_FILE[] = __FILE__;
#	endif
#endif /* WIN32 */

#ifdef WIN32
/////////////////////////////////////////////////////////////////////////////
// GetPassword dialog


GetPassword::GetPassword(CWnd* pParent /*=NULL*/)
	: CDialog(GetPassword::IDD, pParent)
{
	//{{AFX_DATA_INIT(GetPassword)
	m_passwd = _T("");
	//}}AFX_DATA_INIT
}


void GetPassword::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(GetPassword)
	DDX_Text(pDX, IDC_PASSWD, m_passwd);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(GetPassword, CDialog)
	//{{AFX_MSG_MAP(GetPassword)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// GetPassword message handlers

BOOL GetPassword::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// Extra initialization here
	
	//force the window to the foreground in case it was covered by terminal or DOS box
	if( !IsWindowVisible() )
	{
		SetForegroundWindow();
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

#endif /* WIN32 */

#if qUnix
class UCvsGetPasswd : public UWidget
{
	UDECLARE_DYNAMIC(UCvsGetPasswd)
public:
	UCvsGetPasswd() : UWidget(::UEventGetWidID()) {}
	virtual ~UCvsGetPasswd() {}

	enum
	{
		kOK = EV_COMMAND_START,	// 0
		kCancel,				// 1
		kPasswd					// 2
	};

	virtual void DoDataExchange(bool fill);

	UStr m_passwd;

protected:
	ev_msg int OnOK(void);
	ev_msg int OnCancel(void);

	UDECLARE_MESSAGE_MAP()
};

UIMPLEMENT_DYNAMIC(UCvsGetPasswd, UWidget)

UBEGIN_MESSAGE_MAP(UCvsGetPasswd, UWidget)
	ON_UCOMMAND(UCvsGetPasswd::kOK, UCvsGetPasswd::OnOK)
	ON_UCOMMAND(UCvsGetPasswd::kCancel, UCvsGetPasswd::OnCancel)
UEND_MESSAGE_MAP()

int UCvsGetPasswd::OnOK(void)
{
	EndModal(true);
	return 0;
}

int UCvsGetPasswd::OnCancel(void)
{
	EndModal(false);
	return 0;
}

void UCvsGetPasswd::DoDataExchange(bool fill)
{
	if(fill)
	{
	}
	else
	{
		UEventSendMessage(GetWidID(), EV_GETTEXT, kPasswd, &m_passwd);
	}
}
#endif // qUnix

char *CompatGetPassword(const char *prompt)
{
#ifdef WIN32
	static char passwd[255];
	GetPassword gp;
	if(gp.DoModal() == IDOK)
	{
		strcpy(passwd, gp.m_passwd);
		return passwd;
	}
	return "";
#endif /* WIN32 */
//{ Mac-Part
#ifdef qMacCvsPP
	if(!CMacCvsApp::app->IsFrontProcess() && CMacCvsApp::app->NotifyUser(0L))
	{
		while(!CMacCvsApp::app->IsFrontProcess())
		{
			CMacCvsApp::app->SpendSomeTime();
		}
	}

	static UStr thepasswd;
	KCItemRef theItem				= NULL;
	
	LStr255 pstr;
	UStr realPrompt;
#if !qCarbon || UNIVERSAL_INTERFACES_VERSION >= 0x0334
	LStr255 phost, puser;
	bool doUseKeyChain = false;
	OSStatus theStatus;
	UStr loopKey;
#endif

	if(GetEnvValue(prompt, "prompt", realPrompt))
	{
		pstr = realPrompt;

#if !qCarbon || UNIVERSAL_INTERFACES_VERSION >= 0x0334
		// copied from MacCvsPro::CMacCVSDoc.cpp
		UStr host, user, authen;
		if(CMacCvsApp::CanUseKeychain() &&
			GetEnvValue(prompt, "host", host) &&
			GetEnvValue(prompt, "user", user))
		{
			doUseKeyChain = true;
			
			OSStatus theStatus;
			UInt32 theLength;
			
			phost = host;
			puser = user;
			
			UStr authen;
			if(GetEnvValue(prompt, "authen", authen))
			{
				phost += ":";
				phost += authen;
			}
			
			UStr dir;
			if(GetEnvValue(prompt, "dir", dir))
			{
				phost += ":";
				phost += dir;
			}
			
			static char ppass[255];
			
			theStatus = KCFindGenericPassword(phost, puser, sizeof(ppass) - 1,
				ppass, &theLength, &theItem);
			
			user = puser;
			host = phost;
			loopKey = user;
			loopKey << '@';
			loopKey << host;
			// we could have an endless loop is the password is not valid
			if (theStatus == noErr && CMacCvsApp::app->CheckLoopingPassword(loopKey))
			{
				if (theItem != NULL)
					KCReleaseItem(&theItem);

				ppass[theLength] = '\0';
				
				return ppass;
			}
		}
#endif
	}
	else
		pstr = prompt;

	StDialogHandler	theHandler(dlg_Passwd, LCommander::GetTopCommander());
	LWindow *theDialog = theHandler.GetDialog();
	ThrowIfNil_(theDialog);

	LEditText *passwd = dynamic_cast<LEditText*>
		(theDialog->FindPaneByID(item_GetPasswd));
	LStaticText *promptbox = dynamic_cast<LStaticText*>
		(theDialog->FindPaneByID('PROM'));
	theDialog->SetLatentSub(passwd);

	promptbox->SetText(pstr);
	
	theDialog->Show();
	MessageT hitMessage;
	while (true)
	{		// Let DialogHandler process events
		hitMessage = theHandler.DoDialog();
		
		if (hitMessage == msg_OK || hitMessage == msg_Cancel)
			break;
	}
	theDialog->Hide();
	
	if(hitMessage == msg_OK)
	{		
		passwd->GetText(pstr);

#if !qCarbon || UNIVERSAL_INTERFACES_VERSION >= 0x0334
		if (doUseKeyChain && CMacCvsApp::CanUseKeychain())
		{			
			if (theItem != NULL)
			{
				theStatus = KCSetData(theItem, pstr[0], &pstr[1]);
				
				if (theStatus == noErr)
					theStatus = KCUpdateItem(theItem);
			}
			else
			{
				theStatus = KCAddGenericPassword(phost, puser,
					pstr[0], &pstr[1], &theItem);
				
				KCAttribute theAttribute;
				
				if (theStatus == noErr)
				{
					UStr theDescriptionText("MacCVS password");
					theAttribute.tag = kDescriptionKCItemAttr;
					theAttribute.length = theDescriptionText.length();
					theAttribute.data = (void *)(const char *)theDescriptionText;
					theStatus = KCSetAttribute(theItem, &theAttribute);
				}
				
				OSType theTypeCreator;
				
				if (theStatus == noErr)
				{
					theTypeCreator = 'mCVS';
					theAttribute.tag = kCreatorKCItemAttr;
					theAttribute.length = sizeof(theTypeCreator);
					theAttribute.data = &theTypeCreator;
					theStatus = KCSetAttribute(theItem, &theAttribute);
				}
				
				if (theStatus == noErr)
				{
					theTypeCreator = 'APPL';
					theAttribute.tag = kTypeKCItemAttr;
					theStatus = KCSetAttribute(theItem, &theAttribute);
				}
				
				if (theStatus == noErr)
				{
					Boolean theCustomIcon = true;
					theAttribute.tag = kCustomIconKCItemAttr;
					theAttribute.length = sizeof(theCustomIcon);
					theAttribute.data = &theCustomIcon;
					theStatus = KCSetAttribute(theItem, &theAttribute);
				}
				
				if (theStatus == noErr)
					theStatus = KCUpdateItem(theItem);
			}

			if (theItem != NULL)
				KCReleaseItem(&theItem);
			
			CMacCvsApp::app->PushValidPassword(loopKey);
		}
#endif

		thepasswd = pstr;
		return thepasswd.length() == 0 ? (char *)"" : (char *)thepasswd;
	}

#if !qCarbon || UNIVERSAL_INTERFACES_VERSION >= 0x0334
	if (theItem != NULL)
		KCReleaseItem(&theItem);
#endif

	return "";
#endif /* qMacCvsPP */
//}
#if qUnix
	static char passwd[255];
	void *wid = UCreate_PasswdDlg();

	UCvsGetPasswd *dlg = new UCvsGetPasswd();
	UEventSendMessage(dlg->GetWidID(), EV_INIT_WIDGET, kUMainWidget, wid);	

	if(dlg->DoModal())
	{
		strcpy(passwd, dlg->m_passwd);
	}
	else
		strcpy(passwd, "");

	delete dlg;
	return passwd;
#endif // qUnix
}