safe-strings-20040619

Begin to make use of the Microsoft strsafe library to avoid buffer
overruns.
This commit is contained in:
Jeffrey Altman 2004-06-19 16:18:58 +00:00 committed by Jeffrey Altman
parent b899b0ff57
commit 83d1e43ad5
2 changed files with 30 additions and 16 deletions

View File

@ -326,7 +326,8 @@ AFSD_SDKLIBS =\
dnsapi.lib \
rpcrt4.lib \
user32.lib \
Dbghelp.lib
Dbghelp.lib \
strsafe.lib
AFSD_EXELIBS =\
$(DESTDIR)\lib\libosi.lib \

View File

@ -17,6 +17,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <strsafe.h>
#include <osi.h>
#include "afsd.h"
@ -78,6 +79,10 @@ BOOL reportSessionStartups = FALSE;
cm_initparams_v1 cm_initParams;
char *cm_sysName = 0;
int cm_sysNameCount = 0;
char *cm_sysNameList[MAXNUMSYSNAMES];
/*
* AFSD Initialization Log
*
@ -111,21 +116,21 @@ afsi_start()
afsi_file = INVALID_HANDLE_VALUE;
if (getenv("TEMP"))
{
strcpy(wd, getenv("TEMP"));
StringCbCopyA(wd, sizeof(wd), getenv("TEMP"));
}
else
{
code = GetWindowsDirectory(wd, sizeof(wd));
if (code == 0) return;
}
strcat(wd, "\\afsd_init.log");
StringCbCatA(wd, sizeof(wd), "\\afsd_init.log");
GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
afsi_file = CreateFile(wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
SetFilePointer(afsi_file, 0, NULL, FILE_END);
GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, u, sizeof(u));
strcat(t, ": Create log file\n");
strcat(u, ": Created log file\n");
StringCbCatA(t, sizeof(t), ": Create log file\n");
StringCbCatA(u, sizeof(u), ": Created log file\n");
WriteFile(afsi_file, t, strlen(t), &zilch, NULL);
WriteFile(afsi_file, u, strlen(u), &zilch, NULL);
p = "PATH=";
@ -140,16 +145,16 @@ static int afsi_log_useTimestamp = 1;
void
afsi_log(char *pattern, ...)
{
char s[100], t[100], d[100], u[300];
char s[256], t[100], d[100], u[512];
int zilch;
va_list ap;
va_start(ap, pattern);
vsprintf(s, pattern, ap);
StringCbVPrintfA(s, sizeof(s), pattern, ap);
if ( afsi_log_useTimestamp ) {
GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, d, sizeof(d));
sprintf(u, "%s %s: %s\n", d, t, s);
StringCbPrintfA(u, sizeof(u), "%s %s: %s\n", d, t, s);
if (afsi_file != INVALID_HANDLE_VALUE)
WriteFile(afsi_file, u, strlen(u), &zilch, NULL);
#ifdef NOTSERVICE
@ -175,7 +180,7 @@ void afsd_ForceTrace(BOOL flush)
return;
len = GetTempPath(sizeof(buf)-10, buf);
strcpy(&buf[len], "/afsd.log");
StringCbCopyA(&buf[len], sizeof(buf)-len, "/afsd.log");
handle = CreateFile(buf, GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (handle == INVALID_HANDLE_VALUE) {
@ -214,6 +219,7 @@ int afsd_InitCM(char **reasonP)
/*int freelanceEnabled;*/
WSADATA WSAjunk;
lana_number_t lanaNum;
int i;
WSAStartup(0x0101, &WSAjunk);
@ -246,7 +252,7 @@ int afsd_InitCM(char **reasonP)
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, code, 0, (LPTSTR)&msgBuf, 0, NULL);
sprintf(buf,
StringCbPrintfA(buf, sizeof(buf),
"Failure in configuration while opening Registry: %s",
msgBuf);
osi_panic(buf, __FILE__, __LINE__);
@ -360,7 +366,7 @@ int afsd_InitCM(char **reasonP)
if (code == ERROR_SUCCESS)
afsi_log("Root volume %s", cm_rootVolumeName);
else {
strcpy(cm_rootVolumeName, "root.afs");
StringCbCopyA(cm_rootVolumeName, sizeof(cm_rootVolumeName), "root.afs");
afsi_log("Default root volume name root.afs");
}
@ -371,7 +377,7 @@ int afsd_InitCM(char **reasonP)
afsi_log("Mount root %s", cm_mountRoot);
cm_mountRootLen = strlen(cm_mountRoot);
} else {
strcpy(cm_mountRoot, "/afs");
StringCbCopyA(cm_mountRoot, sizeof(cm_mountRoot), "/afs");
cm_mountRootLen = 4;
/* Don't log */
}
@ -384,7 +390,7 @@ int afsd_InitCM(char **reasonP)
else {
GetWindowsDirectory(cm_CachePath, sizeof(cm_CachePath));
cm_CachePath[2] = 0; /* get drive letter only */
strcat(cm_CachePath, "\\AFSCache");
StringCbCatA(cm_CachePath, sizeof(cm_CachePath), "\\AFSCache");
afsi_log("Default cache path %s", cm_CachePath);
}
@ -410,15 +416,22 @@ int afsd_InitCM(char **reasonP)
/* Don't log */
}
dummyLen = sizeof(cm_sysName);
for ( i=0; i < MAXNUMSYSNAMES; i++ ) {
cm_sysNameList[i] = osi_Alloc(MAXSYSNAME);
cm_sysNameList[i][0] = '\0';
}
cm_sysName = cm_sysNameList[0];
dummyLen = MAXSYSNAME;
code = RegQueryValueEx(parmKey, "SysName", NULL, NULL,
cm_sysName, &dummyLen);
if (code == ERROR_SUCCESS)
afsi_log("Sys name %s", cm_sysName);
else {
strcat(cm_sysName, "i386_nt40");
StringCbCopyA(cm_sysName, MAXSYSNAME, "i386_nt40");
afsi_log("Default sys name %s", cm_sysName);
}
cm_sysNameCount = 1;
dummyLen = sizeof(cryptall);
code = RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
@ -545,7 +558,7 @@ int afsd_InitCM(char **reasonP)
afsi_log("Using >%s< as SMB server name", cm_NetbiosName);
} else {
/* something went horribly wrong. We can't proceed without a netbios name */
sprintf(buf,"Netbios name could not be determined: %li", code);
StringCbPrintfA(buf,sizeof(buf),"Netbios name could not be determined: %li", code);
osi_panic(buf, __FILE__, __LINE__);
}