#include #include #include #include #include #include #include #include #include #include "mysqlconf.h" #include "mysqlconf.m" HELP_FILE mysql_conf_helpfile("mysql", "mysql"); static const char subsys_mysql[]="mysql"; static CONFIG_FILE mycnf_cfg("/etc/my.cnf" ,help_nil,CONFIGF_OPTIONAL ,"root","root",0644,subsys_mysql); static LINUXCONF_SUBSYS sub (subsys_mysql,P_MSG_U(I_SUBMYSQL,"MySQL SQL server")); static MYSQL *mysql_con = NULL; static const char *yesno[] = { "N", "Y" }; static const char *my_func_type[] = { "function", "aggregate" } ; static const char *return_value[] = { "STRING", "REAL", "INTEGER" }; /* Generic utility functions */ static void bsnprintf(char *buffer, size_t size, const char *format, ...) { if (size < 1) return; va_list ap; va_start(ap, format); buffer[size - 1] = 0; vsnprintf(buffer, size - 1, format, ap); va_end (ap); } static void show_error_msg(MYSQL *con) { char err_msg[1024]; bsnprintf(err_msg, 1024, MSG_U(E_MYSQL_CONFAIL, "Error %u while connecting to mysql (%s)"), mysql_errno(mysql_con), mysql_error(mysql_con)); xconf_notice(err_msg); } static int query_printf(MYSQL *con, const char *format, ...) { va_list ap; va_start(ap, format); char buf[8192]; vsprintf(buf, format, ap); // printf("DEBUG: %s\n", buf); if (mysql_query(mysql_con, buf) != 0) { show_error_msg(mysql_con); return -1; } return 0; } static MYSQL *get_connection() { if (mysql_con) return mysql_con; while(1){ SSTRING username, password; DIALOG pass_query; pass_query.newf_str(MSG_U(F_USERNAME, "Username:"), username); pass_query.newf_pass(MSG_U(P_PASSWORD, "Password:"), password); MENU_STATUS button = pass_query.edit(MSG_U(T_CONNECTION, "Connection"), MSG_U(I_CONNECTION, "Enter Username and Password\n" "to connect to database."), help_nil); if (button == MENU_CANCEL || button == MENU_ESCAPE) { return NULL; } if (button == MENU_ACCEPT) { mysql_con = (MYSQL *)malloc(sizeof(MYSQL)); if (!mysql_con) { xconf_notice(MSG_U(E_MYSQLINITFAIL, "Unable to allocate a connection")); return NULL; } mysql_init(mysql_con); if (!mysql_real_connect(mysql_con, "localhost", username.get(), password.get(), "mysql", 0, NULL, 0)) { show_error_msg(mysql_con); free(mysql_con); mysql_con = NULL; return NULL; } return mysql_con; } } } static int find_string(const char *key, const char *strings[], int size) { if (!key || !strings || size <= 0) { return -1; } for (int i = 0; i < size; i++) { if (strcmp(key, strings[i]) == 0) return i; } return -1; } static FIELD_COMBO *user_combo(DIALOG& dia, const char *prompt, SSTRING& var) { FIELD_COMBO *cb = dia.newf_combo(prompt, var); if (!cb) return NULL; if (query_printf(mysql_con, "select distinct user from user where user != ''") != 0) return NULL; MYSQL_RES *cb_rs = mysql_store_result(mysql_con); if (!cb_rs) { show_error_msg(mysql_con); return NULL; } MYSQL_ROW cb_r; while ((cb_r = mysql_fetch_row(cb_rs)) != NULL) { cb->addopt((const char *)cb_r[0]); } return cb; } static FIELD_COMBO *db_combo(DIALOG& dia, const char *prompt, SSTRING& var) { FIELD_COMBO *cb = dia.newf_combo(prompt, var); if (!cb) return NULL; if (query_printf(mysql_con, "show databases") != 0) return NULL; MYSQL_RES *cb_rs = mysql_store_result(mysql_con); if (!cb_rs) { show_error_msg(mysql_con); return NULL; } MYSQL_ROW cb_r; while ((cb_r = mysql_fetch_row(cb_rs)) != NULL) { cb->addopt((const char *)cb_r[0]); } return cb; } static FIELD_COMBO *host_combo(DIALOG &dia, const char *prompt, SSTRING &var) { FIELD_COMBO *cb = dia.newf_combo(prompt, var); if (!cb) return NULL; cb->addopt("localhost"); master_registry.start_session(); cb->addopt(master_registry.get("netbase.hostname")); master_registry.end_session(); return cb; } /* Appends str2 at the end of str1, using sep if len(str1) > 0 */ static char *str_append(char *str1, const char *str2, const char *sep) { if (*str1 != 0) { strcat(str1, sep); } strcat(str1, str2); return str1; } /* Add/Remove/Modify entries in host access table */ static void mysqlconf_host_addone(MYSQL_RES *rs) { MYSQL_FIELD *field = mysql_fetch_fields(rs); DIALOG_MENU dia; SSTRING db, host; // dia.newf_str(MSG_R(F_DBAADDHOSTNAME), host); if (!host_combo(dia, MSG_R(F_DBAADDHOSTNAME), host)) return; if (!db_combo(dia, MSG_R(F_DBAADDDATABASE), db)) return; int priv_num = mysql_num_fields(rs) - 2; char privs[priv_num]; for(int i = 0; i < priv_num; i++) { privs[i] = 0; dia.newf_chk("", privs[i], field[i + 2].name); } if(dia.edit(MSG_U(T_ADDHOST,"Add a host"), MSG_U(I_ADDHOSTEXPL, "Insert the access rights for hosts"), help_nil) == MENU_ACCEPT) { SSTRING myquery; myquery.setfrom("insert into host values('"); myquery.append(host); myquery.append("','"); myquery.append(db); myquery.append("'"); for(int i = 0; i < priv_num; i++) { myquery.append(",'"); myquery.append(yesno[privs[i]]); myquery.append("'"); } myquery.append(")"); query_printf(mysql_con, myquery.get()); query_printf(mysql_con, "flush privileges"); } } static void mysqlconf_host_editone(int n, MYSQL_RES *rs) { mysql_data_seek(rs, n); MYSQL_ROW r = mysql_fetch_row(rs); const char *host = (const char *)r[0]; const char *db = (const char *)r[1]; char header[8192]; bsnprintf(header, sizeof(header), MSG_U(I_EDITHOSTEXPL, "Settings for users from\nhost: %s\naccessing database: %s"), host, db); DIALOG_MENU dia; MYSQL_FIELD *field = mysql_fetch_fields(rs); char privs[mysql_num_fields(rs) - 2]; for(unsigned i = 0; i < (mysql_num_fields(rs) - 2); i++) { privs[i] = find_string((const char *) r[i + 2], yesno, sizeof(yesno)/sizeof(char*)); dia.newf_chk("", privs[i], field[i + 2].name); } int myselect = 0; while (1) { MENU_STATUS n = dia.edit(MSG_U(T_EDITHOST, "Edit access rights"), header, help_nil, myselect, MENU_CANCEL|MENUBUT_ACCEPT|MENUBUT_DEL); switch(n) { case MENU_ACCEPT: { SSTRING myquery("update host set "); unsigned i; for(i = 0; i < mysql_num_fields(rs) - 4; i++){ myquery.append(field[i + 3].name); myquery.append("='"); myquery.append(yesno[privs[i]]); myquery.append("',"); } myquery.append(field[i + 3].name); myquery.append("='"); myquery.append(yesno[privs[i]]); myquery.append("' where db='"); myquery.append(db); myquery.append("' and host='"); myquery.append(host); myquery.append("'"); if (mysql_query(mysql_con, myquery.get()) != 0) { show_error_msg(mysql_con); return; } query_printf(mysql_con, "flush privileges"); return; } case MENU_DEL: if(xconf_delok()) { query_printf(mysql_con, "delete from host where db='%s' and host='%s'", db, host); query_printf(mysql_con, "flush privileges"); } return; case MENU_QUIT: case MENU_ESCAPE: case MENU_CANCEL: return; default: break; } } } static void mysqlconf_host() { mysql_con = get_connection(); if (!mysql_con) return; int nb; MYSQL_RES *res_set; glocal.res_set = NULL; (MSG_U(T_CONFHOST, "Host"), MSG_U(I_CONFHOSTEXPL, "MySQL Host configuration"), help_nil); newf_head(MSG_U(H_HOSTDB, "Host\tDb")); addwhat(""); if (query_printf(mysql_con, "select * from host") != 0) { return; } mysql_free_result (glocal.res_set); glocal.res_set = mysql_store_result(mysql_con); if (glocal.res_set == NULL) { show_error_msg(mysql_con); return; } MYSQL_ROW row; char buf[1024]; glocal.nb = 0; while ((row = mysql_fetch_row(glocal.res_set)) != NULL) { bsnprintf(buf, 1024, "%s", row[1]); new_menuitem((const char *) row[0], buf); glocal.nb++; } mysqlconf_host_editone(no, glocal.res_set); mysqlconf_host_addone(glocal.res_set); mysql_free_result (glocal.res_set); } /* Add/Remove/Modify entries in columns privilege table */ static void mysqlconf_colpriv() { } /* Add/Remove/Modify entries in table priviliege table */ static void mysqlconf_tabpriv_editone(int n, MYSQL_RES *rs) { } static void mysqlconf_tabpriv_addone(MYSQL_RES *rs) { /* MYSQL_FIELD *field = mysql_fetch_fields(rs); */ DIALOG_MENU dia; SSTRING user, db, host, table, grantor, timestamp; char select, insert, update, del, create; char drop, grant, references, index, alter; char tinsert, tupdate, tselect, treferences ; if (!host_combo(dia, MSG_R(F_DBAADDHOSTNAME), host)) return; if (!db_combo(dia, MSG_R(F_DBAADDDATABASE), db)) return; dia.newf_str(MSG_U(F_USER, "User"), user); dia.newf_str(MSG_U(F_TABLE, "Table name"), table); dia.newf_str(MSG_U(F_GRANTOR, "Grantor"), grantor); dia.newf_str(MSG_U(F_TIMESTAMP, "Timestamp"), timestamp); dia.newf_chk(MSG_U(F_COLUMNSPRIV, "Table privileges"), select, MSG_U(F_SELECTPRIV, "Select")); dia.newf_chk("", insert, MSG_U(F_INSERTPRIV, "Insert")); dia.newf_chk("", update, MSG_U(F_UPDATEPRIV, "Update")); dia.newf_chk("", del, MSG_U(F_DELETEPRIV, "Delete")); dia.newf_chk("", create, MSG_U(F_CREATEPRIV, "Create")); dia.newf_chk("", drop, MSG_U(F_DROPPRIV, "Drop")); dia.newf_chk("", grant, MSG_U(F_GRANTPRIV, "Grant")); dia.newf_chk("", references, MSG_U(F_REFPRIV, "References")); dia.newf_chk("", index, MSG_U(F_INDEXTPRIV, "Index")); dia.newf_chk("", alter, MSG_U(F_ALTERPRIV, "Alter")); dia.newf_chk(MSG_U(F_TABLESPRIV, "Columns privileges"), tselect, MSG_R(F_SELECTPRIV)); dia.newf_chk("", tinsert, MSG_R(F_INSERTPRIV)); dia.newf_chk("", tupdate, MSG_R(F_UPDATEPRIV)); dia.newf_chk("", treferences, MSG_R(F_REFPRIV)); if(dia.edit(MSG_U(T_TABPRIVADD, "Configure Tables privileges"), MSG_U(I_TABPRIVADD, "Add a new row in the Tables_priv table to configure \n" "the tables privileges."), help_nil) == MENU_ACCEPT) { // char buf[1024]; query_printf(mysql_con, "insert into tables_priv values('%s', '%s', '%s', '%s', '%s', '%s')", host.get(), db.get(), user.get(), table.get(), grantor.get(), timestamp.get()); query_printf(mysql_con, "flush privileges"); } } static void mysqlconf_tabpriv() { mysql_con = get_connection(); if (!mysql_con) return; int nb; MYSQL_RES *res_set; glocal.res_set = NULL; (MSG_U(T_TABPRIV, "Configure Tables privileges"), MSG_U(I_TABPRIV, "You can now add, remove or modify\n" "a new row of tables privileges"), help_nil); newf_head(MSG_U(H_TABPRIVHEAD, "Host\tDb\tUser\tTable\tGrantor\tTimestamp\tTable_priv\tColumn_priv")); addwhat(""); if (query_printf(mysql_con, "select * from tables_priv") != 0) { return; } mysql_free_result (glocal.res_set); glocal.res_set = mysql_store_result(mysql_con); if (glocal.res_set == NULL) { show_error_msg(mysql_con); return; } MYSQL_ROW row; char buf[1024]; glocal.nb = 0; while ((row = mysql_fetch_row(glocal.res_set)) != NULL) { bsnprintf(buf, 1024, "%s\t%s\t%s\t%s\t%s\t%s\t%s", row[1], row[2], row[3], row[4], row[5], row[6], row[7]); new_menuitem((const char *) row[0], buf); glocal.nb++; } mysqlconf_tabpriv_editone(no, glocal.res_set); mysqlconf_tabpriv_addone(glocal.res_set); mysql_free_result (glocal.res_set); } /* Add/Remove/Modify entries in user def function table */ static void mysqlconf_func_editone(int n, MYSQL_RES *rs) { DIALOG_MENU dia; mysql_data_seek(rs,n); MYSQL_ROW r = mysql_fetch_row(rs); int ret = (int) atoi(r[1]); SSTRING dl((const char *) r[2]); char type = find_string((const char *) r[3], my_func_type, sizeof(my_func_type) / sizeof(char *)); dia.newf_info(MSG_U(F_FUNCEDITNAME, "Name"), (const char *)r[0]); dia.newf_num(MSG_U(F_FUNCEDITRET, "Return Type"), ret); dia.newf_str(MSG_U(F_FUNCEDIT, "Object File"), dl); dia.newf_radio("", type, 0, MSG_U(F_FUNCEDITFUNC, "function")); dia.newf_radio("", type, 1, MSG_U(F_FUNCEDITAGGR, "aggregate")); int my_selection = 0; MENU_STATUS button = dia.edit(MSG_U(T_FUNCEDIT, "Modify a User Defined Function"), MSG_U(I_FUNCEDIT, "You can modify the fields for\n" "this User Defined Function"), help_nil, my_selection, MENUBUT_CANCEL|MENUBUT_ACCEPT|MENUBUT_DEL); switch(button) { case MENU_ACCEPT: query_printf(mysql_con, "update func set ret=%d,dl='%s',type='%s' where name='%s'", ret, dl.get(), my_func_type[type], (const char *)r[0]); break; case MENU_DEL: if(xconf_delok()) { query_printf(mysql_con, "delete from func where name='%s'", (const char *) r[0]); } break; default: break; } } static void mysqlconf_func_addone(MYSQL_RES *rs) { DIALOG_MENU dia; SSTRING func_name, so_name, ret_type; char func_type; dia.newf_str(MSG_U(F_FUNCADDNAME, "Name"), func_name); dia.newf_str(MSG_U(F_FUNCADDDDL, "Object File"), so_name); FIELD_COMBO *combo = dia.newf_combo(MSG_U(F_FUNCADDRET, "Return Type"), ret_type); combo->addopt("0", MSG_U(F_STRING, "String")); combo->addopt("1", MSG_U(F_REAL, "Real")); combo->addopt("2", MSG_U(F_INTEGER, "Integer")); dia.newf_radio("", func_type, 0, MSG_U(F_FUNCADDFUNC, "function")); dia.newf_radio("", func_type, 1, MSG_U(F_FUNCADDAGGR, "aggregate")); MENU_STATUS button = dia.edit(MSG_U(T_FUNCADD, "Add an User Defined Function"), MSG_U(I_FUNCADD, "You can use this dialog to create\n" "a new user defined function, by specifing\n" "the name, the return type, the object file\n" "containing the code, and the type"), help_nil); int x = func_type; switch(button) { case MENU_ACCEPT: query_printf(mysql_con, "insert into func values('%s', %s, '%s', '%s')", func_name.get(), ret_type.get(), so_name.get(), my_func_type[x]); break; default: break; } } static void mysqlconf_func() { mysql_con = get_connection(); if (!mysql_con) return; int nb; MYSQL_RES *res_set; glocal.res_set = NULL; (MSG_U(T_FUNCCONF, "Configure User Defined Function"), MSG_U(I_FUNCCONF, "Add, remove or modify User Defined Functions"), help_nil); newf_head(MSG_U(H_FUNCCONF, "Name\tRet\tSO Name\tType")); addwhat(""); if (query_printf(mysql_con, "select * from func") != 0) { return; } mysql_free_result (glocal.res_set); glocal.res_set = mysql_store_result(mysql_con); if (glocal.res_set == NULL) { show_error_msg(mysql_con); return; } MYSQL_ROW row; char buf[1024]; buf[1023] = 0; glocal.nb = 0; while ((row = mysql_fetch_row(glocal.res_set)) != NULL) { snprintf(buf, 1023, "%s\t%s\t%s", return_value[atoi(row[1])], row[2], (const char *) row[3]); new_menuitem((const char *) row[0], buf); glocal.nb++; } mysqlconf_func_editone(no, glocal.res_set); mysqlconf_func_addone(glocal.res_set); mysql_free_result (glocal.res_set); } /* Add/Remove/Modify entries in DB table */ static void mysqlconf_dbaccess_editone(int n, MYSQL_RES *rs) { mysql_data_seek(rs, n); MYSQL_ROW r = mysql_fetch_row(rs); const char *host = (const char *)r[0]; const char *db = (const char *)r[1]; const char *user = (const char *)r[2]; char header[8192]; sprintf(header, MSG_U(I_DBAEDIT, "Settings for user %s@%s accessing %s"), user, host, db); DIALOG_MENU dia; MYSQL_FIELD *field = mysql_fetch_fields(rs); char privs[mysql_num_fields(rs) - 3]; for(unsigned i = 0; i < (mysql_num_fields(rs) - 3); i++) { privs[i] = strcmp ((const char *) r[i + 3], "N"); dia.newf_chk("", privs[i], field[i + 3].name); } int myselect = 0; while (1) { MENU_STATUS n = dia.edit(MSG_U(T_DBAEDIT, "Edit access to databases"), header, help_nil, myselect, MENUBUT_CANCEL|MENUBUT_ACCEPT|MENUBUT_DEL); if (n == MENU_CANCEL || n == MENU_ESCAPE) break; switch(n) { case MENU_ACCEPT: { SSTRING myquery("update db set "); unsigned i; for(i = 0; i < mysql_num_fields(rs) - 4; i++){ myquery.append(field[i + 3].name); myquery.append("='"); myquery.append(yesno[privs[i]]); myquery.append("',"); } myquery.append(field[i + 3].name); myquery.append("='"); myquery.append(yesno[privs[i]]); myquery.append("' where user='"); myquery.append(user); myquery.append("' and db='"); myquery.append(db); myquery.append("' and host='"); myquery.append(host); myquery.append("'"); // printf("DEBUG: %s\n", myquery.get()); if (mysql_query(mysql_con, myquery.get()) != 0) { show_error_msg(mysql_con); return; } query_printf(mysql_con, "flush privileges"); return; } case MENU_DEL: if(xconf_delok()) { query_printf(mysql_con, "delete from db where user='%s' and db='%s' and host='%s'", user, db, host); query_printf(mysql_con, "flush privileges"); } return; case MENU_QUIT: case MENU_ESCAPE: case MENU_CANCEL: return; default: break; } } } static void mysqlconf_dbaccess_addone(MYSQL_RES *rs) { MYSQL_FIELD *field = mysql_fetch_fields(rs); DIALOG_MENU dia; SSTRING user, db, host; if (!host_combo(dia,MSG_U(F_DBAADDHOSTNAME, "Host"), host)) return; if (!db_combo(dia, MSG_U(F_DBAADDDATABASE, "Database"), db)) return; if (!user_combo(dia, MSG_U(F_DBAADDUSER, "Username"), user)) return; int priv_num = mysql_num_fields(rs) - 3; char privs[priv_num]; for(int i = 0; i < priv_num; i++) { privs[i] = 0; dia.newf_chk("", privs[i], field[i + 3].name); } if(dia.edit(MSG_U(T_DBAADD, "Add access to databases"), MSG_U(I_DBAADD, "Insert the access rights to a database\n" "based on the user which is connecting\n" "the host which he connects from\n" "and the database itself"), help_nil) == MENU_ACCEPT) { SSTRING myquery; myquery.setfrom("insert into db values('"); myquery.append(host); myquery.append("','"); myquery.append(db); myquery.append("','"); myquery.append(user); myquery.append("'"); for(int i = 0; i < priv_num; i++) { myquery.append(",'"); myquery.append(yesno[privs[i]]); myquery.append("'"); } myquery.append(")"); query_printf(mysql_con, myquery.get()); query_printf(mysql_con, "flush privileges"); } } static void mysqlconf_dbaccess() { mysql_con = get_connection(); if (!mysql_con) return; int nb; MYSQL_RES *res_set; glocal.res_set = NULL; (MSG_U(T_DBA, "Configure access to databases"), MSG_U(I_DBA, "You can add, delete or modify\n" "the access rights to databases, based on\n" "the name of the database, the user and the\n" "host the user is connecting from."), help_nil); newf_head(MSG_U(H_DBAHEAD, "Host\tDB\tUser")); addwhat(""); if (query_printf(mysql_con, "select * from db") != 0) { return; } mysql_free_result (glocal.res_set); glocal.res_set = mysql_store_result(mysql_con); if (glocal.res_set == NULL) { show_error_msg(mysql_con); return; } MYSQL_ROW row; char buf[1024]; buf[1023] = 0; glocal.nb = 0; while ((row = mysql_fetch_row(glocal.res_set)) != NULL) { snprintf(buf, 1023, "%s\t%s", row[1], row[2]); new_menuitem((const char *) row[0], buf); glocal.nb++; } mysqlconf_dbaccess_editone(no, glocal.res_set); mysqlconf_dbaccess_addone(glocal.res_set); mysql_free_result (glocal.res_set); } /* Add/Remove/Modify users */ static void mysqlconf_user_changepw(int n, MYSQL_RES *rs) { SSTRING user, password1, password2; MENU_STATUS button; mysql_data_seek(rs, n); MYSQL_ROW r = mysql_fetch_row(rs); user.setfrom((const char *)r[1]); do { DIALOG dia; dia.newf_pass(MSG_U(P_NEWPASSWORD, "New password"), password1); dia.newf_pass(MSG_U(P_NEWPASSWORDR, "Retype new password"), password2); button = dia.edit(MSG_U(T_CHANGEPASSWORD, "Change Password"), MSG_U(I_CHANGEPASSWORD, "type twice the new password you want to use\n"), help_nil); } while( (password1.cmp(password2)) && (button != MENU_CANCEL) ); if(button != MENU_CANCEL) { query_printf(mysql_con, "update user set password=password('%s') where user='%s'", password1.get(), user.get()); query_printf(mysql_con, "flush privileges"); } } static void mysqlconf_user_editone(int n, MYSQL_RES *rs) { mysql_data_seek(rs, n); MYSQL_ROW r = mysql_fetch_row(rs); const char *host = (const char *)r[0]; const char *user = (const char *)r[1]; fprintf (stderr,"host %s %s\n",host,user); char userathost[8192]; bsnprintf(userathost, sizeof(userathost), MSG_U(T_MYSQLUSERED, "Settings for user %s@%s"), user, host); DIALOG_MENU dia; dia.setbutinfo(MENU_USR1, MSG_U(B_MYSQLUSRPASSWD, "Password"), "Password"); MYSQL_FIELD *field = mysql_fetch_fields(rs); int numf = mysql_num_fields (rs); char privs[numf]; for(int i = 3; i < numf; i++) { privs[i] = strcmp ((const char *) r[i], "N"); dia.newf_chk("", privs[i], field[i].name); } int myselect = 0; while (1) { MENU_STATUS ms = dia.edit(MSG_U(T_MODIFYUSERSET, "Modify user settings"), userathost, help_nil, myselect, MENUBUT_CANCEL|MENUBUT_ACCEPT|MENUBUT_DEL|MENUBUT_USR1); if (n == MENU_CANCEL || n == MENU_ESCAPE) break; switch(ms) { case MENU_ACCEPT: { SSTRING myquery("update user set "); int i; for(i = 3; i < numf-1; i++){ myquery.appendf("%s='%s',",field[i].name,yesno[privs[i]]); } myquery.appendf("%s='%s'",field[i].name,yesno[privs[i]]); myquery.appendf(" where user=\"%s\" and host=\"%s\"" ,user,host); printf("DEBUG: %s\n", myquery.get()); if (mysql_query(mysql_con, myquery.get()) != 0) { show_error_msg(mysql_con); return; } query_printf(mysql_con, "flush privileges"); return; } case MENU_DEL: if(xconf_delok()) { query_printf(mysql_con, "delete from user where user=\"%s\" and host=\"%s\"", user, host); query_printf(mysql_con, "flush privileges"); } return; case MENU_USR1: mysqlconf_user_changepw(n, rs); break; case MENU_QUIT: case MENU_ESCAPE: case MENU_CANCEL: return; default: break; } } } static void mysqlconf_user_addone(MYSQL_RES *rs) { MYSQL_FIELD *field = mysql_fetch_fields(rs); int numf = mysql_num_fields(rs); DIALOG_MENU dia; SSTRING user, host, password; if (!host_combo(dia, MSG_U(F_USERADDHOST, "Host"), host)) return; if (!user_combo(dia, MSG_U(F_USERADDUSER, "User"), user)) return; dia.newf_str(MSG_U(P_USERADDPASS, "Password"), password); char privs[numf]; for(int i = 3; i < numf; i++) { privs[i] = 0; dia.newf_chk("", privs[i], field[i].name); } if(dia.edit(MSG_U(T_USERADD, "Adding a MySQL user"), MSG_U(I_USERADD, "Set up the user entry according to\n" "the username to be used, the host the user\n" "will connect from and the password to be used\n"), help_nil) == MENU_ACCEPT) { SSTRING myquery; myquery.setfrom("insert into user values(\""); myquery.append(host); myquery.append("\",\""); myquery.append(user); myquery.append("\",Password(\""); myquery.append(password); myquery.append("\")"); for(int i = 3; i < numf; i++) { myquery.appendf(",'%s'",yesno[privs[i]]); } myquery.append(")"); query_printf(mysql_con, myquery.get()); query_printf(mysql_con, "flush privileges"); } } static void mysqlconf_user() { mysql_con = get_connection(); if (!mysql_con) return; int nb; MYSQL_RES *res_set; glocal.res_set = NULL; (MSG_U(T_USERS, "MySQL users"), MSG_U(I_USERS, "Click on the user name\n" "to edit its properties.\n"), help_nil); newf_head(MSG_U(H_USERCONF, "Host\tUser")); addwhat(""); if (query_printf(mysql_con, "select * from user") != 0) { return; } mysql_free_result (glocal.res_set); glocal.res_set = mysql_store_result(mysql_con); if (glocal.res_set == NULL) { show_error_msg(mysql_con); return; } MYSQL_ROW row; glocal.nb = 0; while ((row = mysql_fetch_row(glocal.res_set)) != NULL) { new_menuitem((const char *) row[0], (const char *) row[1]); glocal.nb++; } mysqlconf_user_editone(no, glocal.res_set); mysqlconf_user_addone(glocal.res_set); mysql_free_result (glocal.res_set); } /* Add/Remove databases */ static void mysqlconf_db_editone(int no, MYSQL_RES *rs) { if (xconf_delok()) { mysql_data_seek(rs, no); MYSQL_ROW r = mysql_fetch_row(rs); if (r != NULL) { query_printf(mysql_con, "drop database %s", (char *) r[0]); } } } static void mysqlconf_db_addone() { DIALOG dia; SSTRING dbname; dia.newf_str("Database Name:", dbname); int my_selection = 0; int ret = dia.edit(MSG_U(T_DBADD, "Add a new database"), MSG_U(I_DBADD, "Enter name of the database\nto be added"), help_nil, my_selection, MENUBUT_CANCEL|MENUBUT_ACCEPT); if(ret == MENU_ACCEPT) { if(!dbname.is_empty()) { query_printf(mysql_con, "create database %s", dbname.get()); } } } static void mysqlconf_db() { mysql_con = get_connection(); if (!mysql_con) return; int nb; MYSQL_RES *res_set; glocal.res_set = NULL; (MSG_U(T_DBCONF, "Databases"), MSG_U(I_DBCONF,"Click onto the database you want to remove.\n"), help_nil); newf_head(MSG_U(H_MYSQLDBCONF, "Databases")); addwhat(""); if (mysql_query(mysql_con, "show databases") != 0) { show_error_msg(mysql_con); return; } mysql_free_result (glocal.res_set); glocal.res_set = mysql_store_result(mysql_con); if (glocal.res_set == NULL) { show_error_msg(mysql_con); return; } MYSQL_ROW row; glocal.nb = 0; while ((row = mysql_fetch_row(glocal.res_set)) != NULL) { new_menuitem((const char *) row[0], (const char *) row[0]); glocal.nb++; } mysqlconf_db_editone(no, glocal.res_set); mysqlconf_db_addone(); mysql_free_result (glocal.res_set); } static void mysqlconf_server() { DIALOG dia; // MENU_STATUS my_button; SSTRING csocket, ssocket; SSTRING basedir, bind_address, character_set_dir, chroot_dir, datadir, default_charset, default_table_type, init_file; int cport, sport; CONFDB mycnf(mycnf_cfg, true, '#'); mycnf.setcursys("client", true); csocket.setfrom(mycnf.getval(NULL, "socket", "/var/lib/mysql/mysql.sock")); cport = mycnf.getvalnum(NULL, "port", 3306); mycnf.setcursys("mysqld", true); ssocket.setfrom(mycnf.getval(NULL, "socket", "/var/lib/mysql/mysql.sock")); sport = mycnf.getvalnum(NULL, "port", 3306); basedir.setfrom(mycnf.getval(NULL, "basedir", "/")); bind_address.setfrom(mycnf.getval(NULL, "bind-address", "*")); // character_set_dir.setfrom(mycnf.getval(NULL, "language", "/usr/share/mysql/english/")); dia.newf_title(MSG_U(T_CLIENT, "Client"), 1, "", MSG_R(T_CLIENT)); dia.newf_str(MSG_U(F_CSOCKET, "Socket"), csocket); dia.newf_num(MSG_U(F_CPORT, "Port"), cport); dia.newf_title(MSG_U(T_SERVER, "Server"), 1, "", MSG_R(T_SERVER)); dia.newf_str(MSG_U(F_SSOCKET, "Socket"), ssocket); dia.newf_num(MSG_U(F_SPORT, "Port"), sport); dia.newf_str(MSG_U(F_BASEDIR, "Base dir"), basedir); dia.newf_str(MSG_U(F_BIND_ADDRESS, "Bind address"), bind_address); // dia.newf_str(MSG_U(F_CHARACTER_SET_DIR, "Character set dir"), character_set_dir); int my_selection; MENU_STATUS code = dia.edit(MSG_U(T_SERVERCONF, "Global server configuration"), MSG_U(I_SERVERCONF,"This dialog allows you to specify parameters\nwhich are globally valid for MySQL"), help_nil, my_selection, MENUBUT_ACCEPT|MENUBUT_CANCEL); if (code == MENU_ESCAPE || code == MENU_QUIT || code == MENU_CANCEL){ return; } else { mycnf.setcursys("client", true); mycnf.replace(NULL, "port", cport); mycnf.replace(NULL, "socket", csocket); mycnf.save(); mycnf.setcursys("mysqld", true); mycnf.replace(NULL, "port", sport); mycnf.replace(NULL, "socket", ssocket); mycnf.replace(NULL, "basedir", basedir); mycnf.replace(NULL, "bind-address", bind_address); // mycnf.replace(NULL, "character-sets-dir", character_set_dir); mycnf.save(); } } void mysqlconf_conf() { MENU_STATUS my_button; enum choices my_selection = SERVCONF; DIALOG_MENU dia_mymenu; dia_mymenu.new_menuitem("", MSG_U(M_MYSQLSERVER, "Configure Server")); dia_mymenu.new_menuitem("", MSG_U(M_MYSQLDB, "Configure Databases")); dia_mymenu.new_menuitem("", MSG_U(M_MYSQLUSERS, "Configure user based access")); dia_mymenu.new_menuitem("", MSG_U(M_MYSQLHOST, "Configure host based access")); dia_mymenu.new_menuitem("", MSG_U(M_MYSQLDBACCESS, "Configure db based access")); dia_mymenu.new_menuitem("", MSG_U(M_MYSQLFUNC, "Configure user defined functions")); // dia_mymenu.new_menuitem("", MSG_U(M_MYSQLCOLPRIV, "Configure Columns_priv")); // dia_mymenu.new_menuitem("", MSG_U(M_MYSQLTABPRIV, "Configure Tables_priv")); while(1) { my_button = dia_mymenu.editmenu(MSG_U(T_MYSQLDIALOGTT, "MySQL setup"), MSG_U(I_MYSQLDIALOGTT, "This module will allow you to configure MySQL"), mysql_conf_helpfile, (int &)my_selection, MENUBUT_QUIT); if((my_button==MENU_QUIT)||(my_button==MENU_ESCAPE)){ return; } switch(my_selection) { case SERVCONF: mysqlconf_server(); break; case DBCONF: mysqlconf_db(); break; case USERCONF: mysqlconf_user(); break; case DBACCONF: mysqlconf_dbaccess(); break; case HOSTCONF: mysqlconf_host(); break; case FUNCONF: mysqlconf_func(); break; /* case COLPCONF: mysqlconf_colpriv(); break; case TABPCONF: mysqlconf_tabpriv(); break; */ } } } #include static REGISTER_VARIABLE_LOOKUP_MSG mysql_var_list[]={ {"client_port",NULL,P_MSG_R(F_CPORT),mysqlconf_server,NULL}, {"client_socket",NULL,P_MSG_R(F_CSOCKET),mysqlconf_server,NULL}, {"server_port",NULL,P_MSG_R(F_SPORT),mysqlconf_server,NULL}, {"server_socket",NULL,P_MSG_R(F_SSOCKET),mysqlconf_server,NULL}, { NULL, NULL, NULL, NULL } }; static REGISTER_VARIABLES mysql_registry("mysql",mysql_var_list);