void setclassenvironment(login_cap_t *lc, const struct passwd * pwd, int paths) { struct login_vars *vars = paths ? pathvars : envars; int hlen = pwd ? strlen(pwd->pw_dir) : 0; int nlen = pwd ? strlen(pwd->pw_name) : 0; char pch = 0; if (hlen && pwd->pw_dir[hlen-1] != '/') ++pch; while (vars->tag != NULL) { const char * var = paths ? login_getpath(lc, vars->tag, NULL) : login_getcapstr(lc, vars->tag, NULL, NULL); char * np = substvar(var, pwd, hlen, pch, nlen); if (np != NULL) { setenv(vars->var, np, vars->overwrite); free(np); } else if (vars->def != NULL) { setenv(vars->var, vars->def, 0); } ++vars; } /* * If we're not processing paths, then see if there is a setenv list by * which the admin and/or user may set an arbitrary set of env vars. */ if (!paths) { const char **set_env = login_getcaplist(lc, "setenv", ","); if (set_env != NULL) { while (*set_env != NULL) { char *p = strchr(*set_env, '='); if (p != NULL) { /* Discard invalid entries */ char *np; *p++ = '\0'; if ((np = substvar(p, pwd, hlen, pch, nlen)) != NULL) { setenv(*set_env, np, 1); free(np); } } ++set_env; } } } }
// This function takes a template string (tmpl) which can have // placeholders in it such as $1 for substring matches in a regexp // that was run against subject, and subjectlen, with the 'nummatches' // matches in ovector. The NUL-terminated newly composted string is // placed into 'newstr', as long as it doesn't exceed 'newstrlen' // bytes. Trailing whitespace and commas are removed. Returns zero for success static int dotmplsubst(const char *subject, int subjectlen, int *ovector, int nummatches, char *tmpl, char *newstr, int newstrlen) { int newlen; char *srcstart=tmpl, *srcend; char *dst = newstr; char *newstrend = newstr + newstrlen; // Right after the final char if (!newstr || !tmpl) return -1; if(newstrlen < 3) return -1; while(*srcstart) { // First do any literal text before '$' srcend = strchr(srcstart, '$'); if (!srcend) { // Only literal text remain! while(*srcstart) { if (dst >= newstrend - 1) return -1; *dst++ = *srcstart++; } *dst = '\0'; while (--dst >= newstr) { if (isspace(*dst) || *dst == ',') *dst = '\0'; else break; } return 0; } else { // Copy the literal text up to the '$', then do the substitution newlen = srcend - srcstart; if (newlen > 0) { if (newstrend - dst <= newlen - 1) return -1; memcpy(dst, srcstart, newlen); dst += newlen; } srcstart = srcend; newlen = substvar(srcstart, &srcend, dst, newstrend - dst, subject, subjectlen, ovector, nummatches); if (newlen == -1) return -1; dst += newlen; srcstart = srcend; } } if (dst >= newstrend - 1) return -1; *dst = '\0'; while (--dst >= newstr) { if (isspace(*dst) || *dst == ',') *dst = '\0'; else break; } return 0; }
int exec_menu(MENU menu) { int i=0, cidx=0, fine; int off, ecnt=1; char tmp[MAXLEN]; char output[MAXLEN]; MENU tmenu = menu; int ret; int cpid; fine=1; while(cidx < tmenu.ncmd && fine) { off=1; switch(tmenu.cmd[cidx][0]) { case '-': if(*(tmenu.cmd[cidx] + off)=='!') off++; parsecmd(tmenu.cmd[cidx] +off, tmp); cpid = fork(); if(cpid) { waitpid(cpid,&ret,0); if(ret!=-1 && WIFEXITED(ret)) ret = WEXITSTATUS(ret); } else { close(comport); ret=system(tmp); if(ret!=-1 && WIFEXITED(ret)) ret = WEXITSTATUS(ret); exit(ret); } if(off==1) ret=1; break; case '+': if(*(tmenu.cmd[cidx] + off)=='!') off++; parsecmd(tmenu.cmd[cidx] +off, tmp); ret=execute(tmp, output, MAXLEN); sprintf(tmp, "$%d", ecnt++); setvar(tmp, output); if(off==1) ret=1; break; case '<': argcnt = split(tmenu.cmd[cidx]+1, args[0], MAXARGS, MAXARGLEN); substvar(args[0], MAXARGS, MAXARGLEN); /*for(i=0; i<argcnt; i++) printf("%d: --%s--\n",i,args[i]);*/ if(strcmp(args[0],"YesNo")==0 && argcnt==2) { ret=get_yesno(args[1]); sprintf(output,"%d",ret-1); setvar("$YesNo",output); } if(strcmp(args[0],"OnOff")==0 && argcnt==3) { ret=get_onoff(args[1], atoi(args[2])); sprintf(output,"%d",ret-1); setvar("$OnOff",output); } if(strcmp(args[0],"Percent")==0 && argcnt==4) { ret=get_percent(args[1], atoi(args[2]), atoi(args[3])); sprintf(output,"%d",ret-1); setvar("$Percent",output); } if(strcmp(args[0],"Choice")==0 && argcnt>=4) { init_choice(); for(i=3; i<argcnt; i++) addchoice(args[i]); ret=get_choice(args[1], atoi(args[2])); sprintf(output,"%d",ret); setvar("$Choice",output); } if(strcmp(args[0],"Real")==0 && (argcnt==4 || argcnt==5)) { if(argcnt==4) strcpy(args[4],""); ret=get_real(output, args[1], args[2], args[3], args[4]); setvar("$Real",output); } if(strcmp(args[0],"Int")==0 && (argcnt==5 || argcnt==6)) { if(argcnt==5) strcpy(args[5],"0"); ret=get_integer(output, args[1], args[2], atoi(args[3]), atoi(args[4]), atoi(args[5])); setvar("$Int",output); } if(strcmp(args[0],"Phone")==0 && (argcnt==3 || argcnt==4)) { if(argcnt==3) strcpy(args[3],""); ret=get_phone(output, args[1], args[2], args[3]); setvar("$Phone",output); } if(strcmp(args[0],"Date")==0 && argcnt==2) { ret=get_date(output, args[1]); setvar("$Date",output); } if(strcmp(args[0],"Text")==0 && (argcnt==4 || argcnt==5)) { if(argcnt==4) strcpy(args[4],""); ret=get_text(output, args[1], args[2], atoi(args[3]), args[4]); setvar("$Text",output); } if(strcmp(args[0],"Secret")==0 && argcnt==4) { ret=get_secret(output, args[1], args[2], atoi(args[3])); setvar("$Secret",output); } break; case '>': argcnt = split(tmenu.cmd[cidx]+1, args[0], MAXARGS, MAXARGLEN); substvar(args[0], MAXARGS, MAXARGLEN); /*for(i=0; i<argcnt; i++) printf("%d: --%s--\n",i,args[i]);*/ if(strcmp(args[0],"MsgBox")==0 && (argcnt==2 || argcnt==3)) { if(argcnt==2) strcpy(args[2],""); ret=msgbox(args[1], atoi(args[2])); } if(strcmp(args[0],"Info")==0 && argcnt>=3) ret=info(args[1], args[2]); if(strcmp(args[0],"Status")==0 && argcnt>=2) ret=status(args[1]); break; case ':': for(i=0; i<fncnt; i++) if(strcmp(tmenu.cmd[cidx]+1,flist[i].name)==0) ret = flist[i].fn(); break; } if(ret<=0) fine=0; cidx++; } return 0; }