mixmaster

mixmaster 3.0 patched for libressl
git clone git://parazyd.org/mixmaster.git
Log | Files | Refs | README

mix3.h (13271B)


      1 /* Mixmaster version 3.0  --  (C) 1999 - 2006 Anonymizer Inc. and others.
      2 
      3    Mixmaster may be redistributed and modified under certain conditions.
      4    This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
      5    ANY KIND, either express or implied. See the file COPYRIGHT for
      6    details.
      7 
      8    Function prototypes
      9    $Id: mix3.h 934 2006-06-24 13:40:39Z rabbi $ */
     10 
     11 
     12 #ifndef _MIX3_H
     13 #define _MIX3_H
     14 #define COPYRIGHT "Copyright Anonymizer Inc. et al."
     15 
     16 #include "config.h"
     17 #include "mix.h"
     18 
     19 #ifdef WIN32
     20 #ifndef USE_SOCK
     21 #define _WINSOCKAPI_		/* don't include winsock */
     22 #endif /* not USE_SOCK */
     23 #include <windows.h>
     24 #ifdef _MSC
     25 #define snprintf _snprintf
     26 #endif /* _MSC */
     27 #define DIRSEP '\\'
     28 #define DIRSEPSTR "\\"
     29 #else /* end of WIN32 */
     30 #define DIRSEP '/'
     31 #define DIRSEPSTR "/"
     32 #endif /* else if not WIN32 */
     33 
     34 #define NOT_IMPLEMENTED {printf("Function not implemented.\n");return -1;}
     35 #define SECONDSPERDAY 86400
     36 
     37 #include <time.h>
     38 
     39 /* Dynamically allocated buffers */
     40 
     41 int buf_reset(BUFFER *buffer);
     42 int buf_clear(BUFFER *buffer);
     43 int buf_append(BUFFER *buffer, byte *mess, int len);
     44 int buf_cat(BUFFER *to, BUFFER *from);
     45 int buf_set(BUFFER *to, BUFFER *from);
     46 int buf_rest(BUFFER *to, BUFFER *from);
     47 int buf_appendrnd(BUFFER *to, int n);
     48 int buf_appendzero(BUFFER *to, int n);
     49 int buf_setc(BUFFER *buf, byte c);
     50 int buf_appendc(BUFFER *to, byte b);
     51 int buf_setrnd(BUFFER *b, int n);
     52 int buf_setf(BUFFER *buffer, char *fmt, ...);
     53 int buf_appendf(BUFFER *buffer, char *fmt, ...);
     54 int buf_sets(BUFFER *buf, char *s);
     55 int buf_appends(BUFFER *buffer, char *s);
     56 int buf_nl(BUFFER *buffer);
     57 int buf_pad(BUFFER *buffer, int size);
     58 int buf_prepare(BUFFER *buffer, int size);
     59 int buf_rewind(BUFFER *buffer);
     60 int buf_getc(BUFFER *buffer);
     61 void buf_ungetc(BUFFER *buffer);
     62 int buf_get(BUFFER *buffer, BUFFER *to, int n);
     63 int buf_getline(BUFFER *buffer, BUFFER *line);
     64 int buf_chop(BUFFER *b);
     65 void buf_move(BUFFER *dest, BUFFER *src);
     66 byte *buf_data(BUFFER *buffer);
     67 int buf_isheader(BUFFER *buffer);
     68 int buf_getheader(BUFFER *buffer, BUFFER *field, BUFFER *content);
     69 int buf_appendheader(BUFFER *buffer, BUFFER *field, BUFFER *contents);
     70 int buf_lookahead(BUFFER *buffer, BUFFER *line);
     71 int buf_eq(BUFFER *b1, BUFFER *b2);
     72 int buf_ieq(BUFFER *b1, BUFFER *b2);
     73 void buf_cut_out(BUFFER *buffer, BUFFER *cut_out, BUFFER *rest,
     74 		 int from, int len);
     75 
     76 int buf_appendl(BUFFER *b, long l);
     77 int buf_appendl_lo(BUFFER *b, long l);
     78 long buf_getl(BUFFER *b);
     79 long buf_getl_lo(BUFFER *b);
     80 int buf_appendi(BUFFER *b, int i);
     81 int buf_appendi_lo(BUFFER *b, int i);
     82 int buf_geti(BUFFER *b);
     83 int buf_geti_lo(BUFFER *b);
     84 
     85 /* String comparison */
     86 int strieq(const char *s1, const char *s2);
     87 int strileft(const char *string, const char *keyword);
     88 int striright(const char *string, const char *keyword);
     89 int strifind(const char *string, const char *keyword);
     90 
     91 int streq(const char *s1, const char *s2);
     92 int strfind(const char *string, const char *keyword);
     93 int strleft(const char *string, const char *keyword);
     94 
     95 void strcatn(char *dest, const char *src, int n);
     96 
     97 int bufleft(BUFFER *b, char *k);
     98 int buffind(BUFFER *b, char *k);
     99 int bufeq(BUFFER *b, char *k);
    100 
    101 int bufileft(BUFFER *b, char *k);
    102 int bufifind(BUFFER *b, char *k);
    103 int bufiright(BUFFER *b, char *k);
    104 int bufieq(BUFFER *b, char *k);
    105 
    106 /* Utility functions */
    107 void whoami(char *addr, char *defaultname);
    108 int sendinfofile(char *name, char *log, BUFFER *address, BUFFER *subject);
    109 int stats(BUFFER *out);
    110 int conf(BUFFER *out);
    111 void conf_premail(BUFFER *out);
    112 
    113 void rfc822_addr(BUFFER *line, BUFFER *list);
    114 void rfc822_name(BUFFER *line, BUFFER *name);
    115 void sendmail_begin(void);	/* begin mail sending session */
    116 void sendmail_end(void);	/* end mail sending session */
    117 int sendmail_loop(BUFFER *message, char *from, BUFFER *address);
    118 int sendmail(BUFFER *message, char *from, BUFFER *address);
    119 int mixfile(char *path, const char *name);
    120 int file_to_out(const char *name);
    121 FILE *mix_openfile(const char *name, const char *a);
    122 FILE *openpipe(const char *prog);
    123 int closepipe(FILE *fp);
    124 int maildirWrite(char *maildir, BUFFER *message, int create);
    125 int write_pidfile(char *pidfile);
    126 int clear_pidfile(char *pidfile);
    127 time_t parse_yearmonthday(char* str);
    128 
    129 int url_download(char* url, char* dest);
    130 int download_stats(char *sourcename);
    131 
    132 typedef struct {
    133   char *name;
    134   FILE *f;
    135 } LOCK;
    136 
    137 int lock(FILE *f);
    138 int unlock(FILE *f);
    139 LOCK *lockfile(char *filename);
    140 int unlockfile(LOCK *lock);
    141 
    142 int filtermsg(BUFFER *msg);
    143 BUFFER *readdestblk( );
    144 int doblock(BUFFER *line, BUFFER *filter, int logandreset);
    145 int doallow(BUFFER *line, BUFFER *filter);
    146 int allowmessage(BUFFER *in);
    147 
    148 void errlog(int type, char *format,...);
    149 void clienterr(BUFFER *msgbuf, char *err);
    150 void logmail(char *mailbox, BUFFER *message);
    151 
    152 void mix_status(char *fmt,...);
    153 void mix_genericerror(void);
    154 
    155 #define ERRORMSG 1
    156 #define WARNING 2
    157 #define NOTICE 3
    158 #define LOG 4
    159 #define DEBUGINFO 5
    160 
    161 int decode(BUFFER *in, BUFFER *out);
    162 int encode(BUFFER *b, int linelen);
    163 void id_encode(byte id[], byte *s);
    164 void id_decode(byte *s, byte id[]);
    165 
    166 int decode_header(BUFFER *content);
    167 int boundary(BUFFER *line, BUFFER *mboundary);
    168 void get_parameter(BUFFER *content, char *attribute, BUFFER *value);
    169 int get_type(BUFFER *content, BUFFER *type, BUFFER *subtype);
    170 int mail_encode(BUFFER *in, int encoding);
    171 int hdr_encode(BUFFER *in, int n);
    172 int attachfile(BUFFER *message, BUFFER *filename);
    173 int pgpmime_sign(BUFFER *message, BUFFER *uid, BUFFER *pass, char *secring);
    174 int mime_attach(BUFFER *message, BUFFER *attachment, BUFFER *type);
    175 void mimedecode(BUFFER *msg);
    176 int qp_decode_message(BUFFER *msg);
    177 
    178 #define MIME_8BIT 1   /* transport is 8bit */
    179 #define MIME_7BIT 2   /* transport is 7bit */
    180 
    181 /* randomness */
    182 int rnd_bytes(byte *b, int n);
    183 byte rnd_byte(void);
    184 int rnd_number(int n);
    185 int rnd_add(byte *b, int l);
    186 int rnd_seed(void);
    187 void rnd_time(void);
    188 
    189 int rnd_init(void);
    190 int rnd_final(void);
    191 void rnd_error(void);
    192 
    193 #define RND_QUERY 0
    194 #define RND_NOTSEEDED -1
    195 #define RND_SEEDED 1
    196 #define RND_WILLSEED 2
    197 extern int rnd_state; /* flag for PRNG status */
    198 
    199 /* compression */
    200 int buf_compress(BUFFER *b);
    201 int buf_zip(BUFFER *out, BUFFER *in, int bits);
    202 int buf_uncompress(BUFFER *b);
    203 int buf_unzip(BUFFER *b, int type);
    204 
    205 /* crypto functions */
    206 int digest_md5(BUFFER *b, BUFFER *md);
    207 int isdigest_md5(BUFFER *b, BUFFER *md);
    208 int digestmem_md5(byte *b, int n, BUFFER *md);
    209 int digest_sha1(BUFFER *b, BUFFER *md);
    210 int digest_rmd160(BUFFER *b, BUFFER *md);
    211 
    212 #define KEY_ID_LEN 32
    213 int keymgt(int force);
    214 int key(BUFFER *b);
    215 int adminkey(BUFFER *b);
    216 
    217 #define ENCRYPT 1
    218 #define DECRYPT 0
    219 int buf_crypt(BUFFER *b, BUFFER *key, BUFFER *iv, int enc);
    220 
    221 #ifdef USE_IDEA
    222 int buf_ideacrypt(BUFFER *b, BUFFER *key, BUFFER *iv, int enc);
    223 #endif /* USE_IDEA */
    224 int buf_bfcrypt(BUFFER *b, BUFFER *key, BUFFER *iv, int enc);
    225 int buf_3descrypt(BUFFER *b, BUFFER *key, BUFFER *iv, int enc);
    226 int buf_castcrypt(BUFFER *b, BUFFER *key, BUFFER *iv, int enc);
    227 #ifdef USE_AES
    228 int buf_aescrypt(BUFFER *b, BUFFER *key, BUFFER *iv, int enc);
    229 #endif /* USE_AES */
    230 
    231 int db_getseckey(byte keyid[], BUFFER *key);
    232 int db_getpubkey(byte keyid[], BUFFER *key);
    233 int pk_decrypt(BUFFER *encrypted, BUFFER *privkey);
    234 int pk_encrypt(BUFFER *plaintext, BUFFER *privkey);
    235 int check_seckey(BUFFER *buf, const byte id[]);
    236 int check_pubkey(BUFFER *buf, const byte id[]);
    237 int v2createkey(void);
    238 int getv2seckey(byte keyid[], BUFFER *key);
    239 int seckeytopub(BUFFER *pub, BUFFER *sec, byte keyid[]);
    240 
    241 /* configuration, general remailer functions */
    242 int mix_configline(char *line);
    243 int mix_config(void);
    244 int mix_initialized(void);
    245 int mix_daily(void);
    246 
    247 /* message pool */
    248 #define INTERMEDIATE 0
    249 int pool_send(void);
    250 int pool_read(BUFFER *pool);
    251 int pool_add(BUFFER *msg, char *type);
    252 FILE *pool_new(char *type, char *tmpname, char *path);
    253 int mix_pool(BUFFER *msg, int type, long latent);
    254 int pool_packetfile(char *fname, BUFFER *mid, int packetnum);
    255 void pool_packetexp(void);
    256 int idexp(void);
    257 int pgpmaxexp(void);
    258 void pop3get(void);
    259 
    260 typedef struct {  /* added for binary id.log change */
    261   char id[16];
    262   long time;
    263 } idlog_t;
    264 
    265 /* statistics */
    266 int stats_log(int);
    267 int stats_out(int);
    268 
    269 /* OpenPGP */
    270 #define PGP_ARMOR_NORMAL        0
    271 #define PGP_ARMOR_REM           1
    272 #define PGP_ARMOR_KEY           2
    273 #define PGP_ARMOR_NYMKEY        3
    274 #define PGP_ARMOR_NYMSIG        4
    275 #define PGP_ARMOR_SECKEY        5
    276 
    277 #define PGP_TYPE_UNDEFINED	0
    278 #define PGP_TYPE_PRIVATE	1
    279 #define PGP_TYPE_PUBLIC		2
    280 
    281 int pgp_keymgt(int force);
    282 int pgp_latestkeys(BUFFER* outtxt, int algo);
    283 int pgp_armor(BUFFER *buf, int mode);
    284 int pgp_dearmor(BUFFER *buf, BUFFER *out);
    285 int pgp_pubkeycert(BUFFER *userid, char *keyring, BUFFER *pass,
    286 		   BUFFER *out, int remail);
    287 int pgp_signtxt(BUFFER *msg, BUFFER *uid, BUFFER *pass,
    288 		char *secring, int remail);
    289 int pgp_isconventional(BUFFER *buf);
    290 int pgp_mailenc(int mode, BUFFER *msg, char *sigid,
    291 		BUFFER *pass, char *pubring, char *secring);
    292 int pgp_signhashalgo(BUFFER *algo, BUFFER *userid, char *secring,
    293 		     BUFFER *pass);
    294 
    295 /* menu */
    296 int menu_initialized;
    297 void menu_main(void);
    298 void menu_folder(char command, char *name);
    299 int menu_getuserpass(BUFFER *p, int mode);
    300 
    301 int user_pass(BUFFER *b);
    302 int user_confirmpass(BUFFER *b);
    303 void user_delpass(void);
    304 
    305 /* remailer */
    306 typedef struct {
    307   char name[20];
    308   int version;
    309   char addr[128];
    310   byte keyid[16];
    311   struct {
    312     unsigned int mix:1;
    313     unsigned int compress:1;
    314 
    315     unsigned int cpunk:1;
    316     unsigned int pgp:1;
    317     unsigned int pgponly:1;
    318     unsigned int latent:1;
    319     unsigned int hash:1;
    320     unsigned int ek:1;
    321     unsigned int esub:1;
    322 
    323     unsigned int nym:1;
    324     unsigned int newnym:1;
    325 
    326     unsigned int post:1;
    327     unsigned int middle:1;
    328 
    329     unsigned int star_ex:1;
    330   } flags;
    331   struct rinfo {
    332     int reliability;
    333     int latency;
    334     char history[13];
    335   } info[2];
    336 } REMAILER;
    337 
    338 #define CHAINMAX 421
    339 #define MAXREM 100
    340 int prepare_type2list(BUFFER *out);
    341 int mix2_rlist(REMAILER remailer[], int badchains[MAXREM][MAXREM]);
    342 int t1_rlist(REMAILER remailer[], int badchains[MAXREM][MAXREM]);
    343 int pgp_rlist(REMAILER remailer[], int n);
    344 int pgp_rkeylist(REMAILER remailer[], int keyid[], int n);
    345 void parse_badchains(int badchains[MAXREM][MAXREM], char *file, char *startindicator, REMAILER *remailer, int maxrem);
    346 int chain_select(int hop[], char *chainstr, int maxrem, REMAILER *remailer,
    347 		 int type, BUFFER *feedback);
    348 int chain_rand(REMAILER *remailer, int badchains[MAXREM][MAXREM], int maxrem,
    349 	       int thischain[], int chainlen, int t, int ignore_constraints_if_necessary);
    350 int chain_randfinal(int type, REMAILER *remailer, int badchains[MAXREM][MAXREM],
    351 	       int maxrem, int rtype, int chain[], int chainlen, int ignore_constraints_if_necessary);
    352 
    353 float chain_reliability(char *chain, int chaintype,
    354 			char *reliability_string);
    355 int redirect_message(BUFFER *sendmsg, char *chain, int numcopies, BUFFER *chainlist);
    356 int mix2_encrypt(int type, BUFFER *message, char *chainstr, int numcopies,
    357 		int ignore_constraints_if_necessary, BUFFER *feedback);
    358 int t1_encrypt(int type, BUFFER *message, char *chainstr, int latency,
    359 	       BUFFER *ek, BUFFER *feedback);
    360 
    361 int t1_getreply(BUFFER *msg, BUFFER *ek, int len);
    362 
    363 int t1_decrypt(BUFFER *in);
    364 int t2_decrypt(BUFFER *in);
    365 
    366 int mix2_decrypt(BUFFER *m);
    367 int v2body(BUFFER *body);
    368 int v2body_setlen(BUFFER *body);
    369 int v2partial(BUFFER *body, BUFFER *mid, int packet, int numpackets);
    370 int v2_merge(BUFFER *mid);
    371 int mix_armor(BUFFER *in);
    372 int mix_dearmor(BUFFER *armored, BUFFER *bin);
    373 
    374 /* type 1 */
    375 #define HDRMARK "::"
    376 #define EKMARK "**"
    377 #define HASHMARK "##"
    378 int isline(BUFFER *line, char *text);
    379 
    380 /* nym database */
    381 
    382 #define NYM_WAITING 0
    383 #define NYM_OK 1
    384 #define NYM_DELETED 2
    385 #define NYM_ANY -1
    386 
    387 int nymlist_read(BUFFER *n);
    388 int nymlist_write(BUFFER *list);
    389 int nymlist_get(BUFFER *list, char *nym, BUFFER *config, BUFFER *ek,
    390 		BUFFER *options, BUFFER *name, BUFFER *rblocks, int *status);
    391 int nymlist_append(BUFFER *list, char *nym, BUFFER *config, BUFFER *options,
    392 		   BUFFER *name, BUFFER *chains, BUFFER *eklist, int status);
    393 int nymlist_del(BUFFER *list, char *nym);
    394 int nymlist_getnym(char *nym, BUFFER *config, BUFFER *ek, BUFFER *opt,
    395 		   BUFFER *name, BUFFER *rblocks);
    396 int nymlist_getstatus(char *nym);
    397 
    398 /* Visual C lacks dirent */
    399 #ifdef _MSC
    400 typedef HANDLE DIR;
    401 
    402 struct dirent {
    403   char d_name[PATHMAX];
    404 };
    405 
    406 DIR *opendir(const char *name);
    407 struct dirent *readdir(DIR *dir);
    408 int closedir(DIR *dir);
    409 #endif /* _MSC */
    410 
    411 /* sockets */
    412 #if defined(WIN32) && defined(USE_SOCK)
    413 #include <winsock.h>
    414 int sock_init(void);
    415 void sock_exit(void);
    416 
    417 #else /* end of defined(WIN32) && defined(USE_SOCK) */
    418 typedef int SOCKET;
    419 
    420 #define INVALID_SOCKET -1
    421 SOCKET opensocket(char *hostname, int port);
    422 int closesocket(SOCKET s);
    423 
    424 #endif /* else if not defined(WIN32) && defined(USE_SOCK) */
    425 
    426 #ifdef WIN32
    427 int is_nt_service(void);
    428 void set_nt_exit_event();
    429 #endif /* WIN32 */
    430 
    431 /* check for memory leaks */
    432 #ifdef DEBUG
    433 #define malloc mix3_malloc
    434 #define free mix3_free
    435 BUFFER *mix3_bufnew(char *, int, char*);
    436 #if __GNUC__ >= 2
    437 # define buf_new() mix3_bufnew(__FILE__, __LINE__, __PRETTY_FUNCTION__)
    438 #else /* end of __GNUC__ >= 2 */
    439 # define buf_new() mix3_bufnew(__FILE__, __LINE__, "file")
    440 #endif /* else if not __GNUC__ >= 2 */
    441 #endif /* DEBUG */
    442 
    443 #endif /* not _MIX3_H */