]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
res_config_pgsql: normalize database connection option with cel and cdr by supporting...
authorAbdelkader Boudih <terminale@gmail.com>
Wed, 8 Jan 2025 22:00:39 +0000 (23:00 +0100)
committerasterisk-org-access-app[bot] <120671045+asterisk-org-access-app[bot]@users.noreply.github.com>
Fri, 10 Jan 2025 18:08:29 +0000 (18:08 +0000)
configs/samples/res_pgsql.conf.sample
res/res_config_pgsql.c

index 717821c8e1745ee3c2a54429598c7c502501c3e0..8394fcf4c5fb8c349e6264cd5e7334c5b752fdeb 100644 (file)
@@ -1,25 +1,25 @@
 ;
 ; Sample configuration for res_config_pgsql
 ;
-; The value of dbhost may be either a hostname or an IP address.
-; If dbhost is commented out or the string "localhost", a connection
+; The value of hostname may be either a hostname or an IP address.
+; If hostname is commented out or the string "localhost", a connection
 ; to the local host is assumed and dbsock is used instead of TCP/IP
 ; to connect to the server.
 ;
 [general]
-dbhost=127.0.0.1
-dbport=5432
-dbname=asterisk
-dbuser=asterisk
-dbpass=password
-;dbappname=asterisk    ; Postgres application_name support (optional). Whitespace not allowed.
-;
-; dbsock is specified as the directory where the socket file may be found. The
-; actual socket is constructed as a combination of dbsock and dbport.  For
+;hostname=localhost
+;port=5432
+;dbname=asterisk
+;user=postgres
+;password=password
+;appname=asterisk    ; Postgres application_name support (optional). Whitespace not allowed.
+
+; socket is specified as the directory where the socket file may be found. The
+; actual socket is constructed as a combination of socket and port.  For
 ; example, the values of '/tmp' and '5432', respectively, will specify a socket
 ; file of '/tmp/.s.PGSQL.5432'.
 ;
-;dbsock=/tmp
+;socket=/tmp
 ;
 ; requirements - At startup, each realtime family will make requirements
 ; on the backend.  There are several strategies for handling requirements:
index 2bdffea2c3e4bbbaf93754fb6033a3b30c0a3d63..cb57ee9f4029046cc0242bb1758b0726e16c3d36 100644 (file)
@@ -1441,7 +1441,7 @@ static int parse_config(int is_reload)
        config = ast_config_load(RES_CONFIG_PGSQL_CONF, config_flags);
        if (config == CONFIG_STATUS_FILEUNCHANGED) {
                if (is_reload && pgsqlConn && PQstatus(pgsqlConn) != CONNECTION_OK) {
-                       ast_log(LOG_WARNING,  "PostgreSQL RealTime: Not connected\n");
+                       ast_log(LOG_WARNING, "PostgreSQL RealTime: Not connected\n");
                }
                return 0;
        }
@@ -1459,60 +1459,79 @@ static int parse_config(int is_reload)
                pgsqlConn = NULL;
        }
 
-       if (!(s = ast_variable_retrieve(config, "general", "dbuser"))) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: No database user found, using 'asterisk' as default.\n");
+       /* Check new 'user' option first, then fall back to legacy 'dbuser' */
+       s = ast_variable_retrieve(config, "general", "user");
+       if (!s) {
+               s = ast_variable_retrieve(config, "general", "dbuser");
+       }
+       if (!s) {
+               ast_log(LOG_WARNING, "PostgreSQL RealTime: No database user found, using 'asterisk' as default.\n");
                strcpy(dbuser, "asterisk");
        } else {
                ast_copy_string(dbuser, s, sizeof(dbuser));
        }
 
-       if (!(s = ast_variable_retrieve(config, "general", "dbpass"))) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: No database password found, using 'asterisk' as default.\n");
+       /* Check new 'password' option first, then fall back to legacy 'dbpass' */
+       s = ast_variable_retrieve(config, "general", "password");
+       if (!s) {
+               s = ast_variable_retrieve(config, "general", "dbpass");
+       }
+       if (!s) {
+               ast_log(LOG_WARNING, "PostgreSQL RealTime: No database password found, using 'asterisk' as default.\n");
                strcpy(dbpass, "asterisk");
        } else {
                ast_copy_string(dbpass, s, sizeof(dbpass));
        }
 
-       if (!(s = ast_variable_retrieve(config, "general", "dbhost"))) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: No database host found, using localhost via socket.\n");
+       /* Check new 'hostname' option first, then fall back to legacy 'dbhost' */
+       s = ast_variable_retrieve(config, "general", "hostname");
+       if (!s) {
+               s = ast_variable_retrieve(config, "general", "dbhost");
+       }
+       if (!s) {
+               ast_log(LOG_WARNING, "PostgreSQL RealTime: No database host found, using localhost via socket.\n");
                dbhost[0] = '\0';
        } else {
                ast_copy_string(dbhost, s, sizeof(dbhost));
        }
 
-       if (!(s = ast_variable_retrieve(config, "general", "dbname"))) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: No database name found, using 'asterisk' as default.\n");
-               strcpy(dbname, "asterisk");
-       } else {
-               ast_copy_string(dbname, s, sizeof(dbname));
+       /* Check new 'port' option first, then fall back to legacy 'dbport' */
+       s = ast_variable_retrieve(config, "general", "port");
+       if (!s) {
+               s = ast_variable_retrieve(config, "general", "dbport");
        }
-
-       if (!(s = ast_variable_retrieve(config, "general", "dbport"))) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: No database port found, using 5432 as default.\n");
+       if (!s) {
+               ast_log(LOG_WARNING, "PostgreSQL RealTime: No database port found, using 5432 as default.\n");
                dbport = 5432;
        } else {
                dbport = atoi(s);
        }
 
-       if (!(s = ast_variable_retrieve(config, "general", "dbappname"))) {
+       /* Check new 'appname' option first, then fall back to legacy 'dbappname' */
+       s = ast_variable_retrieve(config, "general", "appname");
+       if (!s) {
+               s = ast_variable_retrieve(config, "general", "dbappname");
+       }
+       if (!s) {
                dbappname[0] = '\0';
        } else {
                ast_copy_string(dbappname, s, sizeof(dbappname));
        }
 
+       /* Handle socket configuration if no host is specified */
        if (!ast_strlen_zero(dbhost)) {
                /* No socket needed */
-       } else if (!(s = ast_variable_retrieve(config, "general", "dbsock"))) {
-               ast_log(LOG_WARNING,
-                               "PostgreSQL RealTime: No database socket found, using '/tmp/.s.PGSQL.%d' as default.\n", dbport);
-               strcpy(dbsock, "/tmp");
        } else {
-               ast_copy_string(dbsock, s, sizeof(dbsock));
+               s = ast_variable_retrieve(config, "general", "socket");
+               if (!s) {
+                       s = ast_variable_retrieve(config, "general", "dbsock");
+               }
+               if (!s) {
+                       ast_log(LOG_WARNING, "PostgreSQL RealTime: No database socket found, using '/tmp/.s.PGSQL.%d' as default.\n", dbport);
+                       strcpy(dbsock, "/tmp");
+               } else {
+                       ast_copy_string(dbsock, s, sizeof(dbsock));
+               }
        }
 
        if (!(s = ast_variable_retrieve(config, "general", "requirements"))) {