Skip to content

Instantly share code, notes, and snippets.

@waffle2k
Created December 11, 2009 15:29
Show Gist options
  • Save waffle2k/254271 to your computer and use it in GitHub Desktop.
Save waffle2k/254271 to your computer and use it in GitHub Desktop.
#include<opendkim/dkim.h>
#include<assert.h>
#include<stdio.h>
#include<string.h>
#include<memory.h>
#include<malloc.h>
// Test the init
static void *init() {
return (dkim_init(NULL, NULL));
}
static const char *getidentity(DKIM * dk, DKIM_SIGINFO * sig) {
char i[255];
DKIM_STAT rc;
memset( i, 0 , 255 );
rc = dkim_sig_getidentity(dk, sig, i, 255);
printf("dkim_sig_getidentity: %s\n", dkim_getresultstr(rc));
return ((const char *)strdup(i));
}
static char *getdomain(DKIM * dk) {
const char *d = dkim_getdomain(dk);
return (d ? strdup(d) : strdup(""));
}
static int verify(char *message, size_t len, DKIM_LIB * lib) {
char *domain_str;
bool b;
DKIM_STAT rc;
DKIM *dk = dkim_verify(lib, (const char *)"test", NULL, &rc);
printf("dkim_verify: %s\n", dkim_getresultstr(rc));
// Send the whole message
rc = dkim_chunk(dk, (unsigned char *)message, len);
printf("dkim_chunk: %s\n", dkim_getresultstr(rc));
if (DKIM_STAT_NOSIG == rc)
return (DKIM_STAT_NOSIG);
// Send the terminating chung
rc = dkim_chunk(dk, NULL, 0);
printf("dkim_chunk: %s\n", dkim_getresultstr(rc));
// Send the EOM signal
rc = dkim_eom(dk, NULL);
printf("dkim_eom: %s\n", dkim_getresultstr(rc));
if( rc != DKIM_STAT_OK )
return( rc );
DKIM_SIGINFO *sig = dkim_getsignature(dk);
if (!sig) {
printf("No policy signature\n");
} else {
printf("Signature Domain (d=): %s\n", dkim_sig_getdomain(sig));
}
char *identity = getidentity( dk, sig );
printf("dkim identity (i=): %s\n", identity);
if( identity )
free( identity );
rc = dkim_sig_process( dk, sig );
printf("dkim_sig_process: %s\n", dkim_getresultstr(rc));
return (0);
}
int main(int argc, char **argv) {
DKIM_LIB *dk = init(); // library object
DKIM_STAT rc; // return code
assert(dk != NULL);
if (argc < 2) {
fprintf(stderr, "You must supply a test message as a param\n");
return (2);
}
// Find the length of the file
FILE *fp = fopen(argv[1], "r");
if (!fp) {
fprintf(stderr, "Could not open /tmp/test.message\n");
return (1);
}
// Get the size etc, of the file
fseek(fp, 0L, SEEK_END);
size_t len = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char *message = malloc(len + 1);
memset(message, 0, len + 1);
fread(message, len, 1, fp);
fclose(fp);
if ((rc = verify(message, len - 1, dk)) != DKIM_STAT_OK) {
fprintf(stderr, "%s\n", dkim_getresultstr(rc));
}
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment