int main( int argc, char *argv[] ) { enum { UNSET, SHOW_HELP, CHECK_VERSION, INSTALL, UNINSTALL } mode = UNSET; PCSTR pSmbdPath = NULL; PSTR pFoundSmbdPath = NULL; DWORD error = 0; DWORD argIndex = 0; LW_RTL_LOG_LEVEL logLevel = LW_RTL_LOG_LEVEL_ERROR; PCSTR pErrorSymbol = NULL; PSTR pVersion = NULL; BOOLEAN smbdExists = FALSE; BOOLEAN force = FALSE; for (argIndex = 1; argIndex < argc; argIndex++) { if (!strcmp(argv[argIndex], "--check-version")) { if (mode == UNSET) { mode = CHECK_VERSION; } else { mode = SHOW_HELP; } } else if (!strcmp(argv[argIndex], "--install")) { if (mode == UNSET) { mode = INSTALL; } else { mode = SHOW_HELP; } } else if (!strcmp(argv[argIndex], "--uninstall")) { if (mode == UNSET) { mode = UNINSTALL; } else { mode = SHOW_HELP; } } else if (!strcmp(argv[argIndex], "--force")) { if (mode == INSTALL || mode== UNINSTALL) { force = TRUE; } else { mode = SHOW_HELP; } } else if (!strcmp(argv[argIndex], "--loglevel")) { argIndex++; if (argIndex >= argc) { error = ERROR_INVALID_PARAMETER; BAIL_ON_LSA_ERROR(error); } if (!strcmp(argv[argIndex], "error")) { logLevel = LW_RTL_LOG_LEVEL_ERROR; } else if (!strcmp(argv[argIndex], "warning")) { logLevel = LW_RTL_LOG_LEVEL_WARNING; } else if (!strcmp(argv[argIndex], "info")) { logLevel = LW_RTL_LOG_LEVEL_INFO; } else if (!strcmp(argv[argIndex], "verbose")) { logLevel = LW_RTL_LOG_LEVEL_VERBOSE; } else if (!strcmp(argv[argIndex], "debug")) { logLevel = LW_RTL_LOG_LEVEL_DEBUG; } else { error = ERROR_INVALID_PARAMETER; BAIL_ON_LSA_ERROR(error); } } else if (argIndex == argc - 1) { pSmbdPath = argv[argIndex]; } else { mode = SHOW_HELP; } } if (mode == UNSET || mode == SHOW_HELP) { ShowUsage(argv[0]); goto cleanup; } LwRtlLogSetCallback(LogCallback, NULL); LwRtlLogSetLevel(logLevel); if (pSmbdPath == NULL) { PCSTR pSearchPath = "/usr/sbin:/usr/local/sbin:/usr/local/samba/sbin:/opt/csw/samba/sbin:/opt/sfw/samba/sbin:/opt/csw/bin:/usr/local/bin"; error = FindFileInPath( "smbd", pSearchPath, &pFoundSmbdPath); if (error == ERROR_FILE_NOT_FOUND) { LW_RTL_LOG_ERROR("The smbd file could not be automatically found on your system. The search path was '%s'. Pass the correct location as the last argument to this program.", pSearchPath); } BAIL_ON_LSA_ERROR(error); pSmbdPath = pFoundSmbdPath; } error = LwCheckFileTypeExists( pSmbdPath, LWFILE_REGULAR, &smbdExists); BAIL_ON_LSA_ERROR(error); if (!smbdExists) { error = LwCheckFileTypeExists( pSmbdPath, LWFILE_SYMLINK, &smbdExists); BAIL_ON_LSA_ERROR(error); } if (!smbdExists) { LW_RTL_LOG_ERROR("Smbd file not found at path '%s'", pSmbdPath); } error = CheckSambaVersion(pSmbdPath, &pVersion); if (force == FALSE) { BAIL_ON_LSA_ERROR(error); } if (mode == CHECK_VERSION) { fprintf(stderr, "Samba version supported\n"); } else if (mode == INSTALL) { if (geteuid() != 0) { fprintf(stderr, "Please use the root account to install the Samba interop libraries\n"); goto cleanup; } error = InstallWbclient(pSmbdPath); BAIL_ON_LSA_ERROR(error); if (pVersion && strncmp(pVersion, "3.0.", sizeof("3.0.") - 1) == 0) { // Only Samba 3.0.x needs this error = InstallLwiCompat(pSmbdPath); BAIL_ON_LSA_ERROR(error); } error = SynchronizePassword( pSmbdPath); BAIL_ON_LSA_ERROR(error); fprintf(stderr, "Install successful\n"); } else if (mode == UNINSTALL) { if (geteuid() != 0) { fprintf(stderr, "Please use the root account to uninstall the Samba interop libraries\n"); goto cleanup; } error = UninstallWbclient(pSmbdPath); BAIL_ON_LSA_ERROR(error); error = UninstallLwiCompat(pSmbdPath); BAIL_ON_LSA_ERROR(error); error = DeletePassword( pSmbdPath); BAIL_ON_LSA_ERROR(error); fprintf(stderr, "Uninstall successful\n"); } else { fprintf(stderr, "Uninstall mode not implemented\n"); error = ERROR_INVALID_PARAMETER; BAIL_ON_LSA_ERROR(error); } cleanup: LW_SAFE_FREE_STRING(pFoundSmbdPath); LW_SAFE_FREE_STRING(pVersion); if (error) { pErrorSymbol = LwWin32ErrorToName(error); if (pErrorSymbol != NULL) { fprintf(stderr, "Error: %s\n", pErrorSymbol); } else { fprintf(stderr, "Unknown error\n"); } } return error; }
int main( int argc, char *argv[] ) { enum { UNSET, SHOW_HELP, CHECK_VERSION, INSTALL, UNINSTALL } mode = UNSET; PCSTR pSmbdPath = NULL; PSTR pFoundSmbdPath = NULL; DWORD error = 0; DWORD argIndex = 0; LW_RTL_LOG_LEVEL logLevel = LW_RTL_LOG_LEVEL_ERROR; PCSTR pErrorSymbol = NULL; for (argIndex = 1; argIndex < argc; argIndex++) { if (!strcmp(argv[argIndex], "--check-version")) { if (mode == UNSET) { mode = CHECK_VERSION; } else { mode = SHOW_HELP; } } else if (!strcmp(argv[argIndex], "--install")) { if (mode == UNSET) { mode = INSTALL; } else { mode = SHOW_HELP; } } else if (!strcmp(argv[argIndex], "--uninstall")) { if (mode == UNSET) { mode = UNINSTALL; } else { mode = SHOW_HELP; } } else if (!strcmp(argv[argIndex], "--loglevel")) { argIndex++; if (argIndex >= argc) { error = ERROR_INVALID_PARAMETER; BAIL_ON_LSA_ERROR(error); } if (!strcmp(argv[argIndex], "error")) { logLevel = LW_RTL_LOG_LEVEL_ERROR; } else if (!strcmp(argv[argIndex], "warning")) { logLevel = LW_RTL_LOG_LEVEL_WARNING; } else if (!strcmp(argv[argIndex], "info")) { logLevel = LW_RTL_LOG_LEVEL_INFO; } else if (!strcmp(argv[argIndex], "verbose")) { logLevel = LW_RTL_LOG_LEVEL_VERBOSE; } else if (!strcmp(argv[argIndex], "debug")) { logLevel = LW_RTL_LOG_LEVEL_DEBUG; } else { error = ERROR_INVALID_PARAMETER; BAIL_ON_LSA_ERROR(error); } } else if (argIndex == argc - 1) { pSmbdPath = argv[2]; } else { mode = SHOW_HELP; } } if (mode == UNSET || mode == SHOW_HELP) { ShowUsage(argv[0]); goto cleanup; } LwRtlLogSetCallback(LogCallback, NULL); LwRtlLogSetLevel(logLevel); if (pSmbdPath == NULL) { error = FindFileInPath( "smbd", "/usr/sbin", &pFoundSmbdPath); BAIL_ON_LSA_ERROR(error); pSmbdPath = pFoundSmbdPath; } error = CheckSambaVersion(pSmbdPath); BAIL_ON_LSA_ERROR(error); if (mode == CHECK_VERSION) { fprintf(stderr, "Samba version supported\n"); } else if (mode == INSTALL) { error = InstallWbclient(pSmbdPath); BAIL_ON_LSA_ERROR(error); error = InstallLwiCompat(pSmbdPath); BAIL_ON_LSA_ERROR(error); error = SynchronizePassword( pSmbdPath); BAIL_ON_LSA_ERROR(error); fprintf(stderr, "Install successful\n"); } else if (mode == UNINSTALL) { error = UninstallWbclient(pSmbdPath); BAIL_ON_LSA_ERROR(error); error = UninstallLwiCompat(pSmbdPath); BAIL_ON_LSA_ERROR(error); error = DeletePassword( pSmbdPath); BAIL_ON_LSA_ERROR(error); fprintf(stderr, "Uninstall successful\n"); } else { fprintf(stderr, "Uninstall mode not implemented\n"); error = ERROR_INVALID_PARAMETER; BAIL_ON_LSA_ERROR(error); } cleanup: LW_SAFE_FREE_STRING(pFoundSmbdPath); if (error) { pErrorSymbol = LwWin32ErrorToName(error); if (pErrorSymbol != NULL) { fprintf(stderr, "Error: %s\n", pErrorSymbol); } else { fprintf(stderr, "Unknown error\n"); } } return error; }
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; }
static void LogOptionCallback( poptContext poptContext, enum poptCallbackReason reason, const struct poptOption *option, const char *arg, void *data ) { switch (reason) { case POPT_CALLBACK_REASON_PRE: if (isatty(0)) { LwRtlLogSetCallback(LogToFile, NULL); } else { LwRtlLogSetCallback(LogToSyslog, NULL); } LwRtlLogSetLevel(LW_RTL_LOG_LEVEL_ERROR); break; case POPT_CALLBACK_REASON_OPTION: switch (option->val) { case LOG_OPTION_SYSLOG: { FILE *fp; LwRtlLogGetCallback(NULL, (LW_PVOID *) &fp); if (fp) { fclose(fp); } LwRtlLogSetCallback(LogToSyslog, NULL); break; } case LOG_OPTION_FILE: { FILE *fp; FILE *old_fp; fp = fopen(arg, "a"); if (fp == NULL) { fprintf(stderr, "Cannot open %s", arg); } LwRtlLogGetCallback(NULL, (LW_PVOID *) &old_fp); if (old_fp) { fclose(old_fp); } LwRtlLogSetCallback(LogToFile, fp); break; } case LOG_OPTION_LEVEL: { int i; for (i = 0; LogLevels[i].name != NULL; ++i) { if (!strcmp(LogLevels[i].name, arg)) { LwRtlLogSetLevel(LogLevels[i].rtlLogLevel); break; } } if (LogLevels[i].name == NULL) { fprintf(stderr, "unknown level %s\n", arg); exit(1); } break; } } break; case POPT_CALLBACK_REASON_POST: break; } }