📄 clients.mx
字号:
@' The contents of this file are subject to the MonetDB Public License@' Version 1.1 (the "License"); you may not use this file except in@' compliance with the License. You may obtain a copy of the License at@' http://monetdb.cwi.nl/Legal/MonetDBLicense-1.1.html@'@' Software distributed under the License is distributed on an "AS IS"@' basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the@' License for the specific language governing rights and limitations@' under the License.@'@' The Original Code is the MonetDB Database System.@'@' The Initial Developer of the Original Code is CWI.@' Portions created by CWI are Copyright (C) 1997-2007 CWI.@' All Rights Reserved.@f clients@a Martin Kersten, Fabian Groffen@v 0.2@+ Client ManagementEach online client is represented with an entry in the clients table.The client may inspect his record at run-time and partially change itsproperties.The administrator sees all client records and has the right toadjust global properties.@malmodule clients;#Operations for all clientscommand setListing(flag:int):intaddress CLTsetListingcomment "Turn on/off echo of MAL instructions: 2 - show mal instruction, 4 - show details of type resolutoin, 8 - show binding information.";command setHistory(s:str)address CLTsetHistorycomment "Designate console history file for readline.";command getId():intaddress CLTgetClientIdcomment "Return a number that uniquely represents the current client.";command getInfo( ):bat[:str,:str] address CLTInfocomment "Pseudo bat with client attributes.";command getScenario():str address CLTgetScenariocomment "Retrieve current scenario name.";command setScenario(msg:str):str address CLTsetScenariocomment "Switch to other scenario handler, return previous one.";command quit(timeout:int):voidaddress CLTquit;command quit():voidaddress CLTquitDefaultcomment "Terminate the server by (soft/hard) forcing all clients to terminate. This command can only be initiated from the console.";command exit():voidaddress CLTexitcomment "Terminate the session for a single client using a soft error.";#Administrator operationscommand getLogins( ):bat[:int,:str] address CLTLogincomment "Pseudo bat of client login time.";command getUsers( ):bat[:int,:str] address CLTuserscomment "Pseudo bat of users logged in.";command setTrace(flag:bit):voidaddress CLTtracecomment "Trace messages from all users.";@-@{@include prelude.mx@+ Monet client dataPortions of the client record can be directly obtained forbackward compatibility. The routine clientInfo provides moredetailed information.@h#ifndef _CLIENTS_H#define _CLIENTS_H#include "mal.h"#ifdef WIN32#ifndef LIBCLIENTS#define clients_export extern __declspec(dllimport)#else#define clients_export extern __declspec(dllexport)#endif#else#define clients_export extern#endifclients_export str CLTsetListing(int *ret, int *flag);clients_export str CLTgetClientId(int *ret);clients_export str CLTgetScenario(str *ret);clients_export str CLTsetScenario(str *ret, str *nme);clients_export str CLTchangeUsername(int *ret, str *old, str *new) ;clients_export str CLTusers(int *ret);clients_export str CLTsetHistory(int *ret, str *fname);clients_export str CLTexit(int *ret);clients_export str CLTquit(int *ret, int *timeout);clients_export str CLTquitDefault(int *ret);clients_export str CLTLogin(int *ret);clients_export str CLTInfo(int *ret);clients_export str CLTtrace(int *res, int *flg);clients_export str CLTstop(int *ret, str *nme);clients_export str CLTaddUser(oid *ret, str *usr, str *pw, bat *sc);clients_export str CLTremoveUser(int *ret, str *usr);clients_export str CLTchangePassword(int *ret, str *old, str *new);clients_export str CLTsetPassword(int *ret, str *usr, str *new);clients_export str CLTcheckPermission(int *ret, str *usr, str *pw, str *sc);clients_export str CLTaddScenario(int *ret, str *usr, str *sc);clients_export str CLTremoveScenario(int *ret, str *usr, str *sc);clients_export str CLTgetUsers(bat *ret, bat *scens);#endif /* _CLIENTS_H */@c#include "mal_config.h"#include "clients.h"#include "mal_scenario.h"#include "mal_instruction.h"#include "mal_client.h"#include "mal_authorize.h"#ifdef HAVE_LIBREADLINE#include <readline/readline.h>#include <readline/history.h>#endifstrCLTsetListing(int *ret, int *flag){ Client c; (void) ret; /*fool compiler */ c = MCgetClient(); *ret = c->listing; c->listing = *flag; return MAL_SUCCEED;}strCLTgetClientId(int *ret){ Client c = MCgetClient(); *ret = c - mal_clients; return MAL_SUCCEED;}strCLTgetScenario(str *ret){ Client c = MCgetClient(); if (c->scenario) *ret = GDKstrdup(c->scenario); else *ret = GDKstrdup("nil"); return MAL_SUCCEED;}strCLTsetScenario(str *ret, str *nme){ Client c = MCgetClient(); str msg = MAL_SUCCEED; msg = setScenario(c, *nme); *ret = 0; if (msg == NULL) *ret = GDKstrdup(c->scenario); return msg;}static char *local_itoa(int i){ static char buf[32]; sprintf(buf, "%d", i); return buf;}voidCLTtimeConvert(time_t l, char *s){ struct tm localt;#ifdef HAVE_LOCALTIME_R (void) localtime_r(&l, &localt);#else /* race condition: return value could be * overwritten in parallel thread before * assignment complete */ localt = *localtime(&l);#endif#ifdef HAVE_ASCTIME_R3 asctime_r(&localt, s, sizeof(s));#else#ifdef HAVE_ASCTIME_R asctime_r(&localt, s);#else /* race condition: return value could be * overwritten in parallel thread before copy * complete, however on Windows, asctime is * thread-safe */ strncpy(s, asctime(&localt), 26);#endif#endif s[24] = 0;}strCLTInfo(int *ret){ Client c = MCgetClient(); BAT *b = BATnew(TYPE_str, TYPE_str, 12); char s[26]; if (b == 0) throw(MAL, "clients.info", "failed to create BAT"); BUNins(b, "user", local_itoa((int)c->user), FALSE); BUNins(b, "password", "", FALSE); /* FIXME: get rid of this */ BUNins(b, "permission", local_itoa(c->permission), FALSE); BUNins(b, "scenario", c->scenario, FALSE); BUNins(b, "timer", local_itoa((int) c->timer), FALSE); BUNins(b, "trace", local_itoa(c->itrace), FALSE); BUNins(b, "listing", local_itoa(c->listing), FALSE); BUNins(b, "debug", local_itoa(c->debug), FALSE); BUNins(b, "cwd", c->cwd, FALSE); CLTtimeConvert((time_t) c->login,s); BUNins(b, "login", s, FALSE); CLTtimeConvert((time_t) c->delay,s); BUNins(b, "delay", s, FALSE); if (!(b->batDirty&2)) b = BATsetaccess(b, BAT_READ); @:Pseudo(client,info,)@ return MAL_SUCCEED;}strCLTLogin(int *ret){ BAT *b = BATnew(TYPE_int, TYPE_str, 12); int i; char s[26]; if (b == 0) throw(MAL, "clients.getLogins", "failed to create BAT"); for (i = 0; i < MAL_MAXCLIENTS; i++) { Client c = mal_clients+i; if (c->mode >= CLAIMED && c->user != oid_nil) { CLTtimeConvert((time_t) c->login,s); BUNins(b, &i, s, FALSE); } } if (!(b->batDirty&2)) b = BATsetaccess(b, BAT_READ); @:Pseudo(client,login,)@ return MAL_SUCCEED;}@-Produce a list of clients currently logged in@cstrCLTusers(int *ret){ BAT *b = BATnew(TYPE_int, TYPE_str, 12); int i; if (b == 0) throw(MAL, "clients.users", "failed to create BAT"); for (i = 0; i < MAL_MAXCLIENTS; i++) { Client c = mal_clients+i; if (c->mode >= CLAIMED && c->user != oid_nil) b = BUNins(b, &i, local_itoa((int)c->user), FALSE); } if (!(b->batDirty&2)) b = BATsetaccess(b, BAT_READ); @:Pseudo(client,users,)@ return MAL_SUCCEED;}strCLTsetHistory(int *ret, str *fname){ Client c= MCgetClient(); (void) ret; if( c->history){#ifdef HAVE_LIBREADLINE write_history(c->history);#endif GDKfree(c->history); } if( *fname == str_nil) c->history = NULL; else { c->history = GDKstrdup(*fname);#ifdef HAVE_LIBREADLINE read_history(c->history);#endif } return MAL_SUCCEED;}strCLTexit(int *ret){ Client c = MCgetClient(); (void) ret; /* fool compiler */ c->mode = FINISHING; throw(MAL, "client.exit", "Exit session with soft error");}strCLTquit(int *ret, int *timeout){ Client c = MCgetClient(); (void) ret; /* fool compiler */ if( c != mal_clients ) throw(MAL, "client.quit", "Server not stopped from console."); MCshutdown(c, *timeout); return MAL_SUCCEED;}strCLTquitDefault(int *ret){ int timeout = 0; return CLTquit(ret,&timeout);}strCLTtrace(int *res, int *flg){ (void) res; /* fool compiler */ (void) MCtraceAllClients(*flg); return MAL_SUCCEED;}strCLTstop(int *ret, str *nme){ (void) ret; /* fool compiler */ (void) nme;/* return stopClient(MCgetClient(), *nme);*/ return MAL_SUCCEED;}@+ User administrationThese commands (except changePassword, changeUsername andcheckPermission) can only be executed by the administrator.@malcommand addUser(nme:str, pw:str, scen:bat[:str,:any_1]):oidaddress CLTaddUsercomment "Allow user with password access to the given scenarios";@cstr CLTaddUser(oid *ret, str *usr, str *pw, bat *sc) { str tmp; rethrow("addUser", tmp, AUTHaddUser(ret, usr, pw, sc)); return(MAL_SUCCEED);}@malcommand removeUser(nme:str):voidaddress CLTremoveUsercomment "Remove the given user from the system";@cstr CLTremoveUser(int *ret, str *usr) { str tmp; (void) ret; rethrow("removeUser", tmp, AUTHremoveUser(usr)); return(MAL_SUCCEED);}@malcommand changeUsername(old:str, new:str):voidaddress CLTchangeUsernamecomment "Change the username of the user into the new string";@cstr CLTchangeUsername(int *ret, str *old, str *new) { str tmp; (void) ret; rethrow("changeUsername", tmp, AUTHchangeUsername(old, new)); return(MAL_SUCCEED);}@malcommand changePassword(old:str, new:str):voidaddress CLTchangePasswordcomment "Change the password for the current user";@cstr CLTchangePassword(int *ret, str *old, str *new) { str tmp; (void) ret; rethrow("changePassword", tmp, AUTHchangePassword(old, new)); return(MAL_SUCCEED);}@malcommand setPassword(user:str, pass:str):voidaddress CLTsetPasswordcomment "Set the password for the given user";@cstr CLTsetPassword(int *ret, str *usr, str *new) { str tmp; (void) ret; rethrow("setPassword", tmp, AUTHsetPassword(usr, new)); return(MAL_SUCCEED);}@malcommand checkPermission(usr:str, pw:str, sc:str):voidaddress CLTcheckPermissioncomment "Check permission for a user";@cstr CLTcheckPermission(int *ret, str *usr, str *pw, str *sc) { str tmp; str ch = ""; str algo = "plain"; oid id; (void) ret; rethrow("checkPermission", tmp, AUTHcheckCredentials(&id, usr, pw, &ch, &algo, sc)); return(MAL_SUCCEED);}@malcommand addScenario(usr:str, sc:str):voidaddress CLTaddScenariocomment "add the given scenario to the allowed scenarios for the given user";@cstr CLTaddScenario(int *ret, str *usr, str *sc) { str tmp; (void) ret; rethrow("addScenario", tmp, AUTHaddScenario(usr, sc)); return(MAL_SUCCEED);}@malcommand removeScenario(usr:str, sc:str):voidaddress CLTremoveScenariocomment "remove the given scenario from the allowed scenarios for the given user";@cstr CLTremoveScenario(int *ret, str *usr, str *sc) { str tmp; (void) ret; rethrow("removeScenario", tmp, AUTHremoveScenario(usr, sc)); return(MAL_SUCCEED);}@malcommand getUsers(s:bat[:str,:any_1]):bat[:oid,:str]address CLTgetUserscomment "return a BAT with user id and name available in the system with access to the given scenario(s)";@cstr CLTgetUsers(bat *ret, bat *scens) { str tmp; rethrow("getUsers", tmp, AUTHgetUsers(ret, scens)); return(MAL_SUCCEED);}@}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -