Map new SQL users to explicitly selected existing Linux accounts

This commit is contained in:
2026-12-01 12:41:25 +00:00
parent b131dcc9d6
commit 53e6c27960
12 changed files with 166 additions and 12 deletions

View File

@@ -39,7 +39,7 @@ static uid_t owner(void){
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;
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);CREATE TABLE IF NOT EXISTS local_mappings(name TEXT PRIMARY KEY,unix_name TEXT NOT NULL,uid INTEGER NOT NULL CHECK(uid>0),gid INTEGER NOT NULL CHECK(gid>0),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;}

View File

@@ -1,5 +1,9 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#define _GNU_SOURCE
#include "identity_api.h"
#include <pwd.h>
#include <stdio.h>
#include <limits.h>
#include "identity.h"
#include "admin.h"
#include <string.h>
@@ -11,6 +15,47 @@ static int supported(void){json_object *p=call("identity-capabilities",NULL);int
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);}
static int local_account(const char *name,int *uid,int *gid){
struct passwd p,*found=NULL;char buf[16384];
if(!name||!*name||strlen(name)>19||!strncmp(name,"marsv",5)||getpwnam_r(name,&p,buf,sizeof buf,&found)||!found||p.pw_uid<1000||p.pw_uid>=60000||!p.pw_gid||p.pw_gid>INT_MAX)return -1;
*uid=(int)p.pw_uid;*gid=(int)p.pw_gid;return 0;
}
static json_object *local_users(void){
json_object *a=json_object_new_array();FILE *f=fopen("/etc/passwd","r");if(!f)return a;struct passwd *p;
while((p=fgetpwent(f))&&json_object_array_length(a)<1000){char name[256];snprintf(name,sizeof name,"%s",p->pw_name);int uid,gid;if(local_account(name,&uid,&gid))continue;
json_object *x=json_object_new_object();addstr(x,"name",name);json_object_object_add(x,"uid",json_object_new_int(uid));json_object_object_add(x,"gid",json_object_new_int(gid));json_object_array_add(a,x);}
fclose(f);return a;
}
static int local_table(sqlite3 *db){sqlite3_stmt *q=NULL;int yes=0;if(sqlite3_prepare_v2(db,"SELECT 1 FROM sqlite_master WHERE type='table' AND name='local_mappings'",-1,&q,NULL)==SQLITE_OK)yes=sqlite3_step(q)==SQLITE_ROW;sqlite3_finalize(q);return yes;}
int identity_local_managed(const char *name){
sqlite3 *db;int rc=mars_id_open(&db,0);if(rc)return rc<0?1:0;int found=0;sqlite3_stmt *q=NULL;
if(local_table(db)){if(sqlite3_prepare_v2(db,"SELECT 1 FROM local_mappings WHERE name=?",-1,&q,NULL)!=SQLITE_OK)found=1;else{sqlite3_bind_text(q,1,name?name:"",-1,SQLITE_TRANSIENT);int step=sqlite3_step(q);found=step!=SQLITE_DONE;}}
sqlite3_finalize(q);sqlite3_close(db);return found;
}
static json_object *local_report(json_object *objects,int full){
json_object *a=json_object_new_array();sqlite3 *db;if(mars_id_open(&db,0))return a;sqlite3_stmt *q=NULL;
if(local_table(db)&&sqlite3_prepare_v2(db,"SELECT name,unix_name,uid,gid FROM local_mappings ORDER BY name LIMIT 1000",-1,&q,NULL)==SQLITE_OK){while(sqlite3_step(q)==SQLITE_ROW){
const char *name=(const char*)sqlite3_column_text(q,0),*unixname=(const char*)sqlite3_column_text(q,1);int uid=sqlite3_column_int(q,2),gid=sqlite3_column_int(q,3),current_uid,current_gid;
const char *status="unknown";int valid=!local_account(unixname,&current_uid,&current_gid)&&uid==current_uid&&gid==current_gid;
if(!valid)status="account-changed";else if(full){status="reserved";for(size_t i=0;i<json_object_array_length(objects);i++){json_object *x=json_object_array_get_idx(objects,i);if(user(x)&&!strcasecmp(name,js(x,"name"))){status=js(x,"unix")&&!strcmp(unixname,js(x,"unix"))?"linked":"conflict";break;}}}
json_object *x=json_object_new_object();addstr(x,"name",name);addstr(x,"unix",unixname);addstr(x,"status",status);json_object_object_add(x,"uid",json_object_new_int(uid));json_object_object_add(x,"gid",json_object_new_int(gid));json_object_array_add(a,x);
}}sqlite3_finalize(q);sqlite3_close(db);return a;
}
static json_object *local_create(json_object *req){
const char *name=js(req,"name"),*unixname=js(req,"unix");int uid,gid;
if(local_account(unixname,&uid,&gid)||!json_object_is_type(get(req,"uid"),json_type_int)||!json_object_is_type(get(req,"gid"),json_type_int)||json_object_get_int64(get(req,"uid"))!=uid||json_object_get_int64(get(req,"gid"))!=gid)return err("Linux-Konto fehlt, ist ungeeignet oder wurde geändert. Auswahl neu laden (UID 1000–59999, kein root).");
if(!json_object_is_type(get(req,"sharedConfirmed"),json_type_boolean)||!json_object_get_boolean(get(req,"sharedConfirmed")))return err("Gemeinsame Unix-Dateirechte ausdrücklich bestätigen.");
sqlite3 *db;if(mars_id_open(&db,1))return err("SQLite konnte nicht geöffnet werden.");sqlite3_stmt *stmt=NULL;int saved=0;
if(sqlite3_prepare_v2(db,"INSERT INTO local_mappings(name,unix_name,uid,gid) VALUES(?,?,?,?)",-1,&stmt,NULL)==SQLITE_OK){sqlite3_bind_text(stmt,1,name,-1,SQLITE_TRANSIENT);sqlite3_bind_text(stmt,2,unixname,-1,SQLITE_TRANSIENT);sqlite3_bind_int(stmt,3,uid);sqlite3_bind_int(stmt,4,gid);saved=sqlite3_step(stmt)==SQLITE_DONE;}
sqlite3_finalize(stmt);sqlite3_close(db);if(!saved)return err("SQL-Zuordnung bereits reserviert oder nicht speicherbar. Identitäten prüfen; nicht blind erneut anlegen.");
json_object *q=json_object_new_object();addstr(q,"op","create-user");addstr(q,"name",name);addstr(q,"unix",unixname);addstr(q,"password",js(req,"password"));json_object *result=bindery_call(q);json_object_put(q);
if(!result||get(result,"error")){json_object_put(result);return err("Bindery-Anlage nicht bestätigt. SQL-Zuordnung bleibt reserviert; Live-Bindery prüfen.");}json_object_put(result);
json_object *live=call("objects",NULL),*objects=get(live,"objects");int exact=0;
if(complete(live))for(size_t i=0;i<json_object_array_length(objects);i++){json_object *x=json_object_array_get_idx(objects,i);if(user(x)&&!strcasecmp(name,js(x,"name"))&&js(x,"unix")&&!strcmp(unixname,js(x,"unix")))exact++;}
json_object_put(live);if(exact!=1)return err("Anlage nicht eindeutig bestätigt. SQL-Zuordnung bleibt reserviert; Live-Bindery prüfen.");
result=json_object_new_object();addstr(result,"alias",unixname);addstr(result,"unix",unixname);addstr(result,"mode","local");json_object_object_add(result,"uid",json_object_new_int(uid));json_object_object_add(result,"gid",json_object_new_int(gid));return result;
}
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);
@@ -22,9 +67,10 @@ json_object *identity_status(void){
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_object_add(o,"localUsers",local_users());json_object *locals=local_report(objs,full);json_object_object_add(o,"localMappings",locals);for(size_t i=0;i<json_object_array_length(locals);i++){json_object *x=json_object_array_get_idx(locals,i);const char *st=js(x,"status");if(!strcmp(st,"account-changed")||!strcmp(st,"conflict"))issue(issues,js(x,"name"),"mapping-invalid","Gespeicherte Linux-Zuordnung passt nicht mehr zu Konto oder Bindery. Keine automatische Rechte- oder Eigentümermigration.");}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);
if(get(req,"unix"))return local_create(req);
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;}

View File

@@ -2,6 +2,7 @@
#ifndef MARS_IDENTITY_API_H
#define MARS_IDENTITY_API_H
#include "core.h"
int identity_local_managed(const char *name);
json_object *identity_status(void);
json_object *identity_create(json_object *req);
#endif

View File

@@ -236,7 +236,7 @@ static int process(struct mg_connection*c,const struct mg_request_info*r,json_ob
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.");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);}
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(!strcmp(op,"map-user")&&identity_local_managed(js(req,"name")))return error(c,409,"SQL-Zuordnung geschützt: bestehende Benutzer nicht ohne geprüfte Eigentümermigration umstellen.");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;}