#include #include #include #include #include #include #include #include #include #include #define BUFSZ 65536 #define NS "urn:ietf:params:xml:ns:epp-1.0" #define DNS "urn:ietf:params:xml:ns:domain-1.0" #define CNS "urn:ietf:params:xml:ns:contact-1.0" #define HNS "urn:ietf:params:xml:ns:host-1.0" static const char*host = "epp-test.switch.ch"; static const char*port = "7001"; static const char*servername = "epp-test.switch.ch"; static const char*clid = "TEST-REGISTRAR-A"; static const char*pass = ""; static const char*newpass = ""; static const char*cacert = "cacert.pem"; static const char*certfile = "client.crt"; static const char*keyfile = "client.key"; static int insecure = 1; static int cafile_set = 0; static int certfile_set = 0; struct conn { SSL*ssl; int fd; char buf[BUFSZ]; int len; }; static void die(const char*msg) { fprintf(stderr, "epp: %s\n", msg); exit(1); } static void usage(void) { fprintf(stderr, "usage: epp [-h host] [-p port] [-n sni] [-c clid] [-P pass] [-N newpass]\n" " [-a cafile] [-C cert] [-K key] [-k] cmd args...\n" "cmds:\n" " login login (with optional newpass via -N)\n" " logout\n" " hello\n" " poll poll req\n" " pollack poll ack\n" " check domain check\n" " info domain info (authed for managing registrar)\n" " create [tech]\n" " transfer \n" " renew \n" " update [addns ...] [remns ...] [addtech ...] [remtech ...] [reg CONTACT] [auth CODE]\n" " delete domain delete (to redemption)\n" " restore rgp restore request\n" " ccheck contact check\n" " cinfo contact info\n" " ccreate \n" " cdelete contact delete\n" " cupdate [org][,street][,city][,pc] contact update (comma, empty leaves)\n" " hcheck host check\n" " hcreate host create\n" " hdelete host delete\n" "env: EPP_HOST EPP_PORT EPP_CLID EPP_PASS override defaults\n"); exit(1); } static SSL_CTX* sslctx(void) { SSL_CTX*ctx; int r; ctx = SSL_CTX_new(TLS_client_method()); if(ctx == 0) die("SSL_CTX_new"); SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); SSL_CTX_set_verify(ctx, insecure ? SSL_VERIFY_NONE : SSL_VERIFY_PEER, 0); if(!insecure){ if(cafile_set && SSL_CTX_load_verify_locations(ctx, cacert, 0) != 1) die("load cafile"); } if(certfile_set){ r = SSL_CTX_use_certificate_chain_file(ctx, certfile); if(r != 1) die("load client cert"); r = SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM); if(r != 1) die("load client key"); } return ctx; } static int connect_server(void) { struct addrinfo hints, *res, *rp; char portbuf[16]; int r; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; snprintf(portbuf, sizeof(portbuf), "%s", port); r = getaddrinfo(host, portbuf, &hints, &res); if(r != 0) die(gai_strerror(r)); for(rp = res; rp; rp = rp->ai_next){ int fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if(fd < 0) continue; if(connect(fd, rp->ai_addr, rp->ai_addrlen) == 0){ freeaddrinfo(res); return fd; } close(fd); } freeaddrinfo(res); die("connect"); return -1; } static struct conn* tls_connect(SSL_CTX*ctx) { struct conn*c; int fd, r; fd = connect_server(); c = malloc(sizeof(*c)); if(c == 0) die("malloc"); c->ssl = SSL_new(ctx); if(c->ssl == 0) die("SSL_new"); c->fd = fd; r = SSL_set_fd(c->ssl, fd); if(r != 1) die("SSL_set_fd"); SSL_set_tlsext_host_name(c->ssl, servername); r = SSL_connect(c->ssl); if(r != 1) die("SSL_connect"); return c; } static void sendall(SSL*ssl, const char*buf, int n) { int off = 0; while(off < n){ int w; w = SSL_write(ssl, buf+off, n-off); if(w <= 0) die("ssl send"); off += w; } } static void sendframe(SSL*ssl, const char*xml) { char head[4]; int n = (int)strlen(xml); head[0] = (char)((n>>24)&0xff); head[1] = (char)((n>>16)&0xff); head[2] = (char)((n>>8)&0xff); head[3] = (char)(n&0xff); sendall(ssl, head, 4); sendall(ssl, xml, n); } static void recvframe(struct conn*c, char*out, int outsz) { char head[4]; int n; size_t got = 0; memset(head, 0, 4); while(got < 4){ size_t r; r = SSL_read_ex(c->ssl, head+got, 4-got, &got); if(r != 1) die("ssl recv head"); } n = ((unsigned char)head[0]<<24)|((unsigned char)head[1]<<16) | ((unsigned char)head[2]<<8)|((unsigned char)head[3]); if(n < 0 || n >= outsz-1) die("frame too large"); got = 0; while(got < (size_t)n){ size_t r; r = SSL_read_ex(c->ssl, out+got, (size_t)n-got, &got); if(r != 1) die("ssl recv body"); } out[n] = 0; } static void printresp(const char*xml) { printf("%s\n", xml); } int main(int argc, char**argv) { int i; char*cmd; SSL_CTX*ctx; struct conn*c; char xml[BUFSZ]; char resp[BUFSZ]; int n; if(argc < 2) usage(); for(i = 1; i < argc; i++){ if(strcmp(argv[i], "-h") == 0 && i+1 < argc){ host = argv[++i]; continue; } if(strcmp(argv[i], "-p") == 0 && i+1 < argc){ port = argv[++i]; continue; } if(strcmp(argv[i], "-n") == 0 && i+1 < argc){ servername = argv[++i]; continue; } if(strcmp(argv[i], "-c") == 0 && i+1 < argc){ clid = argv[++i]; continue; } if(strcmp(argv[i], "-P") == 0 && i+1 < argc){ pass = argv[++i]; continue; } if(strcmp(argv[i], "-N") == 0 && i+1 < argc){ newpass = argv[++i]; continue; } if(strcmp(argv[i], "-a") == 0 && i+1 < argc){ cacert = argv[++i]; cafile_set = 1; continue; } if(strcmp(argv[i], "-C") == 0 && i+1 < argc){ certfile = argv[++i]; certfile_set = 1; continue; } if(strcmp(argv[i], "-K") == 0 && i+1 < argc){ keyfile = argv[++i]; continue; } if(strcmp(argv[i], "-k") == 0){ insecure = 1; continue; } if(strcmp(argv[i], "--verify") == 0){ insecure = 0; continue; } break; } if(i >= argc) usage(); cmd = argv[i++]; ctx = sslctx(); c = tls_connect(ctx); if(strcmp(cmd, "login") == 0){ n = snprintf(xml, sizeof(xml), "" "" "%s%s", NS, clid, pass); if(newpass[0]) n += snprintf(xml+n, sizeof(xml)-n, "%s", newpass); n += snprintf(xml+n, sizeof(xml)-n, "1.0en" "" "%s%s%s" "urn:ietf:params:xml:ns:secDNS-1.1" "urn:ietf:params:xml:ns:rgp-1.0" "urn:ietf:params:xml:ns:changePoll-1.0" "https://www.nic.ch/epp/balance-1.0" "" "LOGIN.1", DNS, CNS, HNS); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "logout") == 0){ n = snprintf(xml, sizeof(xml), "" "LOGOUT.1", NS); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "hello") == 0){ n = snprintf(xml, sizeof(xml), "" "", NS); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "poll") == 0){ n = snprintf(xml, sizeof(xml), "" "POLL.1", NS); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "pollack") == 0 && i < argc){ const char*id = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "ACK.1", NS, id); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "check") == 0 && i < argc){ const char*name = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "%s" "CHK.1", NS, DNS, name); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "info") == 0 && i < argc){ const char*name = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "%s" "INFO.1", NS, DNS, name); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "transfer") == 0 && i+1 < argc){ const char*name = argv[i++]; const char*code = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "" "%s" "%s" "XFER.1", NS, DNS, name, code); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "create") == 0 && i+2 < argc){ const char*name = argv[i++]; const char*hosts = argv[i++]; const char*reg = argv[i++]; const char*tech = 0; if(i < argc) tech = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "" "%s" "%s" "%s", NS, DNS, name, hosts, reg); if(tech) n += snprintf(xml+n, sizeof(xml)-n, "%s", tech); n += snprintf(xml+n, sizeof(xml)-n, "" "CRE.1"); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "renew") == 0 && i+2 < argc){ const char*name = argv[i++]; const char*cur = argv[i++]; const char*years = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "" "%s" "%s" "%s" "RNW.1", NS, DNS, name, cur, years); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "update") == 0 && i < argc){ const char*name = argv[i++]; char addns[512] = ""; char remns[512] = ""; char addtech[512] = ""; char remtech[512] = ""; char reg[256] = ""; char auth[256] = ""; for(; i < argc; i++){ if(strcmp(argv[i], "addns") == 0 && i+1 < argc){ snprintf(addns+strlen(addns), sizeof(addns)-strlen(addns), "%s", argv[++i]); } else if(strcmp(argv[i], "remns") == 0 && i+1 < argc){ snprintf(remns+strlen(remns), sizeof(remns)-strlen(remns), "%s", argv[++i]); } else if(strcmp(argv[i], "addtech") == 0 && i+1 < argc){ snprintf(addtech+strlen(addtech), sizeof(addtech)-strlen(addtech), "%s", argv[++i]); } else if(strcmp(argv[i], "remtech") == 0 && i+1 < argc){ snprintf(remtech+strlen(remtech), sizeof(remtech)-strlen(remtech), "%s", argv[++i]); } else if(strcmp(argv[i], "reg") == 0 && i+1 < argc){ snprintf(reg, sizeof(reg), "%s", argv[++i]); } else if(strcmp(argv[i], "auth") == 0 && i+1 < argc){ snprintf(auth, sizeof(auth), "%s", argv[++i]); } else if(strcmp(argv[i], "reg") != 0 && strcmp(argv[i], "auth") != 0) usage(); } if(!addns[0] && !remns[0] && !addtech[0] && !remtech[0] && !reg[0] && !auth[0]) usage(); n = snprintf(xml, sizeof(xml), "" "" "" "%s", NS, DNS, name); if(addns[0] || addtech[0]) n += snprintf(xml+n, sizeof(xml)-n, "%s%s", addns, addtech); if(remns[0] || remtech[0]) n += snprintf(xml+n, sizeof(xml)-n, "%s%s", remns, remtech); if(reg[0] || auth[0]) n += snprintf(xml+n, sizeof(xml)-n, "%s%s", reg, auth); n += snprintf(xml+n, sizeof(xml)-n, "UPD.1"); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "delete") == 0 && i < argc){ const char*name = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "%s" "DEL.1", NS, DNS, name); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "restore") == 0 && i < argc){ const char*name = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "%s" "" "RGP.1", NS, DNS, name); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "ccheck") == 0 && i < argc){ const char*id = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "%s" "CCHK.1", NS, CNS, id); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "cinfo") == 0 && i < argc){ const char*id = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "%s" "CINF.1", NS, CNS, id); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "ccreate") == 0 && i+8 < argc){ const char*id = argv[i++]; const char*name = argv[i++]; const char*org = argv[i++]; const char*street = argv[i++]; const char*city = argv[i++]; const char*pc = argv[i++]; const char*cc = argv[i++]; const char*voice = argv[i++]; const char*email = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "" "%s" "" "%s" "%s" "%s" "%s%s" "%s" "" "%s" "%s" "" "CCRE.1", NS, CNS, id, name, org, street, city, pc, cc, voice, email); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "cdelete") == 0 && i < argc){ const char*id = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "%s" "CDEL.1", NS, CNS, id); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "cupdate") == 0 && i+1 < argc){ const char*id = argv[i++]; const char*args = argv[i++]; char org[256] = ""; char street[256] = ""; char city[256] = ""; char pc[256] = ""; char *p, *parts[10]; int k = 0; char tmp[1024]; snprintf(tmp, sizeof(tmp), "%s", args); p = strtok(tmp, ","); while(p && k < 10){ parts[k++] = p; p = strtok(0, ","); } if(k > 0){ strncpy(org, parts[0], sizeof(org)-1); } if(k > 1){ strncpy(street, parts[1], sizeof(street)-1); } if(k > 2){ strncpy(city, parts[2], sizeof(city)-1); } if(k > 3){ strncpy(pc, parts[3], sizeof(pc)-1); } char chg[1024] = ""; if(org[0]) snprintf(chg+strlen(chg), sizeof(chg)-strlen(chg), "%s", org); if(street[0]) snprintf(chg+strlen(chg), sizeof(chg)-strlen(chg), "%s", street); if(city[0]) snprintf(chg+strlen(chg), sizeof(chg)-strlen(chg), "%s", city); if(pc[0]) snprintf(chg+strlen(chg), sizeof(chg)-strlen(chg), "%s", pc); n = snprintf(xml, sizeof(xml), "" "" "" "%s%s" "CUPD.1", NS, CNS, id, chg); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "hcheck") == 0 && i < argc){ const char*name = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "%s" "HCHK.1", NS, HNS, name); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "hcreate") == 0 && i+1 < argc){ const char*name = argv[i++]; const char*ip = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "" "%s" "%s" "HCRE.1", NS, HNS, name, ip); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else if(strcmp(cmd, "hdelete") == 0 && i < argc){ const char*name = argv[i++]; n = snprintf(xml, sizeof(xml), "" "" "%s" "HDEL.1", NS, HNS, name); sendframe(c->ssl, xml); recvframe(c, resp, sizeof(resp)); printresp(resp); } else{ fprintf(stderr, "epp: unknown command '%s'\n", cmd); usage(); } SSL_shutdown(c->ssl); SSL_free(c->ssl); close(c->fd); free(c); return 0; }