static ne_session *session_open(int with_lock) { const char *scheme = NULL; ne_session *session; extern ne_lock_store *lock_store; if (!b_uri) return NULL; scheme = uri.scheme ? uri.scheme : "http"; if (!(session = ne_session_create(scheme, uri.host, uri.port ? uri.port : ne_uri_defaultport(scheme)))) { fprintf(stderr, "Failed to create session\n"); return NULL; } ne_ssl_set_verify(session, ssl_verify_cb, NULL); ne_set_server_auth(session, ne_auth_creds_cb, NULL); ne_redirect_register(session); if (with_lock && lock_store) ne_lockstore_register(lock_store, session); return session; }
static int init_session(ne_session *sess) { if (proxy_hostname) { ne_session_proxy(sess, proxy_hostname, proxy_port); } ne_set_useragent(i_session, "davtest/" PACKAGE_VERSION); if (i_username) { ne_set_server_auth(sess, auth, NULL); } if (use_secure) { if (!ne_supports_ssl()) { t_context("No SSL support, reconfigure using --with-ssl"); return FAILHARD; } else { ne_ssl_set_verify(sess, ignore_verify, NULL); } } return OK; }
/* * Connect to a DAV server * This function sets the flag _connected if the connection is established * and returns if the flag is set, so calling it frequently is save. */ static int dav_connect(const char *base_url) { int timeout = 30; int useSSL = 0; int rc; char protocol[6]; char uaBuf[256]; char *path = NULL; char *scheme = NULL; char *host = NULL; unsigned int port = 0; if (_connected) { return 0; } rc = c_parse_uri( base_url, &scheme, &dav_session.user, &dav_session.pwd, &host, &port, &path ); if( rc < 0 ) { DEBUG_WEBDAV("Failed to parse uri %s", base_url ); goto out; } DEBUG_WEBDAV("* scheme %s", scheme ? scheme : "empty"); DEBUG_WEBDAV("* host %s", host ? host : "empty"); DEBUG_WEBDAV("* port %u", port ); DEBUG_WEBDAV("* path %s", path ? path : "empty"); if( strcmp( scheme, "owncloud" ) == 0 ) { strncpy( protocol, "http", 6); } else if( strcmp( scheme, "ownclouds" ) == 0 ) { strncpy( protocol, "https", 6 ); useSSL = 1; } else { strncpy( protocol, "", 6 ); DEBUG_WEBDAV("Invalid scheme %s, go outa here!", scheme ); rc = -1; goto out; } DEBUG_WEBDAV("* user %s", dav_session.user ? dav_session.user : ""); if (port == 0) { port = ne_uri_defaultport(protocol); } rc = ne_sock_init(); DEBUG_WEBDAV("ne_sock_init: %d", rc ); if (rc < 0) { rc = -1; goto out; } dav_session.ctx = ne_session_create( protocol, host, port); if (dav_session.ctx == NULL) { DEBUG_WEBDAV("Session create with protocol %s failed", protocol ); rc = -1; goto out; } ne_set_read_timeout(dav_session.ctx, timeout); snprintf( uaBuf, sizeof(uaBuf), "csyncoC/%s",CSYNC_STRINGIFY( LIBCSYNC_VERSION )); ne_set_useragent( dav_session.ctx, c_strdup( uaBuf )); ne_set_server_auth(dav_session.ctx, ne_auth, 0 ); if( useSSL ) { if (!ne_has_support(NE_FEATURE_SSL)) { DEBUG_WEBDAV("Error: SSL is not enabled."); rc = -1; goto out; } ne_ssl_trust_default_ca( dav_session.ctx ); ne_ssl_set_verify( dav_session.ctx, verify_sslcert, 0 ); } _connected = 1; rc = 0; out: SAFE_FREE(path); SAFE_FREE(host); SAFE_FREE(scheme); return rc; }
void upsdrv_initups(void) { int ret; char *val; FILE *fp; #if HAVE_NE_SET_CONNECT_TIMEOUT && HAVE_NE_SOCK_CONNECT_TIMEOUT /* we don't need to use alarm() */ #else struct sigaction sa; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = netxml_alarm_handler; sigaction(SIGALRM, &sa, NULL); #endif /* allow override of default network timeout value */ val = getval("timeout"); if (val) { timeout = atoi(val); if (timeout < 1) { fatalx(EXIT_FAILURE, "timeout must be greater than 0"); } } val = getval("shutdown_duration"); if (val) { shutdown_duration = atoi(val); if (shutdown_duration < 0) { fatalx(EXIT_FAILURE, "shutdown duration must be greater than or equal to 0"); } } val = getval("shutdown_timer"); if (val) { shutdown_timer = atoi(val); if (shutdown_timer < 0) { fatalx(EXIT_FAILURE, "shutdown timer must be greater than or equal to 0"); } } if (nut_debug_level > 5) { ne_debug_init(stderr, NE_DBG_HTTP | NE_DBG_HTTPBODY); } if (ne_sock_init()) { fatalx(EXIT_FAILURE, "%s: failed to initialize socket libraries", progname); } if (ne_uri_parse(device_path, &uri) || uri.host == NULL) { fatalx(EXIT_FAILURE, "%s: invalid hostname '%s'", progname, device_path); } /* if (uri.scheme == NULL) { uri.scheme = strdup("http"); } if (uri.host == NULL) { uri.host = strdup(device_path); } */ if (uri.port == 0) { uri.port = ne_uri_defaultport(uri.scheme); } upsdebugx(1, "using %s://%s port %d", uri.scheme, uri.host, uri.port); session = ne_session_create(uri.scheme, uri.host, uri.port); /* timeout if we can't (re)connect to the UPS */ #ifdef HAVE_NE_SET_CONNECT_TIMEOUT ne_set_connect_timeout(session, timeout); #endif /* just wait for a couple of seconds */ ne_set_read_timeout(session, timeout); ne_set_useragent(session, subdriver->version); if (strcasecmp(uri.scheme, "https") == 0) { ne_ssl_trust_default_ca(session); } ne_set_server_auth(session, netxml_authenticate, NULL); /* if debug level is set, direct output to stderr */ if (!nut_debug_level) { fp = fopen("/dev/null", "w"); } else { fp = stderr; } if (!fp) { fatal_with_errno(EXIT_FAILURE, "Connectivity test failed"); } /* see if we have a connection */ ret = ne_get(session, subdriver->initups, fileno(fp)); if (!nut_debug_level) { fclose(fp); } else { fprintf(fp, "\n"); } if (ret != NE_OK) { fatalx(EXIT_FAILURE, "Connectivity test: %s", ne_get_error(session)); } upslogx(LOG_INFO, "Connectivity test: %s", ne_get_error(session)); }
/* FIXME: Leaky as a bucket */ void open_connection(const char *url) { char *proxy_host = get_option(opt_proxy), *pnt; ne_server_capabilities caps; int ret, use_ssl = 0; ne_session *sess; if (session.connected) { close_connection(); } else { ne_uri_free(&session.uri); if (session.lastwp) { ne_free(session.lastwp); session.lastwp = NULL; } } /* Single argument: see whether we have a path or scheme */ if (strchr(url, '/') == NULL) { /* No path, no scheme -> just a hostname */ pnt = strchr(url, ':'); if (pnt != NULL) { *pnt++ = '\0'; session.uri.port = atoi(pnt); } else { session.uri.port = 80; } session.uri.host = ne_strdup(url); session.uri.scheme = ne_strdup("http"); } else { /* Parse the URL */ if (ne_uri_parse(url, &session.uri) || session.uri.host == NULL) { printf(_("Could not parse URL `%s'\n"), url); return; } if (session.uri.scheme == NULL) session.uri.scheme = ne_strdup("http"); if (!session.uri.port) session.uri.port = ne_uri_defaultport(session.uri.scheme); if (strcasecmp(session.uri.scheme, "https") == 0) { if (!ne_has_support(NE_FEATURE_SSL)) { printf(_("SSL is not enabled.\n")); return; } use_ssl = 1; } } session.sess = ne_session_create(session.uri.scheme, session.uri.host, session.uri.port); sess = session.sess; if (use_ssl && setup_ssl()) { return; } ne_lockstore_register(session.locks, sess); ne_redirect_register(sess); ne_set_notifier(sess, notifier, NULL); if (session.uri.path == NULL) { session.uri.path = ne_strdup("/"); } else { if (!ne_path_has_trailing_slash(session.uri.path)) { pnt = ne_concat(session.uri.path, "/", NULL); free(session.uri.path); session.uri.path = pnt; } } /* Get the proxy details */ if (proxy_host != NULL) { if (get_option(opt_proxy_port) != NULL) { proxy_port = atoi(get_option(opt_proxy_port)); } else { proxy_port = 8080; } proxy_hostname = proxy_host; } #ifdef ENABLE_NETRC { netrc_entry *found; found = search_netrc(netrc_list, session.uri.host); if (found != NULL) { if (found->account && found->password) { server_username = found->account; server_password = found->password; } } } #endif /* ENABLE_NETRC */ #ifdef NE_SESSFLAG_EXPECT100 ne_set_session_flag(session.sess, NE_SESSFLAG_EXPECT100, get_bool_option(opt_expect100)); #endif /* NE_SESSFLAG_EXPECT100 */ session.connected = 0; ne_set_useragent(session.sess, "cadaver/" PACKAGE_VERSION); ne_set_server_auth(session.sess, supply_creds_server, NULL); ne_set_proxy_auth(session.sess, supply_creds_proxy, NULL); if (proxy_host) { ne_session_proxy(session.sess, proxy_hostname, proxy_port); } ret = ne_options(session.sess, session.uri.path, &caps); switch (ret) { case NE_OK: session.connected = true; if (set_path(session.uri.path)) { close_connection(); } break; case NE_CONNECT: if (proxy_host) { printf(_("Could not connect to `%s' on port %d:\n%s\n"), proxy_hostname, proxy_port, ne_get_error(session.sess)); } else { printf(_("Could not connect to `%s' on port %d:\n%s\n"), session.uri.host, session.uri.port, ne_get_error(session.sess)); } break; case NE_LOOKUP: puts(ne_get_error(session.sess)); break; default: printf(_("Could not open collection:\n%s\n"), ne_get_error(session.sess)); break; } }
/* * Connect to a DAV server * This function sets the flag _connected if the connection is established * and returns if the flag is set, so calling it frequently is save. */ static int dav_connect(const char *base_url) { int useSSL = 0; int rc; char protocol[6] = {'\0'}; char uaBuf[256]; char *path = NULL; char *scheme = NULL; char *host = NULL; unsigned int port = 0; int proxystate = -1; if (_connected) { return 0; } rc = c_parse_uri( base_url, &scheme, &dav_session.user, &dav_session.pwd, &host, &port, &path ); if( rc < 0 ) { DEBUG_WEBDAV("Failed to parse uri %s", base_url ); goto out; } DEBUG_WEBDAV("* scheme %s", scheme ); DEBUG_WEBDAV("* host %s", host ); DEBUG_WEBDAV("* port %u", port ); DEBUG_WEBDAV("* path %s", path ); if( strcmp( scheme, "owncloud" ) == 0 ) { strcpy( protocol, "http"); } else if( strcmp( scheme, "ownclouds" ) == 0 ) { strcpy( protocol, "https"); useSSL = 1; } else { DEBUG_WEBDAV("Invalid scheme %s, go outa here!", scheme ); rc = -1; goto out; } DEBUG_WEBDAV("* user %s", dav_session.user ? dav_session.user : ""); if (port == 0) { port = ne_uri_defaultport(protocol); } #if 0 rc = ne_sock_init(); DEBUG_WEBDAV("ne_sock_init: %d", rc ); if (rc < 0) { rc = -1; goto out; } #endif dav_session.ctx = ne_session_create( protocol, host, port); if (dav_session.ctx == NULL) { DEBUG_WEBDAV("Session create with protocol %s failed", protocol ); rc = -1; goto out; } if (dav_session.read_timeout == 0) dav_session.read_timeout = 300; // set 300 seconds as default. ne_set_read_timeout(dav_session.ctx, dav_session.read_timeout); snprintf( uaBuf, sizeof(uaBuf), "Mozilla/5.0 (%s) csyncoC/%s", get_platform(), CSYNC_STRINGIFY( LIBCSYNC_VERSION )); ne_set_useragent( dav_session.ctx, uaBuf); ne_set_server_auth(dav_session.ctx, ne_auth, 0 ); if( useSSL ) { if (!ne_has_support(NE_FEATURE_SSL)) { DEBUG_WEBDAV("Error: SSL is not enabled."); rc = -1; goto out; } ne_ssl_trust_default_ca( dav_session.ctx ); ne_ssl_set_verify( dav_session.ctx, verify_sslcert, 0 ); } /* Hook called when a request is created. It sets the proxy connection header. */ ne_hook_create_request( dav_session.ctx, request_created_hook, NULL ); /* Hook called after response headers are read. It gets the Session ID. */ ne_hook_post_headers( dav_session.ctx, post_request_hook, NULL ); /* Hook called before a request is sent. It sets the cookies. */ ne_hook_pre_send( dav_session.ctx, pre_send_hook, NULL ); /* Hook called after request is dispatched. Used for handling possible redirections. */ ne_hook_post_send( dav_session.ctx, post_send_hook, NULL ); /* Proxy support */ proxystate = configureProxy( dav_session.ctx ); if( proxystate < 0 ) { DEBUG_WEBDAV("Error: Proxy-Configuration failed."); } else if( proxystate > 0 ) { ne_set_proxy_auth( dav_session.ctx, ne_proxy_auth, 0 ); } _connected = 1; rc = 0; out: SAFE_FREE(path); SAFE_FREE(host); SAFE_FREE(scheme); return rc; }