@@ -155,6 +155,7 @@ static int search_default_file_with_ext(Process_option_func func,
155155 const char *dir, const char *ext,
156156 const char *config_file, int recursion_level);
157157static my_bool mysql_file_getline (char *str, int size, MYSQL_FILE *file);
158+ static int check_file_permissions (const char *file_name);
158159
159160
160161/* *
@@ -861,7 +862,7 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
861862 MYSQL_FILE *fp;
862863 uint line=0 ;
863864 my_bool found_group=0 ;
864- uint i;
865+ uint i, rc ;
865866 MY_DIR *search_dir;
866867 FILEINFO *search_file;
867868
@@ -879,25 +880,10 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
879880 strmov (name,config_file);
880881 }
881882 fn_format (name,name," " ," " ,4 );
882- #if !defined(__WIN__)
883- {
884- MY_STAT stat_info;
885- if (!my_stat (name,&stat_info,MYF (0 )))
886- return 1 ;
887- /*
888- Ignore world-writable regular files.
889- This is mainly done to protect us to not read a file created by
890- the mysqld server, but the check is still valid in most context.
891- */
892- if ((stat_info.st_mode & S_IWOTH) &&
893- (stat_info.st_mode & S_IFMT) == S_IFREG)
894- {
895- fprintf (stderr, " Warning: World-writable config file '%s' is ignored\n " ,
896- name);
897- return 0 ;
898- }
899- }
900- #endif
883+
884+ if ((rc= check_file_permissions (name)) < 2 )
885+ return (int ) rc;
886+
901887 if (is_login_file)
902888 {
903889 if ( !(fp = mysql_file_fopen (key_file_cnf, name, (O_RDONLY | O_BINARY),
@@ -1464,3 +1450,48 @@ int my_default_get_login_file(char *file_name, size_t file_name_size)
14641450
14651451 return 1 ;
14661452}
1453+
1454+ /* *
1455+ Check file permissions of the option file.
1456+
1457+ @param file_name [in] Name of the option file.
1458+
1459+ @return 0 - Non-allowable file permissions.
1460+ 1 - Failed to stat.
1461+ 2 - Success.
1462+ */
1463+ static int check_file_permissions (const char *file_name)
1464+ {
1465+ #if !defined(__WIN__)
1466+ MY_STAT stat_info;
1467+
1468+ if (!my_stat (file_name,&stat_info,MYF (0 )))
1469+ return 1 ;
1470+ /*
1471+ Ignore .mylogin.cnf file if not exclusively readable/writable
1472+ by current user.
1473+ */
1474+ if (is_login_file && (stat_info.st_mode & (S_IXUSR | S_IRWXG | S_IRWXO))
1475+ && (stat_info.st_mode & S_IFMT) == S_IFREG)
1476+ {
1477+ fprintf (stderr, " Warning: %s should be readable/writable only by "
1478+ " current user.\n " , file_name);
1479+ return 0 ;
1480+ }
1481+ /*
1482+ Ignore world-writable regular files.
1483+ This is mainly done to protect us to not read a file created by
1484+ the mysqld server, but the check is still valid in most context.
1485+ */
1486+ else if ((stat_info.st_mode & S_IWOTH) &&
1487+ (stat_info.st_mode & S_IFMT) == S_IFREG)
1488+
1489+ {
1490+ fprintf (stderr, " Warning: World-writable config file '%s' is ignored\n " ,
1491+ file_name);
1492+ return 0 ;
1493+ }
1494+ #endif
1495+ return 2 ; /* Success */
1496+ }
1497+
0 commit comments