main.c

  1/*!
  2 * @file main.c
  3 *
  4 * vim: expandtab:ts=2:sts=2:sw=2
  5 *
  6 * @authors
  7 * Copyright (C) 2020 Anoxinon e.V.
  8 *
  9 * @copyright
 10 * This file is part of xmppc.
 11 *
 12 * xmppc is free software: you can redistribute it and/or modify
 13 * it under the terms of the GNU General Public License as published by
 14 * the Free Software Foundation, either version 3 of the License, or
 15 * (at your option) any later version.
 16 *
 17 * xmppc is distributed in the hope that it will be useful,
 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 20 * GNU General Public License for more details.
 21 *
 22 * You should have received a copy of the GNU General Public License
 23 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 24 *
 25 * German
 26 *
 27 * Diese Datei ist Teil von xmppc.
 28 *
 29 * xmppc ist Freie Software: Sie können es unter den Bedingungen
 30 * der GNU General Public License, wie von der Free Software Foundation,
 31 * Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
 32 * veröffentlichten Version, weiter verteilen und/oder modifizieren.
 33 *
 34 * xmppc wird in der Hoffnung, dass es nützlich sein wird, aber
 35 * OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
 36 * Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
 37 * Siehe die GNU General Public License für weitere Details.
 38 *
 39 * Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
 40 * Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
 41 */
 42
 43/*!
 44 * @mainpage xmppc - XMPP command line
 45 *
 46 * @section whatIsXmppc What is xmppc
 47 *
 48 * xmppc is a command line program for XMPP (https://xmpp.org/).
 49 * The program has been written for the Debian GNU/Linux system (https://www.debian.org/).
 50 *
 51 * @section build Build
 52 *
 53 * @subsection dependencies Dependencies
 54 *
 55 * - The GNU C Library (glibc) - 2.28-10
 56 * - GLib - 2.58.3
 57 * - XMPP library libstrophe - 0.9.2-2
 58 * - GPGME (GnuPG Made Easy) - 1.12.0-6
 59 *
 60 * @subsection compile Compile
 61 *
 62 * @code{.bash}
 63 * ./bootstrap.sh
 64 * ./configure
 65 * make
 66 * @endcode
 67 *
 68 * @subsection documentation Documentation
 69 * @code{.bash}
 70 * cd doc
 71 * make
 72 * @endcode
 73 * @section usage Usage
 74 *
 75 * @code{.bash}
 76 * xmppc --jid <jabberid> --pwd <secret> --mode <mode> <command> <command args>
 77 * @endcode
 78 *
 79 * @code{.bash}
 80 * xmppc --jid user@domain.tld --pwd "my secret" --mode pgp chat friend@domain.tld "Hello!"
 81 * xmppc --jid user@domain.tld --pwd $(pass XMPP/domain/user) --mode omemo list
 82 * @endcode
 83 *
 84 *
 85 * @section development Development
 86 *
 87 * - \subpage module
 88 *
 89 */
 90
 91/*!
 92 * \page module Modules
 93 *
 94 * \section account Account
 95 * \section roster Roster
 96 * \section message Message
 97 * \section muc Multi-User-Chat
 98 * \section omemo OMEMO
 99 * XEP-0384: OMEMO Encryption (Version 0.3.0 (2018-07-31)). xmppc can use used
100 * to request the users OMEMO Device ID List and displays the Fingerprints of
101 * the keys.
102 *
103 * @code{.bash}
104 * xmppc --jid user@domain.tld --pwd $(pass domain.tld/user) --mode omemo
105 * @endcode
106 *
107 * @code{.bash}
108 * xmppc --jid user@domain.tld --pwd $(pass domain.tld/user) --mode omemo| gpg --clear-sign --sign-with 123ABC_MY_OPENPGP_KEY_1234 > omemo.asc
109 * @endcode
110 *
111 *
112 * \section pgp PGP
113 * XEP-0027: Current Jabber OpenPGP Usage
114 * (https://xmpp.org/extensions/xep-0027.html) is obsolete, but there are still
115 * clients which supports XEP-0027 instead of XEP-0373 / XEP-0374. This module
116 * supports sending of OpenPGP encrypted messages via XMPP's XEP-0027.
117 *
118 * @code{.bash}
119 * xmppc --jid user@domain.tld --pwd "my secret" --mode pgp friend@domain.tld "Hello!"
120 * @endcode
121 *
122 * xmppc use GnuPG's GPGME API use lookup the own key and the key of the
123 * recipient. Those two keys will be used to encrypt the message.
124 *
125 * Details \subpage module-pgp
126 *
127 */
128
129#include "config.h"
130#include <assert.h>
131#include <getopt.h>
132#include <stdarg.h>
133#include <stdio.h>
134#include <stdlib.h>
135#include <string.h>
136#include <glib.h>
137#include <strophe.h>
138#include <errno.h>
139
140#include "xmppc.h"
141#include "mode/account.h"
142#include "mode/message.h"
143#include "mode/muc.h"
144#include "mode/omemo.h"
145#include "mode/roster.h"
146#include "mode/pgp.h"
147#include "mode/openpgp.h"
148#include "mode/monitor.h"
149#include "mode/mam.h"
150#include "mode/discovery.h"
151#include "mode/bookmark.h"
152
153/*!
154 * @brief The callback structure.
155 *
156 * This struct is used to call the "execution"-function of the mode module (e.g
157 * account_execute_command, roster_execute_command, ...).
158 *
159 * @authors DebxWoody
160 * @since 0.0.2
161 * @version 1
162 *
163 */
164
165typedef struct {
166  /*! Size of arguments */
167  int argc;
168  /*! Array of C-Strings (arguments) with size of argc */
169  char **argv;
170  /*! Pointer to Execute-Handler */
171  ExecuteHandler callback;
172  /*! XMPPC context object */
173  xmppc_t *xmppc;
174} callback_t;
175
176/*!
177 * @brief Mode Mapping.
178 *
179 * This struct is used to provider the mapping of the mode name (name via
180 * command line interface of the user (e.g. -m account). The technical ID of the
181 * mode which is defined via enum xmppc_mode_t and the pointer of function which
182 * should be used to handle those mode.
183 *
184 * @authors DebxWoody
185 * @since 0.0.2
186 * @version 1
187 */
188
189struct mode_mapping {
190  const char *name;
191  xmppc_mode_t mode;
192  ExecuteHandler callback;
193};
194
195/*!
196 * @brief Mode mapping table.
197 *
198 * Mapping of mode's string, enum and function.
199 *
200 * @authors DebxWoody
201 * @since 0.0.2
202 * @version 1
203 *
204 */
205
206static struct mode_mapping map[] = {
207  /*!
208  * Account Mode Mapping
209  * @since 0.0.2
210  * @version 1
211  */
212  {"account", ACCOUNT, account_execute_command},
213  /*!
214  * roster Mode Mapping
215  * @since 0.0.2
216  * @version 1
217  */
218  {"roster", ROSTER, roster_execute_command},
219  /*!
220  * Message Mode Mapping
221  * @since 0.0.2
222  * @version 1
223  */
224  {"message", MESSAGE, message_execute_command},
225  /*!
226  * MUC Mode Mapping
227  * @since 0.0.2
228  * @version 1
229  */
230  {"muc", MUC, muc_execute_command},
231  /*!
232  * OMEMO Mode Mapping
233  * @since 0.0.2
234  * @version 1
235  */
236  {"omemo", OMEMO, omemo_execute_command},
237  /*!
238  * PGP Mode Mapping
239  * @since 0.0.2
240  * @version 1
241  */
242  {"pgp", PGP, pgp_execute_command},
243  /*!
244  * XEP-0373: OpenPGP for XMPP
245  */
246  {"openpgp", OPENPGP, openpgp_execute_command},
247  /*!
248  * Monitor
249  */
250  {"monitor", MONITOR, monitor_execute_command},
251  //
252  {"discovery", DISCOVERY, discovery_execute_command},
253  //
254  {"bookmark", BOOKMARK, bookmark_execute_command},
255  // 
256  {"mam", MAM, mam_execute_command},
257  // End of Map
258  {NULL, 0}
259};
260
261/*!
262 * \brief Connection Handler Callback of libstrophe.
263 *
264 * This function will be called by libstrophe.
265 *
266 * \param conn See libstrophe documentation
267 * \param status See libstrophe documentation
268 * \param error See libstrophe documentation
269 * \param stream_error See libstrophe documentation
270 *
271 * \param userdata callback_t object for the mode request by user.
272 *
273 * @authors DebxWoody
274 * @since 0.0.2
275 * @version 1
276 *
277 */
278
279void conn_handler(xmpp_conn_t *const conn, const xmpp_conn_event_t status,
280                  const int error, xmpp_stream_error_t *const stream_error,
281                  void *const userdata) {
282  callback_t *callback = (callback_t *)userdata;
283
284  if( error != 0 ) {
285    logError(callback->xmppc,"Connection failed. %s\n", strerror(error));
286    xmpp_stop(xmpp_conn_get_context(conn));
287    return;
288  }
289
290  if( stream_error != NULL ) {
291    logError(callback->xmppc,"Connection failed. %s\n", stream_error->text);
292    xmpp_stop(xmpp_conn_get_context(conn));
293    return;
294  }
295
296
297  switch (status) {
298    case XMPP_CONN_CONNECT:
299      logInfo(callback->xmppc, "Connected\n");
300      if( xmpp_conn_is_secured(conn) ) {
301        logInfo(callback->xmppc, "Secure connection!\n");
302      } else {
303       logWarn(callback->xmppc, "Connection not secure!\n");
304      }
305      callback->callback(callback->xmppc, callback->argc, callback->argv);
306      break;
307    case XMPP_CONN_RAW_CONNECT:
308    case XMPP_CONN_DISCONNECT:
309    case XMPP_CONN_FAIL:
310      logInfo(callback->xmppc, "Stopping XMPP!\n");
311      xmpp_stop(xmpp_conn_get_context(conn));
312  }
313}
314
315static void _show_help();
316
317/*!
318 * \brief C-Main function
319 *
320 * - Parse argv arguments of the command line.
321 * - Checks jid and password for xmpp logging
322 * - Checks mode requested by user
323 * - All other arguments will be used as mode parameters
324 *
325 * \param argc Arguments counter
326 * \param argv Arguments vector
327 * \returns Exit Code
328 *
329 * @authors DebxWoody
330 * @since 0.0.2
331 * @version 2
332 */
333
334int main(int argc, char *argv[]) {
335#if XMPPC_DEVELOPMENT
336  printf("!!! WARNING: XMPPC is running in development mode !!!\n");
337#endif
338
339  INIT_XMPPC(xmppc);
340
341  static int verbose_flag = 0;
342  int c = 0;
343  xmppc_mode_t mode = UNKOWN;
344  char *jid = NULL;
345  char *pwd = NULL;
346  char *account = NULL;
347
348  static struct option long_options[] = {
349      /* These options set a flag. */
350      {"verbose", no_argument, &verbose_flag, 1},
351      {"help", no_argument, 0, 'h'},
352      {"config", required_argument, 0, 'c'},
353      {"account", required_argument, 0, 'a'},
354      {"jid", required_argument, 0, 'j'},
355      {"pwd", required_argument, 0, 'p'},
356      {"mode", required_argument, 0, 'm'},
357      {"file", required_argument, 0, 'f'},
358      {0, 0, 0, 0}};
359  while (c > -1) {
360    int option_index = 0;
361
362    c = getopt_long(argc, argv, "hva:j:p:m:", long_options, &option_index);
363    if (c > -1) {
364      switch (c) {
365      case 'h':
366        _show_help();
367        return EXIT_SUCCESS;
368
369      case 'c':
370        printf("option -c with value `%s'\n", optarg);
371        break;
372
373      case 'f':
374        printf("option -f with value `%s'\n", optarg);
375        break;
376
377      case 'a':
378        account = malloc(strlen(optarg) + 1);
379        strcpy(account, optarg);
380        break;
381
382      case 'j':
383        jid = malloc(strlen(optarg) + 1);
384        strcpy(jid, optarg);
385        break;
386
387      case 'p':
388        pwd = malloc(strlen(optarg) + 1);
389        strcpy(pwd, optarg);
390        break;
391
392      case 'm':
393        for(int i = 0; map[i].name;i++ ) {
394          if (strcmp(optarg, map[i].name) == 0) {
395            mode = map[i].mode;
396            break;
397          }
398        }
399        break;
400
401      case 'v':
402        verbose_flag++;
403        break;
404
405      case '?':
406        break;
407
408      default:
409        abort();
410      }
411    }
412  }
413
414  // Loading config file
415  GKeyFile *config_file = g_key_file_new();
416  GError *error = NULL;
417  GString* configfile = g_string_new( g_get_home_dir());
418  g_string_append(configfile,"/.config/xmppc.conf");
419  gboolean configfilefound = g_key_file_load_from_file(
420    config_file,
421    configfile->str,
422    G_KEY_FILE_NONE,
423    &error);
424
425  if (!configfilefound) {
426   if(!g_error_matches(error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) {
427      logError(&xmppc, "Error loading key file: %s", error->message);
428      return -1;
429    }
430  } else {
431    if(jid == NULL && pwd == NULL) { 
432      logInfo(&xmppc,"Loading default account\n");
433      if( account == NULL ) {
434        account = "default";
435      }
436      jid = g_key_file_get_value (config_file, account, "jid" ,&error);
437      pwd = g_key_file_get_value (config_file, account, "pwd" ,&error);
438    }
439  }
440
441  int paramc = argc- optind;
442  char* paramv[paramc];
443
444  for (int i = optind; i < argc; i++) {
445    char* x= malloc(strlen(argv[i])+1 *sizeof(char)  );
446    strcpy(x,argv[i]);
447    paramv[i-optind] = x;
448  }
449  xmppc_context(&xmppc, verbose_flag);
450
451  logInfo(&xmppc, "Connecting %s ... ", jid);
452  xmppc_connect(&xmppc, jid, pwd);
453
454  ExecuteHandler handler = NULL;
455        for(int i = 0; map[i].name;i++ ) {
456          if (mode ==  map[i].mode) {
457            handler = map[i].callback;
458            break;
459          }
460        }
461
462  if( handler == NULL ) {
463    logError(&xmppc, "Unbekannter mode\n");
464    return -1;
465  } 
466
467  callback_t callback = {paramc, paramv, handler, &xmppc};
468
469  xmpp_conn_set_flags(xmppc.conn, XMPP_CONN_FLAG_MANDATORY_TLS);
470
471  int e =  xmpp_connect_client(xmppc.conn, NULL, 0, conn_handler, &callback);
472  if(XMPP_EOK != e ) {
473    printf("xmpp_connect_client failed");
474  }
475
476  xmpp_run(xmppc.ctx);
477  xmpp_conn_release(xmppc.conn);
478  xmpp_ctx_free(xmppc.ctx);
479  xmpp_shutdown();
480
481  return EXIT_SUCCESS;
482}
483
484static void _show_help() {
485#ifdef XMPPC_DEVELOPMENT
486  printf("%s - Development\n", PACKAGE_STRING);
487#else
488  printf("%s\n", PACKAGE_STRING);
489#endif
490  printf("Usage: xmppc [--account <account>] [ --jid <jid> --pwd <pwd>] --mode <mode> <command> [<parameters> ...]\n");
491  printf("Options:\n");
492  printf("  -h / --help                 Display this information.\n");
493  printf("  -j / --jid <jid>            Jabber ID\n");
494  printf("  -p / --pwd <password>       Passwort\n");
495  printf("  -a / --account <account>    Account\n");
496  printf("  -m / --mode <mode>          xmppc mode\n");
497  printf("\n");
498  printf("Modes:\n");
499  printf("  -m --mode roster            xmppc roster mode\n");
500  printf("    list                      List all contacts\n");
501  printf("    export                    Exports all contacts\n");
502  printf("\n");
503  printf("  -m --mode message           xmppc message mode\n");
504  printf("    chat <jid> <message>      Sending unencrypted message to jid\n");
505  printf("\n");
506  printf("  -m --mode pgp               xmppc pgp mode (XEP-0027) \n");
507  printf("    chat <jid> <message>      Sending pgp encrypted message to jid\n");
508  printf("\n");
509  printf("  -m --mode omemo             xmppc omemo mode (XEP-0384)\n");
510  printf("    list                      List the device IDs and fingerprints\n");
511  printf("\n");
512  printf("  -m --mode openpgp           xmppc openpgp mode (XEP-0373)\n");
513  printf("    signcrypt <jid> <message> Sending pgp signed and encrypted message to jid\n");
514  printf("\n");
515  printf("  -m --mode monitor           Monitot mode\n");
516  printf("    stanza                    Stanza Monitor\n");
517  printf("    monitor                   microblog Monitor microblog (XEP-0277)\n");
518  printf("\n");
519  printf("  -m --mode bookmark          Bookmark mode (XEP-0048)\n");
520  printf("    list                      List bookmarks\n");
521  printf("\n");
522  printf("  -m --mode mam               Message Archive Management (XEP-0313)\n");
523  printf("    list <jid>                List messages from <jid>\n");
524  printf("\n");
525  printf("  -m --mode discovery         Service Discovery (XEP-0030)\n");
526  printf("    info <jid>                info request for <jid>\n");
527  printf("    item <jid>                item request for <jid>\n");
528  printf("\n");
529  printf("\n");
530  printf("Examples:\n");
531  printf("  Usage: xmppc --jid user@domain.tld --pwd \"secret\" --mode roster list\n");
532  printf("  Usage: xmppc --jid user@domain.tld --pwd \"secret\" --mode pgp chat friend@domain.tld \"Hello\"\n");
533  printf("  Usage: xmppc -a account1 --mode discovery item conference@domain.tld\n");
534  printf("  Usage: xmppc --mode bookmark list\n");
535}