]> git.8kb.co.uk Git - pgpool-ii/pgpool-ii_2.2.5/blob - pcp/pcp_systemdb_info.c
Attempt to send a proper failure message to frontend when authentication
[pgpool-ii/pgpool-ii_2.2.5] / pcp / pcp_systemdb_info.c
1 /*
2  * $Header: /cvsroot/pgpool/pgpool-II/pcp/pcp_systemdb_info.c,v 1.3 2008/12/31 10:25:40 t-ishii Exp $
3  *
4  * pgpool: a language independent connection pool server for PostgreSQL 
5  * written by Tatsuo Ishii
6  *
7  * Copyright (c) 2003-2008      PgPool Global Development Group
8  *
9  * Permission to use, copy, modify, and distribute this software and
10  * its documentation for any purpose and without fee is hereby
11  * granted, provided that the above copyright notice appear in all
12  * copies and that both that copyright notice and this permission
13  * notice appear in supporting documentation, and that the name of the
14  * author not be used in advertising or publicity pertaining to
15  * distribution of the software without specific, written prior
16  * permission. The author makes no representations about the
17  * suitability of this software for any purpose.  It is provided "as
18  * is" without express or implied warranty.
19  *
20  * Client program to send "systemDB info" command.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "pcp.h"
29
30 static void usage(void);
31 static void myexit(ErrorCode e);
32
33 int
34 main(int argc, char **argv)
35 {
36         long timeout;
37         char host[MAX_DB_HOST_NAMELEN];
38         int port;
39         char user[MAX_USER_PASSWD_LEN];
40         char pass[MAX_USER_PASSWD_LEN];
41         SystemDBInfo *systemdb_info;
42         int i, j;
43         int ch;
44
45         while ((ch = getopt(argc, argv, "hd")) != -1) {
46                 switch (ch) {
47                 case 'd':
48                         pcp_enable_debug();
49                         break;
50
51                 case 'h':
52                 case '?':
53                 default:
54                         usage();
55                         exit(0);
56                 }
57         }
58         argc -= optind;
59         argv += optind;
60
61         if (argc != 5)
62         {
63                 errorcode = INVALERR;
64                 pcp_errorstr(errorcode);
65                 myexit(errorcode);
66         }
67
68         timeout = atol(argv[0]);
69         if (timeout < 0) {
70                 errorcode = INVALERR;
71                 pcp_errorstr(errorcode);
72                 myexit(errorcode);
73         }
74
75         if (strlen(argv[1]) >= MAX_DB_HOST_NAMELEN)
76         {
77                 errorcode = INVALERR;
78                 pcp_errorstr(errorcode);
79                 myexit(errorcode);
80         }
81         strcpy(host, argv[1]);
82
83         port = atoi(argv[2]);
84         if (port <= 1024 || port > 65535)
85         {
86                 errorcode = INVALERR;
87                 pcp_errorstr(errorcode);
88                 myexit(errorcode);
89         }
90
91         if (strlen(argv[3]) >= MAX_USER_PASSWD_LEN)
92         {
93                 errorcode = INVALERR;
94                 pcp_errorstr(errorcode);
95                 myexit(errorcode);
96         }
97         strcpy(user, argv[3]);
98
99         if (strlen(argv[4]) >= MAX_USER_PASSWD_LEN)
100         {
101                 errorcode = INVALERR;
102                 pcp_errorstr(errorcode);
103                 myexit(errorcode);
104         }
105         strcpy(pass, argv[4]);
106
107         if (pcp_connect(host, port, user, pass))
108         {
109                 pcp_errorstr(errorcode);
110                 myexit(errorcode);
111         }
112
113         if ((systemdb_info = pcp_systemdb_info()) == NULL)
114         {
115                 pcp_errorstr(errorcode);
116                 pcp_disconnect();
117                 myexit(errorcode);
118         } else {
119                 printf("%s %d %s %s %s %s %d %d\n", 
120                            systemdb_info->hostname,
121                            systemdb_info->port,
122                            systemdb_info->user,
123                            systemdb_info->password[0] == '\0' ? "''" : systemdb_info->password,
124                            systemdb_info->schema_name,
125                            systemdb_info->database_name,
126                            systemdb_info->dist_def_num,
127                            systemdb_info->system_db_status);
128
129                 for (i = 0; i < systemdb_info->dist_def_num; i++)
130                 {
131                         DistDefInfo *ddi = &systemdb_info->dist_def_slot[i];
132
133                         printf("%s %s %s %s %d ",
134                                    ddi->dbname,
135                                    ddi->schema_name,
136                                    ddi->table_name,
137                                    ddi->dist_key_col_name,
138                                    ddi->col_num);
139
140                         for (j = 0; j < ddi->col_num; j++)
141                                 printf("%s ", ddi->col_list[j]);
142
143                         for (j = 0; j < ddi->col_num; j++)
144                                 printf("%s ", ddi->type_list[j]);
145                         
146                         printf("%s\n", ddi->dist_def_func);
147                 }
148                 
149                 free_systemdb_info(systemdb_info);
150         }
151
152         pcp_disconnect();
153
154         return 0;
155 }
156
157 static void
158 usage(void)
159 {
160         fprintf(stderr, "pcp_systemdb_info - display the pgpool-II systemDB information\n\n");
161         fprintf(stderr, "Usage: pcp_systemdb_info [-d] timeout hostname port# username password\n");
162         fprintf(stderr, "Usage: pcp_systemdb_info -h\n\n");
163         fprintf(stderr, "  -d       - enable debug message (optional)\n");
164         fprintf(stderr, "  timeout  - connection timeout value in seconds. command exits on timeout\n");
165         fprintf(stderr, "  hostname - pgpool-II hostname\n");
166         fprintf(stderr, "  port#    - pgpool-II port number\n");
167         fprintf(stderr, "  username - username for PCP authentication\n");
168         fprintf(stderr, "  password - password for PCP authentication\n");
169         fprintf(stderr, "  -h       - print this help\n");
170 }
171
172 static void
173 myexit(ErrorCode e)
174 {
175         if (e == INVALERR)
176         {
177                 usage();
178                 exit(e);
179         }
180
181         exit(e);
182 }