void LWReraiseEx( LWException** dest, LWException** src, const char* file, unsigned int line ) { if (dest) { LWStackFrame* down = malloc(sizeof(*down)); if (!down) { LWHandle(src); *dest = CreateException(ERROR_OUTOFMEMORY, file, line, NULL, NULL); } else { *dest = *src; *src = NULL; *down = (*dest)->stack; (*dest)->stack.file = file; (*dest)->stack.line = line; (*dest)->stack.down = down; } } else { LWHandle(src); } }
DWORD DJIsValidComputerName( PCSTR pszComputerName, PBOOLEAN pbIsValid ) { DWORD ceError = ERROR_SUCCESS; LWException *exc = NULL; *pbIsValid = FALSE; DJCheckValidComputerName(pszComputerName, &exc); if(LW_IS_OK(exc)) { *pbIsValid = TRUE; } else { ceError = exc->code; LWHandle(&exc); } if (ceError == ERROR_INVALID_COMPUTERNAME || ceError == ERROR_INVALID_COMPUTERNAME) { ceError = ERROR_SUCCESS; } return ceError; }
DWORD DJSetComputerNameEx( PCSTR pszComputerName ) { DWORD dwError = 0; LWException *exc = NULL; HANDLE hLsaConnection = NULL; LW_TRY(&exc, DJSetComputerName(pszComputerName, NULL, &LW_EXC)); LW_CLEANUP_CTERR(&exc, LsaOpenServer(&hLsaConnection)); LW_CLEANUP_CTERR(&exc, LsaSetMachineName(hLsaConnection, pszComputerName)); cleanup: if (hLsaConnection) { LsaCloseServer(hLsaConnection); } if (!LW_IS_OK(exc)) { dwError = exc->code; LWHandle(&exc); } return dwError; }
DWORD DJJoinDomain( PCSTR pszDomain, PCSTR pszOU, PCSTR pszUsername, PCSTR pszPassword ) { DWORD dwError = 0; LWException *exc = NULL; JoinProcessOptions options; DJZeroJoinProcessOptions(&options); options.joiningDomain = TRUE; if (IsNullOrEmptyString(pszDomain)) { LW_RAISE(&exc, ERROR_INVALID_PARAMETER); } LW_CLEANUP_CTERR(&exc, CTStrdup(pszDomain, &options.domainName)); if (!IsNullOrEmptyString(pszOU)) { LW_CLEANUP_CTERR(&exc, CTStrdup(pszOU, &options.ouName)); } if (!IsNullOrEmptyString(pszUsername)) { LW_CLEANUP_CTERR(&exc, CTStrdup(pszUsername, &options.username)); } if (!IsNullOrEmptyString(pszPassword)) { LW_CLEANUP_CTERR(&exc, CTStrdup(pszPassword, &options.password)); } LW_CLEANUP_CTERR(&exc, DJGetComputerName(&options.computerName)); LW_TRY(&exc, DJInitModuleStates(&options, &LW_EXC)); LW_TRY(&exc, DJRunJoinProcess(&options, &LW_EXC)); cleanup: DJFreeJoinProcessOptions(&options); if (!LW_IS_OK(exc)) { dwError = exc->code; LWHandle(&exc); } return dwError; }
void LWReraise( LWException** dest, LWException** src ) { if (dest) { *dest = *src; *src = NULL; } else { LWHandle(src); } }
DWORD DJInit( VOID ) { DWORD dwError = 0; LWException *exc = NULL; setlocale(LC_ALL, ""); LW_CLEANUP_CTERR(&exc, dj_disable_logging()); cleanup: if (!LW_IS_OK(exc)) { dwError = exc->code; LWHandle(&exc); } return dwError; }
DWORD DJQueryJoinInformation( PSTR* ppszComputerName, PSTR* ppszDomainName, PSTR* ppszComputerDN ) { DWORD dwError = 0; LWException *exc = NULL; PSTR pszComputerName = NULL; PSTR pszDomainName = NULL; PSTR pszComputerDN = NULL; LW_TRY(&exc, DJQuery(&pszComputerName, &pszDomainName, NULL, &LW_EXC)); if (!IsNullOrEmptyString(pszDomainName)) { LW_TRY(&exc, DJGetComputerDN(&pszComputerDN, &LW_EXC)); } *ppszComputerName = pszComputerName; *ppszDomainName = pszDomainName; *ppszComputerDN = pszComputerDN; cleanup: if (!LW_IS_OK(exc)) { CT_SAFE_FREE_STRING(pszComputerName); CT_SAFE_FREE_STRING(pszDomainName); CT_SAFE_FREE_STRING(pszComputerDN); dwError = exc->code; LWHandle(&exc); } return dwError; }
int main( int argc, char* argv[] ) { LWException *exc = NULL; int columns; PSTR pszLogFilePath = NULL; BOOLEAN bNoLog = FALSE; PSTR logLevel = "warning"; DWORD dwLogLevel; BOOLEAN showHelp = FALSE; BOOLEAN showInternalHelp = FALSE; int remainingArgs = argc; char **argPos = argv; int i; BOOLEAN directoryExists = FALSE; if(CTGetTerminalWidth(fileno(stdout), &columns)) columns = -1; /* Skip the program name */ argPos++; remainingArgs--; setlocale(LC_ALL, ""); while(remainingArgs > 0 && CTStrStartsWith(argPos[0], "--")) { if(!strcmp(argPos[0], "--help")) showHelp = TRUE; else if(!strcmp(argPos[0], "--help-internal")) showInternalHelp = TRUE; else if(!strcmp(argPos[0], "--nolog")) bNoLog = TRUE; //All options after this point take an argument else if(remainingArgs < 2) showHelp = TRUE; else if(!strcmp(argPos[0], "--logfile")) { pszLogFilePath = (++argPos)[0]; remainingArgs--; } else if(!strcmp(argPos[0], "--loglevel")) { logLevel = (++argPos)[0]; remainingArgs--; } else break; remainingArgs--; argPos++; } if(remainingArgs < 1) showHelp = TRUE; if (showInternalHelp) { ShowUsageInternal(); goto cleanup; } if (showHelp) { ShowUsage(); goto cleanup; } if (!strcasecmp(logLevel, "error")) { dwLogLevel = LOG_LEVEL_ERROR; LwRtlLogSetLevel(LW_RTL_LOG_LEVEL_ERROR); } else if (!strcasecmp(logLevel, "warning")) { dwLogLevel = LOG_LEVEL_WARNING; LwRtlLogSetLevel(LW_RTL_LOG_LEVEL_WARNING); } else if (!strcasecmp(logLevel, "info")) { dwLogLevel = LOG_LEVEL_INFO; LwRtlLogSetLevel(LW_RTL_LOG_LEVEL_INFO); } else if (!strcasecmp(logLevel, "verbose")) { dwLogLevel = LOG_LEVEL_VERBOSE; LwRtlLogSetLevel(LW_RTL_LOG_LEVEL_VERBOSE); } else if (!strcasecmp(logLevel, "debug")) { dwLogLevel = LOG_LEVEL_VERBOSE; LwRtlLogSetLevel(LW_RTL_LOG_LEVEL_DEBUG); } else { LW_CLEANUP_CTERR(&exc, LW_ERROR_INVALID_LOG_LEVEL); } if (pszLogFilePath == NULL) { // Determine the default log path LW_CLEANUP_CTERR(&exc, CTCheckDirectoryExists("/var/log", &directoryExists)); if (directoryExists) { pszLogFilePath = "/var/log/domainjoin-cli.log"; } else { pszLogFilePath = "/var/adm/domainjoin-cli.log"; } } if (bNoLog) { LW_CLEANUP_CTERR(&exc, dj_disable_logging()); } else if (!strcmp(pszLogFilePath, ".")) { LW_CLEANUP_CTERR(&exc, dj_init_logging_to_console(dwLogLevel)); } else { DWORD ceError = dj_init_logging_to_file(dwLogLevel, pszLogFilePath); if(ceError == ERROR_ACCESS_DENIED) { fprintf(stderr, "Warning: insufficient permissions to log to %s. To enable logging, please specify a different filename with --logfile <file>.\n", pszLogFilePath); ceError = ERROR_SUCCESS; LW_CLEANUP_CTERR(&exc, dj_disable_logging()); } else if (ceError == ERROR_FILE_NOT_FOUND) { fprintf(stderr, "Warning: parent directory of log file %s does not exist. To enable logging, please specify a different filename with --logfile <file>.\n", pszLogFilePath); ceError = ERROR_SUCCESS; LW_CLEANUP_CTERR(&exc, dj_disable_logging()); } LW_CLEANUP_CTERR(&exc, ceError); } LwRtlLogSetCallback(RtlLogCallback, NULL); if (!strcmp(argPos[0], "join") || !strcmp(argPos[0], "leave")) { DJ_LOG_INFO("Domainjoin invoked with the %s command (remaining arguments will be printed later):", argPos[0]); // Only print up to the 'join' part for (i = 0; i <= argPos - argv; i++) { DJ_LOG_INFO(" [%s]", argv[i]); } } else { DJ_LOG_INFO("Domainjoin invoked with %d arg(s):", argc); for (i = 0; i < argc; i++) { DJ_LOG_INFO(" [%s]", argv[i]); } } if(!strcmp(argPos[0], "setname")) { PSTR pDomainSuffix = 0; argPos++; if(--remainingArgs != 1) { ShowUsage(); goto cleanup; } pDomainSuffix = strchr(argPos[0], '.'); if (pDomainSuffix) { *pDomainSuffix = 0; pDomainSuffix++; } else { pDomainSuffix = ""; } LW_TRY(&exc, DJSetComputerName(argPos[0], pDomainSuffix, &LW_EXC)); } else if(!strcmp(argPos[0], "join")) { argPos++; remainingArgs--; LW_TRY(&exc, DoJoin(remainingArgs, argPos, columns, &LW_EXC)); } else if(!strcmp(argPos[0], "leave")) { argPos++; remainingArgs--; LW_TRY(&exc, DoLeaveNew(remainingArgs, argPos, columns, &LW_EXC)); } else if(!strcmp(argPos[0], "query")) { LW_TRY(&exc, DoQuery(&LW_EXC)); } else if(!strcmp(argPos[0], "fixfqdn")) LW_TRY(&exc, DoFixFqdn(&LW_EXC)); #ifndef ENABLE_MINIMAL else if(!strcmp(argPos[0], "configure")) { argPos++; remainingArgs--; LW_TRY(&exc, DoConfigure(remainingArgs, argPos, &LW_EXC)); } #endif else if(!strcmp(argPos[0], "get_os_type") || !strcmp(argPos[0], "get_arch") || !strcmp(argPos[0], "get_distro") || !strcmp(argPos[0], "get_distro_version")) { LW_TRY(&exc, DoGetDistroInfo(remainingArgs, argPos, &LW_EXC)); } else { LW_RAISE(&exc, LW_ERROR_SHOW_USAGE); goto cleanup; } cleanup: if (!LW_IS_OK(exc) && exc->code == LW_ERROR_SHOW_USAGE) { ShowUsage(); LWHandle(&exc); } else if (!LW_IS_OK(exc)) { //Ignoring the return value from this because we can't do anything //if there is an error fprintf(stdout, "\n"); LWPrintException(stdout, exc, FALSE); DJLogException(LOG_LEVEL_ERROR, exc); LWHandle(&exc); dj_close_log(); return 1; } dj_close_log(); return 0; }