Integrate 0.9.3 SLP/IP, SQLite identities and complete token fix; build release 0.9.3-1
This commit is contained in:
24
native/directory.c
Normal file
24
native/directory.c
Normal file
@@ -0,0 +1,24 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
#define _GNU_SOURCE
|
||||
#include "directory.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
static void msg(json_object *a,const char *s){json_object_array_add(a,json_object_new_string(s));}
|
||||
static int label(const char *s){return s&&*s&&strlen(s)<=47&&strspn(s,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-")==strlen(s);}
|
||||
static int path_ok(const char *s){if(!s||!*s||strlen(s)>191)return 0;char *p=strdup(s),*q=p,*v;int ok=1,n=0; if(!p)return 0;while((v=strsep(&q,"/")))if(!label(v)||++n>8){ok=0;break;}free(p);return ok;}
|
||||
static int has(json_object *a,const char *s){if(!json_object_is_type(a,json_type_array)||!s)return 0;for(size_t i=0;i<json_object_array_length(a);i++){json_object *v=json_object_array_get_idx(a,i);if(json_object_is_type(v,json_type_string)&&!strcmp(s,json_object_get_string(v)))return 1;}return 0;}
|
||||
static int type_ok(json_object *o){json_object *v=get(o,"type");return json_object_is_type(v,json_type_int)&&(json_object_get_int(v)==1||json_object_get_int(v)==2);}
|
||||
static int obj_name(const char *s){return s&&*s&&strlen(s)<=47&&strspn(s,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.!$%&'()@^`{}~")==strlen(s);}
|
||||
json_object *directory_default(void){json_object *o=json_object_new_object(),*a=json_object_new_array();json_object_object_add(o,"version",json_object_new_int(1));addstr(o,"tree","MARS_TREE");addstr(o,"organization","HOME");msg(a,"USERS");msg(a,"GROUPS");msg(a,"SERVERS");json_object_object_add(o,"units",a);json_object_object_add(o,"placements",json_object_new_array());return o;}
|
||||
json_object *directory_validate(json_object *o){json_object *a=json_object_new_array();if(!json_object_is_type(o,json_type_object)){msg(a,"Verzeichnis muss ein JSON-Objekt sein.");return a;}json_object_object_foreach(o,key,val){(void)val;if(strcmp(key,"version")&&strcmp(key,"tree")&&strcmp(key,"organization")&&strcmp(key,"units")&&strcmp(key,"placements"))msg(a,"Unbekanntes Verzeichnisfeld.");}json_object *v=get(o,"version"),*u=get(o,"units"),*p=get(o,"placements");if(!json_object_is_type(v,json_type_int)||json_object_get_int(v)!=1)msg(a,"Unbekannte Verzeichnisversion.");if(!label(js(o,"tree"))||!label(js(o,"organization")))msg(a,"Baum und Organisation: 1–47 Zeichen A–Z, 0–9, _ oder -.");if(!json_object_is_type(u,json_type_array)||json_object_array_length(u)>128){msg(a,"Maximal 128 Organisationseinheiten.");return a;}if(!json_object_is_type(p,json_type_array)||json_object_array_length(p)>1024){msg(a,"Maximal 1024 Objektzuordnungen.");return a;}
|
||||
for(size_t i=0;i<json_object_array_length(u);i++){json_object *x=json_object_array_get_idx(u,i);const char *s=json_object_is_type(x,json_type_string)?json_object_get_string(x):NULL;if(!path_ok(s)||strlen(s)!=(size_t)json_object_get_string_len(x)){msg(a,"Ungültiger OU-Pfad.");continue;}for(size_t j=0;j<i;j++){json_object *y=json_object_array_get_idx(u,j);if(json_object_is_type(y,json_type_string)&&!strcmp(s,json_object_get_string(y)))msg(a,"Organisationseinheit doppelt.");}char buf[192];strcpy(buf,s);char *slash=strrchr(buf,'/');if(slash){*slash=0;if(!has(u,buf))msg(a,"Übergeordnete Organisationseinheit fehlt.");}}
|
||||
for(size_t i=0;i<json_object_array_length(p);i++){json_object *x=json_object_array_get_idx(p,i);if(json_object_is_type(x,json_type_object)){json_object_object_foreach(x,key,val){(void)val;if(strcmp(key,"name")&&strcmp(key,"type")&&strcmp(key,"unit"))msg(a,"Unbekanntes Zuordnungsfeld.");}}const char *n=js(x,"name"),*c=js(x,"unit");if(!type_ok(x)||!obj_name(n)||!c||(*c&&!has(u,c))){msg(a,"Ungültige Objektzuordnung (Benutzer/Gruppe und vorhandene OU erforderlich).");continue;}for(size_t j=0;j<i;j++){json_object *y=json_object_array_get_idx(p,j);if(type_ok(y)&&json_object_get_int(get(x,"type"))==json_object_get_int(get(y,"type"))&&js(y,"name")&&!strcasecmp(n,js(y,"name")))msg(a,"Objekt mehrfach zugeordnet.");}}
|
||||
return a;}
|
||||
json_object *directory_read(const char *file,char revision[65],int *configured){char *s=read_file(file,DIRECTORY_LIMIT,1);if(!s){if(errno!=ENOENT)return NULL;json_object *o=directory_default();sha("",revision);*configured=0;return o;}sha(s,revision);json_tokener *t=json_tokener_new();if(!t){free(s);return NULL;}json_tokener_set_flags(t,JSON_TOKENER_STRICT|JSON_TOKENER_VALIDATE_UTF8);json_object *o=json_tokener_parse_ex(t,s,(int)strlen(s));int good=json_tokener_get_error(t)==json_tokener_success&&json_tokener_get_parse_end(t)==strlen(s);json_tokener_free(t);free(s);json_object *a=directory_validate(o);good=good&&!json_object_array_length(a);json_object_put(a);if(!good){json_object_put(o);return NULL;}*configured=1;return o;}
|
||||
/* 0 OK, -1 I/O, -2 stale, -3 invalid. Caller serializes via existing API lock. */
|
||||
int directory_save(const char *file,json_object *next,const char *rev){json_object *a=directory_validate(next);int invalid=json_object_array_length(a)!=0;json_object_put(a);if(invalid)return -3;char now[65];int configured;json_object *old=directory_read(file,now,&configured);if(!old)return -1;if(!rev||strcmp(rev,now)){json_object_put(old);return -2;}const char *s=json_object_to_json_string_ext(next,JSON_C_TO_STRING_PLAIN);if(strlen(s)>DIRECTORY_LIMIT){json_object_put(old);return -3;}if(configured){char b[PATH_MAX];if(snprintf(b,sizeof b,"%s.previous",file)>=(int)sizeof b){json_object_put(old);return -1;}const char *prior=json_object_to_json_string_ext(old,JSON_C_TO_STRING_PLAIN);if(atomic_write(b,prior,strlen(prior),0600)){json_object_put(old);return -1;}}json_object_put(old);return atomic_write(file,s,strlen(s),0600);}
|
||||
json_object *directory_report(const char *conf,json_object *dir,json_object *live,int matches){json_object *r=json_object_new_object(),*g=groups(conf),*v=validate(conf),*missing=json_object_new_array();json_object_object_add(r,"validation",v);json_object_object_add(r,"missing",missing);json_object_object_add(r,"runningConfigMatches",json_object_new_boolean(matches));json_object_object_add(r,"nativeNds",json_object_new_boolean(0));json_object *objs=get(live,"objects");int available=json_object_is_type(objs,json_type_array)&&!get(live,"error"),complete=available&&!json_object_get_boolean(get(live,"truncated"));json_object_object_add(r,"liveAvailable",json_object_new_boolean(available));json_object_object_add(r,"liveComplete",json_object_new_boolean(complete));if(available)json_object_object_add(r,"objects",json_object_get(objs));else{json_object_object_add(r,"objects",json_object_new_array());addstr(r,"liveError",js(live,"error")?js(live,"error"):"Live-Bindery nicht verfügbar.");}const char *keys[]={"1","2","3","4"},*names[]={"volumes","servers","internalNetworks","networks"};for(int i=0;i<4;i++){json_object *x=get(g,keys[i]);json_object_object_add(r,names[i],x?json_object_get(x):json_object_new_array());}json_object_put(g);
|
||||
if(complete){json_object *p=get(dir,"placements");for(size_t i=0;i<json_object_array_length(p);i++){json_object *x=json_object_array_get_idx(p,i);int found=0;for(size_t j=0;j<json_object_array_length(objs);j++){json_object *y=json_object_array_get_idx(objs,j);if(js(x,"name")&&js(y,"name")&&!strcasecmp(js(x,"name"),js(y,"name"))&&json_object_get_int(get(x,"type"))==json_object_get_int(get(y,"type"))){found=1;break;}}if(!found)json_object_array_add(missing,json_object_get(x));}}
|
||||
return r;}
|
||||
11
native/directory.h
Normal file
11
native/directory.h
Normal file
@@ -0,0 +1,11 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
#ifndef MARS_DIRECTORY_H
|
||||
#define MARS_DIRECTORY_H
|
||||
#include "core.h"
|
||||
#define DIRECTORY_LIMIT (256*1024)
|
||||
json_object *directory_default(void);
|
||||
json_object *directory_validate(json_object *);
|
||||
json_object *directory_read(const char *,char revision[65],int *);
|
||||
int directory_save(const char *,json_object *,const char *revision);
|
||||
json_object *directory_report(const char *,json_object *,json_object *,int);
|
||||
#endif
|
||||
60
native/identity.c
Normal file
60
native/identity.c
Normal file
@@ -0,0 +1,60 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
#define _GNU_SOURCE
|
||||
#include "identity.h"
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#ifndef SQLITE_OPEN_NOFOLLOW
|
||||
#error SQLite 3.31 or newer required
|
||||
#endif
|
||||
/* When built with --wrap, use libc explicitly for collision checks. */
|
||||
#ifdef MARS_WRAPPED
|
||||
extern int __real_getpwnam_r(const char *,struct passwd *,char *,size_t,struct passwd **);
|
||||
extern int __real_getpwuid_r(uid_t,struct passwd *,char *,size_t,struct passwd **);
|
||||
extern int __real_getgrnam_r(const char *,struct group *,char *,size_t,struct group **);
|
||||
extern int __real_getgrgid_r(gid_t,struct group *,char *,size_t,struct group **);
|
||||
#define sys_pwnam __real_getpwnam_r
|
||||
#define sys_pwuid __real_getpwuid_r
|
||||
#define sys_grnam __real_getgrnam_r
|
||||
#define sys_grgid __real_getgrgid_r
|
||||
#else
|
||||
#define sys_pwnam getpwnam_r
|
||||
#define sys_pwuid getpwuid_r
|
||||
#define sys_grnam getgrnam_r
|
||||
#define sys_grgid getgrgid_r
|
||||
#endif
|
||||
static uid_t owner(void){
|
||||
#ifdef MARS_ID_TESTING
|
||||
return getuid();
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
static int safe_path(int write){char dir[sizeof(MARS_ID_DB)];strcpy(dir,MARS_ID_DB);char *s=strrchr(dir,'/');if(!s)return -1;*s=0;struct stat st;if(lstat(dir,&st)||!S_ISDIR(st.st_mode)||st.st_uid!=owner()||(st.st_mode&022))return -1;
|
||||
if(lstat(MARS_ID_DB,&st)){if(errno!=ENOENT)return -1;if(!write)return 1;int fd=open(MARS_ID_DB,O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW|O_CLOEXEC,0644);if(fd<0)return -1;int r=fchmod(fd,0644);if(close(fd))r=-1;return r;}
|
||||
return S_ISREG(st.st_mode)&&st.st_uid==owner()&&!(st.st_mode&022)&&st.st_nlink==1?0:-1;}
|
||||
int mars_id_open(sqlite3 **db,int write){*db=NULL;int p=safe_path(write);if(p)return p;int flags=(write?SQLITE_OPEN_READWRITE:SQLITE_OPEN_READONLY)|SQLITE_OPEN_NOFOLLOW; if(sqlite3_open_v2(MARS_ID_DB,db,flags,NULL)!=SQLITE_OK)goto fail;sqlite3_busy_timeout(*db,2000);if(write&&sqlite3_exec(*db,"CREATE TABLE IF NOT EXISTS identities(uid INTEGER PRIMARY KEY CHECK(uid BETWEEN 200000000 AND 200099999),alias TEXT NOT NULL UNIQUE,name TEXT NOT NULL,created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP);",NULL,NULL,NULL)!=SQLITE_OK)goto fail;return 0;
|
||||
fail:sqlite3_close(*db);*db=NULL;return -1;}
|
||||
int mars_id_name_valid(const char *s){return s&&*s&&strlen(s)<48&&strspn(s,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-")==strlen(s)&&strcmp(s,"SUPERVISOR")&&strcmp(s,"EVERYONE");}
|
||||
static int row(sqlite3_stmt *q,struct mars_identity *out){int rc=sqlite3_step(q);if(rc==SQLITE_DONE)return 1;if(rc!=SQLITE_ROW)return -1;int uid=sqlite3_column_int(q,0);const char *a=(const char*)sqlite3_column_text(q,1),*n=(const char*)sqlite3_column_text(q,2);char expected[20];snprintf(expected,sizeof expected,"marsv%09d",uid);if(uid<MARS_UID_FIRST||uid>MARS_UID_LAST||!a||strcmp(a,expected)||!mars_id_name_valid(n))return -1;out->uid=uid;strcpy(out->alias,a);strcpy(out->name,n);return 0;}
|
||||
int mars_id_lookup(const char *alias,int uid,struct mars_identity *out){if(alias&&(strlen(alias)!=14||strncmp(alias,"marsv",5)||strspn(alias+5,"0123456789")!=9))return 1;if(!alias&&(uid<MARS_UID_FIRST||uid>MARS_UID_LAST))return 1;sqlite3 *db;int rc=mars_id_open(&db,0);if(rc)return rc;sqlite3_stmt *q=NULL;rc=-1;if(sqlite3_prepare_v2(db,alias?"SELECT uid,alias,name FROM identities WHERE alias=?":"SELECT uid,alias,name FROM identities WHERE uid=?",-1,&q,NULL)==SQLITE_OK){if(alias)sqlite3_bind_text(q,1,alias,-1,SQLITE_TRANSIENT);else sqlite3_bind_int(q,1,uid);rc=row(q,out);}sqlite3_finalize(q);sqlite3_close(db);return rc;}
|
||||
static int collision(const struct mars_identity *r){struct passwd p,*pr=NULL;struct group g,*gr=NULL;char b[16384];int x;
|
||||
x=sys_pwnam(r->alias,&p,b,sizeof b,&pr);if(x||pr)return x?-1:1;
|
||||
x=sys_pwuid((uid_t)r->uid,&p,b,sizeof b,&pr);if(x||pr)return x?-1:1;
|
||||
x=sys_grnam(r->alias,&g,b,sizeof b,&gr);if(x||gr)return x?-1:1;
|
||||
x=sys_grgid((gid_t)r->uid,&g,b,sizeof b,&gr);return x?-1:gr?1:0;}
|
||||
int mars_id_reserve(const char *name,struct mars_identity *out){if(!mars_id_name_valid(name)||geteuid()!=owner())return -1;sqlite3 *db;if(mars_id_open(&db,1))return -1;sqlite3_stmt *q=NULL;int rc=-1,uid=MARS_UID_FIRST;
|
||||
if(sqlite3_exec(db,"BEGIN IMMEDIATE",NULL,NULL,NULL)!=SQLITE_OK)goto done;
|
||||
if(sqlite3_prepare_v2(db,"SELECT COALESCE(MAX(uid),199999999)+1 FROM identities",-1,&q,NULL)!=SQLITE_OK||sqlite3_step(q)!=SQLITE_ROW)goto done;
|
||||
uid=sqlite3_column_int(q,0);sqlite3_finalize(q);q=NULL;
|
||||
for(;uid<=MARS_UID_LAST;uid++){out->uid=uid;snprintf(out->alias,sizeof out->alias,"marsv%09d",uid);strcpy(out->name,name);int c=collision(out);if(c<0)goto done;if(!c)break;}if(uid>MARS_UID_LAST)goto done;
|
||||
if(sqlite3_prepare_v2(db,"INSERT INTO identities(uid,alias,name) VALUES(?,?,?)",-1,&q,NULL)!=SQLITE_OK)goto done;
|
||||
sqlite3_bind_int(q,1,uid);sqlite3_bind_text(q,2,out->alias,-1,SQLITE_TRANSIENT);sqlite3_bind_text(q,3,name,-1,SQLITE_TRANSIENT);if(sqlite3_step(q)!=SQLITE_DONE)goto done;sqlite3_finalize(q);q=NULL;if(sqlite3_exec(db,"COMMIT",NULL,NULL,NULL)==SQLITE_OK)rc=0;
|
||||
done:sqlite3_finalize(q);if(rc)sqlite3_exec(db,"ROLLBACK",NULL,NULL,NULL);sqlite3_close(db);return rc;}
|
||||
int mars_id_collision(const struct mars_identity *r){return collision(r);}
|
||||
17
native/identity.h
Normal file
17
native/identity.h
Normal file
@@ -0,0 +1,17 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
#ifndef MARS_IDENTITY_H
|
||||
#define MARS_IDENTITY_H
|
||||
#include <sqlite3.h>
|
||||
#ifndef MARS_ID_DB
|
||||
#define MARS_ID_DB "/var/lib/mars-identities/identities.sqlite3"
|
||||
#endif
|
||||
#define MARS_UID_FIRST 200000000
|
||||
#define MARS_UID_LAST 200099999
|
||||
struct mars_identity { int uid; char alias[20]; char name[48]; };
|
||||
/* 0 success, 1 absent, -1 error. No password material in this database. */
|
||||
int mars_id_lookup(const char *alias,int uid,struct mars_identity *out);
|
||||
int mars_id_reserve(const char *name,struct mars_identity *out);
|
||||
int mars_id_open(sqlite3 **db,int write);
|
||||
int mars_id_collision(const struct mars_identity *r);
|
||||
int mars_id_name_valid(const char *name);
|
||||
#endif
|
||||
30
native/identity_api.c
Normal file
30
native/identity_api.c
Normal file
@@ -0,0 +1,30 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
#include "identity_api.h"
|
||||
#include "identity.h"
|
||||
#include "admin.h"
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
static json_object *err(const char *msg){json_object *o=json_object_new_object();addstr(o,"error",msg);return o;}
|
||||
static void boolean(json_object *o,const char *key,int value){json_object_object_add(o,key,json_object_new_boolean(value));}
|
||||
static json_object *call(const char *op,const char *alias){json_object *q=json_object_new_object();addstr(q,"op",op);if(alias)addstr(q,"alias",alias);json_object *r=bindery_call(q);json_object_put(q);return r;}
|
||||
static int supported(void){json_object *p=call("identity-capabilities",NULL);int ok=p&&!get(p,"error")&&json_object_get_int(get(p,"identityVersion"))==1;json_object_put(p);return ok;}
|
||||
static int complete(json_object *live){return live&&!get(live,"error")&&json_object_is_type(get(live,"objects"),json_type_array)&&!json_object_get_boolean(get(live,"truncated"));}
|
||||
static int user(json_object *x){return json_object_get_int(get(x,"type"))==1&&js(x,"name");}
|
||||
static void issue(json_object *a,const char *name,const char *code,const char *detail){json_object *o=json_object_new_object();addstr(o,"name",name);addstr(o,"code",code);addstr(o,"detail",detail);json_object_array_add(a,o);}
|
||||
json_object *identity_status(void){
|
||||
json_object *o=json_object_new_object(),*a=json_object_new_array(),*issues=json_object_new_array();addstr(o,"mode","bindery");boolean(o,"nativeNds",0);int wrapped=supported();boolean(o,"wrappedBindery",wrapped);json_object_object_add(o,"identities",a);json_object_object_add(o,"issues",issues);
|
||||
json_object *live=call("objects",NULL),*objs=get(live,"objects");int full=complete(live);boolean(o,"liveComplete",full);
|
||||
sqlite3 *db;int rc=mars_id_open(&db,0);boolean(o,"databaseAvailable",rc==0);boolean(o,"databaseUninitialized",rc==1);if(rc<0)addstr(o,"databaseError","SQLite-Pfad oder Datenbank nicht lesbar.");
|
||||
if(!rc){sqlite3_stmt *q=NULL;int integrity=sqlite3_prepare_v2(db,"PRAGMA quick_check(1)",-1,&q,NULL)==SQLITE_OK&&sqlite3_step(q)==SQLITE_ROW&&sqlite3_column_text(q,0)&&!strcmp((const char*)sqlite3_column_text(q,0),"ok");sqlite3_finalize(q);q=NULL;boolean(o,"integrityOk",integrity);if(!integrity)addstr(o,"databaseError","SQLite-Integritätsprüfung fehlgeschlagen. Sicherung prüfen.");
|
||||
if(sqlite3_prepare_v2(db,"SELECT uid,alias,name FROM identities ORDER BY uid LIMIT 1001",-1,&q,NULL)!=SQLITE_OK)addstr(o,"databaseError","SQLite-Schema nicht lesbar.");else{
|
||||
while((rc=sqlite3_step(q))==SQLITE_ROW){if(json_object_array_length(a)>=1000){boolean(o,"truncated",1);break;}json_object *x=json_object_new_object();int uid=sqlite3_column_int(q,0);const char *alias=(const char*)sqlite3_column_text(q,1),*name=(const char*)sqlite3_column_text(q,2);json_object_object_add(x,"uid",json_object_new_int(uid));addstr(x,"alias",alias);addstr(x,"name",name);json_object_array_add(a,x);
|
||||
struct mars_identity id;int valid=alias&&name&&!mars_id_lookup(alias,0,&id)&&id.uid==uid&&!strcmp(id.name,name);int collision=valid?mars_id_collision(&id):-1;boolean(x,"hostConflict",collision!=0);if(collision)issue(issues,name,"uid-conflict","Virtuelle Identität ungültig oder Konflikt mit Linux. Nicht neu zuordnen.");
|
||||
const char *status="unknown";if(full&&valid){int exact=0,other=0;for(size_t j=0;j<json_object_array_length(objs);j++){json_object *b=json_object_array_get_idx(objs,j);const char *u=js(b,"unix");if(!user(b)||!u||strcmp(u,alias))continue;if(!strcasecmp(name,js(b,"name")))exact++;else other++;}status=other||exact>1?"conflict":exact==1?"linked":"reserved";if(other||exact>1)issue(issues,name,"alias-conflict","Virtuelle UID ist mehreren oder falschen Konten zugeordnet.");}addstr(x,"status",status);
|
||||
}if(rc!=SQLITE_ROW&&rc!=SQLITE_DONE)addstr(o,"databaseError","SQLite-Lesefehler.");}sqlite3_finalize(q);sqlite3_close(db);}
|
||||
if(full)for(size_t i=0;i<json_object_array_length(objs);i++){json_object *b=json_object_array_get_idx(objs,i);if(!user(b))continue;const char *alias=js(b,"unix"),*name=js(b,"name");if(!alias||!*alias){if(strcasecmp(name,"SUPERVISOR"))issue(issues,name,"mapping-missing","Kein Linux- oder SQLite-Konto zugeordnet.");continue;}if(strncmp(alias,"marsv",5))continue;struct mars_identity id;if(mars_id_lookup(alias,0,&id)||strcasecmp(id.name,name))issue(issues,name,"mapping-invalid","Bindery verweist auf eine fehlende oder falsche SQLite-Identität.");}
|
||||
json_object_put(live);return o;
|
||||
}
|
||||
json_object *identity_create(json_object *req){const char *name=js(req,"name"),*p=js(req,"password");if(!mars_id_name_valid(name)||!p||strlen(p)<8||strlen(p)>63||strspn(p,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=.,:;")!=strlen(p))return err("Name oder NetWare-Kennwort ungültig (8–63 Großbuchstaben/ASCII).");if(!supported())return err("nwbind unterstützt SQLite-Identitäten noch nicht. Upstream neu bauen und Dienst neu starten.");json_object *audit=identity_status();int unsafe=get(audit,"databaseError")!=NULL||!json_object_get_boolean(get(audit,"liveComplete"));json_object *issues=get(audit,"issues");for(size_t i=0;i<json_object_array_length(issues);i++){const char *code=js(json_object_array_get_idx(issues,i),"code");if(code&&(!strcmp(code,"uid-conflict")||!strcmp(code,"alias-conflict")||!strcmp(code,"mapping-invalid")))unsafe=1;}json_object_put(audit);if(unsafe)return err("Identitätsprüfung fehlgeschlagen. Unter Benutzer & Gruppen → Identitäten prüfen die Ursache klären.");json_object *live=call("objects",NULL),*objs=get(live,"objects");if(!complete(live)){json_object_put(live);return err("Vollständige Live-Bindery-Liste erforderlich. Serverstatus prüfen.");}for(size_t i=0;i<json_object_array_length(objs);i++){json_object *x=json_object_array_get_idx(objs,i);if(user(x)&&!strcasecmp(name,js(x,"name"))){json_object_put(live);return err("Benutzer existiert bereits. Bestehendes Konto bearbeiten statt neu anlegen.");}}json_object_put(live);
|
||||
struct mars_identity id;if(mars_id_reserve(name,&id))return err("SQLite-Reservierung fehlgeschlagen. Datenbank und UID-Bereich prüfen.");json_object *probe=call("identity-probe",id.alias);int good=probe&&!get(probe,"error")&&json_object_get_int(get(probe,"uid"))==id.uid&&json_object_get_int(get(probe,"gid"))==id.uid;json_object_put(probe);if(!good)return err("nwbind kann die reservierte Identität nicht auflösen. UID bleibt reserviert; keinen Linux-Benutzer anlegen.");json_object *q=json_object_new_object();addstr(q,"op","create-user");addstr(q,"name",name);addstr(q,"unix",id.alias);addstr(q,"password",p);json_object *r=bindery_call(q);json_object_put(q);if(!r||get(r,"error")){json_object_put(r);return err("Bindery-Anlage nicht bestätigt. UID bleibt reserviert. Live-Bindery vor Wiederholung prüfen.");}json_object_put(r);
|
||||
/* A successful IPC return alone is not evidence of a correct mapping. */
|
||||
live=call("objects",NULL);objs=get(live,"objects");int exact=0,conflict=0;if(complete(live))for(size_t i=0;i<json_object_array_length(objs);i++){json_object *x=json_object_array_get_idx(objs,i);const char *u=js(x,"unix");if(user(x)&&u&&!strcmp(u,id.alias)){if(!strcasecmp(name,js(x,"name")))exact++;else conflict++;}}json_object_put(live);if(exact!=1||conflict)return err("Konto möglicherweise angelegt, Zuordnung aber nicht bestätigt. Benutzerliste aktualisieren und prüfen; nicht blind wiederholen.");r=json_object_new_object();addstr(r,"alias",id.alias);json_object_object_add(r,"uid",json_object_new_int(id.uid));return r;}
|
||||
7
native/identity_api.h
Normal file
7
native/identity_api.h
Normal file
@@ -0,0 +1,7 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
#ifndef MARS_IDENTITY_API_H
|
||||
#define MARS_IDENTITY_API_H
|
||||
#include "core.h"
|
||||
json_object *identity_status(void);
|
||||
json_object *identity_create(json_object *req);
|
||||
#endif
|
||||
34
native/identity_wrap.c
Normal file
34
native/identity_wrap.c
Normal file
@@ -0,0 +1,34 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
#define _GNU_SOURCE
|
||||
#include "identity.h"
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
extern int mars_id_collision(const struct mars_identity *);
|
||||
extern struct passwd *__real_getpwnam(const char *);extern struct passwd *__real_getpwuid(uid_t);
|
||||
extern struct group *__real_getgrnam(const char *);extern struct group *__real_getgrgid(gid_t);
|
||||
extern int __real_getpwnam_r(const char *,struct passwd *,char *,size_t,struct passwd **);
|
||||
extern int __real_getpwuid_r(uid_t,struct passwd *,char *,size_t,struct passwd **);
|
||||
extern int __real_getgrnam_r(const char *,struct group *,char *,size_t,struct group **);
|
||||
extern int __real_getgrgid_r(gid_t,struct group *,char *,size_t,struct group **);
|
||||
extern int __real_initgroups(const char *,gid_t);
|
||||
extern int __real_getgrouplist(const char *,gid_t,gid_t *,int *);
|
||||
static int lookup(const char *name,int uid,struct mars_identity *i){int r=mars_id_lookup(name,uid,i);/* Never fall through to a host account in the reserved namespace. */
|
||||
if(r==1&&((name&&!strncmp(name,"marsv",5))||(!name&&uid>=MARS_UID_FIRST&&uid<=MARS_UID_LAST)))return -1;
|
||||
return !r&&mars_id_collision(i)?-1:r;}
|
||||
static int pw(const struct mars_identity *i,struct passwd *p,char *b,size_t n,struct passwd **result){*result=NULL;size_t a=strlen(i->alias)+1;const char data[]="!\0/var/lib/mars-identities/no-home\0/usr/sbin/nologin\0";if(n<a+sizeof data)return ERANGE;memcpy(b,i->alias,a);memcpy(b+a,data,sizeof data);char *home=b+a+2;*p=(struct passwd){.pw_name=b,.pw_passwd=b+a,.pw_uid=i->uid,.pw_gid=i->uid,.pw_gecos=b,.pw_dir=home,.pw_shell=home+strlen(home)+1};*result=p;return 0;}
|
||||
static int gr(const struct mars_identity *i,struct group *g,char *b,size_t n,struct group **result){*result=NULL;/* pointer alignment for caller-provided buffer */size_t pad=(sizeof(char*)-((size_t)b%sizeof(char*)))%sizeof(char*),a=strlen(i->alias)+1;if(n<pad+sizeof(char*)+a+2)return ERANGE;char **members=(char**)(b+pad);*members=NULL;char *s=b+pad+sizeof(char*);memcpy(s,i->alias,a);s[a]='!';s[a+1]=0;*g=(struct group){.gr_name=s,.gr_passwd=s+a,.gr_gid=i->uid,.gr_mem=members};*result=g;return 0;}
|
||||
#define WRAP_R(fn,kind,kt,expr,realarg,fill) int __wrap_##fn(kt key,struct kind *p,char *b,size_t n,struct kind **r){struct mars_identity i;int x=(expr);if(x==1)return __real_##fn(realarg,p,b,n,r);if(x){*r=NULL;return EIO;}return fill(&i,p,b,n,r);}
|
||||
WRAP_R(getpwnam_r,passwd,const char *,lookup(key,0,&i),key,pw)
|
||||
WRAP_R(getpwuid_r,passwd,uid_t,lookup(NULL,(int)key,&i),key,pw)
|
||||
WRAP_R(getgrnam_r,group,const char *,lookup(key,0,&i),key,gr)
|
||||
WRAP_R(getgrgid_r,group,gid_t,lookup(NULL,(int)key,&i),key,gr)
|
||||
#define WRAP(fn,kind,kt,expr,fill) struct kind *__wrap_##fn(kt key){static _Thread_local struct kind p;static _Thread_local char b[512];struct kind *r=NULL;struct mars_identity i;int x=(expr);if(x==1)return __real_##fn(key);if(x){errno=EIO;return NULL;}int e=fill(&i,&p,b,sizeof b,&r);if(e)errno=e;return r;}
|
||||
WRAP(getpwnam,passwd,const char *,lookup(key,0,&i),pw)
|
||||
WRAP(getpwuid,passwd,uid_t,lookup(NULL,(int)key,&i),pw)
|
||||
WRAP(getgrnam,group,const char *,lookup(key,0,&i),gr)
|
||||
WRAP(getgrgid,group,gid_t,lookup(NULL,(int)key,&i),gr)
|
||||
int __wrap_initgroups(const char *user,gid_t gid){struct mars_identity i;int r=lookup(user,0,&i);if(r==1)return __real_initgroups(user,gid);if(r||gid!=(gid_t)i.uid){errno=EINVAL;return -1;}return setgroups(1,&gid);}
|
||||
int __wrap_getgrouplist(const char *user,gid_t gid,gid_t *groups,int *n){struct mars_identity i;int r=lookup(user,0,&i);if(r==1)return __real_getgrouplist(user,gid,groups,n);if(r||gid!=(gid_t)i.uid){errno=EINVAL;return -1;}int cap=*n;*n=1;if(cap<1)return -1;groups[0]=gid;return 1;}
|
||||
@@ -3,7 +3,10 @@
|
||||
#include "core.h"
|
||||
#include "supervisor.h"
|
||||
#include "admin.h"
|
||||
#include "directory.h"
|
||||
#include "identity_api.h"
|
||||
#include "../src/gateway.h"
|
||||
#include "../src/ncp_ip.h"
|
||||
#include "gateway_auto.h"
|
||||
#include <civetweb.h>
|
||||
#include <pthread.h>
|
||||
@@ -56,6 +59,21 @@ static char *comparison(const char*a,const char*b){if(!strcmp(a,b))return strdup
|
||||
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. */
|
||||
/* NCP/IP settings are separate from the IPX gateway; only admin may change them. */
|
||||
static int ncp_ip_api(struct mg_connection*c,int post,json_object*req){
|
||||
if(auth_default()||no_services)return error(c,403,"Zuerst MARS einrichten und Web-Kennwort ändern.");
|
||||
char *old=read_file(NCP_IP_CONFIG,4096,1);int missing=!old&&errno==ENOENT;
|
||||
if(missing)old=strdup("{\"enabled\":false,\"address\":\"\"}");
|
||||
if(!old)return error(c,500,"TCP/IP-Konfiguration nicht lesbar. Dateirechte prüfen.");
|
||||
char rev[65];sha(old,rev);struct json_tokener *tok=json_tokener_new();json_object *cfg=NULL;size_t oldlen=strlen(old);if(tok){json_tokener_set_flags(tok,JSON_TOKENER_STRICT);cfg=json_tokener_parse_ex(tok,old,(int)oldlen);if(json_tokener_get_error(tok)!=json_tokener_success||json_tokener_get_parse_end(tok)!=oldlen){json_object_put(cfg);cfg=NULL;}json_tokener_free(tok);}free(old);
|
||||
uint32_t addr,mask;int valid=ncp_ip_config(cfg,&addr,&mask);
|
||||
if(post){const char *r=js(req,"revision");if(!r||strcmp(r,rev)){json_object_put(cfg);return error(c,409,"Konfiguration wurde geändert. Neu laden.");}
|
||||
json_object *next=get(req,"config");if(!confirm(req)||ncp_ip_config(next,&addr,&mask)<0){json_object_put(cfg);return error(c,400,"Eine vorhandene private IPv4-Adresse auswählen und bestätigen.");}
|
||||
json_object *save=json_object_new_object();json_object_object_add(save,"enabled",json_object_new_boolean(json_object_get_boolean(get(next,"enabled"))));addstr(save,"address",js(next,"address")?js(next,"address"):"");json_object_object_add(save,"slp",json_object_new_boolean(json_object_get_boolean(get(next,"slp"))));
|
||||
const char *str=json_object_to_json_string_ext(save,JSON_C_TO_STRING_PLAIN);int rc=atomic_write(NCP_IP_CONFIG,str,strlen(str),0600);json_object_put(save);json_object_put(cfg);if(rc)return error(c,500,"TCP/IP-Einstellung konnte nicht gespeichert werden.");return ok(c,NULL);
|
||||
}
|
||||
json_object *out=json_object_new_object();json_object_object_add(out,"config",cfg?cfg:json_object_new_object());addstr(out,"revision",rev);json_object_object_add(out,"valid",json_object_new_boolean(valid>=0));json_object_object_add(out,"addresses",ncp_ip_addresses());json_object_object_add(out,"port",json_object_new_int(NCP_TCP_PORT));json_object_object_add(out,"nativeNds",json_object_new_boolean(0));char *runtime=read_file(NCP_IP_STATUS,1024,1);struct stat rst;int fresh=runtime&&!stat(NCP_IP_STATUS,&rst)&&time(NULL)>=rst.st_mtime&&time(NULL)-rst.st_mtime<5;addstr(out,"runtime",fresh?runtime:"Kein aktueller TCP/IP-Status. NetWare starten bzw. neu starten.");free(runtime);return reply(c,200,out,NULL);
|
||||
}
|
||||
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.");
|
||||
@@ -176,12 +194,28 @@ static int process(struct mg_connection*c,const struct mg_request_info*r,json_ob
|
||||
if(access(p,R_OK))return error(c,404,"Browser-Quellpaket noch nicht installiert.");
|
||||
mg_send_file(c,p);return 200;
|
||||
}
|
||||
if(!strcmp(u,"/api/identities")){
|
||||
if(auth_default()||no_services)return error(c,403,"Eingerichteter Dienstmodus erforderlich.");
|
||||
if(!post)return reply(c,200,identity_status(),NULL);
|
||||
if(!confirm(req)||!js(req,"op")||strcmp(js(req,"op"),"create"))return error(c,400,"Bestätigte Benutzeranlage erforderlich.");
|
||||
json_object *out=identity_create(req);return reply(c,get(out,"error")?400:200,out,NULL);
|
||||
}
|
||||
if(!strcmp(u,"/api/directory")){
|
||||
if(auth_default())return error(c,403,"Zuerst das Web-Kennwort ändern.");
|
||||
char path[PATH_MAX],rev[65];int configured;
|
||||
if(snprintf(path,sizeof path,"%s.directory.json",config_path)>=(int)sizeof path)return error(c,500,"Verzeichnispfad zu lang.");
|
||||
json_object *dir=directory_read(path,rev,&configured);if(!dir)return error(c,500,"Verzeichnisdatei ungültig oder nicht lesbar. Sicherung prüfen.");
|
||||
if(post){json_object *next=get(req,"config");json_object *errs=directory_validate(next);if(json_object_array_length(errs)){json_object *o=json_object_new_object();addstr(o,"error","Verzeichnisentwurf ungültig.");json_object_object_add(o,"errors",errs);json_object_put(dir);return reply(c,400,o,NULL);}json_object_put(errs);
|
||||
int rc=confirm(req)?directory_save(path,next,js(req,"revision")):-3;json_object_put(dir);if(rc)return error(c,rc==-2?409:rc==-3?400:500,rc==-2?"Verzeichnis wurde inzwischen geändert. Neu laden.":"Verzeichnis konnte nicht gespeichert werden; Entwurf und Bestätigung prüfen.");return ok(c,NULL);}
|
||||
char *conf=read_file(config_path,MAX_CONF,0);if(!conf){json_object_put(dir);return error(c,500,"nwserv.conf nicht lesbar.");}json_object *query=json_object_new_object();addstr(query,"op","objects");json_object *live=no_services?NULL:bindery_call(query);json_object_put(query);json_object *o=directory_report(conf,dir,live,!no_services&&supervisor_config_matches());free(conf);json_object_put(live);json_object_object_add(o,"config",dir);addstr(o,"revision",rev);json_object_object_add(o,"configured",json_object_new_boolean(configured));json_object_object_add(o,"status",supervisor_status());return reply(c,200,o,NULL);
|
||||
}
|
||||
if(!strcmp(u,"/api/browser"))return browser_api(c,post,req);
|
||||
if(!strcmp(u,"/api/ncp-ip"))return ncp_ip_api(c,post,req);
|
||||
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);}
|
||||
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.");const char *unixname=js(req,"unix");if(!strcmp(u,"/api/bindery")&&unixname&&!strncmp(unixname,"marsv",5)&&(!strcmp(op,"create-user")||!strcmp(op,"map-user")))return error(c,400,"Reservierte SQLite-Identitäten nur über die geführte Benutzeranlage verwenden.");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);}
|
||||
char*current=read_file(config_path,MAX_CONF,0);if(!current)return error(c,500,"nwserv.conf nicht lesbar.");char rev[65];sha(current,rev);int ret=404;
|
||||
if(!post&&!strcmp(u,"/api/state")){ret=reply(c,200,state(s,current),NULL);goto done;}if(!post&&!strcmp(u,"/api/download")){if(auth_default())ret=error(c,403,"Kennwort ändern.");else ret=send_data(c,200,"text/plain; charset=utf-8",current,strlen(current),NULL,NULL);goto done;}
|
||||
if(!post){ret=error(c,404,"Nicht gefunden.");goto done;}const char*given=js(req,"revision");if(!given||strcmp(given,rev)){ret=error(c,409,"Konfiguration geändert. Bitte neu laden.");goto done;}
|
||||
|
||||
Reference in New Issue
Block a user