/* * printers.c -- MCDE: the printer queues of CUPS and the printers on the * network (McdeSys.h) * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * The queues come from CUPS through lpstat(1) with fixed arguments (the * output is only read); the default printer is set with lpadmin -d under * the administrator's terminal (MCDE_SYS_EPERM as a user) and a queue is * made with lpadmin -p NAME -E -v URI -m everywhere -- the driverless flow * CUPS documents for an IPP Everywhere printer. * * libcups itself was tried first (2026-11-14): cupsGetDests2() returns 0x2 * and leaves num_dests untouched, both through dlopen and linked directly, * while _cupsGetDests() answers no queues and lpstat(1) lists the same two * the desktop has. The library in this environment is not usable, so the * tool that speaks to it is called until a host with a working libcups and * its headers builds the library path (follow-up task). * * The printers in reach come from mDNS: avahi-browse(1) is asked for * _ipp._tcp with fixed arguments; its output is parsed, never executed. * * C99 and POSIX only (ext/src/mcde/AGENTS.md). */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include "private.h" /* the first of PATHS that is executable */ static const char * Tool(const char *const *paths) { const char *p = mcde_sys_which(paths); if (p != NULL) return p; return NULL; } int mcde_sys_printers(McdeSysPrinter *list, size_t max) { static const char *const where[] = { "/usr/bin/lpstat", "/usr/local/bin/lpstat", "/usr/sbin/lpstat", MCDE_PKG_PREFIX "/bin/lpstat", NULL }; static char out[16384], def[256]; const char *lpstat = Tool(where); char *argv[3], *line, *last = NULL; const char *defname; int count = 0, i; if (list == NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_PRN_NO_PLACE, "There is no place for the printers.")); if (lpstat == NULL) return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_PRN_NO_LPSTAT, "This system has no CUPS (lpstat).")); argv[0] = (char *) lpstat; argv[1] = (char *) "-p"; argv[2] = NULL; if (mcde_sys_capture(argv, out, sizeof out) != MCDE_SYS_OK) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_PRN_NO_ANSWER, "CUPS did not answer.")); /* "printer NAME is idle. enabled since ..." -- a continuation line * carries a stopped printer's reason */ argv[1] = (char *) "-d"; def[0] = '\0'; defname = def; if (mcde_sys_capture(argv, def, sizeof def) == MCDE_SYS_OK) { char *p = strstr(def, ": "); if (p != NULL) { p += 2; p[strcspn(p, "\r\n")] = '\0'; defname = p; } } for (line = strtok_r(out, "\n", &last); line != NULL && count < (int) max; line = strtok_r(NULL, "\n", &last)) { McdeSysPrinter *p; char *state, *dot; if (strncmp(line, "printer ", 8) != 0) continue; p = &list[count]; memset(p, 0, sizeof *p); state = line + 8; { char *sp = strchr(state, ' '); if (sp == NULL) continue; *sp = '\0'; (void)snprintf(p->name, sizeof p->name, "%s", state); state = sp + 1; } while (*state == ' ') state++; if (strncmp(state, "is ", 3) == 0) state += 3; (void)snprintf(p->state, sizeof p->state, "%.*s", (int) strcspn(state, "\r\n"), state); /* "idle. enabled since ..." -> "idle" */ dot = strstr(p->state, ". "); if (dot != NULL) *dot = '\0'; p->is_default = defname[0] != '\0' && strcmp(p->name, defname) == 0; p->accepting = strstr(p->state, "disabled") == NULL; count++; } (void) i; return count; } int mcde_sys_printer_default(const char *name) { static const char *const where[] = { "/usr/sbin/lpadmin", "/usr/bin/lpadmin", MCDE_PKG_PREFIX "/sbin/lpadmin", NULL }; const char *lpadmin = Tool(where); char *argv[4]; if (name == NULL || name[0] == '\0' || strchr(name, '/') != NULL || strchr(name, ' ') != NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_PRN_NO_NAME, "No printer was named.")); if (lpadmin == NULL) return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_PRN_NO_LPADMIN, "This system has no CUPS administration (lpadmin).")); if (geteuid() != 0) return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_PRN_DEFAULT_EPERM, "The default printer needs the administrator.")); argv[0] = (char *) lpadmin; argv[1] = (char *) "-d"; argv[2] = (char *) name; argv[3] = NULL; if (mcde_sys_run(argv) != MCDE_SYS_OK) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_PRN_DEFAULT_FAIL, "CUPS did not take the default printer.")); return MCDE_SYS_OK; } McdeSysJob * mcde_sys_print_file(const char *path, const char *printer) { static const char *const where[] = { "/usr/bin/lp", "/usr/local/bin/lp", "/usr/sbin/lp", MCDE_PKG_PREFIX "/bin/lp", NULL }; const char *lp = getenv("MCDE_PRINT_COMMAND"); char *argv[5]; int n = 0; if (path == NULL || path[0] == '\0' || access(path, R_OK) != 0) { (void) mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_NOT_THERE, "%s is not there."), path != NULL ? path : ""); return NULL; } if (lp == NULL) lp = Tool(where); if (lp == NULL) { (void) mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_NOT_THERE, "%s is not there."), "lp"); return NULL; } argv[n++] = (char *) lp; if (printer != NULL && printer[0] != '\0') { argv[n++] = (char *) "-d"; argv[n++] = (char *) printer; } argv[n++] = (char *) path; argv[n] = NULL; return mcde_sys_job_start(argv, NULL); } /* the checks every name and URI pass before they reach a command line */ static int NameOk(const char *name) { const char *p; if (name == NULL || name[0] == '\0' || strlen(name) > 60) return 0; for (p = name; *p != '\0'; p++) if (!((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '_' || *p == '-')) return 0; return 1; } static int UriOk(const char *uri) { const char *p; if (uri == NULL) return 0; if (strncmp(uri, "ipp://", 6) != 0 && strncmp(uri, "ipps://", 7) != 0 && strncmp(uri, "socket://", 9) != 0 && strncmp(uri, "usb://", 6) != 0) return 0; for (p = uri; *p != '\0'; p++) if (*p == ' ' || *p == ';' || *p == '|' || *p == '\'' || *p == '"' || *p == '`' || *p == '$') return 0; return 1; } int mcde_sys_printer_add(const char *name, const char *uri) { static const char *const where[] = { "/usr/sbin/lpadmin", "/usr/bin/lpadmin", MCDE_PKG_PREFIX "/sbin/lpadmin", NULL }; const char *lpadmin = Tool(where); char *argv[8]; if (!NameOk(name) || !UriOk(uri)) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_PRN_BAD_NAME, "The printer's name and address must be plain (letters, digits, - and _; ipp://...).")) ; if (lpadmin == NULL) return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_PRN_NO_LPADMIN, "This system has no CUPS administration (lpadmin).")); if (geteuid() != 0) return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_PRN_ADD_EPERM, "Adding a printer needs the administrator.")); argv[0] = (char *) lpadmin; argv[1] = (char *) "-p"; argv[2] = (char *) name; argv[3] = (char *) "-E"; argv[4] = (char *) "-v"; argv[5] = (char *) uri; argv[6] = (char *) "-m"; argv[7] = (char *) "everywhere"; argv[8] = NULL; if (mcde_sys_run(argv) != MCDE_SYS_OK) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_PRN_ADD_FAIL, "CUPS did not take the printer (is the address right?).")); return MCDE_SYS_OK; } int mcde_sys_printer_remove(const char *name) { static const char *const where[] = { "/usr/sbin/lpadmin", "/usr/bin/lpadmin", MCDE_PKG_PREFIX "/sbin/lpadmin", NULL }; const char *lpadmin = Tool(where); char *argv[4]; if (!NameOk(name)) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_PRN_NO_NAME, "No printer was named.")); if (lpadmin == NULL) return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_PRN_NO_LPADMIN, "This system has no CUPS administration (lpadmin).")); if (geteuid() != 0) return mcde_sys_fail(MCDE_SYS_EPERM, mcde_sys_msg(MSG_PRN_REMOVE_EPERM, "Removing a printer needs the administrator.")); argv[0] = (char *) lpadmin; argv[1] = (char *) "-x"; argv[2] = (char *) name; argv[3] = NULL; if (mcde_sys_run(argv) != MCDE_SYS_OK) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_PRN_REMOVE_FAIL, "CUPS did not take the removal.")); return MCDE_SYS_OK; } /* avahi-browse escapes a space as \032 and punctuation as \ddd: three * DECIMAL digits (032 is a space, 041 a bracket) */ static void Unescape(char *s) { char *o = s; while (*s != '\0') { if (s[0] == '\\' && s[1] >= '0' && s[1] <= '9' && s[2] >= '0' && s[2] <= '9' && s[3] >= '0' && s[3] <= '9') { *o++ = (char) (((s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0')) & 0xff); s += 4; } else *o++ = *s++; } *o = '\0'; } /* a TXT record's value for KEY in the avahi-browse line, or "" */ static const char * TxtValue(const char *txt, const char *key, char *buf, size_t len) { char pat[32]; const char *p = txt; (void)snprintf(pat, sizeof pat, "\"%s=", key); if ((p = strstr(p, pat)) != NULL) { const char *v = p + strlen(pat), *e = strchr(v, '"'); size_t n; if (e == NULL) return ""; n = (size_t) (e - v); if (n >= len) n = len - 1; memcpy(buf, v, n); buf[n] = '\0'; return buf; } return ""; } int mcde_sys_printers_discovered(char (*names)[MCDE_SYS_PRINTER_TEXT], char (*uris)[MCDE_SYS_PRINTER_URI], size_t max) { static const char *const where[] = { "/usr/bin/avahi-browse", "/usr/local/bin/avahi-browse", MCDE_PKG_PREFIX "/bin/avahi-browse", NULL }; static char out[16384]; char *argv[5], *line, *last = NULL; int count = 0; const char *avahi = Tool(where); if (names == NULL || uris == NULL) return mcde_sys_fail(MCDE_SYS_EINVAL, mcde_sys_msg(MSG_PRN_NO_PLACE, "There is no place for the printers.")); if (avahi == NULL) return mcde_sys_fail(MCDE_SYS_ENOTSUP, mcde_sys_msg(MSG_PRN_NO_MDNS, "There is no mDNS browser (avahi-browse) on this system.")); argv[0] = (char *) avahi; argv[1] = (char *) "-rtp"; argv[2] = (char *) "_ipp._tcp"; argv[3] = NULL; if (mcde_sys_capture_limited(argv, out, sizeof out, 4) != MCDE_SYS_OK) return mcde_sys_fail(MCDE_SYS_EFAIL, mcde_sys_msg(MSG_PRN_MDNS_FAIL, "The mDNS browser did not answer.")); /* "=;iface;proto;name;type;domain;host;address;port;txt" */ for (line = strtok_r(out, "\n", &last); line != NULL; line = strtok_r(NULL, "\n", &last)) { char *f[11], *l2 = NULL, rp[128], addr[64], port[16]; int k; if (line[0] != '=') continue; f[0] = strtok_r(line, ";", &l2); for (k = 1; k < 11; k++) f[k] = strtok_r(NULL, ";", &l2); if (f[3] == NULL || f[7] == NULL || f[8] == NULL || (size_t) count >= max) break; (void)snprintf(addr, sizeof addr, "%s", f[7]); (void)snprintf(port, sizeof port, "%s", f[8]); TxtValue(f[9] != NULL ? f[9] : "", "rp", rp, sizeof rp); Unescape(f[3]); if (count > 0) { int same = 0; /* IPv4 and IPv6 give the same name */ for (k = 0; k < count; k++) if (strcmp(names[k], f[3]) == 0) same = 1; if (same) continue; } (void)snprintf(names[count], MCDE_SYS_PRINTER_TEXT, "%s", f[3]); (void)snprintf(uris[count], MCDE_SYS_PRINTER_URI, "ipp://%s:%s/%s", addr, port, rp[0] != '\0' ? rp : "ipp/print"); count++; } return count; }