Reindent server/core/service.c

This commit is contained in:
Johan Wikman
2015-11-30 19:36:44 +02:00
parent ed06d4f14c
commit 8601068dc2
2 changed files with 1540 additions and 1410 deletions

File diff suppressed because it is too large Load Diff

View File

@ -38,26 +38,26 @@
* @verbatim * @verbatim
* Revision History * Revision History
* *
* Date Who Description * Date Who Description
* 14/06/13 Mark Riddoch Initial implementation * 14/06/13 Mark Riddoch Initial implementation
* 18/06/13 Mark Riddoch Addition of statistics and function * 18/06/13 Mark Riddoch Addition of statistics and function
* prototypes * prototypes
* 23/06/13 Mark Riddoch Added service user and users * 23/06/13 Mark Riddoch Added service user and users
* 06/02/14 Massimiliano Pinto Added service flag for root user access * 06/02/14 Massimiliano Pinto Added service flag for root user access
* 25/02/14 Massimiliano Pinto Added service refresh limit feature * 25/02/14 Massimiliano Pinto Added service refresh limit feature
* 07/05/14 Massimiliano Pinto Added version_string field to service * 07/05/14 Massimiliano Pinto Added version_string field to service
* struct * struct
* 29/05/14 Mark Riddoch Filter API mechanism * 29/05/14 Mark Riddoch Filter API mechanism
* 26/06/14 Mark Riddoch Added WeightBy support * 26/06/14 Mark Riddoch Added WeightBy support
* 09/09/14 Massimiliano Pinto Added service option for localhost authentication * 09/09/14 Massimiliano Pinto Added service option for localhost authentication
* 09/10/14 Massimiliano Pinto Added service resources via hashtable * 09/10/14 Massimiliano Pinto Added service resources via hashtable
* *
* @endverbatim * @endverbatim
*/ */
struct server; struct server;
struct router; struct router;
struct router_object; struct router_object;
struct users; struct users;
/** /**
* The servprotocol structure is used to link a service to the protocols that * The servprotocol structure is used to link a service to the protocols that
@ -65,23 +65,24 @@ struct users;
* that should be loaded to support the client connection and the port that the * that should be loaded to support the client connection and the port that the
* protocol should use to listen for incoming client connections. * protocol should use to listen for incoming client connections.
*/ */
typedef struct servprotocol { typedef struct servprotocol
char *protocol; /**< Protocol module to load */ {
unsigned short port; /**< Port to listen on */ char *protocol; /**< Protocol module to load */
char *address; /**< Address to listen with */ unsigned short port; /**< Port to listen on */
DCB *listener; /**< The DCB for the listener */ char *address; /**< Address to listen with */
struct servprotocol DCB *listener; /**< The DCB for the listener */
*next; /**< Next service protocol */ struct servprotocol *next; /**< Next service protocol */
} SERV_PROTOCOL; } SERV_PROTOCOL;
/** /**
* The service statistics structure * The service statistics structure
*/ */
typedef struct { typedef struct
time_t started; /**< The time when the service was started */ {
int n_failed_starts; /**< Number of times this service has failed to start */ time_t started; /**< The time when the service was started */
int n_sessions; /**< Number of sessions created on service since start */ int n_failed_starts; /**< Number of times this service has failed to start */
int n_current; /**< Current number of sessions */ int n_sessions; /**< Number of sessions created on service since start */
int n_current; /**< Current number of sessions */
} SERVICE_STATS; } SERVICE_STATS;
/** /**
@ -89,42 +90,47 @@ typedef struct {
for this service to allow the gateway to login to the backend for this service to allow the gateway to login to the backend
database and extact information such as the user table or other database and extact information such as the user table or other
database status or configuration data. database status or configuration data.
*/ */
typedef struct { typedef struct
char *name; /**< The user name to use to extract information */ {
char *authdata; /**< The authentication data requied */ char *name; /**< The user name to use to extract information */
char *authdata; /**< The authentication data requied */
} SERVICE_USER; } SERVICE_USER;
/** /**
* The service refresh rate holds the counter and last load time_t * The service refresh rate holds the counter and last load time_t
for this service to load users data from the backend database for this service to load users data from the backend database
*/ */
typedef struct { typedef struct
int nloads; {
time_t last; int nloads;
time_t last;
} SERVICE_REFRESH_RATE; } SERVICE_REFRESH_RATE;
typedef struct server_ref_t{ typedef struct server_ref_t
struct server_ref_t *next; {
SERVER* server; struct server_ref_t *next;
SERVER* server;
}SERVER_REF; }SERVER_REF;
typedef enum { typedef enum
SSL_DISABLED, {
SSL_ENABLED, SSL_DISABLED,
SSL_REQUIRED SSL_ENABLED,
SSL_REQUIRED
} ssl_mode_t; } ssl_mode_t;
enum{ enum
SERVICE_SSLV3, {
SERVICE_TLS10, SERVICE_SSLV3,
SERVICE_TLS10,
#ifdef OPENSSL_1_0 #ifdef OPENSSL_1_0
SERVICE_TLS11, SERVICE_TLS11,
SERVICE_TLS12, SERVICE_TLS12,
#endif #endif
SERVICE_SSL_MAX, SERVICE_SSL_MAX,
SERVICE_TLS_MAX, SERVICE_TLS_MAX,
SERVICE_SSL_TLS_MAX SERVICE_SSL_TLS_MAX
}; };
#define DEFAULT_SSL_CERT_VERIFY_DEPTH 100 /*< The default certificate verification depth */ #define DEFAULT_SSL_CERT_VERIFY_DEPTH 100 /*< The default certificate verification depth */
@ -142,117 +148,117 @@ enum{
* and a set of client side protocol/port pairs used to listen for new connections * and a set of client side protocol/port pairs used to listen for new connections
* to the service. * to the service.
*/ */
typedef struct service { typedef struct service
char *name; /**< The service name */ {
int state; /**< The service state */ char *name; /**< The service name */
SERV_PROTOCOL *ports; /**< Linked list of ports and protocols int state; /**< The service state */
* that this service will listen on. SERV_PROTOCOL *ports; /**< Linked list of ports and protocols
*/ * that this service will listen on.
char *routerModule; /**< Name of router module to use */ */
char **routerOptions;/**< Router specific option strings */ char *routerModule; /**< Name of router module to use */
struct router_object char **routerOptions; /**< Router specific option strings */
*router; /**< The router we are using */ struct router_object *router; /**< The router we are using */
void *router_instance; void *router_instance; /**< The router instance for this service */
/**< The router instance for this service */ char *version_string; /** version string for this service listeners */
char *version_string;/** version string for this service listeners */ SERVER_REF *dbref; /** server references */
SERVER_REF *dbref; /** server references */ SERVICE_USER credentials; /**< The cedentials of the service user */
SERVICE_USER credentials; /**< The cedentials of the service user */ SPINLOCK spin; /**< The service spinlock */
SPINLOCK spin; /**< The service spinlock */ SERVICE_STATS stats; /**< The service statistics */
SERVICE_STATS stats; /**< The service statistics */ struct users *users; /**< The user data for this service */
struct users *users; /**< The user data for this service */ int enable_root; /**< Allow root user access */
int enable_root; /**< Allow root user access */ int localhost_match_wildcard_host; /**< Match localhost against wildcard */
int localhost_match_wildcard_host; /**< Match localhost against wildcard */ HASHTABLE *resources; /**< hastable for service resources, i.e. database names */
HASHTABLE *resources; /**< hastable for service resources, i.e. database names */ CONFIG_PARAMETER* svc_config_param;/*< list of config params and values */
CONFIG_PARAMETER* int svc_config_version; /*< Version number of configuration */
svc_config_param; /*< list of config params and values */ bool svc_do_shutdown; /*< tells the service to exit loops etc. */
int svc_config_version; /*< Version number of configuration */ bool users_from_all; /*< Load users from one server or all of them */
bool svc_do_shutdown; /*< tells the service to exit loops etc. */ bool strip_db_esc; /*< Remove the '\' characters from database names
bool users_from_all; /*< Load users from one server or all of them */ * when querying them from the server. MySQL Workbench seems
bool strip_db_esc; /*< Remove the '\' characters from database names * to escape at least the underscore character. */
* when querying them from the server. MySQL Workbench seems bool optimize_wildcard; /*< Convert wildcard grants to individual database grants */
* to escape at least the underscore character. */ SPINLOCK users_table_spin; /**< The spinlock for users data refresh */
bool optimize_wildcard; /*< Convert wildcard grants to individual database grants */ SERVICE_REFRESH_RATE rate_limit; /**< The refresh rate limit for users table */
SPINLOCK FILTER_DEF **filters; /**< Ordered list of filters */
users_table_spin; /**< The spinlock for users data refresh */ int n_filters; /**< Number of filters */
SERVICE_REFRESH_RATE int conn_timeout; /*< Session timeout in seconds */
rate_limit; /**< The refresh rate limit for users table */ ssl_mode_t ssl_mode; /*< one of DISABLED, ENABLED or REQUIRED */
FILTER_DEF **filters; /**< Ordered list of filters */ char *weightby;
int n_filters; /**< Number of filters */ struct service *next; /**< The next service in the linked list */
int conn_timeout; /*< Session timeout in seconds */ SSL_CTX *ctx;
ssl_mode_t ssl_mode; /*< one of DISABLED, ENABLED or REQUIRED */ SSL_METHOD *method; /*< SSLv3 or TLS1.0/1.1/1.2 methods
char *weightby; * see: https://www.openssl.org/docs/ssl/SSL_CTX_new.html */
struct service *next; /**< The next service in the linked list */ int ssl_cert_verify_depth; /*< SSL certificate verification depth */
SSL_CTX *ctx; int ssl_method_type; /*< Which of the SSLv3 or TLS1.0/1.1/1.2 methods to use */
SSL_METHOD *method; /*< SSLv3 or TLS1.0/1.1/1.2 methods char* ssl_cert; /*< SSL certificate */
* see: https://www.openssl.org/docs/ssl/SSL_CTX_new.html */ char* ssl_key; /*< SSL private key */
int ssl_cert_verify_depth; /*< SSL certificate verification depth */ char* ssl_ca_cert; /*< SSL CA certificate */
int ssl_method_type; /*< Which of the SSLv3 or TLS1.0/1.1/1.2 methods to use */ bool ssl_init_done; /*< If SSL has already been initialized for this service */
char* ssl_cert; /*< SSL certificate */ bool retry_start; /*< If starting of the service should be retried later */
char* ssl_key; /*< SSL private key */ bool log_auth_warnings; /*< Log authentication failures and warnings */
char* ssl_ca_cert; /*< SSL CA certificate */
bool ssl_init_done; /*< If SSL has already been initialized for this service */
bool retry_start; /*< If starting of the service should be retried later */
bool log_auth_warnings; /*< Log authentication failures and warnings */
} SERVICE; } SERVICE;
typedef enum count_spec_t {COUNT_NONE=0, COUNT_ATLEAST, COUNT_EXACT, COUNT_ATMOST} count_spec_t; typedef enum count_spec_t
{
COUNT_NONE = 0,
COUNT_ATLEAST,
COUNT_EXACT,
COUNT_ATMOST
} count_spec_t;
#define SERVICE_STATE_ALLOC 1 /**< The service has been allocated */ #define SERVICE_STATE_ALLOC 1 /**< The service has been allocated */
#define SERVICE_STATE_STARTED 2 /**< The service has been started */ #define SERVICE_STATE_STARTED 2 /**< The service has been started */
#define SERVICE_STATE_FAILED 3 /**< The service failed to start */ #define SERVICE_STATE_FAILED 3 /**< The service failed to start */
#define SERVICE_STATE_STOPPED 4 /**< The service has been stopped */ #define SERVICE_STATE_STOPPED 4 /**< The service has been stopped */
extern SERVICE *service_alloc(const char *, const char *); extern SERVICE *service_alloc(const char *, const char *);
extern int service_free(SERVICE *); extern int service_free(SERVICE *);
extern SERVICE *service_find(char *); extern SERVICE *service_find(char *);
extern int service_isvalid(SERVICE *); extern int service_isvalid(SERVICE *);
extern int serviceAddProtocol(SERVICE *, char *, char *, unsigned short); extern int serviceAddProtocol(SERVICE *, char *, char *, unsigned short);
extern int serviceHasProtocol(SERVICE *, char *, unsigned short); extern int serviceHasProtocol(SERVICE *, char *, unsigned short);
extern void serviceAddBackend(SERVICE *, SERVER *); extern void serviceAddBackend(SERVICE *, SERVER *);
extern int serviceHasBackend(SERVICE *, SERVER *); extern int serviceHasBackend(SERVICE *, SERVER *);
extern void serviceAddRouterOption(SERVICE *, char *); extern void serviceAddRouterOption(SERVICE *, char *);
extern void serviceClearRouterOptions(SERVICE *); extern void serviceClearRouterOptions(SERVICE *);
extern int serviceStart(SERVICE *); extern int serviceStart(SERVICE *);
extern int serviceStartAll(); extern int serviceStartAll();
extern void serviceStartProtocol(SERVICE *, char *, int); extern void serviceStartProtocol(SERVICE *, char *, int);
extern int serviceStop(SERVICE *); extern int serviceStop(SERVICE *);
extern int serviceRestart(SERVICE *); extern int serviceRestart(SERVICE *);
extern int serviceSetUser(SERVICE *, char *, char *); extern int serviceSetUser(SERVICE *, char *, char *);
extern int serviceGetUser(SERVICE *, char **, char **); extern int serviceGetUser(SERVICE *, char **, char **);
extern bool serviceSetFilters(SERVICE *, char *); extern bool serviceSetFilters(SERVICE *, char *);
extern int serviceSetSSL(SERVICE *service, char* action); extern int serviceSetSSL(SERVICE *service, char* action);
extern int serviceInitSSL(SERVICE* service); extern int serviceInitSSL(SERVICE* service);
extern int serviceSetSSLVersion(SERVICE *service, char* version); extern int serviceSetSSLVersion(SERVICE *service, char* version);
extern int serviceSetSSLVerifyDepth(SERVICE* service, int depth); extern int serviceSetSSLVerifyDepth(SERVICE* service, int depth);
extern void serviceSetCertificates(SERVICE *service, char* cert,char* key, char* ca_cert); extern void serviceSetCertificates(SERVICE *service, char* cert,char* key, char* ca_cert);
extern int serviceEnableRootUser(SERVICE *, int ); extern int serviceEnableRootUser(SERVICE *, int );
extern int serviceSetTimeout(SERVICE *, int ); extern int serviceSetTimeout(SERVICE *, int );
extern void serviceSetRetryOnFailure(SERVICE *service, char* value); extern void serviceSetRetryOnFailure(SERVICE *service, char* value);
extern void serviceWeightBy(SERVICE *, char *); extern void serviceWeightBy(SERVICE *, char *);
extern char *serviceGetWeightingParameter(SERVICE *); extern char *serviceGetWeightingParameter(SERVICE *);
extern int serviceEnableLocalhostMatchWildcardHost(SERVICE *, int); extern int serviceEnableLocalhostMatchWildcardHost(SERVICE *, int);
int serviceStripDbEsc(SERVICE* service, int action); extern int serviceStripDbEsc(SERVICE* service, int action);
int serviceAuthAllServers(SERVICE *service, int action); extern int serviceAuthAllServers(SERVICE *service, int action);
int serviceOptimizeWildcard(SERVICE *service, int action); extern int serviceOptimizeWildcard(SERVICE *service, int action);
extern void service_update(SERVICE *, char *, char *, char *); extern void service_update(SERVICE *, char *, char *, char *);
extern int service_refresh_users(SERVICE *); extern int service_refresh_users(SERVICE *);
extern void printService(SERVICE *); extern void printService(SERVICE *);
extern void printAllServices(); extern void printAllServices();
extern void dprintAllServices(DCB *); extern void dprintAllServices(DCB *);
extern bool service_set_param_value(SERVICE* service,
CONFIG_PARAMETER* param,
char* valstr,
count_spec_t count_spec,
config_param_type_t type);
extern void dprintService(DCB *, SERVICE *);
extern void dListServices(DCB *);
extern void dListListeners(DCB *);
extern char* service_get_name(SERVICE* svc);
extern void service_shutdown();
extern int serviceSessionCountAll();
extern RESULTSET *serviceGetList();
extern RESULTSET *serviceGetListenerList();
bool service_set_param_value (
SERVICE* service,
CONFIG_PARAMETER* param,
char* valstr,
count_spec_t count_spec,
config_param_type_t type);
extern void dprintService(DCB *, SERVICE *);
extern void dListServices(DCB *);
extern void dListListeners(DCB *);
char* service_get_name(SERVICE* svc);
void service_shutdown();
extern int serviceSessionCountAll();
extern RESULTSET *serviceGetList();
extern RESULTSET *serviceGetListenerList();
#endif #endif