bool CmdLineHandler::toBeLeader() { USES_CONVERSION; bool ret = true; //create share memory sharedMemHandle_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, SHARED_MEM_SIZE, A2W(userShareMemName_)); if (NULL == sharedMemHandle_) { fatalbox("%s", "can't get shared memory handle!"); exit(-1); } sharedType_ = (GetLastError() == ERROR_ALREADY_EXISTS) ? SHARED_TYPE_FOLLOWER : SHARED_TYPE_LEADER; sharedBuffer_ = (char* ) MapViewOfFile(sharedMemHandle_, // handle to map object FILE_MAP_ALL_ACCESS, // read/write permission 0, 0, SHARED_MEM_SIZE); if (NULL == sharedBuffer_){ fatalbox("%s", "can't get shared memory!"); CloseHandle(sharedMemHandle_); exit(-1); } if (isLeader()){ memset(sharedBuffer_, 0, SHARED_MEM_SIZE); } return isLeader(); }
void write_setting_i(void *handle, char const *key, int value) { int fd = *(int *)handle; Handle h; int id; OSErr error; Str255 pkey; UseResFile(fd); if (ResError() != noErr) fatalbox("Failed to open saved session (%d)", ResError()); /* XXX assume all systems have the same "int" format */ error = PtrToHand(&value, &h, sizeof(int)); if (error != noErr) fatalbox("Failed to allocate memory (%d)", error); /* Put the data in a resource. */ id = Unique1ID(FOUR_CHAR_CODE('Int ')); if (ResError() != noErr) fatalbox("Failed to get ID for resource %s (%d)", key, ResError()); c2pstrcpy(pkey, key); AddResource(h, FOUR_CHAR_CODE('Int '), id, pkey); if (ResError() != noErr) fatalbox("Failed to add resource %s (%d)", key, ResError()); }
void des3_decrypt_blk(unsigned char *blk, int len) { DWORD dlen; dlen = len; if(CryptDecrypt(hDESKey[1][2], 0, FALSE, 0, blk, &dlen) == 0) fatalbox("Error decrypting block!\n"); if(CryptEncrypt(hDESKey[1][1], 0, FALSE, 0, blk, &dlen, len + 8) == 0) fatalbox("Error decrypting block!\n"); if(CryptDecrypt(hDESKey[1][0], 0, FALSE, 0, blk, &dlen) == 0) fatalbox("Error decrypting block!\n"); }
static int localproxy_try_send(Local_Proxy_Socket ps) { int sent = 0; while (bufchain_size(&ps->pending_output_data) > 0) { void *data; int len, ret; bufchain_prefix(&ps->pending_output_data, &data, &len); ret = write(ps->to_cmd, data, len); if (ret < 0 && errno != EWOULDBLOCK) { /* We're inside the Unix frontend here, so we know * that the frontend handle is unnecessary. */ logevent(NULL, strerror(errno)); fatalbox("%s", strerror(errno)); } else if (ret <= 0) { break; } else { bufchain_consume(&ps->pending_output_data, ret); sent += ret; } } if (bufchain_size(&ps->pending_output_data) == 0) uxsel_del(ps->to_cmd); else uxsel_set(ps->to_cmd, 2, localproxy_select_result); return sent; }
void MD5Final(unsigned char digest[16], struct MD5Context *ctx) { DWORD cb = 16; if(CryptGetHashParam(ctx->hHash, HP_HASHVAL, digest, &cb, 0) == 0) fatalbox("Error during CryptGetHashParam!\n"); if(ctx->hHash) CryptDestroyHash(ctx->hHash); ctx->hHash = 0; }
void close_settings_r(void *handle) { int fd; if (handle == NULL) return; fd = *(int *)handle; CloseResFile(fd); if (ResError() != noErr) fatalbox("Close of saved session failed (%d)", ResError()); sfree(handle); }
int ProcessQuotaCmd(const char* line) { int direction = 0, number, pos; if (line[0] != '-') return 0; if (line[1] == '0') direction = 0; else if (line[1] == '1') direction = 1; else fatalbox("Invalid data received in ReadQuotas: Unknown direction"); if (line[2] == '-') { bytesAvailable[direction] = -1; return 0; } number = 0; for (pos = 2;; pos++) { if (line[pos] == 0 || line[pos] == '\r' || line[pos] == '\n') break; if (line[pos] < '0' || line[pos] > '9') fatalbox("Invalid data received in ReadQuotas: Bytecount not a number"); number *= 10; number += line[pos] - '0'; } if (bytesAvailable[direction] == -1) bytesAvailable[direction] = number; else bytesAvailable[direction] += number; return 1; }
void write_setting_s(void *handle, char const *key, char const *value) { int fd = *(int *)handle; Handle h; int id; OSErr error; Str255 pkey; UseResFile(fd); if (ResError() != noErr) fatalbox("Failed to open saved session (%d)", ResError()); error = PtrToHand(value, &h, strlen(value)); if (error != noErr) fatalbox("Failed to allocate memory"); /* Put the data in a resource. */ id = Unique1ID(FOUR_CHAR_CODE('TEXT')); if (ResError() != noErr) fatalbox("Failed to get ID for resource %s (%d)", key, ResError()); c2pstrcpy(pkey, key); AddResource(h, FOUR_CHAR_CODE('TEXT'), id, pkey); if (ResError() != noErr) fatalbox("Failed to add resource %s (%d)", key, ResError()); }
void write_setting_filename(void *handle, const char *key, Filename fn) { int fd = *(int *)handle; AliasHandle h; int id; OSErr error; Str255 pkey; UseResFile(fd); if (ResError() != noErr) fatalbox("Failed to open saved session (%d)", ResError()); if (filename_is_null(fn)) { /* Generate a special "null" alias */ h = (AliasHandle)NewHandle(sizeof(**h)); if (h == NULL) fatalbox("Failed to create fake alias"); (*h)->userType = 'pTTY'; (*h)->aliasSize = sizeof(**h); } else { error = NewAlias(NULL, &fn.fss, &h); if (error == fnfErr) { /* * NewAlias can't create an alias for a nonexistent file. * Create an alias for the directory, and record the * filename as well. */ FSSpec tmpfss; FSMakeFSSpec(fn.fss.vRefNum, fn.fss.parID, NULL, &tmpfss); error = NewAlias(NULL, &tmpfss, &h); if (error != noErr) fatalbox("Failed to create alias"); (*h)->userType = 'pTTY'; SetHandleSize((Handle)h, (*h)->aliasSize + fn.fss.name[0] + 1); if (MemError() != noErr) fatalbox("Failed to create alias"); memcpy((char *)*h + (*h)->aliasSize, fn.fss.name, fn.fss.name[0] + 1); } if (error != noErr) fatalbox("Failed to create alias"); } /* Put the data in a resource. */ id = Unique1ID(rAliasType); if (ResError() != noErr) fatalbox("Failed to get ID for resource %s (%d)", key, ResError()); c2pstrcpy(pkey, key); AddResource((Handle)h, rAliasType, id, pkey); if (ResError() != noErr) fatalbox("Failed to add resource %s (%d)", key, ResError()); }
void close_settings_w(void *handle) { struct write_settings *ws = handle; OSErr error; CloseResFile(ws->fd); if ((error = ResError()) != noErr) goto out; error = FSpExchangeFiles(&ws->tmpfile, &ws->dstfile); if (error != noErr) goto out; error = FSpDelete(&ws->tmpfile); if (error != noErr) goto out; return; out: fatalbox("Close of saved session failed (%d)", error); safefree(handle); }
void *safemalloc(size_t n, size_t size) { void *p; if (n > 0x7fffffff / size) { p = NULL; } else { size *= n; if (size == 0) size = 1; p = User::Alloc((TInt)size); } if ( !p ) { fatalbox("Out of memory"); exit(1); } return p; }
/* * Receive a block of data from the SSH link. Block until all data * is available. * * To do this, we repeatedly call the SSH protocol module, with our * own trap in from_backend() to catch the data that comes back. We * do this until we have enough data. */ static unsigned char *outptr; /* where to put the data */ static unsigned outlen; /* how much data required */ static unsigned char *pending = NULL; /* any spare data */ static unsigned pendlen = 0, pendsize = 0; /* length and phys. size of buffer */ int from_backend(int is_stderr, char *data, int datalen) { unsigned char *p = (unsigned char *) data; unsigned len = (unsigned) datalen; /* * stderr data is just spouted to local stderr and otherwise * ignored. */ if (is_stderr) { fwrite(data, 1, len, stderr); return 0; } /* * If this is before the real session begins, just return. */ if (!outptr) return 0; if (outlen > 0) { unsigned used = outlen; if (used > len) used = len; memcpy(outptr, p, used); outptr += used; outlen -= used; p += used; len -= used; } if (len > 0) { if (pendsize < pendlen + len) { pendsize = pendlen + len + 4096; pending = (pending ? srealloc(pending, pendsize) : smalloc(pendsize)); if (!pending) fatalbox("Out of memory"); } memcpy(pending + pendlen, p, len); pendlen += len; } return 0; }
void del_settings(char const *sessionname) { OSErr error; FSSpec sessfile; short sessVRefNum; long sessDirID; Str255 psessionname; error = get_session_dir(kDontCreateFolder, &sessVRefNum, &sessDirID); c2pstrcpy(psessionname, sessionname); error = FSMakeFSSpec(sessVRefNum, sessDirID, psessionname, &sessfile); if (error != noErr) goto out; error = FSpDelete(&sessfile); return; out: fatalbox("Delete session failed (%d)", error); }
static void s_wrpkt_start(int type, int len) { int pad, biglen; len += 5; /* type and CRC */ pad = 8 - (len%8); biglen = len + pad; pktout.length = len-5; if (pktout.maxlen < biglen) { pktout.maxlen = biglen; pktout.data = (pktout.data == NULL ? malloc(biglen+4) : realloc(pktout.data, biglen+4)); if (!pktout.data) fatalbox("Out of memory"); } pktout.type = type; pktout.body = pktout.data+4+pad+1; }
void *saferealloc(void *ptr, size_t n, size_t size) { void *p; if (n > 0x7fffffff / size) { p = NULL; } else { size *= n; if (!ptr) { p = User::Alloc((TInt)size); } else { p = User::ReAlloc(ptr, (TInt) size); } } if (!p) { fatalbox("Out of memory"); exit(1); } return p; }
/* * NB: Destination file must exist. */ void *open_settings_w_fsp(FSSpec *dstfile) { short tmpVRefNum; long tmpDirID; struct write_settings *ws; OSErr error; Str255 tmpname; ws = snew(struct write_settings); ws->dstfile = *dstfile; /* Create a temporary file to save to first. */ error = FindFolder(ws->dstfile.vRefNum, kTemporaryFolderType, kCreateFolder, &tmpVRefNum, &tmpDirID); if (error != noErr) goto out; c2pstrcpy(tmpname, tmpnam(NULL)); error = FSMakeFSSpec(tmpVRefNum, tmpDirID, tmpname, &ws->tmpfile); if (error != noErr && error != fnfErr) goto out; if (error == noErr) { error = FSpDelete(&ws->tmpfile); if (error != noErr) goto out; } FSpCreateResFile(&ws->tmpfile, PUTTY_CREATOR, SESS_TYPE, smSystemScript); if ((error = ResError()) != noErr) goto out; ws->fd = FSpOpenResFile(&ws->tmpfile, fsWrPerm); if (ws->fd == -1) {error = ResError(); goto out;} /* Set up standard resources. Doesn't matter if these fail. */ copy_resource('STR ', -16396); copy_resource('TMPL', TMPL_Int); return ws; out: safefree(ws); fatalbox("Failed to open session for write (%d)", error); }
void sk_init(void) { #ifndef NO_IPV6 winsock2_module = #endif winsock_module = load_system32_dll("ws2_32.dll"); if (!winsock_module) { winsock_module = load_system32_dll("wsock32.dll"); } if (!winsock_module) { fatalbox("Unable to load any WinSock library"); } #ifndef NO_IPV6 /* Check if we have getaddrinfo in Winsock */ if (GetProcAddress(winsock_module, "getaddrinfo") != NULL) { #ifdef NET_SETUP_DIAGNOSTICS logevent(NULL, "Native WinSock IPv6 support detected"); #endif GET_WINDOWS_FUNCTION(winsock_module, getaddrinfo); GET_WINDOWS_FUNCTION(winsock_module, freeaddrinfo); GET_WINDOWS_FUNCTION(winsock_module, getnameinfo); GET_WINDOWS_FUNCTION(winsock_module, gai_strerror); } else { /* Fall back to wship6.dll for Windows 2000 */ wship6_module = load_system32_dll("wship6.dll"); if (wship6_module) { #ifdef NET_SETUP_DIAGNOSTICS logevent(NULL, "WSH IPv6 support detected"); #endif GET_WINDOWS_FUNCTION(wship6_module, getaddrinfo); GET_WINDOWS_FUNCTION(wship6_module, freeaddrinfo); GET_WINDOWS_FUNCTION(wship6_module, getnameinfo); GET_WINDOWS_FUNCTION(wship6_module, gai_strerror); } else { #ifdef NET_SETUP_DIAGNOSTICS logevent(NULL, "No IPv6 support detected"); #endif } } GET_WINDOWS_FUNCTION(winsock2_module, WSAAddressToStringA); #else #ifdef NET_SETUP_DIAGNOSTICS logevent(NULL, "PuTTY was built without IPv6 support"); #endif #endif GET_WINDOWS_FUNCTION(winsock_module, WSAAsyncSelect); GET_WINDOWS_FUNCTION(winsock_module, WSAEventSelect); GET_WINDOWS_FUNCTION(winsock_module, select); GET_WINDOWS_FUNCTION(winsock_module, WSAGetLastError); GET_WINDOWS_FUNCTION(winsock_module, WSAEnumNetworkEvents); GET_WINDOWS_FUNCTION(winsock_module, WSAStartup); GET_WINDOWS_FUNCTION(winsock_module, WSACleanup); GET_WINDOWS_FUNCTION(winsock_module, closesocket); GET_WINDOWS_FUNCTION(winsock_module, ntohl); GET_WINDOWS_FUNCTION(winsock_module, htonl); GET_WINDOWS_FUNCTION(winsock_module, htons); GET_WINDOWS_FUNCTION(winsock_module, ntohs); GET_WINDOWS_FUNCTION(winsock_module, gethostname); GET_WINDOWS_FUNCTION(winsock_module, gethostbyname); GET_WINDOWS_FUNCTION(winsock_module, getservbyname); GET_WINDOWS_FUNCTION(winsock_module, inet_addr); GET_WINDOWS_FUNCTION(winsock_module, inet_ntoa); GET_WINDOWS_FUNCTION(winsock_module, connect); GET_WINDOWS_FUNCTION(winsock_module, bind); #ifdef MPEXT GET_WINDOWS_FUNCTION(winsock_module, getsockopt); #endif GET_WINDOWS_FUNCTION(winsock_module, setsockopt); GET_WINDOWS_FUNCTION(winsock_module, socket); GET_WINDOWS_FUNCTION(winsock_module, listen); GET_WINDOWS_FUNCTION(winsock_module, send); GET_WINDOWS_FUNCTION(winsock_module, ioctlsocket); GET_WINDOWS_FUNCTION(winsock_module, accept); GET_WINDOWS_FUNCTION(winsock_module, recv); GET_WINDOWS_FUNCTION(winsock_module, WSAIoctl); /* Try to get the best WinSock version we can get */ if (!sk_startup(2,2) && !sk_startup(2,0) && !sk_startup(1,1)) { fatalbox("Unable to initialise WinSock"); } sktree = newtree234(cmpfortree); }
static void mac_startup(void) { Handle menuBar; TECInfoHandle ti; #if !TARGET_API_MAC_CARBON /* Init Memory Manager */ MaxApplZone(); /* Init QuickDraw */ InitGraf(&qd.thePort); /* Init Font Manager */ InitFonts(); /* Init Window Manager */ InitWindows(); /* Init Menu Manager */ InitMenus(); /* Init TextEdit */ TEInit(); /* Init Dialog Manager */ InitDialogs(NULL); #endif /* Get base system version (only used if there's no better selector) */ if (Gestalt(gestaltSystemVersion, &mac_gestalts.sysvers) != noErr || (mac_gestalts.sysvers &= 0xffff) < 0x700) fatalbox("PuTTYgen requires System 7 or newer"); /* Find out if we've got Color Quickdraw */ if (Gestalt(gestaltQuickdrawVersion, &mac_gestalts.qdvers) != noErr) mac_gestalts.qdvers = gestaltOriginalQD; /* ... and the Appearance Manager? */ if (Gestalt(gestaltAppearanceVersion, &mac_gestalts.apprvers) != noErr) if (Gestalt(gestaltAppearanceAttr, NULL) == noErr) mac_gestalts.apprvers = 0x0100; else mac_gestalts.apprvers = 0; #if TARGET_RT_MAC_CFM /* Paranoia: Did we manage to pull in AppearanceLib? */ if (&RegisterAppearanceClient == kUnresolvedCFragSymbolAddress) mac_gestalts.apprvers = 0; #endif #if TARGET_CPU_68K mac_gestalts.cntlattr = 0; mac_gestalts.windattr = 0; #else /* Mac OS 8.5 Control Manager (proportional scrollbars)? */ if (Gestalt(gestaltControlMgrAttr, &mac_gestalts.cntlattr) != noErr || &SetControlViewSize == kUnresolvedCFragSymbolAddress) mac_gestalts.cntlattr = 0; /* Mac OS 8.5 Window Manager? */ if (Gestalt(gestaltWindowMgrAttr, &mac_gestalts.windattr) != noErr || &SetWindowContentColor == kUnresolvedCFragSymbolAddress) mac_gestalts.windattr = 0; #endif /* Text Encoding Conversion Manager? */ if ( #if TARGET_RT_MAC_CFM &TECGetInfo == kUnresolvedCFragSymbolAddress || #else InitializeUnicodeConverter(NULL) != noErr || #endif TECGetInfo(&ti) != noErr) mac_gestalts.encvvers = 0; else { mac_gestalts.encvvers = (*ti)->tecVersion; mac_gestalts.uncvattr = (*ti)->tecUnicodeConverterFeatures; DisposeHandle((Handle)ti); } /* Navigation Services? */ if (NavServicesAvailable()) mac_gestalts.navsvers = NavLibraryVersion(); else mac_gestalts.navsvers = 0; /* We've been tested with the Appearance Manager */ if (mac_gestalts.apprvers != 0) RegisterAppearanceClient(); menuBar = GetNewMBar(128); if (menuBar == NULL) fatalbox("Unable to create menu bar."); SetMenuBar(menuBar); AppendResMenu(GetMenuHandle(mApple), 'DRVR'); mac_adjustmenus(); DrawMenuBar(); InitCursor(); windows.about = NULL; windows.licence = NULL; flags = FLAG_INTERACTIVE; /* Install Apple Event handlers. */ #if 0 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerUPP(&mac_aevt_oapp), 0, FALSE); AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerUPP(&mac_aevt_odoc), 0, FALSE); AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerUPP(&mac_aevt_pdoc), 0, FALSE); #endif AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP(&mac_aevt_quit), 0, FALSE); }
static void ssh_protocol(unsigned char *in, int inlen, int ispkt) { int i, j, len; unsigned char session_id[16]; unsigned char *rsabuf, *keystr1, *keystr2; unsigned char cookie[8]; struct RSAKey servkey, hostkey; struct MD5Context md5c; unsigned long supported_ciphers_mask; int cipher_type; extern struct ssh_cipher ssh_3des; extern struct ssh_cipher ssh_blowfish; crBegin; random_init(); while (!ispkt) crReturnV; if (pktin.type != 2) fatalbox("Public key packet not received"); memcpy(cookie, pktin.body, 8); MD5Init(&md5c); i = makekey(pktin.body+8, &servkey, &keystr1); j = makekey(pktin.body+8+i, &hostkey, &keystr2); supported_ciphers_mask = (pktin.body[12+i+j] << 24) | (pktin.body[13+i+j] << 16) | (pktin.body[14+i+j] << 8) | (pktin.body[15+i+j]); MD5Update(&md5c, keystr2, hostkey.bytes); MD5Update(&md5c, keystr1, servkey.bytes); MD5Update(&md5c, pktin.body, 8); MD5Final(session_id, &md5c); for (i=0; i<32; i++) session_key[i] = random_byte(); len = (hostkey.bytes > servkey.bytes ? hostkey.bytes : servkey.bytes); rsabuf = malloc(len); if (!rsabuf) fatalbox("Out of memory"); verify_ssh_host_key(savedhost, &hostkey); for (i=0; i<32; i++) { rsabuf[i] = session_key[i]; if (i < 16) rsabuf[i] ^= session_id[i]; } if (hostkey.bytes > servkey.bytes) { rsaencrypt(rsabuf, 32, &servkey); rsaencrypt(rsabuf, servkey.bytes, &hostkey); } else { rsaencrypt(rsabuf, 32, &hostkey); rsaencrypt(rsabuf, hostkey.bytes, &servkey); } cipher_type = cfg.cipher == CIPHER_BLOWFISH ? SSH_CIPHER_BLOWFISH : SSH_CIPHER_3DES; if ((supported_ciphers_mask & (1 << cipher_type)) == 0) { c_write("Selected cipher not supported, falling back to 3DES\r\n", 53); cipher_type = SSH_CIPHER_3DES; } s_wrpkt_start(3, len+15); pktout.body[0] = cipher_type; memcpy(pktout.body+1, cookie, 8); pktout.body[9] = (len*8) >> 8; pktout.body[10] = (len*8) & 0xFF; memcpy(pktout.body+11, rsabuf, len); pktout.body[len+11] = pktout.body[len+12] = 0; /* protocol flags */ pktout.body[len+13] = pktout.body[len+14] = 0; s_wrpkt(); free(rsabuf); cipher = cipher_type == SSH_CIPHER_BLOWFISH ? &ssh_blowfish : &ssh_3des; cipher->sesskey(session_key); do { crReturnV; } while (!ispkt); if (pktin.type != 14) fatalbox("Encryption not successfully enabled"); fflush(stdout); { static char username[100]; static int pos = 0; static char c; if (!*cfg.username) { c_write("login as: ", 10); while (pos >= 0) { do { crReturnV; } while (ispkt); while (inlen--) switch (c = *in++) { case 10: case 13: username[pos] = 0; pos = -1; break; case 8: case 127: if (pos > 0) { c_write("\b \b", 3); pos--; } break; case 21: case 27: while (pos > 0) { c_write("\b \b", 3); pos--; } break; case 3: case 4: random_save_seed(); exit(0); break; default: if (c >= ' ' && c <= '~' && pos < 40) { username[pos++] = c; c_write(&c, 1); } break; } } c_write("\r\n", 2); username[strcspn(username, "\n\r")] = '\0'; } else { char stuff[200]; strncpy(username, cfg.username, 99); username[99] = '\0'; sprintf(stuff, "Sent username \"%s\".\r\n", username); c_write(stuff, strlen(stuff)); } s_wrpkt_start(4, 4+strlen(username)); pktout.body[0] = pktout.body[1] = pktout.body[2] = 0; pktout.body[3] = strlen(username); memcpy(pktout.body+4, username, strlen(username)); s_wrpkt(); } do { crReturnV; } while (!ispkt); while (pktin.type == 15) { static char password[100]; static int pos; static char c; c_write("password: "******"\r\n", 2); s_wrpkt_start(9, 4+strlen(password)); pktout.body[0] = pktout.body[1] = pktout.body[2] = 0; pktout.body[3] = strlen(password); memcpy(pktout.body+4, password, strlen(password)); s_wrpkt(); memset(password, 0, strlen(password)); do { crReturnV; } while (!ispkt); if (pktin.type == 15) { c_write("Access denied\r\n", 15); } else if (pktin.type != 14) { fatalbox("Strange packet received, type %d", pktin.type); } } if (!cfg.nopty) { i = strlen(cfg.termtype); s_wrpkt_start(10, i+5*4+1); pktout.body[0] = (i >> 24) & 0xFF; pktout.body[1] = (i >> 16) & 0xFF; pktout.body[2] = (i >> 8) & 0xFF; pktout.body[3] = i & 0xFF; memcpy(pktout.body+4, cfg.termtype, i); i += 4; pktout.body[i++] = (rows >> 24) & 0xFF; pktout.body[i++] = (rows >> 16) & 0xFF; pktout.body[i++] = (rows >> 8) & 0xFF; pktout.body[i++] = rows & 0xFF; pktout.body[i++] = (cols >> 24) & 0xFF; pktout.body[i++] = (cols >> 16) & 0xFF; pktout.body[i++] = (cols >> 8) & 0xFF; pktout.body[i++] = cols & 0xFF; memset(pktout.body+i, 0, 9); /* 0 pixwidth, 0 pixheight, 0.b endofopt */ s_wrpkt(); ssh_state = SSH_STATE_INTERMED; do { crReturnV; } while (!ispkt); if (pktin.type != 14 && pktin.type != 15) { fatalbox("Protocol confusion"); } else if (pktin.type == 15) { c_write("Server refused to allocate pty\r\n", 32); } }
static void ssh_gotdata(unsigned char *data, int datalen) { static long len, biglen, to_read; static unsigned char *p; static int i, pad; crBegin; while (1) { for (i = len = 0; i < 4; i++) { while (datalen == 0) crReturnV; len = (len << 8) + *data; data++, datalen--; } #ifdef FWHACK if (len == 0x52656d6f) { /* "Remo"te server has closed ... */ len = 0x300; /* big enough to carry to end */ } #endif pad = 8 - (len%8); biglen = len + pad; len -= 5; /* type and CRC */ pktin.length = len; if (pktin.maxlen < biglen) { pktin.maxlen = biglen; pktin.data = (pktin.data == NULL ? malloc(biglen) : realloc(pktin.data, biglen)); if (!pktin.data) fatalbox("Out of memory"); } p = pktin.data, to_read = biglen; while (to_read > 0) { static int chunk; chunk = to_read; while (datalen == 0) crReturnV; if (chunk > datalen) chunk = datalen; memcpy(p, data, chunk); data += chunk; datalen -= chunk; p += chunk; to_read -= chunk; } if (cipher) cipher->decrypt(pktin.data, biglen); pktin.type = pktin.data[pad]; pktin.body = pktin.data+pad+1; if (pktin.type == 36) { /* SSH_MSG_DEBUG */ /* FIXME: log it */ } else ssh_protocol(NULL, 0, 1); } crFinishV; }
void sk_init(void) { WORD winsock_ver; WSADATA wsadata; winsock_ver = MAKEWORD(2, 0); winsock_module = LoadLibrary("WS2_32.DLL"); if (!winsock_module) { winsock_module = LoadLibrary("WSOCK32.DLL"); winsock_ver = MAKEWORD(1, 1); } if (!winsock_module) fatalbox("Unable to load any WinSock library"); #ifndef NO_IPV6 wship6_module = LoadLibrary("wship6.dll"); if (wship6_module) { GET_WINSOCK_FUNCTION(wship6_module, getaddrinfo); GET_WINSOCK_FUNCTION(wship6_module, freeaddrinfo); GET_WINSOCK_FUNCTION(wship6_module, getnameinfo); } #endif GET_WINSOCK_FUNCTION(winsock_module, WSAAsyncSelect); GET_WINSOCK_FUNCTION(winsock_module, WSAEventSelect); GET_WINSOCK_FUNCTION(winsock_module, select); GET_WINSOCK_FUNCTION(winsock_module, WSAGetLastError); GET_WINSOCK_FUNCTION(winsock_module, WSAEnumNetworkEvents); GET_WINSOCK_FUNCTION(winsock_module, WSAStartup); GET_WINSOCK_FUNCTION(winsock_module, WSACleanup); GET_WINSOCK_FUNCTION(winsock_module, closesocket); GET_WINSOCK_FUNCTION(winsock_module, ntohl); GET_WINSOCK_FUNCTION(winsock_module, htonl); GET_WINSOCK_FUNCTION(winsock_module, htons); GET_WINSOCK_FUNCTION(winsock_module, ntohs); GET_WINSOCK_FUNCTION(winsock_module, gethostbyname); GET_WINSOCK_FUNCTION(winsock_module, getservbyname); GET_WINSOCK_FUNCTION(winsock_module, inet_addr); GET_WINSOCK_FUNCTION(winsock_module, inet_ntoa); GET_WINSOCK_FUNCTION(winsock_module, connect); GET_WINSOCK_FUNCTION(winsock_module, bind); GET_WINSOCK_FUNCTION(winsock_module, setsockopt); GET_WINSOCK_FUNCTION(winsock_module, socket); GET_WINSOCK_FUNCTION(winsock_module, listen); GET_WINSOCK_FUNCTION(winsock_module, send); GET_WINSOCK_FUNCTION(winsock_module, ioctlsocket); GET_WINSOCK_FUNCTION(winsock_module, accept); GET_WINSOCK_FUNCTION(winsock_module, recv); GET_WINSOCK_FUNCTION(winsock_module, WSAIoctl); if (p_WSAStartup(winsock_ver, &wsadata)) { fatalbox("Unable to initialise WinSock"); } if (LOBYTE(wsadata.wVersion) != LOBYTE(winsock_ver)) { p_WSACleanup(); fatalbox("WinSock version is incompatible with %d.%d", LOBYTE(winsock_ver), HIBYTE(winsock_ver)); } sktree = newtree234(cmpfortree); }
int random_byte(void) { unsigned char b; if(!CryptGenRandom(hCryptProv, 1, &b)) fatalbox("random number generator failure!"); return b; }
void MD5Init(struct MD5Context *ctx) { if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &ctx->hHash)) fatalbox("Error during CryptBeginHash!\n"); }
static int ReadQuotas(int i) { #ifdef _WINDOWS HANDLE hin; DWORD savemode, newmode; hin = GetStdHandle(STD_INPUT_HANDLE); GetConsoleMode(hin, &savemode); newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT; newmode &= ~ENABLE_ECHO_INPUT; SetConsoleMode(hin, newmode); while (bytesAvailable[i] == 0) { DWORD read; BOOL r; char buffer[21]; r = ReadFile(hin, buffer, 20, &read, 0); if (!r || read == 0) fatalbox("ReadFile failed in ReadQuotas"); buffer[read] = 0; if (buffer[0] != '-') { if (input_pushback != 0) fatalbox("input_pushback not null!"); else input_pushback = strdup(buffer); } else ProcessQuotaCmd(buffer); } SetConsoleMode(hin, savemode); #else char* line; while (bytesAvailable[i] == 0) { int error = 0; line = read_input_line(1, &error); if (line == NULL || error) { fatalbox("read_input_line failed in ReadQuotas"); break; } if (line[0] != '-') { if (input_pushback != 0) fatalbox("input_pushback not null!"); else input_pushback = strdup(line); } else ProcessQuotaCmd(line); sfree(line); } #endif //_WINDOWS return 1; }
void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len) { if(CryptHashData(ctx->hHash, buf, len, 0) == 0) fatalbox("Error during CryptHashSessionKey!\n"); }
void rsaencrypt(unsigned char *data, int length, struct RSAKey *rsakey) { int i; unsigned char *pKeybuf, *pKeyin; HCRYPTKEY hRsaKey; PUBLICKEYSTRUC *pBlob; RSAPUBKEY *pRPK; unsigned char *buf; DWORD dlen; DWORD bufsize; /* allocate buffer for public key blob */ if((pBlob = malloc(sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY) + rsakey->bytes)) == NULL) fatalbox("Out of memory"); /* allocate buffer for message encryption block */ bufsize = (length + rsakey->bytes) << 1; if((buf = malloc(bufsize)) == NULL) fatalbox("Out of memory"); /* construct public key blob from host public key */ pKeybuf = ((unsigned char*)pBlob) + sizeof(PUBLICKEYSTRUC) + sizeof(RSAPUBKEY); pKeyin = ((unsigned char*)rsakey->modulus); /* change big endian to little endian */ for(i=0; i<rsakey->bytes; i++) pKeybuf[i] = pKeyin[rsakey->bytes-i-1]; pBlob->bType = PUBLICKEYBLOB; pBlob->bVersion = 0x02; pBlob->reserved = 0; pBlob->aiKeyAlg = CALG_RSA_KEYX; pRPK = (RSAPUBKEY*)(((unsigned char*)pBlob) + sizeof(PUBLICKEYSTRUC)); pRPK->magic = 0x31415352; /* "RSA1" */ pRPK->bitlen = rsakey->bits; pRPK->pubexp = rsakey->exponent; /* import public key blob into key container */ if(CryptImportKey(hCryptProv, (void*)pBlob, sizeof(PUBLICKEYSTRUC)+sizeof(RSAPUBKEY)+rsakey->bytes, 0, 0, &hRsaKey) == 0) fatalbox("Error importing RSA key!"); /* copy message into buffer */ memcpy(buf, data, length); dlen = length; /* using host public key, encrypt the message */ if(CryptEncrypt(hRsaKey, 0, TRUE, 0, buf, &dlen, bufsize) == 0) fatalbox("Error encrypting using RSA key!"); /* * For some strange reason, Microsoft CryptEncrypt using public * key, returns the cyphertext in backwards (little endian) * order, so reverse it! */ for(i = 0; i < (int)dlen; i++) data[i] = buf[dlen - i - 1]; /* make it big endian */ CryptDestroyKey(hRsaKey); free(buf); free(pBlob); }