Path: blob/main/crypto/krb5/src/windows/leashdll/timesync.c
34914 views
/* timesync stuff for leash - 7/28/94 - evanr */12#include <windows.h>3#include "leashdll.h"45#include <time.h>6#include <sys\timeb.h>7#include <stdlib.h>8#include <string.h>910#include <winsock2.h>1112#include <stdio.h>13#include "leasherr.h"14#include "leashids.h"1516int ProcessTimeSync(char *, int, char *);1718#define TM_OFFSET 22089888001920/* timezone.h has a winsock.h conflict */21struct timezone {22int tz_minuteswest;23int tz_dsttime;24};2526/************************************/27/* settimeofday(): */28/************************************/29int30settimeofday(31struct timeval *tv,32struct timezone *tz33)34{35SYSTEMTIME systime;36struct tm *newtime;3738newtime = gmtime((time_t *)&(tv->tv_sec));39systime.wYear = 1900+newtime->tm_year;40systime.wMonth = 1+newtime->tm_mon;41systime.wDay = newtime->tm_mday;42systime.wHour = newtime->tm_hour;43systime.wMinute = newtime->tm_min;44systime.wSecond = newtime->tm_sec;45systime.wMilliseconds = 0;46return SetSystemTime(&systime);47}4849/************************************/50/* gettimeofday(): */51/************************************/52int53gettimeofday(54struct timeval *tv,55struct timezone *tz56)57{58struct _timeb tb;59_tzset();60_ftime(&tb);61if (tv) {62tv->tv_sec = tb.time;63tv->tv_usec = tb.millitm * 1000;64}65if (tz) {66tz->tz_minuteswest = tb.timezone;67tz->tz_dsttime = tb.dstflag;68}69return 0;70}717273LONG74get_time_server_name(75char *timeServerName,76const char *valueName77)78{79HMODULE hmLeash;80char hostname[128];81char value[80];82DWORD dwType;83DWORD dwCount;84int check = 0;85HKEY hKey;86HKEY rKey1;87HKEY rKey2;88LONG lResult;89BOOL bEnv;9091memset(value, '\0', sizeof(value));92memset(hostname, '\0', sizeof(hostname));9394GetEnvironmentVariable("TIMEHOST", hostname, sizeof(hostname));95bEnv = (GetLastError() == ERROR_ENVVAR_NOT_FOUND);9697if (!(bEnv && hostname[0]))98{99// Check registry for TIMEHOST100rKey1 = HKEY_CURRENT_USER;101rKey2 = HKEY_LOCAL_MACHINE;102103for (check = 0; check < 2; check++)104{105if (ERROR_SUCCESS == RegOpenKeyEx(check == 0 ? rKey1 : rKey2,106"Software\\MIT\\Leash32\\Settings",1070, KEY_QUERY_VALUE, &hKey))108{109memset(value, '\0', sizeof(value));110lResult = RegQueryValueEx(hKey, (LPTSTR)valueName, NULL,111&dwType, NULL, &dwCount);112if (lResult == ERROR_SUCCESS)113{114lResult = RegQueryValueEx(hKey, (LPTSTR)valueName,115NULL, &dwType,116(LPTSTR)value, &dwCount);117if (lResult == ERROR_SUCCESS && *value)118{119// found120strcpy(hostname, value);121break;122}123}124}125}126127if (!*hostname)128{129// Check resource string for TIMEHOST130if ((hmLeash = GetModuleHandle(LEASH_DLL)) != NULL)131{132if (!LoadString(hmLeash, LSH_TIME_HOST, hostname,133sizeof(hostname)))134memset(hostname, '\0', sizeof(hostname));135}136}137if (!*hostname)138{139// OK, _Default_ it will be! :)140strcpy(hostname, "time");141}142}143strcpy(timeServerName, hostname);144return 0;145}146147/************************************/148/* Leash_timesync(): */149/************************************/150LONG Leash_timesync(int MessageP)151{152char tmpstr[2048];153char hostname[128];154int Port;155int rc;156struct servent *sp;157WORD wVersionRequested;158WSADATA wsaData;159char name[80];160161if (pkrb5_init_context == NULL)162return(0);163164wVersionRequested = 0x0101;165memset(name, '\0', sizeof(name));166memset(hostname, '\0', sizeof(hostname));167memset(tmpstr, '\0', sizeof(tmpstr));168169if ((rc = WSAStartup(wVersionRequested, &wsaData)))170{171wsprintf(tmpstr, "Couldn't initialize WinSock to synchronize time\n\rError Number: %d", rc);172WSACleanup();173return(LSH_BADWINSOCK);174}175176sp = getservbyname("time", "udp");177if (sp == 0)178Port = htons(IPPORT_TIMESERVER);179else180Port = sp->s_port;181182get_time_server_name(hostname, TIMEHOST);183184rc = ProcessTimeSync(hostname, Port, tmpstr);185186#ifdef USE_MESSAGE_BOX187if(MessageP != 0)188{189if (rc && !*tmpstr)190{191strcpy(tmpstr, "Unable to synchronize time!\n\n");192if (*hostname)193{194char tmpstr1[2048];195196memset(tmpstr1, '\0', sizeof(tmpstr1));197sprintf(tmpstr1, "Unreachable server: %s\n", hostname);198strcat(tmpstr, tmpstr1);199}200}201202MessageBox(NULL, tmpstr, "Time Server",203MB_ICONERROR | MB_OK);204}205#endif /* USE_MESSAGE_BOX */206WSACleanup();207return(rc);208}209210211/************************************/212/* ProcessTimeSync(): */213/************************************/214int ProcessTimeSync(char *hostname, int Port, char *tmpstr)215{216char buffer[512];217int cc;218long *nettime;219int s;220long hosttime;221struct hostent *host;222struct timeval tv;223struct timezone tz;224u_long argp;225struct sockaddr_in sin;226int i;227228if ((host = gethostbyname(hostname)) == NULL)229return(LSH_BADTIMESERV);230231sin.sin_port = (short)Port;232sin.sin_family = host->h_addrtype;233memcpy((struct sockaddr *)&sin.sin_addr, host->h_addr, host->h_length);234if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)235{236return(LSH_NOSOCKET);237}238239argp = 1;240if (ioctlsocket(s, FIONBIO, &argp) != 0)241{242closesocket(s);243return(LSH_NOCONNECT);244}245246if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)247{248closesocket(s);249return(LSH_NOCONNECT);250}251send(s, buffer, 40, 0);252if (gettimeofday (&tv, &tz) < 0)253{254closesocket(s);255return(LSH_GETTIMEOFDAY);256}257258for (i = 0; i < 4; i++)259{260if ((cc = recv(s, buffer, 512, 0)) > 0)261break;262Sleep(500);263}264if (i == 4)265{266closesocket(s);267return(LSH_RECVTIME);268}269270if (cc != 4)271{272closesocket(s);273return(LSH_RECVBYTES);274}275276nettime = (long *)buffer;277hosttime = (long) ntohl (*nettime) - TM_OFFSET;278(&tv)->tv_sec = hosttime;279if (settimeofday(&tv, &tz) < 0)280{281closesocket(s);282return(LSH_SETTIMEOFDAY);283}284285sprintf(tmpstr, "The time has been synchronized with the server: %s\n\n", hostname);286strcat(tmpstr, "To be able to use the Kerberos server, it was necessary to \nset the system time to: ") ;287strcat(tmpstr, ctime((time_t *)&hosttime));288strcat(tmpstr, "\n");289closesocket(s);290return(0);291}292293294