Example #1
0
int connect_to_server() {
    int i = 0;
    for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->has_auth) {
      do_export_auth (i);
      do_import_auth (i);
      bl_do_dc_signed (i);
      write_auth_file ();
    }
    write_auth_file ();

    fflush (stdout);
    fflush (stderr);

    read_state_file ();
    read_secret_chat_file ();

    set_interface_callbacks ();

  //  do_get_difference ();
  //  net_loop (0, dgot);
    #ifdef USE_LUA
      lua_diff_end ();
    #endif
    send_all_unsent ();

    tgStarted();
  //  do_get_dialog_list ();
  //  if (wait_dialog_list) {
  //    dialog_list_got = 0;
  //    net_loop (0, dlgot);
  //  }

    return main_loop ();
}
Example #2
0
int loop (void) {
  on_start ();
  if (binlog_enabled) {
    double t = get_double_time ();
    logprintf ("replay log start\n");
    replay_log ();
    logprintf ("replay log end in %lf seconds\n", get_double_time () - t);
    write_binlog ();
    #ifdef USE_LUA
      lua_binlog_end ();
    #endif
  } else {
    read_auth_file ();
  }
  update_prompt ();

  assert (DC_list[dc_working_num]);
  if (!DC_working || !DC_working->auth_key_id) {
//  if (auth_state == 0) {
    DC_working = DC_list[dc_working_num];
    assert (!DC_working->auth_key_id);
    dc_authorize (DC_working);
    assert (DC_working->auth_key_id);
    auth_state = 100;
    write_auth_file ();
  }
  
  if (verbosity) {
    logprintf ("Requesting info about DC...\n");
  }
  do_help_get_config ();
  net_loop (0, mcs);
  if (verbosity) {
    logprintf ("DC_info: %d new DC got\n", new_dc_num);
  }
  int i;
  for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->auth_key_id) {
    dc_authorize (DC_list[i]);
    assert (DC_list[i]->auth_key_id);
    write_auth_file ();
  }

  if (auth_state == 100 || !(DC_working->has_auth)) {
      registeringStarted();
      start_registering();
  }

  registeringFinished();
  return connect_to_server();
}
static void
timer_callback (CFRunLoopTimerRef timer, void *info)
{
    pending_timer = FALSE;
	
    /* Update authentication names. Need to write .Xauthority file first
	 without the existing entries, then again with the new entries.. */
	
    write_auth_file (FALSE);
	
    free_auth_items ();
    make_auth_keys (server_name);
	
    write_auth_file (TRUE);
}
Example #4
0
File: loop.c Project: tkosgrabar/tg
void empty_auth_file (void) {
  struct dc *DC = alloc_dc (1, strdup (TG_SERVER), 443);
  assert (DC);
  dc_working_num = 1;
  auth_state = 0;
  write_auth_file ();
}
Example #5
0
void empty_auth_file (void) {
//  alloc_dc (1, tstrdup (test_dc ? TG_SERVER_TEST : TG_SERVER), 443); 
  /* alloc_dc (1, tstrdup (test_dc ? TG_SERVER_TEST : TG_SERVER), 65521); */
  alloc_dc (1, tstrdup (test_dc ? TG_SERVER_TEST : TG_SERVER), 65521);
  dc_working_num = 1;
  auth_state = 0;
  write_auth_file ();
}
Example #6
0
File: loop.c Project: tkosgrabar/tg
int loop (void) {
  on_start ();
  read_auth_file ();
  readline_active = 1;
  rl_set_prompt ("");

  assert (DC_list[dc_working_num]);
  if (auth_state == 0) {
    DC_working = DC_list[dc_working_num];
    assert (!DC_working->auth_key_id);
    dc_authorize (DC_working);
    assert (DC_working->auth_key_id);
    auth_state = 100;
    write_auth_file ();
  }
  
  if (verbosity) {
    logprintf ("Requesting info about DC...\n");
  }
  do_help_get_config ();
  net_loop (0, mcs);
  if (verbosity) {
    logprintf ("DC_info: %d new DC got\n", new_dc_num);
  }
  int i;
  for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->auth_key_id) {
    dc_authorize (DC_list[i]);
    assert (DC_list[i]->auth_key_id);
    write_auth_file ();
  }

  if (auth_state == 100) {
    if (!default_username) {
      size_t size = 0;
      char *user = 0;

      if (!user) {
        printf ("Telephone number (with '+' sign): ");         
        if (net_getline (&user, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        set_default_username (user);
      }
    }
    int res = do_auth_check_phone (default_username);
    assert (res >= 0);
    logprintf ("%s\n", res > 0 ? "phone registered" : "phone not registered");
    if (res > 0) {
      do_send_code (default_username);
      char *code = 0;
      size_t size = 0;
      printf ("Code from sms: ");
      while (1) {
        if (net_getline (&code, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        if (do_send_code_result (code) >= 0) {
          break;
        }
        printf ("Invalid code. Try again: ");
        free (code);
      }
      auth_state = 300;
    } else {
      printf ("User is not registered. Do you want to register? [Y/n] ");
      char *code;
      size_t size;
      if (net_getline (&code, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }
      if (!*code || *code == 'y') {
        printf ("Ok, starting registartion.\n");
      } else {
        printf ("Then try again\n");
        exit (EXIT_SUCCESS);
      }
      char *first_name;
      printf ("Name: ");
      if (net_getline (&first_name, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }
      char *last_name;
      printf ("Name: ");
      if (net_getline (&last_name, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }

      int dc_num = do_get_nearest_dc ();
      assert (dc_num >= 0 && dc_num <= MAX_DC_NUM && DC_list[dc_num]);
      dc_working_num = dc_num;
      DC_working = DC_list[dc_working_num];
      
      do_send_code (default_username);
      printf ("Code from sms: ");
      while (1) {
        if (net_getline (&code, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        if (do_send_code_result_auth (code, first_name, last_name) >= 0) {
          break;
        }
        printf ("Invalid code. Try again: ");
        free (code);
      }
      auth_state = 300;
    }
  }

  for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->has_auth) {
    do_export_auth (i);
    do_import_auth (i);
    DC_list[i]->has_auth = 1;
    write_auth_file ();
  }
  write_auth_file ();

  fflush (stdin);
  fflush (stdout);
  fflush (stderr);

  rl_callback_handler_install (get_default_prompt (), interpreter);
  rl_attempted_completion_function = (CPPFunction *) complete_text;
  rl_completion_entry_function = complete_none;

  do_get_dialog_list ();

  return main_loop ();
}
Example #7
0
void on_login (struct tgl_state *TLS) {
  write_auth_file ();
}
Example #8
0
int 
auth_finalize(void)
{
    char temp_name[1024];	/* large filename size */

    if (xauth_modified) {
	if (dieing) {
	    if (verbose) {
		/*
		 * called from a signal handler -- printf is *not* reentrant; also
		 * fileno() might not be reentrant, avoid it if possible, and use
		 * stderr instead of stdout
		 */
#ifdef STDERR_FILENO
		WRITES(STDERR_FILENO, "\nAborting changes to authority file ");
		WRITES(STDERR_FILENO, xauth_filename);
		WRITES(STDERR_FILENO, "\n");
#else
		WRITES(fileno(stderr), "\nAborting changes to authority file ");
		WRITES(fileno(stderr), xauth_filename);
		WRITES(fileno(stderr), "\n");
#endif
	    }
	} else if (!xauth_allowed) {
	    fprintf (stderr, 
		     "%s:  %s not writable, changes ignored\n",
		     ProgramName, xauth_filename);
	} else {
	    if (verbose) {
		printf ("%s authority file %s\n", 
			ignore_locks ? "Ignoring locks and writing" :
			"Writing", xauth_filename);
	    }
	    temp_name[0] = '\0';
	    if (write_auth_file (temp_name) == -1) {
		fprintf (stderr,
			 "%s:  unable to write authority file %s\n",
			 ProgramName, temp_name);
	    } else {
		(void) unlink (xauth_filename);
#if defined(WIN32) || defined(__UNIXOS2__)|| defined(__CYGWIN__)
		if (rename(temp_name, xauth_filename) == -1)
#else
		if (link (temp_name, xauth_filename) == -1)
#endif
		{
		    fprintf (stderr,
		     "%s:  unable to link authority file %s, use %s\n",
			     ProgramName, xauth_filename, temp_name);
		} else {
		    (void) unlink (temp_name);
		}
	    }
	}
    }

    if (xauth_locked) {
	XauUnlockAuth (xauth_filename);
    }
    (void) umask (original_umask);
    return 0;
}
Example #9
0
void fetch_user (struct user *U) {
  unsigned x = fetch_int ();
  assert (x == CODE_user_empty || x == CODE_user_self || x == CODE_user_contact ||  x == CODE_user_request || x == CODE_user_foreign || x == CODE_user_deleted);
  U->id = fetch_int ();
  U->flags &= ~(FLAG_EMPTY | FLAG_DELETED | FLAG_USER_SELF | FLAG_USER_FOREIGN | FLAG_USER_CONTACT);
  if (x == CODE_user_empty) {
    U->flags |= FLAG_EMPTY;
    return;
  }
  if (x == CODE_user_self) {
    assert (!our_id || (our_id == U->id));
    if (!our_id) {
      our_id = U->id;
      write_auth_file ();
    }
  }
  if (U->first_name) { free (U->first_name); }
  if (U->last_name) { free (U->last_name); }
  if (U->print_name) { free (U->print_name); }
  U->first_name = fetch_str_dup ();
  U->last_name = fetch_str_dup ();
  if (!strlen (U->first_name)) {
    if (!strlen (U->last_name)) {
      U->print_name = strdup ("none");
    } else {
      U->print_name = strdup (U->last_name);
    }
  } else {
    if (!strlen (U->last_name)) {
      U->print_name = strdup (U->first_name);
    } else {
      U->print_name = malloc (strlen (U->first_name) + strlen (U->last_name) + 2);
      sprintf (U->print_name, "%s_%s", U->first_name, U->last_name);
    }
  }
  char *s = U->print_name;
  while (*s) {
    if (*s == ' ') { *s = '_'; }
    s++;
  }
  int cc = 0;
  while (1) {
    int ok = 1;
    int i;
    for (i = 0; i < user_num + chat_num; i++) {
      if (Peers[i] != (void *)U && Peers[i]->print_name && !strcmp (Peers[i]->print_name, U->print_name)) {
        ok = 0;
        break;
      }
    }
    if (ok) {
      break;
    }
    cc ++;
    assert (cc <= 99);
    if (cc == 1) {
      int l = strlen (U->print_name);
      char *s = malloc (l + 3);
      memcpy (s, U->print_name, l);
      s[l + 2] = 0;
      s[l] = '#';
      s[l + 1] = '1';
      free (U->print_name);
      U->print_name = s;
    } else if (cc == 10) {
      int l = strlen (U->print_name);
      char *s = malloc (l + 2);
      memcpy (s, U->print_name, l);
      s[l + 1] = 0;
      s[l] = '0';
      s[l - 1] = '1';
      free (U->print_name);
      U->print_name = s;
    } else {
      int l = strlen (U->print_name);
      U->print_name[l - 1] ++;
      if (U->print_name[l - 1] > '9') {
        U->print_name[l - 1] = '0';
        U->print_name[l - 2] ++;
      }
    }
  }
  if (x == CODE_user_deleted) {
    U->flags |= FLAG_DELETED;
    return;
  }
  if (x == CODE_user_self) {
    U->flags |= FLAG_USER_SELF;
  } else {
    U->access_hash = fetch_long ();
  }
  if (x == CODE_user_foreign) {
    U->flags |= FLAG_USER_FOREIGN;
    U->phone = 0;
  } else {
    if (U->phone) { free (U->phone); }
    U->phone = fetch_str_dup ();
  }
  //logprintf ("name = %s, surname = %s, phone = %s\n", U->first_name, U->last_name, U->phone);
  unsigned y = fetch_int ();
  //fprintf (stderr, "y = 0x%08x\n", y);
  if (y == CODE_user_profile_photo_empty) {
    U->photo_small.dc = -2;
    U->photo_big.dc = -2;
  } else {
    assert (y == CODE_user_profile_photo || y == 0x990d1493);
    if (y == CODE_user_profile_photo) {
      fetch_long ();
    }
    fetch_file_location (&U->photo_small);
    fetch_file_location (&U->photo_big);
  }
  fetch_user_status (&U->status);
  if (x == CODE_user_self) {
    assert (fetch_int () == (int)CODE_bool_false);
  }
  if (x == CODE_user_contact) {
    U->flags |= FLAG_USER_CONTACT;
  }
}
Example #10
0
int loop (void) {
  on_start ();
  if (binlog_enabled) {
    replay_log ();
    write_binlog ();
  } else {
    read_auth_file ();
  }
  update_prompt ();

  assert (DC_list[dc_working_num]);
  if (!DC_working || !DC_working->auth_key_id) {
//  if (auth_state == 0) {
    DC_working = DC_list[dc_working_num];
    assert (!DC_working->auth_key_id);
    dc_authorize (DC_working);
    assert (DC_working->auth_key_id);
    auth_state = 100;
    write_auth_file ();
  }
  
  if (verbosity) {
    logprintf ("Requesting info about DC...\n");
  }
  do_help_get_config ();
  net_loop (0, mcs);
  if (verbosity) {
    logprintf ("DC_info: %d new DC got\n", new_dc_num);
  }
  int i;
  for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->auth_key_id) {
    dc_authorize (DC_list[i]);
    assert (DC_list[i]->auth_key_id);
    write_auth_file ();
  }

  if (auth_state == 100 || !(DC_working->has_auth)) {
    if (!default_username) {
      size_t size = 0;
      char *user = 0;

      if (!user) {
        printf ("Telephone number (with '+' sign): ");         
        if (net_getline (&user, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        set_default_username (user);
      }
    }
    int res = do_auth_check_phone (default_username);
    assert (res >= 0);
    logprintf ("%s\n", res > 0 ? "phone registered" : "phone not registered");
    if (res > 0) {
      do_send_code (default_username);
      char *code = 0;
      size_t size = 0;
      printf ("Code from sms: ");
      while (1) {
        if (net_getline (&code, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        if (do_send_code_result (code) >= 0) {
          break;
        }
        printf ("Invalid code. Try again: ");
        free (code);
      }
      auth_state = 300;
    } else {
      printf ("User is not registered. Do you want to register? [Y/n] ");
      char *code;
      size_t size;
      if (net_getline (&code, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }
      if (!*code || *code == 'y' || *code == 'Y') {
        printf ("Ok, starting registartion.\n");
      } else {
        printf ("Then try again\n");
        exit (EXIT_SUCCESS);
      }
      char *first_name;
      printf ("Name: ");
      if (net_getline (&first_name, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }
      char *last_name;
      printf ("Name: ");
      if (net_getline (&last_name, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }

      int dc_num = do_get_nearest_dc ();
      assert (dc_num >= 0 && dc_num <= MAX_DC_NUM && DC_list[dc_num]);
      dc_working_num = dc_num;
      DC_working = DC_list[dc_working_num];
      
      do_send_code (default_username);
      printf ("Code from sms: ");
      while (1) {
        if (net_getline (&code, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        if (do_send_code_result_auth (code, first_name, last_name) >= 0) {
          break;
        }
        printf ("Invalid code. Try again: ");
        free (code);
      }
      auth_state = 300;
    }
  }

  for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->has_auth) {
    do_export_auth (i);
    do_import_auth (i);
    DC_list[i]->has_auth = 1;
    if (binlog_enabled) {
      int *ev = alloc_log_event (8);
      ev[0] = LOG_DC_SIGNED;
      ev[1] = i;
      add_log_event (ev, 8);
    }
    write_auth_file ();
  }
  write_auth_file ();

  fflush (stdin);
  fflush (stdout);
  fflush (stderr);

  read_state_file ();
  read_secret_chat_file ();

  set_interface_callbacks ();

  do_get_difference ();
  net_loop (0, dgot);
  do_get_dialog_list ();

  return main_loop ();
}
Example #11
0
File: loop.c Project: AmesianX/tg
int loop (void) {
  //on_start ();
  tgl_set_callback (TLS, &upd_cb);
  //TLS->temp_key_expire_time = 60;
  struct event_base *ev = event_base_new ();
  tgl_set_ev_base (TLS, ev);
  tgl_set_net_methods (TLS, &tgl_conn_methods);
  tgl_set_timer_methods (TLS, &tgl_libevent_timers);
  assert (TLS->timer_methods);
  tgl_set_download_directory (TLS, get_downloads_directory ());
  tgl_register_app_id (TLS, TELEGRAM_CLI_APP_ID, TELEGRAM_CLI_APP_HASH); 
  tgl_init (TLS);
 
  if (binlog_enabled) {
    double t = tglt_get_double_time ();
    if (verbosity >= E_DEBUG) {
      logprintf ("replay log start\n");
    }
    tgl_replay_log (TLS);
    if (verbosity >= E_DEBUG) {
      logprintf ("replay log end in %lf seconds\n", tglt_get_double_time () - t);
    }
    tgl_reopen_binlog_for_writing (TLS);
  } else {
    read_auth_file ();
    read_state_file ();
    read_secret_chat_file ();
  }

  binlog_read = 1;
  #ifdef USE_LUA
    lua_binlog_end ();
  #endif
  
  if (sfd >= 0) {
    struct event *ev = event_new (TLS->ev_base, sfd, EV_READ | EV_PERSIST, accept_incoming, 0);
    event_add (ev, 0);
  }
  if (usfd >= 0) {
    struct event *ev = event_new (TLS->ev_base, usfd, EV_READ | EV_PERSIST, accept_incoming, 0);
    event_add (ev, 0);
  }
  update_prompt ();
   
  if (reset_authorization) {
    tgl_peer_t *P = tgl_peer_get (TLS, TGL_MK_USER (TLS->our_id));
    if (P && P->user.phone && reset_authorization == 1) {
      set_default_username (P->user.phone);
    }
    bl_do_reset_authorization (TLS);
  }

  net_loop (0, all_authorized);

  int i;
  for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i] && !tgl_authorized_dc (TLS, TLS->DC_list[i])) {
    assert (0);
  }

  if (!tgl_signed_dc (TLS, TLS->DC_working)) {
    if (disable_output) {
      fprintf (stderr, "Can not login without output\n");
      do_halt (1);
    }
    if (!default_username) {
      size_t size = 0;
      char *user = 0;

      if (!user) {
        printf ("Telephone number (with '+' sign): ");         
        if (net_getline (&user, &size) == -1) {
          perror ("getline()");
          do_halt (1);
        }
        set_default_username (user);
      }
    }
    tgl_do_send_code (TLS, default_username, sign_in_callback, 0);
    net_loop (0, sent_code);
   
    if (verbosity >= E_DEBUG) {
      logprintf ("%s\n", should_register ? "phone not registered" : "phone registered");
    }
    if (!should_register) {
      char *code = 0;
      size_t size = 0;
      printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): ");
      while (1) {
        if (net_getline (&code, &size) == -1) {
          perror ("getline()");
          do_halt (1);
        }
        if (!strcmp (code, "call")) {
          printf ("You typed \"call\", switching to phone system.\n");
          tgl_do_phone_call (TLS, default_username, hash, 0, 0);
          printf ("Calling you! Code: ");
          continue;
        }
        if (tgl_do_send_code_result (TLS, default_username, hash, code, sign_in_result, 0) >= 0) {
          break;
        }
        printf ("Invalid code. Try again: ");
        free (code);
      }
    } else {
      printf ("User is not registered. Do you want to register? [Y/n] ");
      char *code;
      size_t size;
      if (net_getline (&code, &size) == -1) {
        perror ("getline()");
        do_halt (1);
      }
      if (!*code || *code == 'y' || *code == 'Y') {
        printf ("Ok, starting registartion.\n");
      } else {
        printf ("Then try again\n");
        do_halt (1);
      }
      char *first_name;
      printf ("First name: ");
      if (net_getline (&first_name, &size) == -1) {
        perror ("getline()");
        do_halt (1);
      }
      char *last_name;
      printf ("Last name: ");
      if (net_getline (&last_name, &size) == -1) {
        perror ("getline()");
        do_halt (1);
      }
      printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): ");
      while (1) {
        if (net_getline (&code, &size) == -1) {
          perror ("getline()");
          do_halt (1);
        }
        if (!strcmp (code, "call")) {
          printf ("You typed \"call\", switching to phone system.\n");
          tgl_do_phone_call (TLS, default_username, hash, 0, 0);
          printf ("Calling you! Code: ");
          continue;
        }
        if (tgl_do_send_code_result_auth (TLS, default_username, hash, code, first_name, last_name, sign_in_result, 0) >= 0) {
          break;
        }
        printf ("Invalid code. Try again: ");
        free (code);
      }
    }

    net_loop (0, signed_in);    
    //bl_do_dc_signed (TLS->DC_working);
  }

  for (i = 0; i <= TLS->max_dc_num; i++) if (TLS->DC_list[i] && !tgl_signed_dc (TLS, TLS->DC_list[i])) {
    tgl_do_export_auth (TLS, i, export_auth_callback, (void*)(long)TLS->DC_list[i]);    
    cur_a_dc = TLS->DC_list[i];
    net_loop (0, dc_signed_in);
    assert (tgl_signed_dc (TLS, TLS->DC_list[i]));
  }
  write_auth_file ();
  
  fflush (stdout);
  fflush (stderr);

  //read_state_file ();
  //read_secret_chat_file ();

  set_interface_callbacks ();

  tglm_send_all_unsent (TLS);
  tgl_do_get_difference (TLS, sync_from_start, get_difference_callback, 0);
  net_loop (0, dgot);
  assert (!(TLS->locks & TGL_LOCK_DIFF));
  TLS->started = 1;
  if (wait_dialog_list) {
    d_got_ok = 0;
    tgl_do_get_dialog_list (TLS, dlist_cb, 0);
    net_loop (0, dgot);
  }
  #ifdef USE_LUA
    lua_diff_end ();
  #endif

  if (start_command) {
    safe_quit = 1;
    while (*start_command) {
      char *start = start_command;
      while (*start_command && *start_command != '\n') {
        start_command ++;
      }
      if (*start_command) {
        *start_command = 0;
        start_command ++;
      } 
      interpreter_ex (start, 0);
    }
  }

  /*tgl_do_get_dialog_list (get_dialogs_callback, 0);
  if (wait_dialog_list) {
    dialog_list_got = 0;
    net_loop (0, dlgot);
  }*/

  return main_loop ();
}
int
main (int argc, char **argv)
{
    char **xargv;
    int i, j;
    int fd;
	
    xargv = alloca (sizeof (char *) * (argc + 32));
	
    if (!read_boolean_pref (CFSTR ("no_auth"), FALSE))
		auth_file = XauFileName ();
	
    /* The standard X11 behaviour is for the server to quit when the first
	 client exits. But it can be useful for debugging (and to mimic our
	 behaviour in the beta releases) to not do that. */
	
    xinit_kills_server = read_boolean_pref (CFSTR ("xinit_kills_server"), TRUE);
	
    for (i = 1; i < argc; i++)
    {
		if (argv[i][0] == ':')
			server_name = argv[i];
    }
	
    if (server_name == NULL)
    {
		static char name[8];
		
		/* No display number specified, so search for the first unused.
		 
		 There's a big old race condition here if two servers start at
		 the same time, but that's fairly unlikely. We could create
		 lockfiles or something, but that's seems more likely to cause
		 problems than the race condition itself.. */
		
		for (i = 0; i < MAX_DISPLAYS; i++)
		{
			if (!display_exists_p (i))
				break;
		}
		
		if (i == MAX_DISPLAYS)
		{
			fprintf (stderr, "%s: couldn't allocate a display number", argv[0]);
			exit (1);
		}
		
		sprintf (name, ":%d", i);
		server_name = name;
    }
	
    if (auth_file != NULL)
    {
		/* Create new Xauth keys and add them to the .Xauthority file */
		
		make_auth_keys (server_name);
		write_auth_file (TRUE);
    }
	
    /* Construct our new argv */
	
    i = j = 0;
	
    xargv[i++] = argv[j++];
	
    if (auth_file != NULL)
    {
		xargv[i++] = "-auth";
		xargv[i++] = auth_file;
    }
	
    /* By default, don't listen on tcp sockets if Xauth is disabled. */
	
    if (read_boolean_pref (CFSTR ("nolisten_tcp"), auth_file == NULL))
    {
		xargv[i++] = "-nolisten";
		xargv[i++] = "tcp";
    }
	
    while (j < argc)
    {
		if (argv[j++][0] != ':')
			xargv[i++] = argv[j-1];
    }
	
    xargv[i++] = (char *) server_name;
    xargv[i++] = NULL;
	
    /* Detach from any controlling terminal and connect stdin to /dev/null */
	
#ifdef TIOCNOTTY
    fd = open ("/dev/tty", O_RDONLY);
    if (fd != -1)
    {
		ioctl (fd, TIOCNOTTY, 0);
		close (fd);
    }
#endif
	
    fd = open ("/dev/null", O_RDWR, 0);
    if (fd >= 0)
    {
		dup2 (fd, 0);
		if (fd > 0)
			close (fd);
    }
	
    if (!start_server (xargv))
		return 1;
	
    if (!wait_for_server ())
    {
		kill (server_pid, SIGTERM);
		return 1;
    }
	
    if (!start_client ())
    {
		kill (server_pid, SIGTERM);
		return 1;
    }
	
    signal (SIGCHLD, sigchld_handler);
	
    signal (SIGTERM, termination_signal_handler);
    signal (SIGHUP, termination_signal_handler);
    signal (SIGINT, termination_signal_handler);
    signal (SIGQUIT, termination_signal_handler);

    if (setjmp (exit_continuation) == 0)
    {
		if (install_ipaddr_source ())
			CFRunLoopRun ();
		else
			while (1) pause ();
    }
	
    signal (SIGCHLD, SIG_IGN);

    if (client_pid >= 0) kill (client_pid, SIGTERM);
    if (server_pid >= 0) kill (server_pid, SIGTERM);
	
    if (auth_file != NULL)
    {
		/* Remove our Xauth keys */
		
		write_auth_file (FALSE);
    }
	
    free_auth_items ();
	
    return 0;
}
Example #13
0
File: loop.c Project: BUSHA/tg
int loop (void) {
  on_start ();
  if (binlog_enabled) {
    double t = get_double_time ();
    logprintf ("replay log start\n");
    replay_log ();
    logprintf ("replay log end in %lf seconds\n", get_double_time () - t);
    write_binlog ();
    #ifdef USE_LUA
      lua_binlog_end ();
    #endif
  } else {
    read_auth_file ();
  }
  update_prompt ();

  assert (DC_list[dc_working_num]);
  if (!DC_working || !DC_working->auth_key_id) {
//  if (auth_state == 0) {
    DC_working = DC_list[dc_working_num];
    assert (!DC_working->auth_key_id);
    dc_authorize (DC_working);
    assert (DC_working->auth_key_id);
    auth_state = 100;
    write_auth_file ();
  }
  
  if (verbosity) {
    logprintf ("Requesting info about DC...\n");
  }
  do_help_get_config ();
  net_loop (0, mcs);
  if (verbosity) {
    logprintf ("DC_info: %d new DC got\n", new_dc_num);
  }
  int i;
  for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->auth_key_id) {
    dc_authorize (DC_list[i]);
    assert (DC_list[i]->auth_key_id);
    write_auth_file ();
  }

  if (auth_state == 100 || !(DC_working->has_auth)) {
    if (!default_username) {
      size_t size = 0;
      char *user = 0;

      if (!user) {
        printf ("Telephone number (with '+' sign): ");         
        if (net_getline (&user, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        set_default_username (user);
      }
    }
    int res = do_auth_check_phone (default_username);
    assert (res >= 0);
    logprintf ("%s\n", res > 0 ? "phone registered" : "phone not registered");
    if (res > 0 && !register_mode) {
      do_send_code (default_username);
      char *code = 0;
      size_t size = 0;
      printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): ");
      while (1) {
        if (net_getline (&code, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        if (!strcmp (code, "call")) {
          printf ("You typed \"call\", switching to phone system.\n");
          do_phone_call (default_username);
          printf ("Calling you! Code: ");
          continue;
        }
        if (do_send_code_result (code) >= 0) {
          break;
        }
        printf ("Invalid code. Try again: ");
        tfree_str (code);
      }
      auth_state = 300;
    } else {
      printf ("User is not registered. Do you want to register? [Y/n] ");
      char *code;
      size_t size;
      if (net_getline (&code, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }
      if (!*code || *code == 'y' || *code == 'Y') {
        printf ("Ok, starting registartion.\n");
      } else {
        printf ("Then try again\n");
        exit (EXIT_SUCCESS);
      }
      char *first_name;
      printf ("First name: ");
      if (net_getline (&first_name, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }
      char *last_name;
      printf ("Last name: ");
      if (net_getline (&last_name, &size) == -1) {
        perror ("getline()");
        exit (EXIT_FAILURE);
      }

      int dc_num = do_get_nearest_dc ();
      assert (dc_num >= 0 && dc_num <= MAX_DC_NUM && DC_list[dc_num]);
      dc_working_num = dc_num;
      DC_working = DC_list[dc_working_num];
      
      do_send_code (default_username);
      printf ("Code from sms (if you did not receive an SMS and want to be called, type \"call\"): ");
      while (1) {
        if (net_getline (&code, &size) == -1) {
          perror ("getline()");
          exit (EXIT_FAILURE);
        }
        if (!strcmp (code, "call")) {
          printf ("You typed \"call\", switching to phone system.\n");
          do_phone_call (default_username);
          printf ("Calling you! Code: ");
          continue;
        }
        if (do_send_code_result_auth (code, first_name, last_name) >= 0) {
          break;
        }
        printf ("Invalid code. Try again: ");
        tfree_str (code);
      }
      auth_state = 300;
    }
  }

  for (i = 0; i <= MAX_DC_NUM; i++) if (DC_list[i] && !DC_list[i]->has_auth) {
    do_export_auth (i);
    do_import_auth (i);
    bl_do_dc_signed (i);
    write_auth_file ();
  }
  write_auth_file ();

  fflush (stdout);
  fflush (stderr);

  read_state_file ();
  read_secret_chat_file ();

  set_interface_callbacks ();

  do_get_difference ();
  net_loop (0, dgot);
  #ifdef USE_LUA
    lua_diff_end ();
  #endif
  send_all_unsent ();


  do_get_dialog_list ();
  if (wait_dialog_list) {
    dialog_list_got = 0;
    net_loop (0, dlgot);
  }

  return main_loop ();
}
Example #14
0
Tt_status _Tt_auth::
generate_auth_cookie()
{
    static const char		*funcname = "_Tt_auth::make_auth_cookie()";
    _tt_AuthFileEntry   *entry;
    int			 exists;
    char                *filename;
    int			 original_umask;
    int			 retval;
    Tt_status		 status;

    _entries_head = _entries_tail = NULL;
    status = TT_OK;
    _auth_cookie = _tt_GenerateMagicCookie(_TT_ICEAUTH_MAGIC_COOKIE_LEN);

    if (! (filename = _tt_AuthFileName())) {
	_tt_syslog(0, LOG_ERR, "%s:  Missing TTAUTHORITY file.\n", funcname);
        return TT_AUTHFILE_MISSING;
    }
    
    if (_tt_AuthLockSuccess !=
	(retval = _tt_LockAuthFile(filename,
    		    		  _TT_ICEAUTH_DEFAULT_RETRIES,
		    		  _TT_ICEAUTH_DEFAULT_TIMEOUT,
		    		  _TT_ICEAUTH_DEFAULT_DEADTIME))) {
	const char *reason = "unknown error";

	_tt_UnlockAuthFile(filename);
	if (retval == _tt_AuthLockTimeout) {
	    reason = "timeout";
	    status = TT_AUTHFILE_LOCK_TIMEOUT;
	}
	else {
	    reason = "lock error";
	    status = TT_AUTHFILE_LOCK;
	}
	_tt_syslog(0, LOG_ERR,
		   "%s:  %s in locking authority file \"%s\".\n",
		   funcname, reason, filename);
	return status;
    }

    original_umask = umask(077);

    exists = (0 == access(filename, F_OK));
    if (exists && (0 != access(filename, R_OK | W_OK)))
    {
	_tt_syslog(0, LOG_ERR,
		   "%s:  \"%s\" not writable, auth entry not added.\n",
		   funcname, filename);
        status = TT_AUTHFILE_ACCESS;
	goto cleanup;
    }

    if (exists && TT_OK != (status = read_auth_file(filename))) {
	_tt_syslog(0, LOG_ERR,
		   "%s:  could not read \"%s\".  New entry not written.\n",
		   funcname, filename);
        goto cleanup;
    }

    entry = (_tt_AuthFileEntry*) malloc(sizeof(_tt_AuthFileEntry));
    if (NULL == entry) {
	_tt_syslog(0, LOG_ERR,
		   "%s:  memory error.  New entry not written.\n",
                   funcname);
	status = TT_ERR_NOMEM;
        goto cleanup;
    }

    entry->protocol_name = strdup(_TT_ICEAUTH_PROTOCOL_NAME);
    entry->protocol_data_length = 0;
    entry->protocol_data = strdup("");
    entry->network_id = strdup((char*) _sessionid);
    entry->auth_name = strdup(_TT_ICEAUTH_AUTH_NAME);
    entry->auth_data_length = strlen((char*) _auth_cookie) + 1;
    entry->auth_data = strdup((char*) _auth_cookie);

    status = modify_auth_entry(entry, &_entries_head);

    status = write_auth_file(filename);
    if (TT_OK != status) {
	_tt_syslog(0, LOG_ERR,
		   "%s:  could not write \"%s\".  New entry not written.\n",
		   funcname, filename);
        goto cleanup;
    }

cleanup:
    (void) umask(original_umask);
    _tt_UnlockAuthFile(filename);
    return status;
}