If this is a valid plugin return full path name, otherwise NULL */
| 1855 | |
| 1856 | /* If this is a valid plugin return full path name, otherwise NULL */ |
| 1857 | static const char *plugin_fullpath(const tal_t *ctx, const char *dir, |
| 1858 | const char *basename) |
| 1859 | { |
| 1860 | struct stat st; |
| 1861 | const char *fullname; |
| 1862 | struct utf8_state utf8 = UTF8_STATE_INIT; |
| 1863 | |
| 1864 | for (size_t i = 0; basename[i]; i++) { |
| 1865 | if (!utf8_decode(&utf8, basename[i])) |
| 1866 | continue; |
| 1867 | /* Not valid UTF8? Let's not go there... */ |
| 1868 | if (errno != 0) |
| 1869 | return NULL; |
| 1870 | if (utf8.used_len != 1) |
| 1871 | continue; |
| 1872 | if (!cispunct(utf8.c)) |
| 1873 | continue; |
| 1874 | if (utf8.c != '-' && utf8.c != '_' && utf8.c != '.') |
| 1875 | return NULL; |
| 1876 | } |
| 1877 | |
| 1878 | fullname = path_join(ctx, dir, basename); |
| 1879 | if (stat(fullname, &st) != 0) |
| 1880 | return tal_free(fullname); |
| 1881 | /* Only regular files please (or symlinks to such: stat not lstat!) */ |
| 1882 | if ((st.st_mode & S_IFMT) != S_IFREG) |
| 1883 | return tal_free(fullname); |
| 1884 | /* Must be executable by someone. */ |
| 1885 | if (!(st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH))) |
| 1886 | return tal_free(fullname); |
| 1887 | |
| 1888 | /* Someone actually runs this on NTFS, where everything apparently is |
| 1889 | * executable! This prevents the most obvious damage. */ |
| 1890 | if (streq(basename, "README.md")) |
| 1891 | return tal_free(fullname); |
| 1892 | |
| 1893 | return fullname; |
| 1894 | } |
| 1895 | |
| 1896 | char *add_plugin_dir(struct plugins *plugins, const char *dir, bool error_ok) |
| 1897 | { |
no test coverage detected