/* read key from /dev/tty */ char *readkey(char *prompt, char *myname) { char *line; FILE *fin; struct termios tio, saved_tio; fin = fopen("/dev/tty", "r"); if (fin==NULL) { fprintf(stderr, "%s: cannot open /dev/tty\n", myname); exit(2); } fprintf(stderr, "%s", prompt); fflush(stderr); /* disable echo */ tcgetattr(fileno(fin), &tio); saved_tio = tio; tio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); tcsetattr(fileno(fin), TCSANOW, &tio); /* read key */ line = xreadline(fin, myname); /* restore echo, print newline, close file */ tcsetattr(fileno(fin), TCSANOW, &saved_tio); fprintf(stderr, "\n"); fflush(stderr); fclose(fin); return line; }
static void _prompt_loop(void) { int i; char buf[128]; int num_plugs = 48; int plug[48]; int plug_origin = 1; for (i = 0; i < num_plugs; i++) { plug[i] = 0; } /* Process comands. */ while (1) { if (xreadline("", buf, sizeof(buf)) == NULL) break; if (strlen(buf) == 0) { /* */ } else if (!strcmp(buf, "exprange on")) { /* */ } else if (!strcmp(buf, "exprange off")) { /* */ } else if (!strcmp(buf, "status")) { for (i = 0; i < num_plugs; i++) printf ("plug %d: %s\n", i + plug_origin, plug[i] ? "on" : "off"); } else if (sscanf(buf, "status %d", &i) == 1) { i -= plug_origin; if (i < 0 || i >= num_plugs) break; printf ("plug %d: %s\n", i + plug_origin, plug[i] ? "on" : "off"); } else if (sscanf(buf, "on %d", &i) == 1) { i -= plug_origin; if (i < 0 || i >= num_plugs) break; plug[i] = 1; } else if (sscanf(buf, "off %d", &i) == 1) { i -= plug_origin; if (i < 0 || i >= num_plugs) break; plug[i] = 0; } else if (sscanf(buf, "cycle %d", &i) == 1) { i -= plug_origin; if (i < 0 || i >= num_plugs) break; plug[i] = 0; sleep(2); plug[i] = 1; } else break; printf("swpdu> "); } printf("Error, exiting"); exit (1); }
/* read a yes/no response from the user */ static int prompt(void) { char *line; FILE *fin; int r; fin = fopen("/dev/tty", "r"); if (fin==NULL) { fin = stdin; } line = xreadline(fin, cmd.name); r = line && (!strcmp(line, "y") || !strcmp(line, "yes")); free(line); return r; }
static void prompt_loop(void) { char buf[128], tmp[32]; char plug[8][4]; int num_plugs = 8; int plug_origin = 1; int i; int authenticated = 0; for (i = 0; i < num_plugs; i++) strcpy(plug[i], "OFF"); for (;;) { if (xreadline("httppower> ", buf, sizeof(buf)) == NULL) break; if (!strcmp(buf, "help")) { printf("Commands are:\n"); printf(" auth admin:admin\n"); printf(" get\n"); printf(" post outlet [1-8]=ON|OFF\n"); } else if (!strcmp(buf, "auth admin:admin")) { authenticated = 1; } else if (!strcmp(buf, "get")) { if (!authenticated) goto err; printf(DLI_GET, plug[0], plug[1], plug[2], plug[3], plug[4], plug[5], plug[6], plug[7]); } else if (sscanf(buf, "post outlet %d=%s", &i, tmp) == 2) { if (!authenticated) goto err; if (i < plug_origin || i >= num_plugs + plug_origin) goto err; if (!strcmp(tmp, "ON")) strcpy(plug[i - plug_origin], "ON"); else if (!strcmp(tmp, "OFF")) strcpy(plug[i - plug_origin], "OFF"); else goto err; printf(DLI_POST); } else goto err; continue; err: printf("Error\n"); } }
int file_get_line(char *line, size_t n, FILE *fp) { int len; if(session_exit) return -1; /* if(!fgets(line,n,fp)) return -1; len=strcspn(line,"\r\n"); line[len]=0; */ struct pollfd fds; fds.fd=fileno(fp); fds.events=POLLIN; poll(&fds,1,-1); if(session_exit) return -1; len=xreadline(fileno(fp),line,n); return len; }
static void _prompt_loop(void) { int i; char buf[128]; int num_plugs = 10; int num_plugs_ext = 12; int plug[12]; char beacon[12][4]; int temp[12]; int plug_origin = 1; int logged_in = 0; char arg[80]; for (i = 0; i < num_plugs_ext; i++) { plug[i] = 0; temp[i] = 73 + i; strcpy(beacon[i], "OFF"); } /* User must first authenticate. */ switch (personality) { case V2: printf(V2_BANNER); break; case V3: printf(V3_BANNER); break; case V4: printf(V4_BANNER); break; case NONE: break; } while (!logged_in) { if (xreadline("", buf, sizeof(buf)) == NULL) return; if (strcmp(buf, "auth icebox") == 0) { logged_in = 1; printf(V3_RESP_OK); } else printf(V3_ERROR_AUTH); } /* Process comands. */ while (logged_in) { if (xreadline("", buf, sizeof(buf)) == NULL) break; if (strlen(buf) == 0) { goto err; } else if (!strcmp(buf, "q")) { logged_in = 0; } else if (!strcmp(buf, "ps *") || !strcmp(buf, "ns *")) { printf(V3_POWER_STATUS, plug[0], plug[1], plug[2], plug[3], plug[4], plug[5], plug[6], plug[7], plug[8], plug[9]); } else if (personality != V2 && !strcmp(buf, "be *")) { printf(V3_BEACON_STATUS, beacon[0], beacon[1], beacon[2], beacon[3], beacon[4], beacon[5], beacon[6], beacon[7], beacon[8], beacon[9], beacon[10], beacon[11]); } else if (personality != V2 && !strcmp(buf, "is *")) { printf(V3_TEMP, temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8], temp[9], temp[10], temp[11]); } else if (personality == V2 && !strcmp(buf, "ts *")) { printf(V2_TEMP, temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8], temp[9], temp[10], temp[11]); } else if (!strcmp(buf, "ph *")) { for (i = 0; i < num_plugs; i++) plug[i] = 1; } else if (sscanf(buf, "ph %d", &i) == 1) { if (i >= plug_origin && i < num_plugs + plug_origin) plug[i - plug_origin] = 1; else goto err; } else if (!strcmp(buf, "pl *")) { for (i = 0; i < num_plugs; i++) plug[i] = 0; } else if (sscanf(buf, "pl %d", &i) == 1) { if (i >= plug_origin && i < num_plugs + plug_origin) plug[i - plug_origin] = 0; else goto err; } else if (!strcmp(buf, "rp *")) { } else if (sscanf(buf, "rp %d", &i) == 1) { if (!(i >= plug_origin && i < num_plugs + plug_origin)) goto err; } else if (personality != V2 && sscanf(buf, "be %d %s", &i, arg) == 2) { if (i >= plug_origin && i < num_plugs_ext + plug_origin) { if (!strcmp(arg, "off")) strcpy(beacon[i - plug_origin], "OFF"); else if (!strcmp(arg, "on")) strcpy(beacon[i - plug_origin], "ON"); else goto err; } else goto err; } else goto err; if (logged_in) printf(V3_RESP_OK); continue; err: printf(V3_ERROR_CMD); } }
int main(int ac, char *av[]) { int r; FILE *f; /* read command line */ cmd = read_commandline(ac, av); /* if --keyfile requested, read one (normal mode) or two (change key mode) keywords from file, which may be "-" for stdin. Note that in this case, we ignore any keywords given on the command line etc. */ if (cmd.keyfile) { if (strcmp(cmd.keyfile, "-")==0) { f = stdin; } else { f = fopen(cmd.keyfile, "r"); if (!f) { fprintf(stderr, "%s: could not read key from %s: %s\n", cmd.name, cmd.keyfile, strerror(errno)); exit(1); } } cmd.keyword = xreadline(f, cmd.name); if (cmd.keyword==NULL) { /* end of file */ fprintf(stderr, "%s: error reading keyfile\n", cmd.name); exit(1); } if (cmd.mode==KEYCHANGE) { cmd.keyword2 = xreadline(f, cmd.name); if (cmd.keyword2==NULL) { /* end of file */ fprintf(stderr, "%s: error reading keyfile\n", cmd.name); exit(1); } } if (strcmp(cmd.keyfile, "-")!=0) { fclose(f); } } /* read keyword from terminal if necessary */ if (cmd.keyword==NULL) { if (!cmd.prompt) { switch (cmd.mode) { case ENCRYPT: default: cmd.prompt = "Enter encryption key: "; break; case DECRYPT: case CAT: cmd.prompt = "Enter decryption key: "; break; case KEYCHANGE: cmd.prompt = "Enter old key: "; break; case UNIXCRYPT: cmd.prompt = "Enter key: "; break; } } cmd.keyword = readkey(cmd.prompt, "", cmd.name); if (cmd.keyword==NULL) { /* end of file: exit gracefully */ exit(0); } /* in some circumstances, prompt for the key a second time */ if (cmd.timid && cmd.mode==ENCRYPT) { char *repeat; repeat = readkey(cmd.prompt, "(repeat) ", cmd.name); if (repeat==NULL || strcmp(repeat, cmd.keyword)!=0) { fprintf(stderr, "Sorry, the keys you entered did not match.\n"); exit(7); } } } /* read keyword2 from terminal if necessary */ if (cmd.mode==KEYCHANGE && cmd.keyword2==NULL) { if (cmd.prompt2 == NULL) { cmd.prompt2 = "Enter new key: "; } cmd.keyword2 = readkey(cmd.prompt2, "", cmd.name); if (cmd.keyword2==NULL) { /* end of file: exit gracefully */ exit(0); } /* in some circumstances, prompt for the key a second time */ if (cmd.timid) { char *repeat; repeat = readkey(cmd.prompt2, "(repeat) ", cmd.name); if (repeat==NULL || strcmp(repeat, cmd.keyword2)!=0) { fprintf(stderr, "Sorry, the keys you entered did not match.\n"); exit(7); } } } #ifdef __CYGWIN__ /* reset stdin/stdout to binary mode under Windows */ setmode(0,O_BINARY); setmode(1,O_BINARY); #endif /* filter mode */ if (cmd.filter) { switch (cmd.mode) { case ENCRYPT: default: r = ccencrypt_streams(stdin, stdout, cmd.keyword); break; case DECRYPT: case CAT: r = ccdecrypt_streams(stdin, stdout, cmd.keyword); break; case KEYCHANGE: r = cckeychange_streams(stdin, stdout, cmd.keyword, cmd.keyword2); break; case UNIXCRYPT: r = unixcrypt_streams(stdin, stdout, cmd.keyword); break; } if (r) { fprintf(stderr, "%s: %s\n", cmd.name, ccrypt_error(r)); if (r==-2 && (ccrypt_errno==CCRYPT_EFORMAT || ccrypt_errno==CCRYPT_EMISMATCH)) { return 4; } else { return 3; } } fflush(stdout); return 0; } /* non-filter mode: traverse files */ r = traverse_toplevel(cmd.infiles, cmd.count); if (r==1) { return 4; } else if (r) { return 8; } else { return 0; } }
static void _prompt_loop(void) { int seq, i; char buf[128]; char prompt[16]; for (seq = 0;; seq++) { snprintf(prompt, sizeof(prompt), "%d vpc> ", seq); if (xreadline(prompt, buf, sizeof(buf)) == NULL) break; if (strlen(buf) == 0) continue; if (strcmp(buf, "logoff") == 0) { /* logoff */ printf("%d OK\n", seq); logged_in = 0; break; } if (strcmp(buf, "login") == 0) { /* logon */ logged_in = 1; goto ok; } if (!logged_in) { printf("%d Please login\n", seq); continue; } if (sscanf(buf, "stat %d", &i) == 1) { /* stat <plugnum> */ if (i < 0 || i >= NUM_PLUGS) { printf("%d BADVAL: %d\n", seq, i); continue; } printf("plug %d: %s\n", i, plug[i] ? "ON" : "OFF"); goto ok; } if (strcmp(buf, "stat *") == 0) { /* stat * */ for (i = 0; i < NUM_PLUGS; i++) printf("plug %d: %s\n", i, plug[i] ? "ON" : "OFF"); goto ok; } if (sscanf(buf, "beacon %d", &i) == 1) { /* beacon <plugnum> */ if (i < 0 || i >= NUM_PLUGS) { printf("%d BADVAL: %d\n", seq, i); continue; } printf("plug %d: %s\n", i, beacon[i] ? "ON" : "OFF"); goto ok; } if (strcmp(buf, "beacon *") == 0) { /* beacon * */ for (i = 0; i < NUM_PLUGS; i++) printf("plug %d: %s\n", i, beacon[i] ? "ON" : "OFF"); goto ok; } if (sscanf(buf, "temp %d", &i) == 1) { /* temp <plugnum> */ if (i < 0 || i >= NUM_PLUGS) { printf("%d BADVAL: %d\n", seq, i); continue; } printf("plug %d: %d\n", i, temp[i]); } if (strcmp(buf, "temp *") == 0) { /* temp * */ for (i = 0; i < NUM_PLUGS; i++) printf("plug %d: %d\n", i, temp[i]); goto ok; } if (sscanf(buf, "spew %d", &i) == 1) { /* spew <linecount> */ if (i <= 0) { printf("%d BADVAL: %d\n", seq, i); continue; } _spew(i); goto ok; } if (sscanf(buf, "on %d", &i) == 1) { /* on <plugnum> */ if (i < 0 || i >= NUM_PLUGS) { printf("%d BADVAL: %d\n", seq, i); continue; } plug[i] = 1; goto ok; } if (sscanf(buf, "off %d", &i) == 1) { /* off <plugnum> */ if (i < 0 || i >= NUM_PLUGS) { printf("%d BADVAL: %d\n", seq, i); continue; } plug[i] = 0; goto ok; } if (sscanf(buf, "flash %d", &i) == 1) { /* flash <plugnum> */ if (i < 0 || i >= NUM_PLUGS) { printf("%d BADVAL: %d\n", seq, i); continue; } beacon[i] = 1; goto ok; } if (sscanf(buf, "unflash %d", &i) == 1) { /* unflash <plugnum> */ if (i < 0 || i >= NUM_PLUGS) { printf("%d BADVAL: %d\n", seq, i); continue; } beacon[i] = 0; goto ok; } if (strcmp(buf, "on *") == 0) { /* on * */ for (i = 0; i < NUM_PLUGS; i++) plug[i] = 1; goto ok; } if (strcmp(buf, "off *") == 0) { /* off * */ for (i = 0; i < NUM_PLUGS; i++) plug[i] = 0; goto ok; } if (sscanf(buf, "reset %d", &i) == 1) { /* reset <plugnum> */ if (i < 0 || i >= NUM_PLUGS) { printf("%d BADVAL: %d\n", seq, i); continue; } sleep(1); /* noop */ goto ok; } if (strcmp(buf, "reset *") == 0) { /* reset * */ sleep(1); /* noop */ goto ok; } printf("%d UNKNOWN: %s\n", seq, buf); continue; ok: printf("%d OK\n", seq); } }
int main(int ac, char *av[]) { int r; FILE *f; #if ENABLE_NLS setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain(GETTEXT_PACKAGE); #endif /* read command line */ cmd = read_commandline(ac, av); /* if --keyfile requested, read one (normal mode) or two (change key mode) keywords from file, which may be "-" for stdin. Note that in this case, we ignore any keywords given on the command line etc. */ if (cmd.keyfile) { if (strcmp(cmd.keyfile, "-")==0) { f = stdin; } else { f = fopen(cmd.keyfile, "r"); if (!f) { fprintf(stderr, _("%s: could not read key from %s: %s\n"), cmd.name, cmd.keyfile, strerror(errno)); exit(9); } } cmd.keyword = xreadline(f, cmd.name); if (cmd.keyword==NULL) { /* end of file */ fprintf(stderr, _("%s: error reading keyfile\n"), cmd.name); exit(9); } if (cmd.mode==KEYCHANGE) { cmd.keyword2 = xreadline(f, cmd.name); if (cmd.keyword2==NULL) { /* end of file */ fprintf(stderr, _("%s: error reading keyfile\n"), cmd.name); exit(9); } } if (strcmp(cmd.keyfile, "-")!=0) { fclose(f); } } /* read keyword from terminal if necessary */ if (cmd.keyword==NULL) { if (!cmd.prompt) { switch (cmd.mode) { case ENCRYPT: default: cmd.prompt = _("Enter encryption key: "); break; case DECRYPT: case CAT: cmd.prompt = _("Enter decryption key: "); break; case KEYCHANGE: cmd.prompt = _("Enter old key: "); break; case UNIXCRYPT: cmd.prompt = _("Enter key: "); break; } } cmd.keyword = readkey(cmd.prompt, "", cmd.name); if (cmd.keyword==NULL) { /* end of file: exit gracefully */ fprintf(stderr, _("%s: no key given\n"), cmd.name); exit(9); } /* in some circumstances, prompt for the key a second time */ if (cmd.timid && cmd.mode==ENCRYPT) { char *repeat; repeat = readkey(cmd.prompt, _("(repeat) "), cmd.name); if (repeat==NULL || strcmp(repeat, cmd.keyword)!=0) { fprintf(stderr, _("Sorry, the keys you entered did not match.\n")); exit(7); } } } /* read keyword2 from terminal if necessary */ if (cmd.mode==KEYCHANGE && cmd.keyword2==NULL) { if (cmd.prompt2 == NULL) { cmd.prompt2 = _("Enter new key: "); } cmd.keyword2 = readkey(cmd.prompt2, "", cmd.name); if (cmd.keyword2==NULL) { /* end of file: exit gracefully */ fprintf(stderr, _("%s: no key given\n"), cmd.name); exit(9); } /* in some circumstances, prompt for the key a second time */ if (cmd.timid) { char *repeat; repeat = readkey(cmd.prompt2, _("(repeat) "), cmd.name); if (repeat==NULL || strcmp(repeat, cmd.keyword2)!=0) { fprintf(stderr, _("Sorry, the keys you entered did not match.\n")); exit(7); } } } /* reset stdin/stdout to binary mode under Windows */ setmode(0,O_BINARY); setmode(1,O_BINARY); /* if --keyref given, check encryption keys against named file */ if (cmd.keyref && (cmd.mode == ENCRYPT || cmd.mode == KEYCHANGE)) { f = fopen(cmd.keyref, "rb"); if (!f) { fprintf(stderr, _("%s: could not open %s: %s\n"), cmd.name, cmd.keyref, strerror(errno)); exit(10); } if (cmd.mode == ENCRYPT) { r = keycheck_stream(f, cmd.keyword); } else { r = keycheck_stream(f, cmd.keyword2); } if (r == -2 && (ccrypt_errno == CCRYPT_EFORMAT || ccrypt_errno == CCRYPT_EMISMATCH)) { fprintf(stderr, _("The encryption key does not match the reference file.\n")); exit(10); } else if (r==-2 || r==-1) { /* e.g. i/o error: fatal */ fprintf(stderr, "%s: %s: %s\n", cmd.name, cmd.keyref, ccrypt_error(r)); exit(10); } } /* filter mode */ if (cmd.filter) { switch (cmd.mode) { case ENCRYPT: default: r = ccencrypt_streams(stdin, stdout, cmd.keyword); break; case DECRYPT: case CAT: r = ccdecrypt_streams(stdin, stdout, cmd.keyword); break; case KEYCHANGE: r = cckeychange_streams(stdin, stdout, cmd.keyword, cmd.keyword2); break; case UNIXCRYPT: r = unixcrypt_streams(stdin, stdout, cmd.keyword); break; } if (r) { fprintf(stderr, "%s: %s\n", cmd.name, ccrypt_error(r)); if (r==-2 && (ccrypt_errno==CCRYPT_EFORMAT || ccrypt_errno==CCRYPT_EMISMATCH)) { return 4; } else { return 3; } } fflush(stdout); return 0; } /* non-filter mode: traverse files */ r = traverse_toplevel(cmd.infiles, cmd.count); if (r==1) { return 4; } else if (r) { return 8; } else { return 0; } }
static void prompt_loop(void) { char buf[128]; static char plug[4]; int authenticated; strcpy(plug, "Off"); switch (personality) { case SER: authenticated = 0; break; case SSH: authenticated = 1; break; case SER_LOGIN: authenticated = 2; break; case NONE: break; } for (;;) { switch (authenticated) { case 0: if (xreadline(LOM_LOGIN_PROMPT, buf, sizeof(buf)) == NULL) goto done; if (!strcmp(buf, "admin")) authenticated = 1; break; case 1: if (xreadline(LOM_PASSWD_PROMPT, buf, sizeof(buf)) == NULL) goto done; if (!strcmp(buf, "admin")) { authenticated = 2; printf(LOM_BANNER); } break; case 2: if (xreadline(LOM_PROMPT, buf, sizeof(buf)) == NULL) goto done; if (!strcmp(buf, "exit")) { goto done; } else if (!strcmp(buf, "platform get power state")) { printf("%s\n", plug); } else if (!strcmp(buf, "platform set power state -W -f -q on")) { if (!strcmp(plug, "Off")) { strcpy(plug, "On"); printf(LOM_ON_RESP); } } else if (!strcmp(buf, "platform set power state -W -f -q off")) { if (!strcmp(plug, "On")) { strcpy(plug, "Off"); printf(LOM_OFF_RESP); } } else { printf(LOM_CMD_INVAL, buf); } } continue; done: break; } }
static void _prompt_loop(void) { char buf[128]; char bufnode[128]; hash_t hstatus = NULL; hostlist_t hl = NULL; hostlist_iterator_t hlitr = NULL; char *node; assert(hostname); if (!(hstatus = hash_create(HASH_SIZE, (hash_key_f)hash_key_string, (hash_cmp_f)strcmp, (hash_del_f)NULL))) { perror("hash_create"); exit(1); } if (!(hl = hostlist_create(hostname))) { perror("hostlist_create"); exit(1); } if (!(hlitr = hostlist_iterator_create(hl))) { perror("hostlist_iterator"); exit(1); } /* all nodes begin as off */ while ((node = hostlist_next(hlitr))) { if (!hash_insert(hstatus, (void *)node, OFF_STATUS)) { perror("hash_insert"); exit(1); } /* XXX: Don't free 'node' here, it needs to be alloc'd for * the hash key. It's a mem-leak. Fix later. */ } hostlist_iterator_destroy(hlitr); hostlist_destroy(hl); while (1) { if (xreadline(CMD_PROMPT, buf, sizeof(buf)) == NULL) { break; } else if (strlen(buf) == 0) { continue; } else if (!strcmp(buf, "quit")) { break; } else if (!strcmp(buf, "stat")) { _stat(hstatus, hostname); } else if (sscanf(buf, "stat %s", bufnode) == 1) { _stat(hstatus, bufnode); } else if (!strcmp(buf, "on")) { _onoff(hstatus, hostname, ON_STATUS); } else if (sscanf(buf, "on %s", bufnode) == 1) { _onoff(hstatus, bufnode, ON_STATUS); } else if (!strcmp(buf, "off")) { _onoff(hstatus, hostname, OFF_STATUS); } else if (sscanf(buf, "off %s", bufnode) == 1) { _onoff(hstatus, bufnode, OFF_STATUS); } else printf("unknown command - type \"help\"\n"); } hash_destroy(hstatus); }