admin.c revision 2d7e8507f5e77f2dc490708152058063edc313e5
894b27768c68091df4918b3219c91ed77d2d4054mcpowers/**
894b27768c68091df4918b3219c91ed77d2d4054mcpowers * The contents of this file are subject to the terms of the Common Development and
894b27768c68091df4918b3219c91ed77d2d4054mcpowers * Distribution License (the License). You may not use this file except in compliance with the
894b27768c68091df4918b3219c91ed77d2d4054mcpowers * License.
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwick *
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwick * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
894b27768c68091df4918b3219c91ed77d2d4054mcpowers * specific language governing permission and limitations under the License.
894b27768c68091df4918b3219c91ed77d2d4054mcpowers *
894b27768c68091df4918b3219c91ed77d2d4054mcpowers * When distributing Covered Software, include this CDDL Header Notice in each file and include
894b27768c68091df4918b3219c91ed77d2d4054mcpowers * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
894b27768c68091df4918b3219c91ed77d2d4054mcpowers * Header, with the fields enclosed by brackets [] replaced by your own identifying
894b27768c68091df4918b3219c91ed77d2d4054mcpowers * information: "Portions copyright [year] [name of copyright owner]".
894b27768c68091df4918b3219c91ed77d2d4054mcpowers *
894b27768c68091df4918b3219c91ed77d2d4054mcpowers * Copyright 2014 - 2015 ForgeRock AS.
894b27768c68091df4918b3219c91ed77d2d4054mcpowers */
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#include "platform.h"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#include "am.h"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#include "utility.h"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#include "net_client.h"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#include "list.h"
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwick#include "version.h"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#include "zip.h"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#ifdef _WIN32
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define LIB_FILE_EXT "dll"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define APACHE_DEFAULT_CONF_FILE "c:\\Apache\\conf\\httpd.conf"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#else
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define LIB_FILE_EXT "so"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define APACHE_DEFAULT_CONF_FILE "/opt/apache/conf/httpd.conf"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#endif
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#ifdef AM_BINARY_LICENSE
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define LICENSE_FILE ".."FILE_PATH_SEP"legal"FILE_PATH_SEP"license.txt"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#else
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define LICENSE_FILE ".."FILE_PATH_SEP"legal"FILE_PATH_SEP"CDDLv1.0.txt"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#endif
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers/*configuration template patterns*/
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define AM_INSTALL_OPENAMURL "AM_OPENAM_URL"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define AM_INSTALL_REALM "AM_AGENT_REALM"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define AM_INSTALL_AGENTURL "AM_AGENT_URL"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define AM_INSTALL_AGENT "AM_AGENT_NAME"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define AM_INSTALL_PASSWORD "AM_AGENT_PASSWORD"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define AM_INSTALL_KEY "AM_AGENT_KEY"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define AM_INSTALL_DEBUGPATH "AM_DEBUG_FILE_PATH"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define AM_INSTALL_AUDITPATH "AM_AUDIT_FILE_PATH"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define AM_INSTALL_AGENT_FQDN "AM_AGENT_FQDN"
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowerstypedef void (*param_handler)(int, char **);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstruct command_line {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers const char* option;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers param_handler handler;
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwick};
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstruct am_conf_entry {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char name[AM_PATH_SIZE];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char path[AM_PATH_SIZE];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char web[AM_PATH_SIZE];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers struct am_conf_entry *next;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers};
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersenum {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers AM_I_UNKNOWN = 0,
894b27768c68091df4918b3219c91ed77d2d4054mcpowers AM_I_APACHE,
894b27768c68091df4918b3219c91ed77d2d4054mcpowers AM_I_IIS,
894b27768c68091df4918b3219c91ed77d2d4054mcpowers AM_I_VARNISH
894b27768c68091df4918b3219c91ed77d2d4054mcpowers};
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers/* forward declarations (IIS specific) */
894b27768c68091df4918b3219c91ed77d2d4054mcpowersvoid list_iis_sites(int, char **);
894b27768c68091df4918b3219c91ed77d2d4054mcpowersint enable_module(const char *, const char *);
894b27768c68091df4918b3219c91ed77d2d4054mcpowersint disable_module(const char *, const char *);
894b27768c68091df4918b3219c91ed77d2d4054mcpowersint test_module(const char *);
894b27768c68091df4918b3219c91ed77d2d4054mcpowersint install_module(const char *, const char *);
894b27768c68091df4918b3219c91ed77d2d4054mcpowersint remove_module();
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic const char *am_container_str(int v) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers switch (v) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers case AM_I_APACHE: return "Apache";
894b27768c68091df4918b3219c91ed77d2d4054mcpowers case AM_I_IIS: return "IIS";
894b27768c68091df4918b3219c91ed77d2d4054mcpowers case AM_I_VARNISH: return "Varnish";
894b27768c68091df4918b3219c91ed77d2d4054mcpowers default: return "unknown";
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers}
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic int instance_type = AM_I_UNKNOWN;
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic char app_path[AM_URI_SIZE];
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic char log_path[AM_URI_SIZE];
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic char lic_path[AM_URI_SIZE];
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwickstatic char instance_path[AM_URI_SIZE];
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic char instance_config[AM_URI_SIZE];
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic char config_template[AM_URI_SIZE];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic void install_log(const char *format, ...) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char ts[64];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers struct tm now;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers FILE *f = fopen(log_path, "a+");
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#ifdef _WIN32
894b27768c68091df4918b3219c91ed77d2d4054mcpowers time_t tv;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers time(&tv);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers localtime_s(&now, &tv);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#else
894b27768c68091df4918b3219c91ed77d2d4054mcpowers struct timeval tv;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers gettimeofday(&tv, NULL);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers localtime_r(&tv.tv_sec, &now);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#endif
894b27768c68091df4918b3219c91ed77d2d4054mcpowers strftime(ts, sizeof (ts) - 1, "%Y-%m-%d %H:%M:%S", &now);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (f != NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers va_list args;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(f, "%s ", ts);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers va_start(args, format);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers vfprintf(f, format, args);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers va_end(args);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(f, "\n");
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fclose(f);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers}
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#ifdef _WIN32
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic DWORD old_mode;
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic HANDLE cons_handle;
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic BOOL exit_handler(DWORD s)
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#else
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic void exit_handler(int s)
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#endif
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwick{
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("installation aborted");
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(stdout, "\n");
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#ifdef _WIN32
894b27768c68091df4918b3219c91ed77d2d4054mcpowers switch (s) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers case CTRL_BREAK_EVENT:
894b27768c68091df4918b3219c91ed77d2d4054mcpowers case CTRL_C_EVENT:
894b27768c68091df4918b3219c91ed77d2d4054mcpowers {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fflush(stdout);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers SetConsoleMode(cons_handle, old_mode);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers ExitProcess(-1);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers default: break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers return TRUE;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#else
894b27768c68091df4918b3219c91ed77d2d4054mcpowers exit(1);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#endif
894b27768c68091df4918b3219c91ed77d2d4054mcpowers}
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic char *prompt_and_read(const char *p) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char *r;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers printf("%s ", p);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers#define USER_INPUT_BUFFER_SIZE 256
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if ((r = malloc(USER_INPUT_BUFFER_SIZE + 1)) == NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(stderr, "error: out of memory\n");
894b27768c68091df4918b3219c91ed77d2d4054mcpowers exit(1);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (fgets(r, USER_INPUT_BUFFER_SIZE, stdin) == NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers free(r);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers return NULL;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers trim(r, '\n');
894b27768c68091df4918b3219c91ed77d2d4054mcpowers trim(r, '\r');
894b27768c68091df4918b3219c91ed77d2d4054mcpowers trim(r, ' ');
894b27768c68091df4918b3219c91ed77d2d4054mcpowers return r;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers}
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic void password_decrypt(int argc, char **argv) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (argc == 4) {
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwick char *key = argv[2];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char *password = strdup(argv[3]);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (decrypt_password(key, &password) > 0) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(stdout, "\nPassword value: %s\n\n", password);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_free(password);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers}
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic void password_encrypt(int argc, char **argv) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (argc == 4) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char *key = argv[2];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char *password = strdup(argv[3]);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (encrypt_password(key, &password) > 0) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(stdout, "\nEncrypted password value: %s\n\n", password);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers } else {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(stdout, "\nError encrypting password - invalid arguments.\n\n");
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_free(password);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers}
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic void generate_key(int argc, char **argv) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char *encoded = NULL, key[37];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers size_t sz = 16; /*limit the number of random characters in a key*/
894b27768c68091df4918b3219c91ed77d2d4054mcpowers uuid(key, sizeof (key));
894b27768c68091df4918b3219c91ed77d2d4054mcpowers encoded = base64_encode(key, &sz);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(stdout, "\nEncryption key value: %s\n\n", encoded);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_free(encoded);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers}
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic void show_version(int argc, char **argv) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(stdout, "\n%s\n", DESCRIPTION);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(stdout, " Version: %s\n", VERSION);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(stdout, " %s\n", VERSION_VCS);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(stdout, " Build machine: %s\n", BUILD_MACHINE);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fprintf(stdout, " Build date: %s %s\n\n", __DATE__, __TIME__);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers}
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwickstatic int am_read_instances(const char *path, struct am_conf_entry **list) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers int ret = 0;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char buff[AM_PATH_SIZE * 3];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char a[AM_PATH_SIZE], b[AM_PATH_SIZE], c[AM_PATH_SIZE];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers FILE *fin = fopen(path, "r");
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (fin != NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers while (fgets(buff, (AM_PATH_SIZE * 3), fin)) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (buff[0] == '#' || buff[0] == '\n') {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers continue;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (sscanf(buff, "%s %s %s", a, b, c) == 3) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers struct am_conf_entry *e = malloc(sizeof (struct am_conf_entry));
894b27768c68091df4918b3219c91ed77d2d4054mcpowers strncpy(e->name, a, sizeof (e->name) - 1);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers strncpy(e->path, b, sizeof (e->path) - 1);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers strncpy(e->web, c, sizeof (e->web) - 1);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers e->next = NULL;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers AM_LIST_INSERT(*list, e);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers ret++;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fclose(fin);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers } else {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers ret = AM_FILE_ERROR;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers return ret;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers}
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic int am_cleanup_instance(const char *pth, const char *name) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers int ret = AM_EINVAL;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char *p1 = NULL;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char buff[AM_PATH_SIZE * 3];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char key[AM_PATH_SIZE];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (pth != NULL && name != NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers FILE *fout, *fin = fopen(pth, "r");
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (fin != NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_asprintf(&p1, "%s_edit", pth);
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwick if (p1 == NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fclose(fin);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers return AM_ENOMEM;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fout = fopen(p1, "w");
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (fout != NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers /*configuration line begins with an instance name followed by a space*/
894b27768c68091df4918b3219c91ed77d2d4054mcpowers snprintf(key, sizeof (key),
894b27768c68091df4918b3219c91ed77d2d4054mcpowers strstr(pth, ".agents") != NULL ? "%s " : "%s", name);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers while (fgets(buff, AM_PATH_SIZE * 3, fin)) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (strstr(buff, key) == NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fputs(buff, fout);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fclose(fout);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers } else {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers ret = AM_FILE_ERROR;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers fclose(fin);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (copy_file(p1, pth) == AM_SUCCESS) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_delete_file(p1);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers free(p1);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers } else {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers ret = AM_FILE_ERROR;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers return ret;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers}
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowersstatic int create_agent_instance(int status, const char *web_conf_path, const char *openam_url,
894b27768c68091df4918b3219c91ed77d2d4054mcpowers const char *agent_realm, const char *agent_url, const char *agent_user, const char *agent_password) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers FILE *f = NULL;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers int rv = AM_ERROR;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char *created_name_path = NULL, *created_name_simple = NULL;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char *agent_conf_template = NULL;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (am_create_agent_dir(FILE_PATH_SEP, instance_path,
894b27768c68091df4918b3219c91ed77d2d4054mcpowers &created_name_path, &created_name_simple) == 0) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("agent instance configuration directories created");
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers size_t agent_conf_template_sz = 0;
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwick
894b27768c68091df4918b3219c91ed77d2d4054mcpowers /*create agent configuration file (from a template)*/
894b27768c68091df4918b3219c91ed77d2d4054mcpowers agent_conf_template = load_file(config_template, &agent_conf_template_sz);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (agent_conf_template != NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char *log_path = NULL, *audit_log_path = NULL, *conf_file_path = NULL;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = AM_SUCCESS;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_asprintf(&conf_file_path, "%s"FILE_PATH_SEP"config"FILE_PATH_SEP"agent.conf", created_name_path);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_asprintf(&log_path, "%s"FILE_PATH_SEP"logs"FILE_PATH_SEP"debug"FILE_PATH_SEP, created_name_path);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_asprintf(&audit_log_path, "%s"FILE_PATH_SEP"logs"FILE_PATH_SEP"audit"FILE_PATH_SEP, created_name_path);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers do {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers struct url u;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers char *encoded, *password, key[37];
894b27768c68091df4918b3219c91ed77d2d4054mcpowers size_t sz = 16;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (log_path == NULL || audit_log_path == NULL || conf_file_path == NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = AM_ENOMEM;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers /*do a search-n-replace (in memory)*/
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("updating %s", AM_INSTALL_OPENAMURL);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = string_replace(&agent_conf_template, AM_INSTALL_OPENAMURL, openam_url, &agent_conf_template_sz);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (rv != AM_SUCCESS) break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = parse_url(agent_url, &u);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (rv != AM_SUCCESS) break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("updating %s %s", AM_INSTALL_AGENT_FQDN, u.host);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = string_replace(&agent_conf_template, AM_INSTALL_AGENT_FQDN, u.host, &agent_conf_template_sz);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (rv != AM_SUCCESS) break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("updating %s", AM_INSTALL_REALM);
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwick rv = string_replace(&agent_conf_template, AM_INSTALL_REALM, agent_realm, &agent_conf_template_sz);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (rv != AM_SUCCESS) break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("updating %s", AM_INSTALL_AGENTURL);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = string_replace(&agent_conf_template, AM_INSTALL_AGENTURL, agent_url, &agent_conf_template_sz);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (rv != AM_SUCCESS) break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("updating %s", AM_INSTALL_AGENTURL);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = string_replace(&agent_conf_template, AM_INSTALL_AGENT, agent_user, &agent_conf_template_sz);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (rv != AM_SUCCESS) break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers uuid(key, sizeof (key));
894b27768c68091df4918b3219c91ed77d2d4054mcpowers encoded = base64_encode(key, &sz);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("updating %s", AM_INSTALL_KEY);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = string_replace(&agent_conf_template, AM_INSTALL_KEY, encoded, &agent_conf_template_sz);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (rv != AM_SUCCESS) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_free(encoded);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers password = strdup(agent_password);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (password == NULL) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = AM_ENOMEM;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_free(encoded);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (encrypt_password(encoded, &password) > 0) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("updating %s", AM_INSTALL_PASSWORD);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = string_replace(&agent_conf_template, AM_INSTALL_PASSWORD, password, &agent_conf_template_sz);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_free(password);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers am_free(encoded);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (rv != AM_SUCCESS) break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("updating %s", AM_INSTALL_DEBUGPATH);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = string_replace(&agent_conf_template, AM_INSTALL_DEBUGPATH, log_path, &agent_conf_template_sz);
9b009fc1b553084f6003dcd46b171890049de0ffValerie Bubb Fenwick if (rv != AM_SUCCESS) break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("updating %s", AM_INSTALL_AUDITPATH);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = string_replace(&agent_conf_template, AM_INSTALL_AUDITPATH, audit_log_path, &agent_conf_template_sz);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (rv != AM_SUCCESS) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("failed to update agent configuration template file %s (%s)",
894b27768c68091df4918b3219c91ed77d2d4054mcpowers config_template, am_strerror(rv));
894b27768c68091df4918b3219c91ed77d2d4054mcpowers break;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers /*write an updated template to the agent configuration file*/
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("writing configuration to %s", conf_file_path);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers if (write_file(conf_file_path, agent_conf_template, agent_conf_template_sz) > 0) {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = AM_SUCCESS;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers } else {
894b27768c68091df4918b3219c91ed77d2d4054mcpowers install_log("failed to write agent configuration to %s", conf_file_path);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers rv = AM_FILE_ERROR;
894b27768c68091df4918b3219c91ed77d2d4054mcpowers }
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
894b27768c68091df4918b3219c91ed77d2d4054mcpowers } while (0);
894b27768c68091df4918b3219c91ed77d2d4054mcpowers
AM_FREE(agent_conf_template, conf_file_path, log_path, audit_log_path);
} else {
install_log("failed to open agent configuration template file %s", config_template);
rv = AM_ENOMEM;
}
if (rv == AM_SUCCESS) {
/*update installer (instance) configuration*/
f = fopen(instance_config, "a");
if (f != NULL) {
fprintf(f, "%s %s %s\n", created_name_simple, created_name_path, web_conf_path);
fclose(f);
install_log("agent instance configuration updated");
} else {
install_log("failed to update agent instance configuration file %s", instance_config);
rv = AM_FILE_ERROR;
}
}
/*container specific updates*/
switch (instance_type) {
case AM_I_APACHE:
{
if (rv == AM_SUCCESS && copy_file(web_conf_path, NULL) == AM_SUCCESS) {
/*update Apache httpd.conf (global context only)*/
f = fopen(web_conf_path, "a");
if (f != NULL) {
fprintf(f, "\n\nLoadModule amagent_module %s.."FILE_PATH_SEP"lib"FILE_PATH_SEP"mod_openam."LIB_FILE_EXT"\n"
"AmAgent On\n"
"AmAgentConf %s"FILE_PATH_SEP"config"FILE_PATH_SEP"agent.conf\n\n",
app_path, created_name_path);
fclose(f);
install_log("webserver configuration %s updated", web_conf_path);
}
} else {
install_log("failed to create a backup copy of %s", web_conf_path);
rv = AM_FILE_ERROR;
}
}
break;
case AM_I_IIS:
{
if (rv == AM_SUCCESS && status == 0) {
char schema_file[AM_URI_SIZE];
char lib_file[AM_URI_SIZE];
snprintf(schema_file, sizeof (schema_file),
"%s.."FILE_PATH_SEP"config"FILE_PATH_SEP"mod_iis_openam_schema.xml",
app_path);
snprintf(lib_file, sizeof (lib_file),
"%s.."FILE_PATH_SEP"lib"FILE_PATH_SEP"mod_iis_openam."LIB_FILE_EXT,
app_path);
/*need to add module to global configuration first*/
if (install_module(lib_file, schema_file) == 0) {
rv = AM_ERROR;
} else {
install_log("webserver site global configuration updated");
}
}
if (rv == AM_SUCCESS) {
char iis_instc_file[AM_URI_SIZE];
snprintf(iis_instc_file, sizeof (iis_instc_file),
"%s"FILE_PATH_SEP"config"FILE_PATH_SEP"agent.conf",
created_name_path);
/*module is already loaded in global configuration*/
if (enable_module(web_conf_path, iis_instc_file) == 0) {
rv = AM_ERROR;
} else {
install_log("webserver site %s configuration updated", web_conf_path);
}
}
}
break;
case AM_I_VARNISH:
{
//TODO
}
break;
default:
install_log("unknown installation instance type %d", instance_type);
break;
}
/*delete agent configuration directory in case of an error*/
if (rv != AM_SUCCESS) {
install_log("cleaning up agent configuration and directory %s", created_name_path);
am_delete_directory(created_name_path);
am_cleanup_instance(instance_config, created_name_simple);
}
} else {
install_log("failed to create agent instance configuration directories");
}
AM_FREE(created_name_path, created_name_simple);
return rv;
}
static void install_interactive(int argc, char **argv) {
int rv;
int iis_status = 0;
char lic_accepted = AM_FALSE, validated = AM_FALSE;
char *input = NULL;
char *agent_token = NULL;
char lic_file_path[AM_URI_SIZE];
char apache_conf[AM_URI_SIZE];
char openam_url[AM_URI_SIZE];
char agent_realm[AM_URI_SIZE];
char agent_url[AM_URI_SIZE];
char agent_user[AM_URI_SIZE];
char agent_password[AM_URI_SIZE];
/*set up console signal handler*/
#ifdef _WIN32
DWORD new_mode;
cons_handle = GetStdHandle(STD_INPUT_HANDLE);
if (cons_handle == INVALID_HANDLE_VALUE) {
fprintf(stderr, "Failed to get console handle (%d). Exiting.\n", GetLastError());
return;
}
if (!GetConsoleMode(cons_handle, &old_mode)) {
fprintf(stderr, "Failed to get console mode (%d). Exiting.\n", GetLastError());
return;
}
new_mode = old_mode;
new_mode |= (ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
if (!SetConsoleMode(cons_handle, new_mode)) {
fprintf(stderr, "Failed to set console mode (%d). Exiting.\n", GetLastError());
return;
}
SetConsoleCtrlHandler((PHANDLER_ROUTINE) exit_handler, TRUE);
#else
struct sigaction sa;
sa.sa_handler = exit_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
#endif
memset(&apache_conf[0], 0, sizeof (apache_conf));
memset(&openam_url[0], 0, sizeof (openam_url));
memset(&agent_url[0], 0, sizeof (agent_url));
memset(&agent_user[0], 0, sizeof (agent_user));
memset(&agent_realm[0], 0, sizeof (agent_realm));
memset(&agent_password[0], 0, sizeof (agent_password));
snprintf(lic_file_path, sizeof (lic_file_path), "%s%s", app_path, LICENSE_FILE);
install_log("%s for %s Server interactive installation", DESCRIPTION,
am_container_str(instance_type));
if (!file_exists(lic_path)) {
/*display a license*/
char *lic_data = load_file(lic_file_path, NULL);
if (lic_data != NULL) {
fprintf(stdout, "Please read the following License Agreement carefully:\n\n%s\n", lic_data);
free(lic_data);
}
input = prompt_and_read("Do you completely agree with all the terms and conditions \n"
"of this License Agreement (yes/no): [no]:");
if (ISVALID(input) && strcasecmp(input, "yes") == 0) {
install_log("license accepted");
lic_accepted = AM_TRUE;
am_make_path(instance_path);
write_file(lic_path, AM_SPACE_CHAR, 1);
}
am_free(input);
} else {
lic_accepted = AM_TRUE;
install_log("license was accepted earlier");
}
if (!lic_accepted) {
install_log("license was not accepted");
fprintf(stdout, "\nYou need to accept the License terms and conditions to continue.\n");
exit(1);
}
fprintf(stdout, "\n%s for %s Server interactive installation.\n\n", DESCRIPTION,
am_container_str(instance_type));
for (;;) {
switch (instance_type) {
case AM_I_APACHE:
{
char *conf = NULL;
char err = AM_TRUE;
/*Apache HTTPD specific*/
input = prompt_and_read("\nEnter the complete path to the httpd.conf file which is used by Apache HTTPD\n"
"Server to store its configuration.\n"
"[ q or 'ctrl+c' to exit ]\n"
"Configuration file ["APACHE_DEFAULT_CONF_FILE"]:");
if (ISVALID(input) && strcasecmp(input, "q") == 0) {
free(input);
install_log("installation exit");
exit(1);
}
if (!ISVALID(input)) {
am_free(input);
input = strdup(APACHE_DEFAULT_CONF_FILE);
if (input == NULL) {
install_log("installation exit (memory allocation error)");
exit(1);
}
}
conf = load_file(input, NULL);
if (conf != NULL) {
if (strstr(conf, "LoadModule amagent_module") != NULL && strstr(conf, "#LoadModule amagent_module") == NULL) {
fprintf(stdout, "\nError: this server instance is already configured with %s module.\nPlease try again.\n\n", DESCRIPTION);
install_log("server instance %s is already configured with %s",
input, DESCRIPTION);
} else if (strstr(conf, "LoadModule") == NULL) {
fprintf(stdout, "\nError: invalid server configuration file %s.]\nPlease try again.\n\n",
input);
install_log("could not locate LoadModule configuration directive in %s", input);
} else {
strncpy(apache_conf, input, sizeof (apache_conf) - 1);
install_log("server configuration file %s", apache_conf);
free(input);
input = NULL;
err = AM_FALSE;
}
free(conf);
} else {
fprintf(stdout, "\nError: unable to load the server configuration file %s.\nPlease try again.\n\n",
input);
install_log("unable to load server configuration file %s", input);
}
if (err) {
am_free(input);
continue;
}
}
break;
case AM_I_IIS:
{
iis_status = 0;
/* IIS specific */
list_iis_sites(argc, argv);
input = prompt_and_read("\nEnter IIS Server Site identification number.\n"
"[ q or 'ctrl+c' to exit ]\n"
"Site id:");
if (input == NULL || (ISVALID(input) && strcasecmp(input, "q") == 0)) {
free(input);
install_log("installation exit");
exit(1);
}
iis_status = test_module(input);
if (iis_status == ADMIN_IIS_MOD_LOCAL) {
fprintf(stdout, "\nError: this server site is already configured with %s module.\nPlease try again.\n\n", DESCRIPTION);
install_log("IIS server site %s is already configured with %s",
NOTNULL(input), DESCRIPTION);
free(input);
continue;
} else {
install_log("IIS server site %s is not yet configured with %s (status: %d)",
NOTNULL(input), DESCRIPTION, iis_status);
}
strncpy(apache_conf, input, sizeof (apache_conf) - 1);
free(input);
}
break;
case AM_I_VARNISH:
{
fprintf(stdout, "Error: %s installation type not supported yet. Exiting.\n",
am_container_str(instance_type));
install_log("unknown installation type");
exit(1);
}
default:
{
fprintf(stdout, "Error: unknown installation type. Exiting.\n");
install_log("unknown installation type");
exit(1);
}
}
break;
}
for (;;) {
input = prompt_and_read("\nEnter the URL where the OpenAM server is running. Please include the\n"
"deployment URI also as shown below:\n"
"(http://openam.sample.com:58080/openam)\n"
"[ q or 'ctrl+c' to exit ]\n"
"OpenAM server URL:");
if (ISVALID(input) && strcasecmp(input, "q") == 0) {
free(input);
install_log("installation exit");
exit(1);
}
if (ISVALID(input)) {
strncpy(openam_url, input, sizeof (openam_url) - 1);
install_log("OpenAM URL %s", openam_url);
}
am_free(input);
input = prompt_and_read("\nEnter the Agent URL as shown below:\n"
"(http://agent.sample.com:1234)\n"
"[ q or 'ctrl+c' to exit ]\n"
"Agent URL:");
if (ISVALID(input) && strcasecmp(input, "q") == 0) {
free(input);
install_log("installation exit");
exit(1);
}
if (ISVALID(input)) {
strncpy(agent_url, input, sizeof (agent_url) - 1);
install_log("Agent URL %s", agent_url);
}
am_free(input);
input = prompt_and_read("\nEnter the Agent profile name\n"
"[ q or 'ctrl+c' to exit ]\n"
"Agent Profile name:");
if (ISVALID(input) && strcasecmp(input, "q") == 0) {
free(input);
install_log("installation exit");
exit(1);
}
if (ISVALID(input)) {
strncpy(agent_user, input, sizeof (agent_user) - 1);
install_log("Agent Profile name %s", agent_user);
}
am_free(input);
input = prompt_and_read("\nEnter the Agent realm/organization\n"
"[ q or 'ctrl+c' to exit ]\n"
"Agent realm/organization name: [/]:");
if (ISVALID(input) && strcasecmp(input, "q") == 0) {
free(input);
install_log("installation exit");
exit(1);
}
if (ISVALID(input)) {
strncpy(agent_realm, input, sizeof (agent_realm) - 1);
install_log("Agent realm/organization name %s", agent_realm);
} else {
strncpy(agent_realm, "/", sizeof (agent_realm) - 1);
install_log("Agent realm/organization name %s", "/");
}
am_free(input);
input = prompt_and_read("\nEnter the path to a file that contains the password to be used\n"
"for identifying the Agent\n"
"[ q or 'ctrl+c' to exit ]\n"
"The path to the password file:");
if (ISVALID(input) && strcasecmp(input, "q") == 0) {
free(input);
install_log("installation exit");
exit(1);
}
if (ISVALID(input)) {
char *password_data = load_file(input, NULL);
install_log("Agent password file %s", input);
if (password_data != NULL) {
trim(password_data, '\n');
trim(password_data, '\r');
install_log("agent password file %s opened successfully", input);
strncpy(agent_password, password_data, sizeof (agent_password) - 1);
free(password_data);
} else {
install_log("unable to open password file %s", LOGEMPTY(input));
}
}
fprintf(stdout, "\nInstallation parameters:\n\n"
" OpenAM URL: %s\n"
" Agent URL: %s\n"
" Agent Profile name: %s\n"
" Agent realm/organization name: %s\n"
" Agent Profile password file: %s\n\n",
openam_url, agent_url, agent_user, agent_realm, NOTNULL(input));
am_free(input);
input = prompt_and_read("Confirm configuration (yes/no): [no]:");
if (!(ISVALID(input) && strcasecmp(input, "yes") == 0)) {
am_free(input);
fprintf(stdout, "\nRestarting the configuration...\n");
install_log("installation restarted");
continue;
}
break;
}
am_free(input);
install_log("validating configuration parameters...");
fprintf(stdout, "\nValidating...\n");
am_net_init();
rv = am_agent_login(0, openam_url, NULL,
agent_user, agent_password, agent_realm, AM_TRUE, NULL,
&agent_token, NULL, NULL, NULL, install_log);
if (rv != AM_SUCCESS) {
fprintf(stdout, "\nError validating OpenAM - Agent configuration.\n"
"See installation log %s file for more details. Exiting.\n", log_path);
install_log("error validating OpenAM agent configuration");
} else {
fprintf(stdout, "\nValidating... Success.\n");
install_log("validating configuration parameters... success");
validated = AM_TRUE;
}
if (agent_token != NULL) {
fprintf(stdout, "\nCleaning up validation data...\n");
am_agent_logout(0, openam_url, agent_token, NULL, install_log);
}
if (validated) {
fprintf(stdout, "\nCreating configuration...\n");
/*do configure the instance and modify the server configuration*/
switch (instance_type) {
case AM_I_APACHE:
if (create_agent_instance(0, apache_conf, openam_url, agent_realm,
agent_url, agent_user, agent_password) == AM_SUCCESS) {
fprintf(stdout, "\nInstallation complete.\n");
install_log("installation complete");
}
break;
case AM_I_IIS:
if (create_agent_instance(iis_status, apache_conf/*site id*/, openam_url, agent_realm,
agent_url, agent_user, agent_password) == AM_SUCCESS) {
fprintf(stdout, "\nInstallation complete.\n");
install_log("installation complete");
}
break;
case AM_I_VARNISH:
{
//TODO
}
break;
default:
install_log("unknown installation instance type");
}
}
#ifdef _WIN32
SetConsoleMode(cons_handle, old_mode);
SetConsoleCtrlHandler((PHANDLER_ROUTINE) exit_handler, FALSE);
#endif
install_log("installation exit");
am_net_shutdown();
}
static void install_silent(int argc, char **argv) {
char lic_file_path[AM_URI_SIZE];
char lic_accepted = AM_FALSE;
install_log("%s for %s server silent installation", DESCRIPTION,
am_container_str(instance_type));
fprintf(stdout, "\n%s for %s Server installation.\n\n", DESCRIPTION,
am_container_str(instance_type));
snprintf(lic_file_path, sizeof (lic_file_path), "%s%s", app_path, LICENSE_FILE);
if (!file_exists(lic_path)) {
/*display a license*/
char *input, *lic_data = load_file(lic_file_path, NULL);
if (lic_data != NULL) {
fprintf(stdout, "Please read the following License Agreement carefully:\n\n%s\n", lic_data);
free(lic_data);
}
input = prompt_and_read("Do you completely agree with all the terms and conditions \n"
"of this License Agreement (yes/no): [no]:");
if (ISVALID(input) && strcasecmp(input, "yes") == 0) {
install_log("license accepted");
lic_accepted = AM_TRUE;
am_make_path(instance_path);
write_file(lic_path, AM_SPACE_CHAR, 1);
}
am_free(input);
} else {
lic_accepted = AM_TRUE;
install_log("license was accepted earlier");
}
if (!lic_accepted) {
install_log("license was not accepted");
fprintf(stdout, "\nYou need to accept the License terms and conditions to continue.\n");
exit(1);
}
if (argc == 8) {
int rv = AM_ERROR;
char validated = AM_FALSE;
char *agent_token = NULL;
char *agent_password = load_file(argv[7], NULL);
if (agent_password == NULL) {
fprintf(stdout, "\nError reading password file. Exiting.\n");
install_log("installation exit");
return;
}
trim(agent_password, '\n');
trim(agent_password, '\r');
am_net_init();
install_log("validating configuration parameters...");
fprintf(stdout, "\nValidating...\n");
rv = am_agent_login(0, argv[3], NULL,
argv[6], agent_password, argv[5], AM_TRUE, NULL,
&agent_token, NULL, NULL, NULL, install_log);
if (rv != AM_SUCCESS) {
fprintf(stdout, "\nError validating OpenAM - Agent configuration.\n"
"See installation log %s file for more details. Exiting.\n", log_path);
install_log("error validating OpenAM agent configuration");
} else {
fprintf(stdout, "\nValidating... Success.\n");
install_log("validating configuration parameters... success");
validated = AM_TRUE;
}
if (agent_token != NULL) {
fprintf(stdout, "\nCleaning up validation data...\n");
am_agent_logout(0, argv[3], agent_token, NULL, install_log);
}
if (validated) {
fprintf(stdout, "\nCreating configuration...\n");
if (instance_type == AM_I_APACHE) {
if (create_agent_instance(0, argv[2], argv[3], argv[5],
argv[4], argv[6], agent_password) == AM_SUCCESS) {
fprintf(stdout, "\nInstallation complete.\n");
install_log("installation complete");
}
} else if (instance_type == AM_I_IIS) {
if (create_agent_instance(0, argv[2], argv[3], argv[5],
argv[4], argv[6], agent_password) == AM_SUCCESS) {
fprintf(stdout, "\nInstallation complete.\n");
install_log("installation complete");
}
} else if (instance_type == AM_I_VARNISH) {
//TODO
}
}
am_free(agent_password);
am_net_shutdown();
} else {
fprintf(stdout, "\nInvalid arguments. Installation exit.\n");
}
install_log("installation exit");
}
static void delete_conf_entry_list(struct am_conf_entry **list) {
struct am_conf_entry *t = *list;
if (t != NULL) {
delete_conf_entry_list(&t->next);
free(t);
t = NULL;
}
}
static void list_instances(int argc, char **argv) {
struct am_conf_entry *list = NULL, *e, *t;
int rv = am_read_instances(instance_config, &list);
if (rv <= 0) {
fprintf(stdout, "\nNo agent configuration exists.\n");
delete_conf_entry_list(&list);
return;
}
fprintf(stdout, "\n%s configuration instances:\n\n", DESCRIPTION);
AM_LIST_FOR_EACH(list, e, t) {
fprintf(stdout,
" id: %s\n"
" configuration: %s\n"
" server/site: %s\n\n", e->name, e->path, e->web);
}
delete_conf_entry_list(&list);
fprintf(stdout, "\n");
}
static void remove_instance(int argc, char **argv) {
struct am_conf_entry *list = NULL, *e, *t;
int rv;
if (argc != 3) {
fprintf(stdout, "\nNo agent configuration specified.\n");
return;
}
rv = am_read_instances(instance_config, &list);
if (rv <= 0) {
delete_conf_entry_list(&list);
return;
}
AM_LIST_FOR_EACH(list, e, t) {
if (strcmp(e->name, argv[2]) == 0) {
switch (instance_type) {
case AM_I_APACHE:
{
char *input = prompt_and_read("\nWarning! This procedure will remove all "DESCRIPTION" references from \na Web server configuration."
" In case you are running "DESCRIPTION" in a\nmulti-virtualhost mode, an uninstallation must be carried out manually.\n\nContinue (yes/no): [no]:");
if (!ISVALID(input) || strcasecmp(input, "yes") != 0) {
am_free(input);
break;
}
am_free(input);
fprintf(stdout, "\nRemoving %s configuration...\n", e->name);
/*remove LoadModule line*/
rv = am_cleanup_instance(e->web, "LoadModule amagent_module");
/*remove AmAgent On/Off line*/
rv = am_cleanup_instance(e->web, "AmAgent ");
/*remove AmAgentConf line*/
rv = am_cleanup_instance(e->web, "AmAgentConf ");
/*delete agent instance configuration directory*/
am_delete_directory(e->path);
/*remove agent instance configuration*/
am_cleanup_instance(instance_config, e->name);
fprintf(stdout, "\nRemoving %s configuration... Done.\n", e->name);
}
break;
case AM_I_IIS:
{
char iis_instc_file[AM_URI_SIZE];
snprintf(iis_instc_file, sizeof (iis_instc_file),
"%s"FILE_PATH_SEP"config"FILE_PATH_SEP"agent.conf",
e->path);
fprintf(stdout, "\nRemoving %s configuration...\n", e->name);
/*remove IIS module in the site (global module configuration remains)*/
disable_module(e->web, iis_instc_file);
/*delete agent instance configuration directory*/
am_delete_directory(e->path);
/*remove agent instance configuration*/
am_cleanup_instance(instance_config, e->name);
fprintf(stdout, "\nRemoving %s configuration... Done.\n", e->name);
}
break;
case AM_I_VARNISH:
{
//TODO
}
break;
}
}
}
delete_conf_entry_list(&list);
}
static void remove_global(int argc, char **argv) {
int rv;
struct am_conf_entry *list = NULL, *e, *t;
char *input = prompt_and_read("\nWarning! This procedure will remove all "DESCRIPTION" references from \nIIS Server configuration."
"\n\nContinue (yes/no): [no]:");
if (!ISVALID(input) || strcasecmp(input, "yes") != 0) {
am_free(input);
return;
}
am_free(input);
rv = am_read_instances(instance_config, &list);
if (rv > 0) {
AM_LIST_FOR_EACH(list, e, t) {
if (strstr(e->web, "conf") == NULL) {/*all, except Apache agent*/
char iis_instc_file[AM_URI_SIZE];
snprintf(iis_instc_file, sizeof (iis_instc_file),
"%s"FILE_PATH_SEP"config"FILE_PATH_SEP"agent.conf",
e->path);
/*disable agent module in the Site*/
disable_module(e->web, iis_instc_file);
/*delete agent instance configuration directory*/
am_delete_directory(e->path);
/*remove agent instance configuration*/
am_cleanup_instance(instance_config, e->name);
}
}
}
delete_conf_entry_list(&list);
fprintf(stdout, "\nRemoving agent module from IIS Server configuration...\n");
remove_module();
fprintf(stdout, "\nRemoving agent module from IIS Server configuration... Done.\n");
}
static void enable_iis_mod(int argc, char **argv) {
struct am_conf_entry *list = NULL, *e, *t;
int rv;
if (argc != 3) {
fprintf(stdout, "\nNo agent configuration specified.\n");
return;
}
rv = am_read_instances(instance_config, &list);
if (rv <= 0) {
delete_conf_entry_list(&list);
return;
}
AM_LIST_FOR_EACH(list, e, t) {
if (strcmp(e->name, argv[2]) == 0) {
if (instance_type == AM_I_IIS) {
char iis_instc_file[AM_URI_SIZE];
snprintf(iis_instc_file, sizeof (iis_instc_file),
"%s"FILE_PATH_SEP"config"FILE_PATH_SEP"agent.conf",
e->path);
fprintf(stdout, "\nEnabling %s module configuration in site %s...\n", e->name, e->web);
enable_module(e->web, iis_instc_file);
fprintf(stdout, "\nEnabling %s module configuration in site %s... Done.\n", e->name, e->web);
}
}
}
delete_conf_entry_list(&list);
}
static void disable_iis_mod(int argc, char **argv) {
struct am_conf_entry *list = NULL, *e, *t;
int rv;
if (argc == 3) {
fprintf(stdout, "\nNo agent configuration specified.\n");
return;
}
rv = am_read_instances(instance_config, &list);
if (rv <= 0) {
delete_conf_entry_list(&list);
return;
}
AM_LIST_FOR_EACH(list, e, t) {
if (strcmp(e->name, argv[2]) == 0) {
if (instance_type == AM_I_IIS) {
char iis_instc_file[AM_URI_SIZE];
snprintf(iis_instc_file, sizeof (iis_instc_file),
"%s"FILE_PATH_SEP"config"FILE_PATH_SEP"agent.conf",
e->path);
fprintf(stdout, "\nDisabling %s module configuration in site %s...\n", e->name, e->web);
disable_module(e->web, iis_instc_file);
fprintf(stdout, "\nDisabling %s module configuration in site %s... Done.\n", e->name, e->web);
}
}
}
delete_conf_entry_list(&list);
}
static void archive_files(int argc, char **argv) {
int i, rv;
time_t tv;
struct tm fd;
zipFile zf = NULL;
struct am_namevalue *all = NULL, *e, *t;
if (argc < 4) return;
zf = zipOpen(argv[2], APPEND_STATUS_CREATE);
if (zf == NULL) return;
/*read all directory/file info (recursive)*/
for (i = 3; i < argc; i++) {
read_directory(argv[i], &all);
}
time(&tv);
localtime_r(&tv, &fd);
if (all != NULL) {
fprintf(stdout, "Adding to %s:\n", argv[2]);
}
AM_LIST_FOR_EACH(all, e, t) {
zip_fileinfo zi;
size_t off = 0;
uLong file_mode = 0;
/*fix path prefixes*/
if (e->n[0] == '/') {
off = 1;
}
memset(&zi, 0, sizeof (zi));
zi.tmz_date.tm_sec = fd.tm_sec;
zi.tmz_date.tm_min = fd.tm_min;
zi.tmz_date.tm_hour = fd.tm_hour;
zi.tmz_date.tm_mday = fd.tm_mday;
zi.tmz_date.tm_mon = fd.tm_mon;
zi.tmz_date.tm_year = fd.tm_year;
#ifdef _WIN32
{
char fname[AM_URI_SIZE];
char dir[AM_URI_SIZE];
char drive[AM_PATH_SIZE];
char ext[AM_PATH_SIZE];
if (_splitpath_s(e->n,
drive, sizeof (drive) - 1,
dir, sizeof (dir) - 1,
fname, sizeof (fname) - 1,
ext, sizeof (ext) - 1) != 0) {
continue;
}
if (dir[0] == '\\') off = 1;
if (dir[0] == '\\' && dir[1] == '\\') off = 2;
off += strlen(drive);
}
#endif
fprintf(stdout, " %s\n", e->n);
#ifndef _WIN32
if (e->ns == 1) {
/*a directory*/
file_mode = (S_IFDIR | 0755) << 16L;
} else {
/*a file*/
if (strstr(e->n, "agentadmin") != NULL || strstr(e->n, ".so") != NULL) {
file_mode = 0755 << 16L; /* we need execute bit set for these two */
} else {
file_mode = 0644 << 16L;
}
}
zi.external_fa = file_mode;
#endif
rv = zipOpenNewFileInZip(zf, e->n + off, &zi,
NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION);
if (e->ns == 1) {
/*a directory*/
rv = zipCloseFileInZip(zf);
} else {
/*a file*/
FILE *f = fopen(e->n, "rb");
if (f != NULL) {
int rb;
unsigned char b[1024];
while (!feof(f)) {
rb = (int) fread(b, 1, sizeof (b), f);
rv = zipWriteInFileInZip(zf, b, rb);
}
fclose(f);
}
rv = zipCloseFileInZip(zf);
}
}
zipClose(zf, NULL);
delete_am_namevalue_list(&all);
}
int main(int argc, char **argv) {
int i;
char tm[64];
struct tm now;
char instance_type_mod[AM_URI_SIZE];
struct command_line params[] = {
{"--i", install_interactive},
{"--s", install_silent},
{"--l", list_instances},
{"--r", remove_instance},
#ifdef _WIN32
{"--n", list_iis_sites},
{"--g", remove_global},
{"--e", enable_iis_mod},
{"--d", disable_iis_mod},
#endif
{"--v", show_version},
{"--k", generate_key},
{"--p", password_encrypt},
{"--d", password_decrypt},
{"--a", archive_files},
{NULL}
};
if (argc > 1) {
time_t tv;
time(&tv);
localtime_r(&tv, &now);
strftime(tm, sizeof (tm) - 1, "%Y%m%d%H%M%S", &now);
/*get agentadmin path*/
am_bin_path(app_path, sizeof (app_path) - 1);
/*create/update installer log path*/
snprintf(log_path, sizeof (log_path),
"%s.."FILE_PATH_SEP"log",
app_path);
am_make_path(log_path);
strcat(log_path, FILE_PATH_SEP"install_");
strcat(log_path, tm);
strcat(log_path, ".log");
/*instances directory*/
snprintf(instance_path, sizeof (instance_path),
"%s.."FILE_PATH_SEP"instances",
app_path);
/*agent configuration template*/
snprintf(config_template, sizeof (config_template),
"%s.."FILE_PATH_SEP"config"FILE_PATH_SEP"agent.conf.template",
app_path);
/*instances configuration file (internal)*/
snprintf(instance_config, sizeof (instance_config),
"%s.."FILE_PATH_SEP"instances"FILE_PATH_SEP".agents",
app_path);
/*and add a license tracker path*/
snprintf(lic_path, sizeof (lic_path),
"%s.."FILE_PATH_SEP"instances"FILE_PATH_SEP".license",
app_path);
/*determine installer type*/
snprintf(instance_type_mod, sizeof (instance_type_mod),
"%s.."FILE_PATH_SEP"lib"FILE_PATH_SEP"mod_openam."LIB_FILE_EXT, app_path);
if (file_exists(instance_type_mod)) instance_type = AM_I_APACHE;
snprintf(instance_type_mod, sizeof (instance_type_mod),
"%s.."FILE_PATH_SEP"lib"FILE_PATH_SEP"mod_iis_openam."LIB_FILE_EXT, app_path);
if (file_exists(instance_type_mod)) instance_type = AM_I_IIS;
snprintf(instance_type_mod, sizeof (instance_type_mod),
"%s.."FILE_PATH_SEP"lib"FILE_PATH_SEP"vmod_openam."LIB_FILE_EXT, app_path);
if (file_exists(instance_type_mod)) instance_type = AM_I_VARNISH;
/*run through the cli options*/
for (i = 0; params[i].option; ++i) {
if (!strcasecmp(argv[1], params[i].option)) {
params[i].handler(argc, argv);
return 0;
}
}
}
fprintf(stdout, "\n%s\n"
"Usage: agentadmin <option> [<arguments>]\n\n"
"The available options are:\n\n"
"install agent instance:\n"
" agentadmin --i\n\n"
"install agent instance (silent):\n"
" agentadmin --s \"web-server configuration/file parameter\" \\\n"
" \"OpenAM URL\" \"Agent URL\" \"realm\" \"agent user id\" \\\n"
" \"path to the agent password file\"\n\n"
"list configured agent instances:\n"
" agentadmin --l\n\n"
#ifdef _WIN32
"list IIS Server Sites:\n"
" agentadmin --n\n\n"
"remove agent module from IIS Server:\n"
" agentadmin --g\n\n"
"enable agent module in IIS Server site:\n"
" agentadmin --e agent_1\n\n"
"disable agent module in IIS Server site:\n"
" agentadmin --d agent_1\n\n"
#endif
"uninstall agent instance:\n"
" agentadmin --r agent_1\n\n"
"generate encryption key:\n"
" agentadmin --k\n\n"
"encrypt password:\n"
" agentadmin --p \"key\" \"password\"\n\n"
"build and version information:\n"
" agentadmin --v\n\n", DESCRIPTION);
return 0;
}