+static int path_ok(const char *dir)
+{
+ const char *p = dir;
+ char **pp;
+ int sl = 1, ndot = 0;
+
+ for (;;) {
+ if ( *p == '.' ) {
+ ndot++;
+ } else if ( *p == '/' || *p == '\0' ) {
+ if ( sl && ndot > 0 && ndot < 3 )
+ return 0; /* . or .. in path */
+ sl = 1;
+ if ( *p == '\0' )
+ break; /* End of string and all is good */
+ } else {
+ sl = ndot = 0;
+ }
+ p++;
+ }
+
+ if ( ok_paths && *ok_paths ) {
+ int ok = 0;
+ int dirlen = strlen(dir); /* read_packet_line can return embedded \0 */
+
+ for ( pp = ok_paths ; *pp ; pp++ ) {
+ int len = strlen(*pp);
+ if ( len <= dirlen &&
+ !strncmp(*pp, dir, len) &&
+ (dir[len] == '/' || dir[len] == '\0') ) {
+ ok = 1;
+ break;
+ }
+ }
+
+ if ( !ok )
+ return 0; /* Path not in whitelist */
+ }
+
+ return 1; /* Path acceptable */
+}