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 \ dnsapi.lib \
rpcrt4.lib \ rpcrt4.lib \
user32.lib \ user32.lib \
Dbghelp.lib Dbghelp.lib \
strsafe.lib
AFSD_EXELIBS =\ AFSD_EXELIBS =\
$(DESTDIR)\lib\libosi.lib \ $(DESTDIR)\lib\libosi.lib \

View File

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