From 800c7a6f26f0f97dc5fbfb254d007488ab4416dd Mon Sep 17 00:00:00 2001 From: Mario Fetka Date: Sat, 30 May 2026 06:24:19 +0000 Subject: [PATCH] nwconn: implement AFP scan file information Wire NCP 0x23/0x11 AFP 2.0 Scan File Information to a conservative read-only directory scan. The WebSDK documents NWAFPScanFileInformation as taking a volume number, AFP base entry id, last-seen AFP id, search mask, request mask, path string, and file-info buffer. The call scans a directory relative to the AFP base and returns AFP file information for the next matching entry, updating the last-seen id for the caller's next iteration. Reuse the existing AFP file information formatter and libatalk/stat-derived entry-id path. For now, support path-backed scans only: resolve the supplied SYS:-style path through the mars_nwe path machinery, enumerate one child directory entry after the supplied last-seen id, and return a last-seen id plus an AFP file-info record. Entry-id-only scans, persistent CNID lookup, and full AFP search-mask semantics remain future work. Add a Linux afp_scan_info_smoke helper using ncpfs/libncp so the new endpoint can be exercised without an AppleTalk client. This implements the documented read-only scan path needed for directory browsing smoke tests, while keeping write-side AFP and persistent Mac namespace semantics unchanged. --- src/nwconn.c | 251 +++++++++++++++++++++++----- tests/linux/CMakeLists.txt | 4 + tests/linux/afp_scan_info_smoke.c | 266 ++++++++++++++++++++++++++++++ 3 files changed, 478 insertions(+), 43 deletions(-) create mode 100644 tests/linux/afp_scan_info_smoke.c diff --git a/src/nwconn.c b/src/nwconn.c index f811cb4..ed96741 100644 --- a/src/nwconn.c +++ b/src/nwconn.c @@ -621,6 +621,62 @@ static uint16 afp_count_offspring(const char *unixname, const struct stat *stb) return((uint16)count); } +static int afp_fill_file_info_response(const char *unixname, + const uint8 *display_path, + int display_path_len, + int volume, + uint8 *response, + uint32 *entry_id_out, + int *fallback_out) +{ + struct stat stbuff; + uint32 entry_id = 0; + uint32 parent_id = 0; + uint32 resource_size = 0; + uint8 finder_info[NWATALK_FINDER_INFO_LEN]; + int result; + + if (stat(unixname, &stbuff)) + return(-0x9c); /* Invalid Path */ + + result = nwatalk_get_entry_id(unixname, &entry_id); + if (result < 0 || !entry_id) { + entry_id = afp_fallback_entry_id(volume, &stbuff); + if (fallback_out) *fallback_out = 1; + } else if (fallback_out) *fallback_out = 0; + + memset(response, 0, 120); + U32_TO_BE32(entry_id, response + 0); + U32_TO_BE32(parent_id, response + 4); + U16_TO_BE16(afp_basic_attributes(&stbuff), response + 8); + U32_TO_BE32(S_ISDIR(stbuff.st_mode) ? 0 : (uint32)stbuff.st_size, response + 10); + + if (!S_ISDIR(stbuff.st_mode)) + (void)nwatalk_get_resource_fork_size(unixname, &resource_size); + U32_TO_BE32(resource_size, response + 14); + U16_TO_BE16(afp_count_offspring(unixname, &stbuff), response + 18); + + un_date_2_nw(stbuff.st_ctime, response + 20, 1); + un_date_2_nw(stbuff.st_atime, response + 22, 1); + un_date_2_nw(stbuff.st_mtime, response + 24, 1); + un_time_2_nw(stbuff.st_mtime, response + 26, 1); + U16_TO_BE16(0, response + 28); /* Backup Date */ + U16_TO_BE16(0, response + 30); /* Backup Time */ + + memset(finder_info, 0, sizeof(finder_info)); + (void)nwatalk_get_finder_info(unixname, finder_info, sizeof(finder_info)); + memcpy(response + 32, finder_info, NWATALK_FINDER_INFO_LEN); + + afp_leaf_name_from_path(response + 64, 32, display_path, display_path_len); + U32_TO_BE32(get_file_owner(&stbuff), response + 96); + afp_leaf_name_from_path(response + 100, 12, display_path, display_path_len); + U16_TO_BE16(afp_basic_access_privileges(&stbuff), response + 112); + /* ProDOS info at offset 114 stays zero until a real Mac namespace maps it. */ + + if (entry_id_out) *entry_id_out = entry_id; + return(120); +} + static int afp_get_file_information(uint8 *afp_req, int afp_len, uint8 *response, const char *call_name) { @@ -630,12 +686,9 @@ static int afp_get_file_information(uint8 *afp_req, int afp_len, int path_len; int volume; char unixname[PATH_MAX]; - struct stat stbuff; - uint32 entry_id = 0; - uint32 parent_id = 0; - uint32 resource_size = 0; - uint8 finder_info[NWATALK_FINDER_INFO_LEN]; uint8 empty_path = 0; + uint32 entry_id = 0; + int fallback = 0; int result; if (afp_len < 9) { @@ -675,50 +728,156 @@ static int afp_get_file_information(uint8 *afp_req, int afp_len, return(volume); } - if (stat(unixname, &stbuff)) { - XDPRINTF((2,0, "%s stat failed: vol=%d entry=0x%08x path='%s' unix='%s' errno=%d", + result = afp_fill_file_info_response(unixname, afp_req + 9, path_len, + volume, response, &entry_id, &fallback); + if (result < 0) { + XDPRINTF((2,0, "%s stat failed: vol=%d entry=0x%08x path='%s' unix='%s' result=-0x%x errno=%d", call_name, (int)volume_number, request_entry_id, - visable_data(afp_req + 9, path_len), unixname, errno)); - return(-0x9c); /* Invalid Path */ + visable_data(afp_req + 9, path_len), unixname, -result, errno)); + return(result); } - result = nwatalk_get_entry_id(unixname, &entry_id); - if (result < 0 || !entry_id) - entry_id = afp_fallback_entry_id(volume, &stbuff); - - memset(response, 0, 120); - U32_TO_BE32(entry_id, response + 0); - U32_TO_BE32(parent_id, response + 4); - U16_TO_BE16(afp_basic_attributes(&stbuff), response + 8); - U32_TO_BE32(S_ISDIR(stbuff.st_mode) ? 0 : (uint32)stbuff.st_size, response + 10); - - if (!S_ISDIR(stbuff.st_mode)) - (void)nwatalk_get_resource_fork_size(unixname, &resource_size); - U32_TO_BE32(resource_size, response + 14); - U16_TO_BE16(afp_count_offspring(unixname, &stbuff), response + 18); - - un_date_2_nw(stbuff.st_ctime, response + 20, 1); - un_date_2_nw(stbuff.st_atime, response + 22, 1); - un_date_2_nw(stbuff.st_mtime, response + 24, 1); - un_time_2_nw(stbuff.st_mtime, response + 26, 1); - U16_TO_BE16(0, response + 28); /* Backup Date */ - U16_TO_BE16(0, response + 30); /* Backup Time */ - - memset(finder_info, 0, sizeof(finder_info)); - (void)nwatalk_get_finder_info(unixname, finder_info, sizeof(finder_info)); - memcpy(response + 32, finder_info, NWATALK_FINDER_INFO_LEN); - - afp_leaf_name_from_path(response + 64, 32, afp_req + 9, path_len); - U32_TO_BE32(get_file_owner(&stbuff), response + 96); - afp_leaf_name_from_path(response + 100, 12, afp_req + 9, path_len); - U16_TO_BE16(afp_basic_access_privileges(&stbuff), response + 112); - /* ProDOS info at offset 114 stays zero until a real Mac namespace maps it. */ - XDPRINTF((3,0, "%s: vol=%d entry=0x%08x mask=0x%04x path='%s' reply_entry=0x%08x%s", call_name, (int)volume_number, request_entry_id, request_mask, visable_data(afp_req + 9, path_len), entry_id, - (result < 0) ? " fallback" : "")); - return(120); + fallback ? " fallback" : "")); + return(result); +} + +static int afp_join_path(char *dst, int dst_len, const char *base, + const char *leaf) +{ + int len; + + if (!dst || dst_len < 1 || !base || !leaf) return(-1); + len = snprintf(dst, dst_len, "%s/%s", base, leaf); + if (len < 0 || len >= dst_len) return(-1); + return(0); +} + +static int afp_scan_file_information(uint8 *afp_req, int afp_len, + uint8 *response, const char *call_name) +{ + uint8 volume_number; + uint32 request_entry_id; + uint32 last_seen_id; + uint16 search_mask; + uint16 request_mask; + int path_len; + int volume; + char unixname[PATH_MAX]; + char childname[PATH_MAX]; + uint8 display_path[255]; + int display_path_len; + DIR *dir; + struct dirent *de; + uint32 entry_id = 0; + int fallback = 0; + int seen = 0; + int result; + + if (afp_len < 15) { + XDPRINTF((2,0, "%s rejected: short request len=%d", + call_name, afp_len)); + return(-0x7e); /* NCP Boundary Check Failed */ + } + + volume_number = afp_req[1]; + request_entry_id = GET_BE32(afp_req + 2); + last_seen_id = GET_BE32(afp_req + 6); + search_mask = GET_BE16(afp_req + 10); + request_mask = GET_BE16(afp_req + 12); + path_len = (int)afp_req[14]; + if (path_len < 0 || afp_len < 15 + path_len) { + XDPRINTF((2,0, "%s rejected: boundary check len=%d path_len=%d", + call_name, afp_len, path_len)); + return(-0x7e); + } + + if (!nwatalk_backend_available()) { + XDPRINTF((3,0, "%s rejected: libatalk backend unavailable", call_name)); + return(-0xbf); /* invalid namespace */ + } + + if (!path_len) { + XDPRINTF((2,0, "%s rejected: entry-id-only scan unsupported vol=%d entry=0x%08x last=0x%08x mask=0x%04x req=0x%04x", + call_name, (int)volume_number, request_entry_id, last_seen_id, + search_mask, request_mask)); + return(-0x9c); /* Invalid Path until persistent entry-id lookup exists */ + } + + volume = conn_get_kpl_unxname(unixname, sizeof(unixname), 0, + afp_req + 15, path_len); + if (volume < 0) { + XDPRINTF((2,0, "%s path resolve failed: vol=%d entry=0x%08x last=0x%08x path='%s' result=-0x%x", + call_name, (int)volume_number, request_entry_id, last_seen_id, + visable_data(afp_req + 15, path_len), -volume)); + return(volume); + } + + dir = opendir(unixname); + if (!dir) { + XDPRINTF((2,0, "%s opendir failed: vol=%d entry=0x%08x last=0x%08x path='%s' unix='%s' errno=%d", + call_name, (int)volume_number, request_entry_id, last_seen_id, + visable_data(afp_req + 15, path_len), unixname, errno)); + return(-0x9c); /* Invalid Path */ + } + + while ((de = readdir(dir)) != NULL) { + struct stat stbuff; + int child_fallback = 0; + uint32 child_entry_id = 0; + int child_result; + + if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) + continue; + if (afp_join_path(childname, sizeof(childname), unixname, de->d_name) < 0) + continue; + if (stat(childname, &stbuff)) + continue; + + child_result = nwatalk_get_entry_id(childname, &child_entry_id); + if (child_result < 0 || !child_entry_id) { + child_entry_id = afp_fallback_entry_id(volume, &stbuff); + child_fallback = 1; + } + + if (last_seen_id && !seen) { + if (child_entry_id == last_seen_id) + seen = 1; + continue; + } + + display_path_len = snprintf((char *)display_path, sizeof(display_path), + "%s%c%s", visable_data(afp_req + 15, path_len), + ':', de->d_name); + if (display_path_len < 0 || display_path_len >= (int)sizeof(display_path)) { + display_path_len = strlen(de->d_name); + if (display_path_len > (int)sizeof(display_path)) + display_path_len = sizeof(display_path); + memcpy(display_path, de->d_name, display_path_len); + } + + result = afp_fill_file_info_response(childname, display_path, display_path_len, + volume, response + 4, + &entry_id, &fallback); + if (result < 0) + continue; + U32_TO_BE32(entry_id ? entry_id : child_entry_id, response); + closedir(dir); + XDPRINTF((3,0, "%s: vol=%d entry=0x%08x last=0x%08x mask=0x%04x req=0x%04x path='%s' reply_entry=0x%08x%s", + call_name, (int)volume_number, request_entry_id, last_seen_id, + search_mask, request_mask, visable_data(afp_req + 15, path_len), + entry_id ? entry_id : child_entry_id, + (fallback || child_fallback) ? " fallback" : "")); + return(4 + 120); + } + + closedir(dir); + XDPRINTF((3,0, "%s completed: vol=%d entry=0x%08x last=0x%08x path='%s' no more entries", + call_name, (int)volume_number, request_entry_id, last_seen_id, + visable_data(afp_req + 15, path_len))); + return(-0xff); /* No files found / scan complete */ } static int handle_ncp_serv(void) @@ -2691,6 +2850,12 @@ static int handle_ncp_serv(void) afp_call_name(ufunc)); if (result > -1) data_len = result; else completition = (uint8)-result; + } else if (ufunc == 0x11) { + int result = afp_scan_file_information(afp_req, + afp_len, responsedata, + afp_call_name(ufunc)); + if (result > -1) data_len = result; + else completition = (uint8)-result; } else { XDPRINTF((3,0, "AFP call rejected: ufunc=0x%02x (%s), Mac namespace unavailable, libatalk backend=%s", ufunc, afp_call_name(ufunc), diff --git a/tests/linux/CMakeLists.txt b/tests/linux/CMakeLists.txt index 9c09f41..a64edbc 100644 --- a/tests/linux/CMakeLists.txt +++ b/tests/linux/CMakeLists.txt @@ -25,3 +25,7 @@ target_link_libraries(afp_entry_id_smoke ${NCPFS_LIBRARY}) add_executable(afp_file_info_smoke afp_file_info_smoke.c) target_include_directories(afp_file_info_smoke PRIVATE ${NCPFS_INCLUDE_DIR}) target_link_libraries(afp_file_info_smoke ${NCPFS_LIBRARY}) + +add_executable(afp_scan_info_smoke afp_scan_info_smoke.c) +target_include_directories(afp_scan_info_smoke PRIVATE ${NCPFS_INCLUDE_DIR}) +target_link_libraries(afp_scan_info_smoke ${NCPFS_LIBRARY}) diff --git a/tests/linux/afp_scan_info_smoke.c b/tests/linux/afp_scan_info_smoke.c new file mode 100644 index 0000000..37bcb9c --- /dev/null +++ b/tests/linux/afp_scan_info_smoke.c @@ -0,0 +1,266 @@ +/* + * Linux smoke test for NetWare AFP 2.0 Scan File Information. + */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +#ifndef NCPC_SUBFUNCTION +#define NCPC_SUBFUNCTION 0x10000 +#endif +#ifndef NCPC_SFN +#define NCPC_SFN(FN, SFN) ((FN) | ((SFN) << 8) | NCPC_SUBFUNCTION) +#endif + +#define AFP20_SCAN_FILE_INFORMATION 0x11 +#define NWE_INVALID_NAMESPACE 0xbf +#define NWE_INVALID_PATH 0x9c +#define NWE_NO_FILES_FOUND 0xff +#define AFP_SCAN_REPLY_LEN 124 +#define AFP_GET_ALL 0xffff +#define AFP_SEARCH_ALL 0xffff + +static void usage(const char *prog) +{ + fprintf(stderr, + "Usage: %s [--allow-invalid-namespace] [--allow-invalid-path] [--allow-empty] " + "[--volume N] [--entry-id ID] [--last-seen ID] [--search-mask MASK] " + "[--request-mask MASK] [ncpfs options] PATH\n" + "\n" + "ncpfs options are parsed by ncp_initialize(), for example:\n" + " -S SERVER -U USER -P PASSWORD -n\n" + "\n" + "Examples:\n" + " %s -S MARS -U SUPERVISOR -P secret SYS:PUBLIC\n" + " %s --allow-empty -S MARS -U SUPERVISOR -P secret SYS:EMPTYDIR\n", + prog, prog, prog); +} + +static int parse_u32(const char *text, uint32_t *value) +{ + char *end = NULL; + unsigned long v; + + errno = 0; + v = strtoul(text, &end, 0); + if (errno || !end || *end || v > 0xffffffffUL) + return -1; + *value = (uint32_t)v; + return 0; +} + +static uint16_t be16_to_cpu(const uint8_t p[2]) +{ + return ((uint16_t)p[0] << 8) | p[1]; +} + +static uint32_t be32_to_cpu(const uint8_t p[4]) +{ + return ((uint32_t)p[0] << 24) | + ((uint32_t)p[1] << 16) | + ((uint32_t)p[2] << 8) | + p[3]; +} + +static void cpu_to_be16(uint16_t v, uint8_t p[2]) +{ + p[0] = (uint8_t)(v >> 8); + p[1] = (uint8_t)v; +} + +static void cpu_to_be32(uint32_t v, uint8_t p[4]) +{ + p[0] = (uint8_t)(v >> 24); + p[1] = (uint8_t)(v >> 16); + p[2] = (uint8_t)(v >> 8); + p[3] = (uint8_t)v; +} + +static void copy_fixed_string(char *dst, size_t dstlen, + const uint8_t *src, size_t srclen) +{ + size_t i; + + if (!dstlen) + return; + for (i = 0; i + 1 < dstlen && i < srclen && src[i]; i++) + dst[i] = (char)src[i]; + dst[i] = '\0'; +} + +int main(int argc, char **argv) +{ + NWCONN_HANDLE conn; + NW_FRAGMENT reply; + long init_err = 0; + const char *path = NULL; + int allow_invalid_namespace = 0; + int allow_invalid_path = 0; + int allow_empty = 0; + uint32_t volume_number = 0; + uint32_t entry_id = 0; + uint32_t last_seen_id = 0; + uint32_t search_mask = AFP_SEARCH_ALL; + uint32_t request_mask = AFP_GET_ALL; + int i; + size_t path_len; + uint8_t request[1 + 4 + 4 + 2 + 2 + 1 + 255]; + uint8_t reply_buf[AFP_SCAN_REPLY_LEN]; + char long_name[33]; + char short_name[13]; + NWCCODE err; + + if (NWCallsInit(NULL, NULL)) { + fprintf(stderr, "NWCallsInit failed\n"); + return 2; + } + + conn = ncp_initialize(&argc, argv, 1, &init_err); + if (!conn) { + fprintf(stderr, "ncp_initialize/login failed: %ld\n", init_err); + usage(argv[0]); + return 2; + } + + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--allow-invalid-namespace")) { + allow_invalid_namespace = 1; + } else if (!strcmp(argv[i], "--allow-invalid-path")) { + allow_invalid_path = 1; + } else if (!strcmp(argv[i], "--allow-empty")) { + allow_empty = 1; + } else if (!strcmp(argv[i], "--volume")) { + if (++i >= argc || parse_u32(argv[i], &volume_number) || volume_number > 255) { + fprintf(stderr, "invalid --volume value\n"); + ncp_close(conn); + return 2; + } + } else if (!strcmp(argv[i], "--entry-id")) { + if (++i >= argc || parse_u32(argv[i], &entry_id)) { + fprintf(stderr, "invalid --entry-id value\n"); + ncp_close(conn); + return 2; + } + } else if (!strcmp(argv[i], "--last-seen")) { + if (++i >= argc || parse_u32(argv[i], &last_seen_id)) { + fprintf(stderr, "invalid --last-seen value\n"); + ncp_close(conn); + return 2; + } + } else if (!strcmp(argv[i], "--search-mask")) { + if (++i >= argc || parse_u32(argv[i], &search_mask) || search_mask > 0xffff) { + fprintf(stderr, "invalid --search-mask value\n"); + ncp_close(conn); + return 2; + } + } else if (!strcmp(argv[i], "--request-mask")) { + if (++i >= argc || parse_u32(argv[i], &request_mask) || request_mask > 0xffff) { + fprintf(stderr, "invalid --request-mask value\n"); + ncp_close(conn); + return 2; + } + } else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + usage(argv[0]); + ncp_close(conn); + return 0; + } else if (!path) { + path = argv[i]; + } else { + fprintf(stderr, "unexpected argument: %s\n", argv[i]); + usage(argv[0]); + ncp_close(conn); + return 2; + } + } + + if (!path) { + fprintf(stderr, "missing PATH\n"); + usage(argv[0]); + ncp_close(conn); + return 2; + } + + path_len = strlen(path); + if (path_len > 255) { + fprintf(stderr, "PATH is too long for AFP Scan File Information: %zu\n", path_len); + ncp_close(conn); + return 2; + } + + memset(request, 0, sizeof(request)); + request[0] = (uint8_t)volume_number; + cpu_to_be32(entry_id, request + 1); + cpu_to_be32(last_seen_id, request + 5); + cpu_to_be16((uint16_t)search_mask, request + 9); + cpu_to_be16((uint16_t)request_mask, request + 11); + request[13] = (uint8_t)path_len; + memcpy(request + 14, path, path_len); + + memset(reply_buf, 0, sizeof(reply_buf)); + reply.fragAddr.rw = reply_buf; + reply.fragSize = sizeof(reply_buf); + + err = NWRequestSimple(conn, + NCPC_SFN(0x23, AFP20_SCAN_FILE_INFORMATION), + request, + 14 + path_len, + &reply); + if (err == NWE_INVALID_NAMESPACE && allow_invalid_namespace) { + printf("AFP Scan File Information returned invalid namespace as expected for path=%s\n", path); + ncp_close(conn); + return 0; + } + if (err == NWE_INVALID_PATH && allow_invalid_path) { + printf("AFP Scan File Information returned invalid path as expected for path=%s\n", path); + ncp_close(conn); + return 0; + } + if (err == NWE_NO_FILES_FOUND && allow_empty) { + printf("AFP Scan File Information returned no files as expected for path=%s last_seen=0x%08x\n", + path, last_seen_id); + ncp_close(conn); + return 0; + } + if (err) { + fprintf(stderr, + "AFP Scan File Information failed: completion=0x%02x (%u) path=%s last_seen=0x%08x\n", + (unsigned int)err & 0xff, (unsigned int)err, path, last_seen_id); + ncp_close(conn); + return 1; + } + + if (reply.fragSize < AFP_SCAN_REPLY_LEN) { + fprintf(stderr, "short AFP scan reply: %zu bytes\n", reply.fragSize); + ncp_close(conn); + return 1; + } + + copy_fixed_string(long_name, sizeof(long_name), reply_buf + 4 + 64, 32); + copy_fixed_string(short_name, sizeof(short_name), reply_buf + 4 + 100, 12); + + printf("AFP Scan File Info path=%s last_seen=0x%08x next_last_seen=0x%08x " + "entry_id=0x%08x parent_id=0x%08x attrs=0x%04x data_len=%u " + "resource_len=%u offspring=%u long_name=%s short_name=%s rights=0x%04x\n", + path, + last_seen_id, + be32_to_cpu(reply_buf + 0), + be32_to_cpu(reply_buf + 4), + be32_to_cpu(reply_buf + 8), + be16_to_cpu(reply_buf + 12), + be32_to_cpu(reply_buf + 14), + be32_to_cpu(reply_buf + 18), + be16_to_cpu(reply_buf + 22), + long_name, + short_name, + be16_to_cpu(reply_buf + 4 + 112)); + + ncp_close(conn); + return 0; +}