home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
GRIPS 2: Government Rast…rocessing Software & Data
/
GRIPS_2.cdr
/
dos
/
ncsa_tel
/
contribu
/
byu_tel2.hqx
/
telpass
/
tel1.c
next >
Wrap
C/C++ Source or Header
|
1988-07-07
|
20KB
|
890 lines
/*
* NCSA Telnet source code
* National Center for Supercomputing Applications
* January 31, 1988
* (C) Copyright 1988 Board of Trustees of the University of Illinois
*
* Permission is granted to any individual or institution to use, copy,
* modify, or redistribute this software and its documentation provided
* this notice and the copyright notices are retained. This software
* may not be distributed for profit, either in original form or in
* derivative works. The University of Illinois makes no representations
* about the suitability of this software for any purpose.
* THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY,
* EITHER EXPRESS OR IMPLIED, FOR THE PROGRAM AND/OR DOCUMENTATION PROVIDED,
* INCLUDING, WITHOUT LIMITATION, WARRANTY OF MERCHANTABILITY AND WARRANTY
* OF FITNESS FOR A PARTICULAR PURPOSE.
*/
/*
* File Telpass.c
*
* Tim Krauskopf
* Copyright ⌐ 1988 Board of Trustees of the University of Illinois
*
* Portions Copyright Apple Computer, Inc. 1985, 1986
* All rights reserved.
*
* Edit passwords for NCSA telnet. Passwords stored in the data fork of an independent
* password file.
*
*/
# include <types.h> /* Nearly always required */
# include <quickdraw.h> /* To access the qd globals */
# include <toolutils.h> /* CursHandle and iBeamCursor */
# include <fonts.h> /* Only for InitFonts() trap */
# include <events.h> /* GetNextEvent(), ... */
# include <windows.h> /* GetNewWindow(), ... */
# include <controls.h>
# include <lists.h>
# include <files.h>
# include <fcntl.h>
# include <packages.h>
# include <dialogs.h> /* InitDialogs() and GetNewDialog() */
# include <menus.h> /* EnableItem(), DisableItem() */
# include <desk.h> /* SystemTask(), SystemClick() */
# include <textedit.h> /* TENew() */
# include <scrap.h> /* ZeroScrap() */
# include <segload.h> /* UnloadSeg() */
# include <StdIO.h>
extern _DataInit();
/*
* Resource ID constants.
*/
# define appleID 128 /* This is a resource ID */
# define fileID 129 /* ditto */
# define editID 130 /* ditto */
# define appleMenu 0 /* MyMenus[] array indexes */
# define aboutMeCommand 1
# define fileMenu 1
# define newCommand 1
# define openCommand 2
# define saveCommand 3
# define saveasCommand 4
# define quitCommand 5
# define editMenu 2
# define undoCommand 1
# define cutCommand 3
# define copyCommand 4
# define pasteCommand 5
# define clearCommand 6
# define menuCount 3
/*
* For the one and only text window
*/
# define windowID 128
#define drawinID 129
/*
* For the About Sample... DLOG
*/
# define aboutMeDLOG 128
# define newuDLOG 129
# define newpDLOG 130
# define delDLOG 131
# define okButton 1
/*
* For the controls
*/
#define Cnewuser 129
#define Cdelete 130
#define Cpasswd 131
#define NCS 3
# define SETRECT(rectp, _top, _left, _bottom, _right) \
(rectp)->left = (_left), (rectp)->top = (_top), \
(rectp)->right = (_right), (rectp)->bottom = (_bottom)
/*
* HIWORD and LOWORD macros, for readability.
*/
# define HIWORD(aLong) (((aLong) >> 16) & 0xFFFF)
# define LOWORD(aLong) ((aLong) & 0xFFFF)
/*
* Global Data objects, used by routines external to main().
*/
MenuHandle MyMenus[menuCount]; /* The menu handles */
Boolean DoneFlag; /* Becomes TRUE when File/Quit chosen */
char *lines[500],*malloc(); /* for password storage */
register
WindowPtr myWindow; /* Referenced often */
ControlHandle mycontrol[NCS];
ListHandle mylist=NULL;
SFReply reply;
SFTypeList tlst;
Point wh;
long mycreator = 'NCSp',mytype = 'pass';
/*
* data structure for timk
*/
Point lastPt = {0,0}, /* last point */
csize = { 15,200 };
Rect r1,r2,r3;
Rect rview , rframe,
dbounds ;
AppFile theFile;
char s[256],currentfile[256]={""};
int
main()
{
extern void setupMenus();
extern void doCommand();
Rect screenRect, dragRect, txRect;
Point mousePt;
CursHandle ibeamHdl;
EventRecord myEvent;
WindowRecord wRecord;
WindowPtr theActiveWindow, whichWindow;
int i;
short int shorti,shortj;
ListPtr l; /* temp use */
/*
* Initialization traps
*/
UnloadSeg(_DataInit);
InitGraf(&qd.thePort);
InitFonts();
FlushEvents(everyEvent, 0);
InitWindows();
InitMenus();
TEInit();
InitDialogs(nil);
InitCursor();
/*
* setupMenus is execute-once code, so we can unload it now.
*/
setupMenus(); /* Local procedure, below */
UnloadSeg(setupMenus);
/*
* Calculate the drag rectangle in advance.
* This will be used when dragging a window frame around.
* It constrains the area to within 4 pixels from the screen edge
* and below the menu bar, which is 20 pixels high.
*/
screenRect = qd.screenBits.bounds;
SETRECT(&dragRect, 20 + 4, 4, screenRect.bottom-4, screenRect.right-4);
myWindow = GetNewWindow(windowID, &wRecord, (WindowPtr) -1);
SetPort(myWindow);
/*
* get controls
*/
for (i=0; i<NCS; i++)
mycontrol[i] = GetNewControl(i+129,myWindow);
HiliteControl(mycontrol[Cdelete-129], 255);
HiliteControl(mycontrol[Cpasswd-129], 255);
DrawControls(myWindow);
/*
* start list manager
*/
SETRECT(&rview, 50,30,200,150);
SETRECT(&rframe, 49,29,201,166);
SETRECT(&dbounds, 0,0,0,1 );
mylist = LNew( &rview,&dbounds, &csize, 0, myWindow, true,false,false,true);
l = *mylist;
l->selFlags |= lOnlyOne; /* only one selection at a time */
FrameRect(&rframe); /* draw around it */
/*
* if we have a selection, load it at start
*/
CountAppFiles( &shorti, &shortj);
if (!shorti && shortj > 0) {
GetAppFiles( shortj, &theFile); /* get file name */
theFile.fName.text[theFile.fName.length] = 0;
passlist(theFile.fName.text);
ClrAppFiles(shortj);
strcpy(s,"Telpass: ");
strcat(s,theFile.fName.text);
SetWTitle( myWindow, s);
}
/*
* Ready to go.
* Start with a clean event slate, and cycle the main event loop
* until the File/Quit menu item sets DoneFlag.
*
*/
DoneFlag = false;
for ( ;; ) {
if (DoneFlag) {
if (!currentfile[0]) /* if untitled, save it */
saveit();
else
passwrite(currentfile);
LDispose(mylist);
break; /* from main event loop */
}
/*
* Main Event tasks:
*/
SystemTask();
theActiveWindow = FrontWindow(); /* Used often, avoid repeated calls */
if (myWindow == theActiveWindow) {
GetMouse(&mousePt);
}
if ( ! GetNextEvent(everyEvent, &myEvent)) {
/*
* A null or system event, not for me.
* Here is a good place for heap cleanup and/or
* segment unloading if I want to.
*/
continue;
}
/*
* In the unlikely case that the active desk accessory does not
* handle mouseDown, keyDown, or other events, GetNextEvent() will
* give them to us! So before we perform actions on some events,
* we check to see that the affected window in question is really
* our window.
*/
switch (myEvent.what) {
case mouseDown:
switch (FindWindow(&myEvent.where, &whichWindow)) {
case inSysWindow:
SystemClick(&myEvent, whichWindow);
break;
case inMenuBar:
doCommand(MenuSelect(&myEvent.where));
break;
case inDrag:
DragWindow(whichWindow, &myEvent.where, &dragRect);
break;
case inGoAway:
GlobalToLocal(&myEvent.where);
if (TrackGoAway(whichWindow, &myEvent.where))
DoneFlag = true;
break;
case inGrow:
/* There is no grow box. (Fall through) */
case inContent:
if (whichWindow != theActiveWindow) {
SelectWindow(whichWindow);
}
GlobalToLocal(&myEvent.where);
if (whichWindow == myWindow) {
ControlHandle tempc;
FindControl(&myEvent.where,myWindow,&tempc);
if (tempc)
if (TrackControl(tempc, &myEvent.where, NULL)) {
/* find which one */
for (i=0; i<NCS; i++)
if (tempc == mycontrol[i])
switch (i+129) {
case Cnewuser:
donewuser();
break;
case Cpasswd:
donewpass();
break;
case Cdelete:
dodelete();
break;
default:
break;
}
}
/*
* check in list manager
*/
if (PtInRect(&myEvent.where, &rframe)) {
LClick(&myEvent.where, myEvent.modifiers, mylist);
}
/* if something selected, highlight options, if not, dehighlight */
csize.h = csize.v = 0;
if (LGetSelect(true, &csize, mylist)) {
HiliteControl(mycontrol[Cdelete-129], 0);
HiliteControl(mycontrol[Cpasswd-129], 0);
}
else {
HiliteControl(mycontrol[Cdelete-129], 255);
HiliteControl(mycontrol[Cpasswd-129], 255);
}
}
break;
default:
break;
}/*endsw FindWindow*/
break;
case keyDown:
case autoKey:
if (myWindow == theActiveWindow) {
if (myEvent.modifiers & cmdKey) {
doCommand(MenuKey(myEvent.message & charCodeMask));
} else {
if ('x' == (char) (myEvent.message & charCodeMask))
DoneFlag = true;
}
}
break;
case activateEvt:
if ((WindowPtr) myEvent.message == myWindow) {
LActivate(myEvent.modifiers & activeFlag, mylist);
}
break;
case updateEvt:
if ((WindowPtr) myEvent.message == myWindow) {
BeginUpdate(myWindow);
DrawControls(myWindow);
FrameRect(&rframe);
LUpdate(myWindow->visRgn,mylist);
EndUpdate(myWindow);
}
break;
default:
break;
}/*endsw myEvent.what*/
}/*endfor Main Event loop*/
/*
* No cleanup required, but if there was, it would happen here.
*/
return(0); /* Return from main() to allow C runtime cleanup */
}
/*
* Set the segment to Initialize. BEWARE: leading and trailing white space
* would be part of the segment name!
*/
# define __SEG__ Initialize
/*
* Set up the Apple, File, and Edit menus.
* If the MENU resources are missing, we die.
*/
void
setupMenus()
{
extern MenuHandle MyMenus[];
register MenuHandle *pMenu;
/*
* Set up the desk accessories menu.
* The "About Sample..." item, followed by a grey line,
* is presumed to be already in the resource. We then
* append the desk accessory names from the 'DRVR' resources.
*/
MyMenus[appleMenu] = GetMenu(appleID);
AddResMenu(MyMenus[appleMenu], (ResType) 'DRVR');
/*
* Now the File and Edit menus.
*/
MyMenus[fileMenu] = GetMenu(fileID);
MyMenus[editMenu] = GetMenu(editID);
/*
* Now insert all of the application menus in the menu bar.
*/
for (pMenu = &MyMenus[0]; pMenu < &MyMenus[menuCount]; ++pMenu) {
InsertMenu(*pMenu, 0);
}
DrawMenuBar();
return;
}
/*
* Back to the Main segment.
*/
# define __SEG__ Main
showAboutMeDialog()
{
DialogPtr theDialog;
short itemType;
Handle itemHdl;
Rect itemRect;
short itemHit;
theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
do {
ModalDialog(nil, &itemHit);
} while (itemHit != okButton);
DisposDialog(theDialog);
return;
}
/*****************************************************************/
/* get a new user name and insert it into the list
*/
donewuser()
{
DialogPtr theDialog;
short itemType;
Handle itemHdl;
Rect itemRect;
short itemHit;
theDialog = GetNewDialog(newuDLOG, nil, (WindowPtr) -1);
do {
ModalDialog(nil, &itemHit);
if (itemHit == 4 /* cancel */) {
DisposDialog(theDialog);
return(0);
}
} while (itemHit != okButton);
GetDItem(theDialog, 3, &itemType, &itemHdl, &itemRect);
GetIText(itemHdl, s);
DisposDialog(theDialog);
if (s[0]) /* if there was a user name typed */
adduser(s);
}
/*****************************************************************/
/* get a new passwd and insert it into the list
*/
int nchars;
char lastpass[256];
pascal Boolean
pfilt(td,tdevent,itemhit)
DialogPtr td;
EventRecord *tdevent;
short int *itemhit;
{
short itemType;
Handle itemHdl;
Rect itemRect;
int c,i;
switch(tdevent->what) {
case keyDown:
case autoKey:
if ((c = (tdevent->message & charCodeMask)) > 31) {
if (nchars < 200)
lastpass[nchars++] = c;
lastpass[nchars] = 0;
*itemhit = 2;
}
else if (c == 13) {
*itemhit = 1;
}
else if (c == 8) {
if (nchars > 0)
nchars--;
lastpass[nchars] = 0;
*itemhit = 2;
}
GetDItem(td, 2, &itemType, &itemHdl, &itemRect);
for (i=0; i< nchars; i++)
s[i] = '╞';
s[nchars] = 0;
SetIText(itemHdl, s);
return(true);
break;
default:
break;
}
*itemhit=0;
return(false);
}
donewpass()
{
DialogPtr theDialog;
short itemType;
Handle itemHdl;
Rect itemRect;
short itemHit;
char *p,*strchr();
csize.h = csize.v = 0;
if (!LGetSelect(true, &csize, mylist))
return(0); /* no user selected to receive passwd */
theDialog = GetNewDialog(newpDLOG, nil, (WindowPtr) -1);
nchars = 0;
/*
* set username into dialog
*/
GetDItem(theDialog, 5, &itemType, &itemHdl, &itemRect);
strcpy(s, lines[csize.v]);
p = strchr(s,':'); /* find ':' and truncate */
*p = 0;
SetIText(itemHdl, s);
do {
ModalDialog(pfilt, &itemHit);
if (itemHit == 3 /* cancel */) {
DisposDialog(theDialog);
return(0);
}
} while (itemHit != okButton);
DisposDialog(theDialog);
/*
* install the new password, it is now in lastpass
* Have to find which username it goes with first
*/
if (nchars < 1)
return(0);
if (csize.v >= 0)
dochoice(csize.v,lastpass);
}
/*
* Process mouse clicks in menu bar
*/
void
doCommand(mResult)
long mResult;
{
extern MenuHandle MyMenus[];
extern Boolean DoneFlag;
extern TEHandle TextH;
int theMenu, theItem,i;
char daName[256];
GrafPtr savePort;
theItem = LOWORD(mResult);
theMenu = HIWORD(mResult); /* This is the resource ID */
switch (theMenu) {
case appleID:
if (theItem == aboutMeCommand) {
showAboutMeDialog();
} else {
GetItem(MyMenus[appleMenu], theItem, daName);
GetPort(&savePort);
(void) OpenDeskAcc(daName);
SetPort(savePort);
}
break;
case fileID:
switch (theItem) {
case openCommand:
if (strlen(currentfile) > 0)
passwrite(currentfile);
wh.h = wh.v = 50;
tlst[0] = mytype;
SFGetFile(&wh, "Select password file", nil, 1, tlst,
nil, &reply);
if (reply.good) {
reply.fName.text[reply.fName.length] = 0;
passlist(reply.fName.text); /* load this file */
}
break;
case saveCommand:
case newCommand:
if (strlen(currentfile) > 0)
passwrite(currentfile);
else
saveit();
if (theItem == newCommand) {
clearit();
currentfile[0] = 0;
SetWTitle( myWindow, "Telpass: Untitled");
}
break;
case saveasCommand:
saveit();
break;
case quitCommand:
DoneFlag = true; /* Request exit */
break;
default:
break;
}
if (strlen(currentfile) > 0) {
strcpy(s,"Telpass: ");
strcat(s,currentfile);
SetWTitle( myWindow, s);
}
break;
case editID:
/*
* If this is for a 'standard' edit item,
* run it through SystemEdit first.
* SystemEdit will return FALSE if it's not a system window.
*/
if ((theItem <= clearCommand) && SystemEdit(theItem-1)) {
break;
}
/*
* Otherwise, it's my window.
*/
switch (theItem) {
case undoCommand:
break;
case cutCommand:
case copyCommand:
ZeroScrap();
break;
case pasteCommand:
break;
case clearCommand:
break;
default:
break;
} /*endsw theItem*/
break;
default:
break;
}/*endsw theMenu*/
HiliteMenu(0);
return;
}
char space[256];
char space2[256],*p;
FILE *fout,*fin;
int nnames;
/****************************************************************************/
/* dodelete
* remove an entry from the list manager and from the storage list
*/
dodelete()
{
int i;
csize.v = csize.h = 0;
if (LGetSelect(true, &csize, mylist)) {
LDelRow( 1, csize.v, mylist);
if (csize.v >= nnames-1)
csize.v--;
LSetSelect(true, &csize, mylist); /* reselect next one */
for (i=csize.v; i<nnames-1; i++) /* maybe deallocate mem someday? */
lines[i] = lines[i+1];
nnames--;
}
}
/****************************************************************************/
/* dochoice
* prompt for a certain password
*/
dochoice(c,newpass)
int c;
char newpass[];
{
char *p,*strchr();
char space[256],space2[256];
strcpy(space,lines[c]);
do {
p = strchr(space,':');
if (!p)
strcat(space,":");
} while (!p); /* make sure we get a : */
*p = '\0';
p++;
Sencompass(newpass,space2); /* take password */
sprintf(s,"%s:%s",space,space2);
lines[c] = malloc(strlen(s)+1);
strcpy(lines[c],s); /* this string includes passwd */
strcat(space,":╞"); /* indicate passwd present */
LSetCell(space,strlen(space), &csize, mylist);
}
/****************************************************************************/
/* passwrite
* write them out
*/
passwrite(s)
char *s;
{
int i;
short int vref;
FInfo finf;
String(255) vname;
if (strlen(s) == 0)
return(0);
if (NULL == (fout = fopen(s,"w"))) {
return(0);
}
for (i=0; i < nnames; i++) {
fputs(lines[i],fout);
fputs("\n",fout);
}
fclose(fout);
/*
* set correct creator and type
*/
GetVol( &vname, &vref);
GetFInfo(s, vref, &finf);
finf.fdType = mytype;
finf.fdCreator = mycreator;
SetFInfo(s, vref, &finf);
}
/****************************************************************************/
/* clearit
* Clear the current list
* Write the current file out if we have a current file name
*/
clearit()
{
ListPtr l; /* temp use */
passwrite(currentfile);
LDispose(mylist); /* loading new list, delete old one */
csize.h = 200; csize.v = 15;
nnames = 0;
mylist = LNew( &rview,&dbounds, &csize, 0, myWindow, true,false,false,true);
l = *mylist;
l->selFlags |= lOnlyOne; /* only one selection at a time */
SetPort(myWindow); /* make my window the current port */
EraseRect(&rframe);
InvalRect(&rframe);
}
/****************************************************************************/
/* saveit
* Use the file picker to save out the current file
*/
saveit()
{
if (nnames <= 0)
return;
wh.v = wh.h = 50;
SFPutFile(&wh, "Save password file as:", currentfile, nil, &reply);
if (reply.good) {
reply.fName.text[reply.fName.length] = 0;
passwrite(reply.fName.text);
strcpy(currentfile,reply.fName.text);
}
}
/****************************************************************************/
/* passlist
* List the current file
*/
passlist(s)
char *s;
{
char space[256];
clearit();
strcpy(currentfile,s);
if (NULL == (fin = fopen(s,"r"))) {
return(0);
}
nnames = 0;
while (NULL != fgets(space,250,fin)) {
space[strlen(space)-1] = '\0';
adduser(space);
}
InvalRect(&rframe); /* make it draw right */
fclose(fin);
}
/****************************************************************************/
/* adduser
* add a new user to the list
*/
adduser(p)
char *p;
{
char space[256];
lines[nnames] = malloc(strlen(p)+1); /* memory copy includes the password */
strcpy(lines[nnames],p);
strcpy(space,p);
do {
p = strchr(space,':');
if (!p)
strcat(space,":");
} while (!p); /* make sure we get a : */
p++; /* point past : */
if (*p)
*p = '╞';
else
*p = 0;
*(++p) = 0; /* end string */
/*
* when adding to the list manager, replace ugly password with a symbol
*/
LAddRow(1,nnames, mylist);
csize.h = 0; csize.v = nnames;
LSetCell(space,strlen(space), &csize, mylist);
LDraw(&csize,mylist);
nnames++;
csize.h = 0; csize.v = nnames;
}
/****************************************************************************/
/* Scompass
* compute and check the encrypted password
*/
Sencompass(ps,en)
char *ps,*en;
{
int i,ck;
char *p,c;
ck = 0;
p = ps;
while (*p) /* checksum the string */
ck += *p++;
c = ck;
for (i=0; i<10; i++) {
*en = (((*ps ^ c) | 32) & 127); /* XOR with checksum */
if (*ps)
ps++;
else
c++; /* to hide length */
en++;
}
*en = 0;
}