// Create new user object USER *NewUser(char *name, wchar_t *realname, wchar_t *note, UINT authtype, void *authdata) { USER *u; // Validate arguments if (name == NULL || realname == NULL || note == NULL) { return NULL; } if (authtype != AUTHTYPE_ANONYMOUS && authdata == NULL) { return NULL; } u = ZeroMalloc(sizeof(USER)); u->lock = NewLock(); u->ref = NewRef(); u->Name = CopyStr(name); u->RealName = CopyUniStr(realname); u->Note = CopyUniStr(note); u->GroupName = NULL; u->Group = NULL; u->AuthType = authtype; u->AuthData = authdata; u->CreatedTime = SystemTime64(); u->UpdatedTime = SystemTime64(); u->Policy = NULL; u->Traffic = NewTraffic(); return u; }
// Accept the SSTP connection bool AcceptSstp(CONNECTION *c) { SOCK *s; HTTP_HEADER *h; char date_str[MAX_SIZE]; bool ret; bool ret2 = false; SOCK_EVENT *se; // Validate arguments if (c == NULL) { return false; } s = c->FirstSock; GetHttpDateStr(date_str, sizeof(date_str), SystemTime64()); // Return a response h = NewHttpHeader("HTTP/1.1", "200", "OK"); AddHttpValue(h, NewHttpValue("Content-Length", "18446744073709551615")); AddHttpValue(h, NewHttpValue("Server", "Microsoft-HTTPAPI/2.0")); AddHttpValue(h, NewHttpValue("Date", date_str)); ret = PostHttp(s, h, NULL, 0); FreeHttpHeader(h); if (ret) { SetTimeout(s, INFINITE); se = NewSockEvent(); JoinSockToSockEvent(s, se); Debug("ProcessSstpHttps Start.\n"); ret2 = ProcessSstpHttps(c->Cedar, s, se); Debug("ProcessSstpHttps End.\n"); ReleaseSockEvent(se); } Disconnect(s); return ret2; }
// Create a new key void DCGenNewKey(UCHAR *key) { BUF *b; UINT64 tick; UCHAR hash[SHA1_SIZE]; UCHAR rand[SHA1_SIZE]; UINT i; // Validate arguments if (key == NULL) { return; } b = NewBuf(); Rand(rand, sizeof(rand)); WriteBuf(b, rand, sizeof(rand)); tick = TickHighres64(); WriteBufInt64(b, tick); tick = Tick64(); WriteBufInt64(b, tick); tick = SystemTime64(); WriteBufInt64(b, tick); GetCurrentMachineIpProcessHash(hash); WriteBuf(b, hash, sizeof(hash)); HashSha1(key, b->Buf, b->Size); Rand(rand, sizeof(rand)); for (i = 0;i < SHA1_SIZE;i++) { key[i] = key[i] ^ rand[i]; } FreeBuf(b); }
// Real-time clock measuring thread void Tick64Thread(THREAD *thread, void *param) { UINT n = 0; bool first = false; bool create_first_entry = true; UINT tick_span; // Validate arguments if (thread == NULL) { return; } #ifdef OS_WIN32 // Raise the priority of the Win32 thread MsSetThreadPriorityRealtime(); tick_span = TICK64_SPAN_WIN32; #else // OS_WIN32 // Raise the priority of a POSIX threads UnixSetThreadPriorityRealtime(); tick_span = TICK64_SPAN; #endif // OS_WIN32 while (true) { UINT tick; UINT64 tick64; #ifndef OS_WIN32 tick = TickRealtime(); // Get the current system clock if (tk64->LastTick > tick) { if ((tk64->LastTick - tick) >= (UINT64)0x0fffffff) { // The Tick has gone lap around tk64->RoundCount++; } else { // tick skewed (System administrator might change hardware clock) // Normally, the clock skew appears as sub-seconds error tick = tk64->LastTick; } } tk64->LastTick = tick; tick64 = (UINT64)tk64->RoundCount * (UINT64)4294967296LL + (UINT64)tick; Lock(tk64->TickLock); { if (tk64->TickStart == 0) { tk64->TickStart = tick64; } tick64 = tk64->Tick = tick64 - tk64->TickStart + (UINT64)1; } Unlock(tk64->TickLock); #else // OS_WIN32 tick64 = Win32FastTick64(); tick = (UINT)tick64; #endif // OS_WIN32 if (create_first_entry) { ADJUST_TIME *t = ZeroMalloc(sizeof(ADJUST_TIME)); t->Tick = tick64; t->Time = SystemTime64(); tk64->Tick64WithTime64 = tick64; tk64->Time64 = t->Time; Add(tk64->AdjustTime, t); // Notify the completion of the initialization NoticeThreadInit(thread); create_first_entry = false; } // Time correction n += tick_span; if (n >= 1000 || first == false) { UINT64 now = SystemTime64(); if (now < tk64->Time64 || Diff64((now - tk64->Time64) + tk64->Tick64WithTime64, tick64) >= tick_span) { ADJUST_TIME *t = ZeroMalloc(sizeof(ADJUST_TIME)); LockList(tk64->AdjustTime); { t->Tick = tick64; t->Time = now; Add(tk64->AdjustTime, t); Debug("Adjust Time: Tick = %I64u, Time = %I64u\n", t->Tick, t->Time); // To prevent consuming memory infinite on a system that clock is skewd if (LIST_NUM(tk64->AdjustTime) > MAX_ADJUST_TIME) { // Remove the second ADJUST_TIME *t2 = LIST_DATA(tk64->AdjustTime, 1); Delete(tk64->AdjustTime, t2); Debug("NUM_ADJUST TIME: %u\n", LIST_NUM(tk64->AdjustTime)); Free(t2); } } UnlockList(tk64->AdjustTime); tk64->Time64 = now; tk64->Tick64WithTime64 = tick64; } first = true; n = 0; } if (tk64->Halt) { break; } #ifdef OS_WIN32 Wait(halt_tick_event, tick_span); #else // OS_WIN32 SleepThread(tick_span); #endif // OS_WIN32 } }