int main(void) { char buf[MAX_LINE_LENGTH]; size_t len; int c; do { len = 0; c = getchar(); while (c != EOF) { buf[len++] = c; if (c == '\n' || len >= sizeof(buf) - 1) { break; } c = getchar(); } if (len > 0) { buf[len] = '\0'; if (!drop_line(buf)) { printf("%s", buf); } } } while (c != EOF); return 0; }
void drop_lines (gint fd, gint max) { gint count; for (count = 0; count < max; count++) drop_line (fd); }
static void erase_line(Renderer *renderer, int line_number) { int i; for (i = 1; i < (int) renderer->buffer->width-1; ++i) { Buffer_set_pixel_enabled(renderer->buffer, (unsigned int) i, (unsigned int) line_number, 0); } for (i = 1; i < (int) renderer->row_floor-1; ++i) { if (line_number - i > 0) { drop_line(renderer->buffer, line_number - i); } } }
static int ask_count(void) { int read, n; do { char tail; printf(">"); read = scanf("%i%c", &n, &tail); if (!read || tail != '\n') { drop_line(); /* Drop uninterpreted chars. */ printf("Please enter the number of packets to send\n"); read = 0; } else if (n <= 0) { printf("Please enter only positive numbers\n"); read = 0; } } while (!read); return n; }
int main (int argc, char **argv) { gchar *confdir; gchar *host = g_strdup ("localhost"); gchar *port = g_strdup ("22"); gchar *sshkey = NULL; gchar *user = NULL; gchar *password = NULL; gchar *session = g_strdup ("thinnx"); gchar *type = g_strdup ("unix-gnome"); gchar *cookie = NULL; gboolean use_ssl = 0; gchar *link = g_strdup ("adsl"); gchar *kbdtype = g_strdup ("pc104/us"); gchar *geometry = g_strdup ("fullscreen"); gchar *screeninfo = NULL; gchar *session_id = NULL; gchar *session_display = NULL; gchar *pcookie = NULL; gchar **nxssh_argv = (gchar**) g_malloc (sizeof(gchar*) * 9); gchar *restore_id = NULL; pid_t pid; int parent_pipe[2]; /* For talking to the parent */ int child_pipe[2]; /* For talking to the child */ gint in = 0, out = 0; gtk_init (&argc, &argv); homedir = g_get_home_dir (); confdir = g_strdup_printf ("%s/.nx/", homedir); sshkey = g_strdup_printf ("%s/.ssh/id_dsa", homedir); { struct stat info; if (stat (confdir, &info) == -1) { if (mkdir (confdir, 0777) == -1) g_critical ("Could not create directory %s: %s\n", confdir, strerror (errno)); } } { #if GTK_MAJOR_VERSION == 2 GdkScreen *screen; screen = gdk_screen_get_default (); screeninfo = g_strdup_printf ("%dx%dx%d+render", gdk_screen_get_width (screen), gdk_screen_get_height (screen), gdk_visual_get_best_depth ()); #else screeninfo = g_strdup_printf ("%dx%dx%d+render", gdk_screen_width (), gdk_screen_height (), gdk_visual_get_best_depth ()); #endif } { /* get X authentication cookie information */ FILE *xauth_output; gchar xauth[256] = {0}; gchar *tmp = NULL; gchar **tmpv = NULL; gchar *display = NULL; /* avoid problems with "network" DISPLAY's */ display = g_strdup (getenv ("DISPLAY")); tmpv = g_strsplit (display, ":", 3); g_free (display); display = g_strdup_printf (":%s", tmpv[1]); g_strfreev (tmpv); /* get the authorization token */ tmp = g_strdup_printf (XAUTHBINDIR"/xauth list %s | " "grep 'MIT-MAGIC-COOKIE-1' | " "cut -d ' ' -f 5", display); if ((xauth_output = popen (tmp, "r")) == NULL) { g_critical ("Failed to obtain xauth key: %s", strerror(errno)); exit (1); } fread (xauth, sizeof(char), 256, xauth_output); xauth[strlen(xauth) - 1] = '\0'; pclose (xauth_output); g_free (tmp); if (!strcmp (xauth, "")) { message_dialog ("Não foi possível obter um cookie de autenticação\n" "do servidor X. Impossível continuar."); exit (1); } cookie = g_strdup (xauth); } { /* read configuration file */ FILE *fconf; gint fconf_fd; gchar **tmp, *key, *value; struct stat info; if (stat (CONFFILE, &info) == -1) { g_warning ("WARNING: Could not stat %s: %s.\n", CONFFILE, strerror (errno)); } fconf = fopen (CONFFILE, "r"); if (fconf == NULL) { g_critical ("Could not open %s: %s.\n", CONFFILE, strerror (errno)); } else { fconf_fd = fileno (fconf); while (!feof (fconf)) { buffer = read_line (fconf_fd); if (!buffer) break; /* remove comments */ tmp = g_strsplit (buffer, "#", 2); g_free (buffer); buffer = g_strdup (tmp[0]); g_strfreev (tmp); /* check if we still have a key/value pair */ tmp = g_strsplit (buffer, "=", 2); g_free (buffer); if (tmp[1] == NULL || tmp[0] == NULL) { g_strfreev (tmp); continue; } key = tmp[0]; value = tmp[1]; g_strstrip (key); g_strstrip (value); if (!strcmp ("host", key)) { g_free (host); host = g_strdup (value); } else if (!strcmp ("port", key)) { g_free (port); port = g_strdup (value); } else if (!strcmp ("sshkey", key)) { g_free (sshkey); sshkey = g_strdup (value); } else if (!strcmp ("session", key)) { g_free (session); session = g_strdup (value); } else if (!strcmp ("ssl", key)) { if (!strcmp (value, "yes")) use_ssl = 1; } else if (!strcmp ("type", key)) { g_free (type); type = g_strdup (value); } else if (!strcmp ("link", key)) { g_free (link); link = g_strdup (value); } else if (!strcmp ("kbdtype", key)) { g_free (kbdtype); kbdtype = g_strdup (value); } else if (!strcmp ("geometry", key)) { g_free (geometry); geometry = g_strdup (value); } else g_warning ("Unknown option in %s: %s=%s\n", CONFFILE, key, value); g_strfreev (tmp); } fclose (fconf); } } /* grab auth information from the user before anything else */ input_dialog (&user, &password); if (!strcmp (user, "root")) { message_dialog ("O usuário root não pode entrar por aqui!"); exit (1); } pipe (parent_pipe); pipe (child_pipe); pid = fork (); if (pid == -1) { g_critical ("Could not fork!\n"); exit (1); } else if (pid == 0) { close (child_pipe[1]); dup2 (child_pipe[0], STDIN_FILENO); dup2 (parent_pipe[1], STDOUT_FILENO); nxssh_argv[0] = g_strdup (BINDIR"/nxssh"); nxssh_argv[1] = g_strdup ("-nx"); nxssh_argv[2] = g_strdup_printf ("-p%s", port); nxssh_argv[3] = g_strdup ("-i"); nxssh_argv[4] = g_strdup (sshkey); nxssh_argv[5] = g_strdup_printf ("nx@%s", host); nxssh_argv[6] = g_strdup ("-2"); nxssh_argv[7] = g_strdup ("-S"); nxssh_argv[8] = NULL; execv (nxssh_argv[0], nxssh_argv); } else { close(parent_pipe[1]); out = parent_pipe[0]; in = child_pipe[1]; /* Handle initial hand-shaking */ { gboolean ssh_authed = FALSE; while (!ssh_authed) { buffer = read_code (out); if (!strcmp (buffer, "NX> 205")) { flush_buffer (buffer); drop_line (out); drop_line (out); drop_chars (out, 56); write_line (in, "yes"); drop_line (out); drop_line (out); /* buffer != NULL? but why?! */ buffer = NULL; } else if (!strcmp (buffer, "NX> 208")) { /* OK, authenticating... */ } else if (!strcmp (buffer, "NX> 203") || (!strcmp (buffer, "NX> 285")) || (!strcmp (buffer, "NX> 200")) || (!strcmp (buffer, "NX> 202"))) { /* ignored stderr */ } else if (!strncmp (buffer, "nxssh", 5)) { gchar *msg; flush_buffer (buffer); buffer = read_line (out); msg = get_info_after_colon (buffer); g_free (buffer); if (!strcmp (msg, "Name or service not known")) message_dialog ("Não foi possível resolver o nome do servidor."); else if (!strcmp (msg, "Connection refused")) message_dialog ("A conexão foi recusada!\n" "Verifique a porta."); else if (!strcmp (msg, "Connection timed out")) message_dialog ("Tempo limite da conexão expirou!\n" "Verifique servidor e porta."); flush_buffer (msg); fprintf (stderr, "\n"); exit (1); } else if (!strcmp (buffer, "NX> 204")) { message_dialog ("Falha na autenticação inicial!\n" "Confira a chave privada."); g_critical ("Failed to authenticate to SSH using the public key!\n"); exit (1); } else if (!strcmp (buffer, "HELLO N")) { /* OK, time to say HELLO! */ ssh_authed = TRUE; } else protocol_error ("problems waiting for HELLO"); flush_buffer (buffer); buffer = read_line (out); flush_buffer (buffer); } } /* Handle HELLO */ buffer = read_code (out); if (!strcmp (buffer, "NX> 105")) { flush_buffer (buffer); drop_chars (out, 1); write_line (in, "HELLO NXCLIENT - Version 1.4.0"); drop_line (out); } else protocol_error ("problems during HELLO"); /* Handle Login */ buffer = read_code (out); if (!strcmp (buffer, "NX> 134")) { flush_buffer (buffer); buffer = read_line (out); flush_buffer (buffer); } else protocol_error ("HELLO failed?"); buffer = read_code (out); if (!strcmp (buffer, "NX> 105")) { flush_buffer (buffer); drop_chars (out, 1); write_line (in, "login"); drop_line (out); } else protocol_error ("No login? How come!"); buffer = read_code (out); if (!strcmp (buffer, "NX> 101")) { flush_buffer (buffer); drop_chars (out, 7); write_line (in, user); drop_line (out); } else protocol_error ("who took my login prompt away?"); buffer = read_code (out); if (!strcmp (buffer, "NX> 102")) { flush_buffer (buffer); drop_chars (out, 11); write_line (in, password); drop_line (out); } else protocol_error ("where is my password prompt?"); buffer = read_code (out); if (!strcmp (buffer, "NX> 103")) { flush_buffer (buffer); drop_line (out); } else { flush_buffer (buffer); message_dialog ("Login ou senha incorretos!"); exit (1); } buffer = read_code (out); if (!strcmp (buffer, "NX> 105")) { gchar *m; flush_buffer (buffer); drop_chars (out, 1); m = g_strdup_printf ("list %s", user); write_line (in, m); g_free (m); drop_lines (out, 5); while (1) { buffer = read_code (out); if (!strcmp (buffer, "NX> 105")) { flush_buffer (buffer); drop_chars (out, 1); break; } flush_buffer (buffer); buffer = read_line (out); restore_id = get_restore_id (buffer); } } else protocol_error ("session startup, buddy, I don't want problems!"); { gchar *cmdline; if (!restore_id) cmdline = g_strdup_printf ("startsession --session=\"%s\" --type=\"%s\" --cache=\"8M\" --images=\"32M\" --cookie=\"%s\" --link=\"%s\" --kbtype=\"%s\" --nodelay=\"1\" --backingstore=\"never\" --geometry=\"%s\" --media=\"0\" --agent_server=\"\" --agent_user=\"\" --agent_password=\"\" --screeninfo=\"%s\" --encryption=\"%d\"", session, type, cookie, link, kbdtype, geometry, screeninfo, use_ssl); else cmdline = g_strdup_printf ("restoresession --session=\"%s\" --type=\"%s\" --cache=\"8M\" --images=\"32M\" --cookie=\"%s\" --link=\"%s\" --kbtype=\"%s\" --nodelay=\"1\" --backingstore=\"never\" --geometry=\"%s\" --media=\"0\" --agent_server=\"\" --agent_user=\"\" --agent_password=\"\" --screeninfo=\"%s\" --encryption=\"%d\" --id=\"%s\"", session, type, cookie, link, kbdtype, geometry, screeninfo, use_ssl, restore_id); write_line (in, cmdline); g_free (cmdline); cmdline = NULL; drop_lines (out, 4); } session_id = get_session_info (out, "NX> 700"); session_display = get_session_info (out, "NX> 705"); drop_line (out); /* 703 (session type) */ pcookie = get_session_info (out, "NX> 701"); drop_line (out); /* 702 proxy ip */ drop_line (out); /* 706 agent cookie */ drop_line (out); /* 704 session cache */ { gchar *tmp = get_session_info (out, "NX> 707"); /* 707 ssl tunneling */ use_ssl = atoi (tmp); g_free (tmp); } drop_line (out); /* 710 session status: running */ drop_line (out); /* 1002 commit */ drop_line (out); /* 1006 session status: running */ read_code (out); drop_chars (out, 1); /* now prepare to run nxproxy */ { FILE *options; gchar *dirname; gchar *fname; gchar *cmdline; dirname = g_strdup_printf ("%s/.nx/S-%s", homedir, session_id); fname = g_strdup_printf ("%s/options", dirname); g_print ("Dir: %s\nFname: %s\n", dirname, fname); if (mkdir (dirname, 0777) == -1) { /* BOMB or handle 'directory already exists' */ } g_free (dirname); if (use_ssl) buffer = g_strdup_printf ("cookie=%s,root=%s/.nx,session=%s,id=%s,listen=%d:%s", pcookie, homedir, session, session_id, 8008, session_display); else buffer = g_strdup_printf ("cookie=%s,root=%s/.nx,session=%s,id=%s,connect=%s:%s", pcookie, homedir, session, session_id, host, session_display); options = fopen (fname, "w"); fwrite (buffer, sizeof(char), strlen (buffer), options); fclose (options); g_free (buffer); cmdline = g_strdup_printf (BINDIR"/nxproxy -S options=%s:%s", fname, session_display); system (cmdline); g_free (cmdline); g_free (fname); } } write_line (in, "bye"); drop_line (out); if (use_ssl) write_line (in, "switch"); drop_line (out); drop_line (out); drop_line (out); return 0; }