/* * Linux smoke test for NetWare AFP Get DOS Name From Entry ID. */ #include #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 AFP_GET_ENTRY_ID_FROM_PATH_NAME 0x0c #define AFP_GET_DOS_NAME_FROM_ENTRY_ID 0x12 #define NWE_INVALID_NAMESPACE 0xbf #define NWE_INVALID_PATH 0x9c static void usage(const char *prog) { fprintf(stderr, "Usage: %s [--volume N] [--entry-id ID] [--expect PATH] " "[--allow-invalid-namespace] [--allow-invalid-path] [ncpfs options] PATH\n" "\n" "When --entry-id is omitted, the helper first resolves PATH with AFP " "Get Entry ID From Path Name, then calls AFP Get DOS Name From Entry ID.\n" "\n" "Examples:\n" " %s -S MARS -U SUPERVISOR -P secret SYS:PUBLIC/pmdflts.ini\n" " %s --entry-id 0x12345678 --expect PUBLIC/pmdflts.ini " "-S MARS -U SUPERVISOR -P secret SYS:PUBLIC/pmdflts.ini\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 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_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 const char *path_without_volume(const char *path) { const char *colon = strchr(path, ':'); if (colon && colon[1]) return colon + 1; return path; } static void default_dos_expectation(const char *path, char *out, size_t out_len) { const char *src = path_without_volume(path); size_t i; if (!out || !out_len) return; for (i = 0; i + 1 < out_len && src[i]; i++) out[i] = (char)toupper((unsigned char)src[i]); out[i] = '\0'; } static NWCCODE get_entry_id_for_path(NWCONN_HANDLE conn, const char *path, uint32_t *entry_id) { NW_FRAGMENT reply; uint8_t request[2 + 255]; uint8_t reply_buf[4]; size_t path_len = strlen(path); NWCCODE err; if (path_len > 255) return NWE_INVALID_PATH; request[0] = 0; /* NetWare directory handle 0, raw VOL:PATH */ request[1] = (uint8_t)path_len; memcpy(request + 2, 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, AFP_GET_ENTRY_ID_FROM_PATH_NAME), request, 2 + path_len, &reply); if (err) return err; if (reply.fragSize < 4) return NWE_INVALID_PATH; *entry_id = be32_to_cpu(reply_buf); return 0; } int main(int argc, char **argv) { NWCONN_HANDLE conn; NW_FRAGMENT reply; long init_err = 0; const char *path = NULL; const char *expect = NULL; char default_expect[256]; int allow_invalid_namespace = 0; int allow_invalid_path = 0; uint32_t volume_number = 0; uint32_t entry_id = 0; int have_entry_id = 0; int i; uint8_t request[1 + 4]; uint8_t reply_buf[256]; char dos_path[256]; 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], "--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; } have_entry_id = 1; } else if (!strcmp(argv[i], "--expect")) { if (++i >= argc) { fprintf(stderr, "missing --expect value\n"); ncp_close(conn); return 2; } expect = argv[i]; } 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 && !have_entry_id) { fprintf(stderr, "missing PATH or --entry-id\n"); usage(argv[0]); ncp_close(conn); return 2; } if (!have_entry_id) { err = get_entry_id_for_path(conn, path, &entry_id); if (err) { fprintf(stderr, "AFP Get Entry ID before DOS-name lookup failed: completion=0x%02x (%u) path=%s\n", (unsigned int)err & 0xff, (unsigned int)err, path); ncp_close(conn); return 1; } } request[0] = (uint8_t)volume_number; cpu_to_be32(entry_id, request + 1); memset(reply_buf, 0, sizeof(reply_buf)); reply.fragAddr.rw = reply_buf; reply.fragSize = sizeof(reply_buf); err = NWRequestSimple(conn, NCPC_SFN(0x23, AFP_GET_DOS_NAME_FROM_ENTRY_ID), request, sizeof(request), &reply); if (((unsigned int)err & 0xff) == NWE_INVALID_NAMESPACE && allow_invalid_namespace) { printf("AFP Get DOS Name From Entry ID returned invalid namespace as expected: volume=%u entry_id=0x%08x\n", (unsigned int)volume_number, (unsigned int)entry_id); ncp_close(conn); return 0; } if (((unsigned int)err & 0xff) == NWE_INVALID_PATH && allow_invalid_path) { printf("AFP Get DOS Name From Entry ID returned invalid path as expected: volume=%u entry_id=0x%08x\n", (unsigned int)volume_number, (unsigned int)entry_id); ncp_close(conn); return 0; } if (err) { fprintf(stderr, "AFP Get DOS Name From Entry ID failed: completion=0x%02x (%u) volume=%u entry_id=0x%08x\n", (unsigned int)err & 0xff, (unsigned int)err, (unsigned int)volume_number, (unsigned int)entry_id); ncp_close(conn); return 1; } if (reply.fragSize < 1 || reply_buf[0] + 1U > reply.fragSize) { fprintf(stderr, "short AFP DOS-name reply: %zu bytes\n", reply.fragSize); ncp_close(conn); return 1; } memcpy(dos_path, reply_buf + 1, reply_buf[0]); dos_path[reply_buf[0]] = '\0'; if (!expect && path) { default_dos_expectation(path, default_expect, sizeof(default_expect)); expect = default_expect; } if (expect && strcmp(expect, dos_path)) { fprintf(stderr, "AFP Get DOS Name From Entry ID verify mismatch: volume=%u entry_id=0x%08x got=%s expected=%s\n", (unsigned int)volume_number, (unsigned int)entry_id, dos_path, expect); ncp_close(conn); return 1; } printf("AFP Get DOS Name From Entry ID volume=%u entry_id=0x%08x path=%s verified\n", (unsigned int)volume_number, (unsigned int)entry_id, dos_path); ncp_close(conn); return 0; }