Build legacy drivers and publish per-OS packages with web activation and DEB

This commit is contained in:
2026-11-27 20:55:31 +00:00
96 changed files with 3204 additions and 25 deletions

View File

@@ -3,14 +3,20 @@
#include "core.h"
#include "supervisor.h"
#include "admin.h"
#include "../src/gateway.h"
#include "gateway_auto.h"
#include <civetweb.h>
#include <pthread.h>
#include <openssl/crypto.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/wait.h>
#include <spawn.h>
extern char **environ;
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -46,12 +52,77 @@ static json_object *state(struct session*s,const char*current){json_object*o=jso
static char *comparison(const char*a,const char*b){if(!strcmp(a,b))return strdup("");char*s=NULL;if(asprintf(&s,"--- Aktuell (vollständig) ---\n%s\n+++ Entwurf (vollständig) +++\n%s",a,b)<0)return NULL;return s;}
static int confirm(json_object*r){json_object*v=get(r,"confirm");return json_object_is_type(v,json_type_boolean)&&json_object_get_boolean(v);}
static int config_valid(const char*s){json_object*v=validate(s);int ok=json_object_array_length(get(v,"errors"))==0;json_object_put(v);return ok;}
/* Gateway configuration is independent of nwserv.conf, but references its saved IPX network. */
static int gateway_network_exists(uint32_t net){char*conf=read_file(config_path,MAX_CONF,0);if(!conf)return 0;json_object*g=groups(conf);free(conf);json_object*a=get(g,"4");int found=0;for(size_t i=0;i<json_object_array_length(a);i++){const char*s=json_object_get_string(json_object_array_get_idx(a,i));char *end;unsigned long n=strtoul(s,&end,0);if(end!=s&&(*end==' '||*end=='\t')&&n==ntohl(net)){found=1;break;}}json_object_put(g);return found;}
static int gateway_api(struct mg_connection*c,int post,json_object*req,int detect){
if(auth_default()||no_services)return error(c,403,"Gateway nur im eingerichteten Dienstmodus verfügbar.");
char *old=read_file(GW_CONFIG,16384,1);int configured=old!=NULL;json_object*current=NULL;
if(!old){if(errno!=ENOENT)return error(c,500,"Gateway-Konfiguration nicht lesbar; Linux-Konsole prüfen.");current=gw_default();old=strdup(json_object_to_json_string_ext(current,JSON_C_TO_STRING_PLAIN));}else current=json_tokener_parse(old);
if(!old||!current){free(old);json_object_put(current);return error(c,500,"Gateway-Konfiguration ungültig.");}
char rev[65];sha(old,rev);
if(detect){char err[512];char*conf=read_file(config_path,MAX_CONF,0);if(!conf)snprintf(err,sizeof err,"NetWare-Konfiguration nicht lesbar.");json_object*out=conf?ga_detect(conf,current,err,sizeof err):NULL;free(conf);free(old);json_object_put(current);if(!out)return error(c,400,err);return reply(c,200,out,NULL);}
if(post){const char*given=js(req,"revision");json_object*next=get(req,"config");struct gw_config cfg;char msg[256];int code=0;
if(!given||strcmp(given,rev)){snprintf(msg,sizeof msg,"Gateway-Konfiguration inzwischen geändert.");code=409;}
else if(!confirm(req)){snprintf(msg,sizeof msg,"Änderung bestätigen.");code=400;}
else if(gw_parse(next,&cfg,msg,sizeof msg))code=400;
else if(cfg.enabled&&(!gateway_network_exists(cfg.net)||!if_nametoindex(cfg.uplink))){snprintf(msg,sizeof msg,"Gespeichertes externes IPX-Netz und vorhandenen Uplink wählen.");code=400;}
else{const char*text=json_object_to_json_string_ext(next,JSON_C_TO_STRING_PLAIN);if(strlen(text)>16384){snprintf(msg,sizeof msg,"Konfiguration zu groß.");code=400;}else if(atomic_write(GW_CONFIG ".previous",old,strlen(old),0600)||atomic_write(GW_CONFIG,text,strlen(text),0600)){snprintf(msg,sizeof msg,"Gateway-Konfiguration konnte nicht gesichert/geschrieben werden.");code=500;}}
free(old);json_object_put(current);if(code)return error(c,code,msg);return ok(c,NULL);
}
json_object*result=json_object_new_object();json_object_object_add(result,"config",current);json_object_object_add(result,"configured",json_object_new_boolean(configured));addstr(result,"revision",rev);free(old);
char *snapshot=read_file(GW_STATUS,16384,1);struct stat st;int fresh=snapshot&&!stat(GW_STATUS,&st)&&time(NULL)-st.st_mtime>=0&&time(NULL)-st.st_mtime<4;
char *seen=read_file(GW_DISCOVERY,16384,1);struct stat ds;json_object*peers=seen&&!stat(GW_DISCOVERY,&ds)&&time(NULL)-ds.st_mtime>=0&&time(NULL)-ds.st_mtime<4?json_tokener_parse(seen):NULL;free(seen);if(!json_object_is_type(peers,json_type_array)){json_object_put(peers);peers=json_object_new_array();}json_object_object_add(result,"observed",peers);
addstr(result,"status",fresh?snapshot:"Kein aktueller Gateway-Status. Dateiserver/Dispatcher starten oder Journal prüfen.");free(snapshot);
return reply(c,200,result,NULL);
}
static int process(struct mg_connection*c,const struct mg_request_info*r,json_object*req){const char*u=r->local_uri;int post=!strcmp(r->request_method,"POST");struct session*s=session_get(c);char cookie[256];if(post&&!strcmp(u,"/api/login")){struct attempt*a=rate(r->remote_addr);if(!a||a->count>=5)return error(c,429,"Zu viele Versuche. Eine Minute warten.");const char*user=js(req,"user");if(!user||strcmp(user,"admin")||!auth_verify(js(req,"password"))){a->count++;return error(c,401,"Anmeldung fehlgeschlagen.");}a->count=0;if(!(s=new_session()))return error(c,429,"Zu viele aktive Sitzungen.");cookie_str(cookie,s->token);return ok(c,cookie);}
const char*file=NULL,*type=NULL;if(!post){if(!strcmp(u,"/login")){file="login.html";type="text/html";}else if(!strcmp(u,"/login.js")){file="login.js";type="text/javascript";}else if(!strcmp(u,"/style.css")){file="style.css";type="text/css";}}
if(!file&&!s){if(!strcmp(u,"/"))return send_data(c,303,"text/plain","",0,NULL,"/login");return error(c,401,"Anmeldung erforderlich.");}
if(post){const char*csrf=mg_get_header(c,"X-Mars-CSRF");if(!csrf||strlen(csrf)!=64||CRYPTO_memcmp(csrf,s->csrf,64))return error(c,403,"CSRF-Prüfung fehlgeschlagen.");if(!strcmp(u,"/api/logout")){memset(s,0,sizeof *s);cookie_str(cookie,"");return ok(c,cookie);}if(!strcmp(u,"/api/password")){const char*next=js(req,"new"),*rep=js(req,"repeat");if(!auth_verify(js(req,"current"))||!next||!rep||strcmp(next,rep)||auth_set(next,0))return error(c,400,"Aktuelles Kennwort prüfen; neues Kennwort 10–256 Zeichen, zweimal identisch.");memset(sessions,0,sizeof sessions);cookie_str(cookie,"");return ok(c,cookie);}if(auth_default())return error(c,403,"Zuerst das Web-Standardkennwort ändern.");}
if(!post&&!file){if(!strcmp(u,"/")){file="index.html";type="text/html";}else if(!strcmp(u,"/app.js")){file="app.js";type="text/javascript";}}
if(file){char*p=NULL;asprintf(&p,"%s/%s",assets,file);char*d=p?read_file(p,MAX_CONF,0):NULL;free(p);if(!d)return error(c,404,"Datei fehlt.");char mime[64];snprintf(mime,sizeof mime,"%s; charset=utf-8",type);int code=send_data(c,200,mime,d,strlen(d),NULL,NULL);free(d);return code;}
if(post&&!strcmp(u,"/api/drivers/activate")){
if(no_services||!confirm(req))return error(c,400,"Dienstmodus und Bestätigung erforderlich.");
json_object *platforms=get(req,"platforms");
if(!json_object_is_type(platforms,json_type_array)||json_object_array_length(platforms)<1||json_object_array_length(platforms)>6)return error(c,400,"Mindestens ein Betriebssystem wählen.");
pid_t pid;int status;char *args[18]={"/usr/local/libexec/mars-install-drivers","--packages","/usr/local/share/mars-vme/driver-packages"};int ac=3;
for(size_t i=0;i<json_object_array_length(platforms);i++){
const char *os=json_object_get_string(json_object_array_get_idx(platforms,i));
if(!os||(strcmp(os,"WIN95")&&strcmp(os,"WIN98")&&strcmp(os,"WINME")&&strcmp(os,"WINNT40")&&strcmp(os,"WIN2000")&&strcmp(os,"WINXP")))return error(c,400,"Unbekanntes Betriebssystem.");
args[ac++]="--os";args[ac++]=(char*)os;
}
args[ac]=NULL;
if(posix_spawn(&pid,args[0],NULL,NULL,args,environ))return error(c,500,"Treiber-Installer fehlt. DEB oder install.sh aktualisieren.");
pid_t done;do{done=waitpid(pid,&status,0);}while(done<0&&errno==EINTR);
if(done!=pid||!WIFEXITED(status)||WEXITSTATUS(status))return error(c,500,"Bereitstellung fehlgeschlagen: SYS/PUBLIC einrichten; Details im Dienstjournal.");
return ok(c,NULL);
}
if(!post&&!strcmp(u,"/api/drivers")){
char path[PATH_MAX];snprintf(path,sizeof path,"%s/drivers/enabled.json",assets);char *data=read_file(path,4096,0);
json_object *result=data?json_tokener_parse(data):NULL;free(data);
if(!result)result=json_tokener_parse("{\"platforms\":[],\"map\":\"\"}");
return reply(c,200,result,NULL);
}
/* Fixed allowlist only; never interpolate a request path into the filesystem. */
if(!post&&!strncmp(u,"/drivers/",9)){
if(auth_default())return error(c,403,"Kennwort ändern.");
static const char *names[]={"WIN95.zip","WIN98.zip","WINME.zip","WINNT40.zip","WIN2000.zip","WINXP.zip",NULL};
for(int i=0;names[i];i++)if(!strcmp(u+9,names[i])){
char settings[PATH_MAX];snprintf(settings,sizeof settings,"%s/drivers/enabled.json",assets);
char *data=read_file(settings,4096,0);json_object *enabled=data?json_tokener_parse(data):NULL;free(data);int allowed=0;
json_object *list=get(enabled,"platforms");
if(json_object_is_type(list,json_type_array))for(size_t j=0;j<json_object_array_length(list);j++){
const char *os=json_object_get_string(json_object_array_get_idx(list,j));char name[32];snprintf(name,sizeof name,"%s.zip",os?os:"");if(!strcmp(name,names[i]))allowed=1;
}
json_object_put(enabled);if(!allowed)return error(c,404,"Treiberpaket nicht freigegeben.");
char path[PATH_MAX];struct stat st;
if(snprintf(path,sizeof path,"%s/drivers/%s",assets,names[i])>=(int)sizeof path||lstat(path,&st)||!S_ISREG(st.st_mode))return error(c,404,"Treiberpaket nicht installiert.");
mg_send_file(c,path);return 200;
}
return error(c,404,"Unbekanntes Treiberpaket.");
}
if(!strcmp(u,"/api/gateway"))return gateway_api(c,post,req,0);
if(!post&&!strcmp(u,"/api/gateway/detect"))return gateway_api(c,0,NULL,1);
if(!post&&!strcmp(u,"/api/status"))return reply(c,200,supervisor_status(),NULL);
if(!post&&!strcmp(u,"/api/monitor")){if(auth_default())return error(c,403,"Kennwort ändern.");return reply(c,200,monitor_data(),NULL);}
if(!strcmp(u,"/api/bindery")||!strcmp(u,"/api/print")){if(auth_default()||no_services)return error(c,403,"Kennwort ändern oder Dienstmodus verwenden.");if(!post)return error(c,405,"POST erforderlich.");const char *op=js(req,"op");if(!op)return error(c,400,"Aktion fehlt.");if(!strncmp(op,"trustee-",8)&&!supervisor_config_matches())return error(c,409,"Konfiguration seit Serverstart geändert. Dateiserver neu starten.");int readonly=!strcmp(op,"sessions")||!strcmp(op,"objects")||!strcmp(op,"properties")||!strcmp(op,"list")||!strcmp(op,"script-get")||!strcmp(op,"trustee-get")||!strcmp(op,"group-members");if(!readonly&&!confirm(req))return error(c,400,"Aktion bestätigen.");json_object *res=!strcmp(u,"/api/bindery")?bindery_call(req):print_call(req);return reply(c,get(res,"error")?400:200,res,NULL);}