/************************************************************************** * * Copyright (c) 2013 Alcatel-Lucent * * Alcatel Lucent licenses this file to You under the Apache License, * Version 2.0 (the "License"); you may not use this file except in * compliance with the License. A copy of the License is contained the * file LICENSE at the top level of this repository. * You may also obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ************************************************************************** * * misccmds: * * More monitor commands... * * Original author: Ed Sutter (ed.sutter@alcatel-lucent.com) * */ #include "config.h" #include "stddefs.h" #include "tfs.h" #include "tfsprivate.h" #include "genlib.h" #include "ether.h" #include "devices.h" #include "cli.h" #include #include "warmstart.h" char ApplicationInfo[82]; int (*extgetUsrLvl)(void); #if INCLUDE_USRLVL static int setUsrLvl(int, char *); static int UserLevel; char *UlvlHelp[] = { "Display or modify current user level.", "-[c:hp] [new_level|min|max] [password]", #if INCLUDE_VERBOSEHELP "Options:", " -c{cmd,lvl}", " set command's user level", " -h dump system header", " -p build new password file", "", " Note: cmd==ALL, applies action to all commands.", #endif 0 }; int Ulvl(int argc,char *argv[]) { char passwd[32], *pwp; int level, opt, newulvl; newulvl = 0; pwp = (char *)0; level = MINUSRLEVEL; while((opt=getopt(argc,argv,"c:hp")) != -1) { switch(opt) { case 'c': setCmdUlvl(optarg,1); newulvl++; break; case 'h': monHeader(0); break; case 'p': newPasswordFile(); break; default: return(CMD_PARAM_ERROR); } } /* At this point, the newulvl flag is used to indicate that the * -c option was used. If it was, then we return here. */ if(newulvl) { return(CMD_SUCCESS); } /* If there is one or two arguments on the command line, then * the user must want to modify the current user level. If * there are no arguments, then simply display the current * user level. * * If the new user level is lower than the current user level, * then the user can simply enter the new level (one argument). * If the new user level is higher than the current user level, * then the user must also enter a password. The password is * entered either as the second argument or interactively * using getpass(). */ newulvl = 0; if((argc == optind+1) || (argc == optind+2)) { if(!strcmp(argv[optind],"min")) { level = MINUSRLEVEL; } else if(!strcmp(argv[optind],"max")) { level = MAXUSRLEVEL; } else { level = atoi(argv[optind]); } if(argc == optind+1) { if(level > UserLevel) { getpass("Password: ",passwd,sizeof(passwd)-1,0); pwp = passwd; } } else { pwp = argv[optind+1]; } newulvl = 1; } else if(argc != optind) { return(CMD_PARAM_ERROR); } /* At this point,the newulvl flag is used to indicate that an * adjustment to the current user level is to be made. */ if(newulvl) { if(level <= UserLevel) { UserLevel = level; } else { setUsrLvl(level,pwp); } } if(extgetUsrLvl) { printf("User level controlled by application: %d\n",extgetUsrLvl()); } else { printf("Current monitor user level: %d\n",UserLevel); } return(CMD_SUCCESS); } void initUsrLvl(int lvl) { extgetUsrLvl = 0; UserLevel = lvl; } /* getUsrLvl(): * This is the ONLY point of access for retrieval of the user level. * This allows the application to redefine how the monitor retrieves * what it thinks of as the user level. */ int getUsrLvl(void) { if(extgetUsrLvl) { return(extgetUsrLvl()); } return(UserLevel); } int setUsrLvl(int level, char *password) { int olvl; olvl = UserLevel; /* If level is -1, then assume this is only a request for the current */ /* user level. If the incoming level is any other value outside the */ /* range of MINUSRLEVEL and MAXUSRLEVEL, return -1. */ if(level == -1) { return(UserLevel); } if((level > MAXUSRLEVEL) || (level < MINUSRLEVEL)) { return(olvl); } /* If password pointer is NULL, then don't check for password, just set */ /* the level and be done. */ if(!password) { UserLevel = level; } else { if(validPassword(password,level)) { UserLevel = level; } } return(olvl); } /* returnMaxUsrLvl(), setTmpMaxUsrLvl() & clrTmpMaxUsrLvl(): * These three functions are used to allow a few places in the monitor * to temporarily force the user level to MAXUSRLEVEL. This is necessary * for accessing the password file and the tfs log file. * Call setTmpMaxUsrLvl() to enable MAX mode and then clrTmpMaxUsrLvl() * with the value returned by setTmpMaxUsrLvl() when done. */ int returnMaxUsrLvl(void) { return(MAXUSRLEVEL); } void * setTmpMaxUsrLvl(void) { void *fptr; fptr = (void *)extgetUsrLvl; extgetUsrLvl = returnMaxUsrLvl; return(fptr); } void clrTmpMaxUsrLvl(int (*fptr)(void)) { extgetUsrLvl = fptr; } #else int getUsrLvl(void) { return(MAXUSRLEVEL); } #endif char *VersionHelp[] = { "Version information", "[application_info]", 0, }; int Version(int argc,char *argv[]) { extern void ShowVersion(void); if(argc == 1) { ShowVersion(); } else { strncpy(ApplicationInfo,argv[1],80); ApplicationInfo[80] = 0; } return(CMD_SUCCESS); } #if INCLUDE_TFSSCRIPT char *EchoHelp[] = { "Print string to local terminal", "[arg1] ... [argn]", #if INCLUDE_VERBOSEHELP " Special meaning: \\b \\c \\r \\n \\t \\x", #endif 0, }; int Echo(int argc,char *argv[]) { int i, done; char *cp, c, hex[3]; for(i=optind; i optind+11)) { return(CMD_PARAM_ERROR); } func = (int(*)(long,long,long,long,long,long,long,long,long,long)) strtol(argv[optind],(char **)0,0); if((func == 0) && (isdigit(argv[optind][0]) == 0)) { return(CMD_PARAM_ERROR); } /* If useargc flag is not set, then retrieve and convert * args from command line. If the first character of the * argument is an ampersand (&), then a pointer to the argument * is passed; otherwise, the argument is converted to a long * integer using strtol()... */ if(!useargc) { for(j=0,i=optind+1; i