/* CUPS 2.x native API, loaded on demand; no scheduler process spawned here. */ #define _GNU_SOURCE #include "admin.h" #include #include #include #include #include #include #include /* Stable public CUPS 2 ABI, see cups/cups.h. */ typedef struct {char *name,*value;} opt; typedef struct {char *name,*instance;int is_default,num_options;opt *options;} dest; typedef struct {int id;char *dest,*title,*user,*format;int state,size,priority;time_t completed_time,creation_time,processing_time;} job; static struct {void *h;int (*dests)(dest**);void (*freed)(int,dest*);const char *(*option)(const char*,int,opt*);int (*jobs)(job**,const char*,int,int);void (*freej)(int,job*);int (*print)(const char*,const char*,const char*,int,opt*);int (*cancel)(void*,const char*,int,int);int (*add)(const char*,const char*,int,opt**);void (*freeo)(int,opt*);const char *(*err)(void);int (*status)(void);} c; static const char *no_password(const char *prompt,void *http,const char *method,const char *resource,void *data){(void)prompt;(void)http;(void)method;(void)resource;(void)data;return NULL;} static int init(void){if(c.h)return 0;c.h=dlopen("libcups.so.2",RTLD_NOW|RTLD_LOCAL);if(!c.h)return -1; #define LOAD(field,sym) do{*(void **)(&c.field)=dlsym(c.h,sym);if(!c.field)goto bad;}while(0) LOAD(dests,"cupsGetDests");LOAD(freed,"cupsFreeDests");LOAD(option,"cupsGetOption");LOAD(jobs,"cupsGetJobs");LOAD(freej,"cupsFreeJobs");LOAD(print,"cupsPrintFile");LOAD(cancel,"cupsCancelJob2");LOAD(add,"cupsAddOption");LOAD(freeo,"cupsFreeOptions");LOAD(err,"cupsLastErrorString");LOAD(status,"cupsLastError");void (*setcb)(const char *(*)(const char*,void*,const char*,const char*,void*),void*)=NULL;*(void **)(&setcb)=dlsym(c.h,"cupsSetPasswordCB2");if(setcb)setcb(no_password,NULL);return 0; bad:dlclose(c.h);memset(&c,0,sizeof c);return -1; } static int name_ok(const char *s){return s&&*s&&strlen(s)<=127&&strspn(s,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-")==strlen(s);} static json_object *fail(const char *s){json_object *o=json_object_new_object();addstr(o,"error",s?s:"CUPS-Fehler");return o;} static void num(json_object*o,const char*k,long long v){json_object_object_add(o,k,json_object_new_int64(v));} int print_submit(const char *d,const char *m,const char *f,const char *title){if(init()||!name_ok(d)||!m|| (strcmp(m,"raw")&&strcmp(m,"filtered")))return -1;opt *opts=NULL;int n=0;if(!strcmp(m,"raw"))n=c.add("raw","true",n,&opts);int id=c.print(d,f,title,n,opts);c.freeo(n,opts);return id>0?id:-1;} json_object *print_call(json_object *r){const char *op=js(r,"op"),*d=js(r,"dest");if(init())return fail("libcups.so.2 fehlt (CUPS 2.x installieren)");if(!op)return fail("Aktion fehlt"); if(!strcmp(op,"list")){dest *ds=NULL;job *jsv=NULL;int n=c.dests(&ds);int ds_status=c.status();if(n<0||(!n&&ds_status>=0x400)){if(n>0)c.freed(n,ds);return fail(c.err());}json_object *o=json_object_new_object(),*a=json_object_new_array(),*b=json_object_new_array();json_object_object_add(o,"printers",a);json_object_object_add(o,"jobs",b);for(int i=0;i=0x400)){addstr(o,"jobsError",c.err());}else{for(int i=0;i=0x400)return fail(c.err());return json_object_new_object();} if(!strcmp(op,"test")){char p[]="/tmp/mars-test-XXXXXX";int fd=mkstemp(p);if(fd<0)return fail("Testdatei konnte nicht erstellt werden");const char *text="MARS NWE - CUPS Testseite\nNetWare Druckintegration\n\f";ssize_t z=write(fd,text,strlen(text));close(fd);int id=z==(ssize_t)strlen(text)?print_submit(d,"filtered",p,"MARS NWE Testseite"):-1;unlink(p);if(id<0)return fail(c.err());json_object *o=json_object_new_object();num(o,"id",id);return o;}return fail("Unbekannte Druckaktion");}