/* * Initialise the parser. */ extern void parse_init(void) { name_t entry; entry = name_lookup("false"); entry->val = term_boolean(make_boolean(false)); entry = name_lookup("inf"); entry->val = term_num(make_num(1.0/0.0)); entry = name_lookup("nil"); entry->val = term_nil(make_nil()); entry = name_lookup("true"); entry->val = term_boolean(make_boolean(true)); }
/* * Get an operator token. */ static token_t token_getop(context_t cxt) { char buf0[TOKEN_MAXLEN+1]; char *buf = buf0, *str = cxt->str; size_t len = 0; token_t token = TOKEN_ERROR; while (isop(*str)) { buf[len++] = *str++; if (len >= TOKEN_MAXLEN) return TOKEN_ERROR; buf[len] = '\0'; name_t entry = name_lookup(buf); if (entry != NULL) { token = entry->token; cxt->str = str; } else if (binop_lookup(cxt->opinfo, buf, NULL, NULL, NULL, NULL)) { token = TOKEN_OP; cxt->str = str; } } return token; }
void cmd_add(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (argc < 1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Tox ID or address required."); return; } const char *id = argv[1]; char msg[MAX_STR_SIZE]; if (argc > 1) { if (argv[2][0] != '\"') { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Message must be enclosed in quotes."); return; } /* remove opening and closing quotes */ char tmp[MAX_STR_SIZE]; snprintf(tmp, sizeof(tmp), "%s", &argv[2][1]); int len = strlen(tmp) - 1; tmp[len] = '\0'; snprintf(msg, sizeof(msg), "%s", tmp); } else { char selfname[TOX_MAX_NAME_LENGTH]; tox_self_get_name(m, (uint8_t *) selfname); size_t n_len = tox_self_get_name_size(m); selfname[n_len] = '\0'; snprintf(msg, sizeof(msg), "Hello, my name is %s. Care to Tox?", selfname); } char id_bin[TOX_ADDRESS_SIZE] = {0}; uint16_t id_len = (uint16_t) strlen(id); /* try to add tox ID */ if (id_len == 2 * TOX_ADDRESS_SIZE) { size_t i; char xx[3]; uint32_t x; for (i = 0; i < TOX_ADDRESS_SIZE; ++i) { xx[0] = id[2 * i]; xx[1] = id[2 * i + 1]; xx[2] = '\0'; if (sscanf(xx, "%02x", &x) != 1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid Tox ID."); return; } id_bin[i] = x; } cmd_add_helper(self, m, id_bin, msg); } else { /* assume id is a username@domain address and do http name server lookup */ name_lookup(self, m, id_bin, id, msg); } }
/* * Called to set up the raw connection. * * Returns an error message, or NULL on success. * * Also places the canonical host name into `realhost'. It must be * freed by the caller. */ static const char *raw_init(void *frontend_handle, void **backend_handle, Config *cfg, char *host, int port, char **realhost, int nodelay, int keepalive) { static const struct plug_function_table fn_table = { raw_log, raw_closing, raw_receive, raw_sent }; SockAddr addr; const char *err; Raw raw; raw = snew(struct raw_backend_data); raw->fn = &fn_table; raw->s = NULL; *backend_handle = raw; raw->frontend = frontend_handle; /* * Try to find host. */ { char *buf; buf = dupprintf("Looking up host \"%s\"%s", host, (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : ""))); logevent(raw->frontend, buf); sfree(buf); } addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily); if ((err = sk_addr_error(addr)) != NULL) { sk_addr_free(addr); return err; } if (port < 0) port = 23; /* default telnet port */ /* * Open socket. */ raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive, (Plug) raw, cfg); if ((err = sk_socket_error(raw->s)) != NULL) return err; return NULL; }
void $name_grow($name* a) { $name new_ht; new_ht.size = 0; new_ht.capacity = a->capacity << 1; new_ht.items = calloc(new_ht.capacity, sizeof(hashtbl_item)); for (unsigned i = 0; i < a->capacity; ++i) { if (a->items[i].state == VALID) { *$name_lookup(&new_ht, a->items[i].key) = a->items[i].value; } } free(a->items); *a = new_ht; }
isc_boolean_t dns_geoip_match(const isc_netaddr_t *reqaddr, isc_uint8_t *scope, const dns_geoip_databases_t *geoip, const dns_geoip_elem_t *elt) { #ifndef HAVE_GEOIP UNUSED(reqaddr); UNUSED(geoip); UNUSED(elt); return (ISC_FALSE); #else GeoIP *db; GeoIPRecord *record; GeoIPRegion *region; dns_geoip_subtype_t subtype; isc_uint32_t ipnum = 0; int maxlen = 0, id, family; const char *cs; char *s; #ifdef HAVE_GEOIP_V6 const geoipv6_t *ipnum6 = NULL; #else const void *ipnum6 = NULL; #endif INSIST(geoip != NULL); family = reqaddr->family; switch (family) { case AF_INET: ipnum = ntohl(reqaddr->type.in.s_addr); break; case AF_INET6: #ifdef HAVE_GEOIP_V6 ipnum6 = &reqaddr->type.in6; break; #else return (ISC_FALSE); #endif default: return (ISC_FALSE); } subtype = fix_subtype(reqaddr, geoip, elt->subtype); switch (subtype) { case dns_geoip_country_code: maxlen = 2; goto getcountry; case dns_geoip_country_code3: maxlen = 3; goto getcountry; case dns_geoip_country_name: maxlen = 255; getcountry: db = DB46(reqaddr, geoip, country); if (db == NULL) return (ISC_FALSE); INSIST(elt->as_string != NULL); cs = country_lookup(db, subtype, family, ipnum, ipnum6, scope); if (cs != NULL && strncasecmp(elt->as_string, cs, maxlen) == 0) return (ISC_TRUE); break; case dns_geoip_city_countrycode: case dns_geoip_city_countrycode3: case dns_geoip_city_countryname: case dns_geoip_city_region: case dns_geoip_city_regionname: case dns_geoip_city_name: case dns_geoip_city_postalcode: case dns_geoip_city_continentcode: case dns_geoip_city_timezonecode: INSIST(elt->as_string != NULL); db = DB46(reqaddr, geoip, city); if (db == NULL) return (ISC_FALSE); record = city_lookup(db, subtype, family, ipnum, ipnum6, scope); if (record == NULL) break; s = city_string(record, subtype, &maxlen); INSIST(maxlen != 0); if (s != NULL && strncasecmp(elt->as_string, s, maxlen) == 0) return (ISC_TRUE); break; case dns_geoip_city_metrocode: db = DB46(reqaddr, geoip, city); if (db == NULL) return (ISC_FALSE); record = city_lookup(db, subtype, family, ipnum, ipnum6, scope); if (record == NULL) break; if (elt->as_int == record->metro_code) return (ISC_TRUE); break; case dns_geoip_city_areacode: db = DB46(reqaddr, geoip, city); if (db == NULL) return (ISC_FALSE); record = city_lookup(db, subtype, family, ipnum, ipnum6, scope); if (record == NULL) break; if (elt->as_int == record->area_code) return (ISC_TRUE); break; case dns_geoip_region_countrycode: case dns_geoip_region_code: case dns_geoip_region_name: case dns_geoip_region: if (geoip->region == NULL) return (ISC_FALSE); INSIST(elt->as_string != NULL); /* Region DB is not supported for IPv6 */ if (family == AF_INET6) return (ISC_FALSE); region = region_lookup(geoip->region, subtype, ipnum, scope); if (region == NULL) break; s = region_string(region, subtype, &maxlen); INSIST(maxlen != 0); if (s != NULL && strncasecmp(elt->as_string, s, maxlen) == 0) return (ISC_TRUE); break; case dns_geoip_isp_name: db = geoip->isp; goto getname; case dns_geoip_org_name: db = geoip->org; goto getname; case dns_geoip_as_asnum: db = geoip->as; goto getname; case dns_geoip_domain_name: db = geoip->domain; getname: if (db == NULL) return (ISC_FALSE); INSIST(elt->as_string != NULL); /* ISP, Org, AS, and Domain are not supported for IPv6 */ if (family == AF_INET6) return (ISC_FALSE); s = name_lookup(db, subtype, ipnum, scope); if (s != NULL) { size_t l; if (strcasecmp(elt->as_string, s) == 0) return (ISC_TRUE); if (subtype != dns_geoip_as_asnum) break; /* * Just check if the ASNNNN value matches. */ l = strlen(elt->as_string); if (l > 0U && strchr(elt->as_string, ' ') == NULL && strncasecmp(elt->as_string, s, l) == 0 && s[l] == ' ') return (ISC_TRUE); } break; case dns_geoip_netspeed_id: INSIST(geoip->netspeed != NULL); /* Netspeed DB is not supported for IPv6 */ if (family == AF_INET6) return (ISC_FALSE); id = netspeed_lookup(geoip->netspeed, subtype, ipnum, scope); if (id == elt->as_int) return (ISC_TRUE); break; case dns_geoip_countrycode: case dns_geoip_countrycode3: case dns_geoip_countryname: case dns_geoip_regionname: /* * If these were not remapped by fix_subtype(), * the database was unavailable. Always return false. */ break; default: INSIST(0); } return (ISC_FALSE); #endif }
/* * Called to set up the rlogin connection. * * Returns an error message, or NULL on success. * * Also places the canonical host name into `realhost'. It must be * freed by the caller. */ static const char *rlogin_init(void *frontend_handle, void **backend_handle, Conf *conf, char *host, int port, char **realhost, int nodelay, int keepalive) { static const struct plug_function_table fn_table = { rlogin_log, rlogin_closing, rlogin_receive, rlogin_sent, NULL }; SockAddr addr; const char *err; Rlogin rlogin; char *ruser; int addressfamily; char *loghost; rlogin = snew(struct rlogin_tag); rlogin->fn = &fn_table; rlogin->s = NULL; rlogin->closed_on_socket_error = FALSE; rlogin->frontend = frontend_handle; rlogin->term_width = conf_get_int(conf, CONF_width); rlogin->term_height = conf_get_int(conf, CONF_height); rlogin->firstbyte = 1; rlogin->cansize = 0; rlogin->prompt = NULL; rlogin->conf = conf_copy(conf); *backend_handle = rlogin; addressfamily = conf_get_int(conf, CONF_addressfamily); /* * Try to find host. */ { char *buf; buf = dupprintf("Looking up host \"%s\"%s", host, (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : ""))); logevent(rlogin->frontend, buf); sfree(buf); } addr = name_lookup(host, port, realhost, conf, addressfamily); if ((err = sk_addr_error(addr)) != NULL) { sk_addr_free(addr); return err; } if (port < 0) port = 513; /* default rlogin port */ /* * Open socket. */ rlogin->s = new_connection(addr, *realhost, port, 1, 0, nodelay, keepalive, (Plug) rlogin, conf); if ((err = sk_socket_error(rlogin->s)) != NULL) return err; loghost = conf_get_str(conf, CONF_loghost); if (*loghost) { char *colon; sfree(*realhost); *realhost = dupstr(loghost); colon = strrchr(*realhost, ':'); if (colon) { /* * FIXME: if we ever update this aspect of ssh.c for * IPv6 literal management, this should change in line * with it. */ *colon++ = '\0'; } } /* * Send local username, remote username, terminal type and * terminal speed - unless we don't have the remote username yet, * in which case we prompt for it and may end up deferring doing * anything else until the local prompt mechanism returns. */ if ((ruser = get_remote_username(conf)) != NULL) { rlogin_startup(rlogin, ruser); sfree(ruser); } else { int ret; rlogin->prompt = new_prompts(rlogin->frontend); rlogin->prompt->to_server = TRUE; rlogin->prompt->name = dupstr("Rlogin login name"); add_prompt(rlogin->prompt, dupstr("rlogin username: "), TRUE); ret = get_userpass_input(rlogin->prompt, NULL, 0); if (ret >= 0) { rlogin_startup(rlogin, rlogin->prompt->prompts[0]->result); } } return NULL; }
struct X11Display *x11_setup_display(char *display, int authtype, Conf *conf) { struct X11Display *disp = snew(struct X11Display); char *localcopy; int i; if (!display || !*display) { localcopy = platform_get_x_display(); if (!localcopy || !*localcopy) { sfree(localcopy); localcopy = dupstr(":0"); /* plausible default for any platform */ } } else localcopy = dupstr(display); /* * Parse the display name. * * We expect this to have one of the following forms: * * - the standard X format which looks like * [ [ protocol '/' ] host ] ':' displaynumber [ '.' screennumber ] * (X11 also permits a double colon to indicate DECnet, but * that's not our problem, thankfully!) * * - only seen in the wild on MacOS (so far): a pathname to a * Unix-domain socket, which will typically and confusingly * end in ":0", and which I'm currently distinguishing from * the standard scheme by noting that it starts with '/'. */ if (localcopy[0] == '/') { disp->unixsocketpath = localcopy; disp->unixdomain = TRUE; disp->hostname = NULL; disp->displaynum = -1; disp->screennum = 0; disp->addr = NULL; } else { char *colon, *dot, *slash; char *protocol, *hostname; colon = strrchr(localcopy, ':'); if (!colon) { sfree(disp); sfree(localcopy); return NULL; /* FIXME: report a specific error? */ } *colon++ = '\0'; dot = strchr(colon, '.'); if (dot) *dot++ = '\0'; disp->displaynum = atoi(colon); if (dot) disp->screennum = atoi(dot); else disp->screennum = 0; protocol = NULL; hostname = localcopy; if (colon > localcopy) { slash = strchr(localcopy, '/'); if (slash) { *slash++ = '\0'; protocol = localcopy; hostname = slash; } } disp->hostname = *hostname ? dupstr(hostname) : NULL; if (protocol) disp->unixdomain = (!strcmp(protocol, "local") || !strcmp(protocol, "unix")); else if (!*hostname || !strcmp(hostname, "unix")) disp->unixdomain = platform_uses_x11_unix_by_default; else disp->unixdomain = FALSE; if (!disp->hostname && !disp->unixdomain) disp->hostname = dupstr("localhost"); disp->unixsocketpath = NULL; disp->addr = NULL; sfree(localcopy); } /* * Look up the display hostname, if we need to. */ if (!disp->unixdomain) { const char *err; disp->port = 6000 + disp->displaynum; disp->addr = name_lookup(disp->hostname, disp->port, &disp->realhost, conf, ADDRTYPE_UNSPEC); if ((err = sk_addr_error(disp->addr)) != NULL) { sk_addr_free(disp->addr); sfree(disp->hostname); sfree(disp->unixsocketpath); sfree(disp); return NULL; /* FIXME: report an error */ } } /* * Try upgrading an IP-style localhost display to a Unix-socket * display (as the standard X connection libraries do). */ if (!disp->unixdomain && sk_address_is_local(disp->addr)) { SockAddr ux = platform_get_x11_unix_address(NULL, disp->displaynum); const char *err = sk_addr_error(ux); if (!err) { /* Create trial connection to see if there is a useful Unix-domain * socket */ const struct plug_function_table *dummy = &dummy_plug; Socket s = putty_sk_new(sk_addr_dup(ux), 0, 0, 0, 0, 0, (Plug)&dummy #ifdef MPEXT , 0, 0 #endif ); err = sk_socket_error(s); sk_close(s); } if (err) { sk_addr_free(ux); } else { sk_addr_free(disp->addr); disp->unixdomain = TRUE; disp->addr = ux; /* Fill in the rest in a moment */ } } if (disp->unixdomain) { if (!disp->addr) disp->addr = platform_get_x11_unix_address(disp->unixsocketpath, disp->displaynum); if (disp->unixsocketpath) disp->realhost = dupstr(disp->unixsocketpath); else disp->realhost = dupprintf("unix:%d", disp->displaynum); disp->port = 0; } /* * Invent the remote authorisation details. */ if (authtype == X11_MIT) { disp->remoteauthproto = X11_MIT; /* MIT-MAGIC-COOKIE-1. Cookie size is 128 bits (16 bytes). */ disp->remoteauthdata = snewn(16, unsigned char); for (i = 0; i < 16; i++) disp->remoteauthdata[i] = random_byte(); disp->remoteauthdatalen = 16; disp->xdmseen = NULL; } else {
/* * Called to set up the rlogin connection. * * Returns an error message, or NULL on success. * * Also places the canonical host name into `realhost'. It must be * freed by the caller. */ static const char *rlogin_init(void *frontend_handle, void **backend_handle, Config *cfg, char *host, int port, char **realhost, int nodelay, int keepalive) { static const struct plug_function_table fn_table = { rlogin_log, rlogin_closing, rlogin_receive, rlogin_sent }; SockAddr addr; const char *err; Rlogin rlogin; rlogin = snew(struct rlogin_tag); rlogin->fn = &fn_table; rlogin->s = NULL; rlogin->frontend = frontend_handle; rlogin->term_width = cfg->width; rlogin->term_height = cfg->height; rlogin->firstbyte = 1; rlogin->cansize = 0; *backend_handle = rlogin; /* * Try to find host. */ { char *buf; buf = dupprintf("Looking up host \"%s\"%s", host, (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : ""))); logevent(rlogin->frontend, buf); sfree(buf); } addr = name_lookup(host, port, realhost, cfg, cfg->addressfamily); if ((err = sk_addr_error(addr)) != NULL) { sk_addr_free(addr); return err; } if (port < 0) port = 513; /* default rlogin port */ /* * Open socket. */ rlogin->s = new_connection(addr, *realhost, port, 1, 0, nodelay, keepalive, (Plug) rlogin, cfg); if ((err = sk_socket_error(rlogin->s)) != NULL) return err; /* * Send local username, remote username, terminal/speed */ { char z = 0; char *p; sk_write(rlogin->s, &z, 1); sk_write(rlogin->s, cfg->localusername, strlen(cfg->localusername)); sk_write(rlogin->s, &z, 1); sk_write(rlogin->s, cfg->username, strlen(cfg->username)); sk_write(rlogin->s, &z, 1); sk_write(rlogin->s, cfg->termtype, strlen(cfg->termtype)); sk_write(rlogin->s, "/", 1); for (p = cfg->termspeed; isdigit((unsigned char)*p); p++) continue; sk_write(rlogin->s, cfg->termspeed, p - cfg->termspeed); rlogin->bufsize = sk_write(rlogin->s, &z, 1); } return NULL; }
/* * Called to set up the adb connection. * * Returns an error message, or NULL on success. * * Also places the canonical host name into `realhost'. It must be * freed by the caller. */ static const char *adb_init(void *frontend_handle, void **backend_handle, Conf *conf, char *host, int port, char **realhost, int nodelay, int keepalive) { static const struct plug_function_table fn_table = { adb_log, adb_closing, adb_receive, adb_sent, NULL }; SockAddr addr; const char *err; Adb adb; adb = snew(struct adb_backend_data); adb->fn = &fn_table; adb->s = NULL; adb->state = STATE_WARMING_UP; *backend_handle = adb; adb->frontend = frontend_handle; /* * Try to find host. */ { char *buf; buf = dupprintf("Looking up host \"%s\"%s", "localhost", (conf_get_int(conf, CONF_addressfamily) == ADDRTYPE_IPV4 ? " (IPv4)" : (conf_get_int(conf, CONF_addressfamily) == ADDRTYPE_IPV6 ? " (IPv6)" : ""))); logevent(adb->frontend, buf); sfree(buf); } addr = name_lookup("localhost", port, realhost, conf, conf_get_int(conf, CONF_addressfamily)); if ((err = sk_addr_error(addr)) != NULL) { sk_addr_free(addr); return err; } if (port < 0) port = 5037; /* default adb port */ /* * Open socket. */ adb->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive, (Plug) adb, conf); if ((err = sk_socket_error(adb->s)) != NULL) return err; if (*conf_get_str(conf, CONF_loghost)) { char *colon; sfree(*realhost); *realhost = conf_get_str(conf, CONF_loghost); colon = strrchr(*realhost, ':'); if (colon) { /* * FIXME: if we ever update this aspect of ssh.c for * IPv6 literal management, this should change in line * with it. */ *colon++ = '\0'; } } /* send initial data to adb server */ #define ADB_SHELL_DEFAULT_STR "0012" "host:transport-any" #define ADB_SHELL_DEFAULT_STR_LEN (sizeof(ADB_SHELL_DEFAULT_STR)-1) #define ADB_SHELL_USB_STR "0012" "host:transport-usb" #define ADB_SHELL_USB_STR_LEN (sizeof(ADB_SHELL_USB_STR)-1) #define ADB_SHELL_LOCAL_STR "0015" "host:transport-local" #define ADB_SHELL_LOCAL_STR_LEN (sizeof(ADB_SHELL_LOCAL_STR)-1) #define ADB_SHELL_SERIAL_PREFIX "host:transport:" #define ADB_SHELL_SERIAL_PREFIX_LEN (sizeof(ADB_SHELL_SERIAL_PREFIX)-1) # define write_hello(str, len) \ sk_write(adb->s, str, len); \ sk_flush(adb->s); \ adb->state = STATE_SENT_HELLO; do { size_t len; if (host[0] == ':') ++host; len = strlen(host); if (len == 0 || !strcmp("-a", host) || !strcmp(host, "transport-any")) { write_hello(ADB_SHELL_DEFAULT_STR, ADB_SHELL_DEFAULT_STR_LEN); } else if (!strcmp("-d", host) || !strcmp(host, "transport-usb")) { write_hello(ADB_SHELL_USB_STR, ADB_SHELL_USB_STR_LEN); } else if (!strcmp("-e", host) || !strcmp(host, "transport-local")) { write_hello(ADB_SHELL_LOCAL_STR, ADB_SHELL_LOCAL_STR_LEN); } else { char sendbuf[512]; # define ADB_SHELL_HOST_MAX_LEN (sizeof(sendbuf)-4-ADB_SHELL_SERIAL_PREFIX_LEN) if (len > ADB_SHELL_HOST_MAX_LEN) len = ADB_SHELL_HOST_MAX_LEN; sprintf(sendbuf,"%04lx" ADB_SHELL_SERIAL_PREFIX, (unsigned long)(len+ADB_SHELL_SERIAL_PREFIX_LEN)); memcpy(sendbuf+4+ADB_SHELL_SERIAL_PREFIX_LEN, host, len); write_hello(sendbuf, len+4+ADB_SHELL_SERIAL_PREFIX_LEN); } } while (0); return NULL; }
/* * Called to set up the raw connection. * * Returns an error message, or NULL on success. * * Also places the canonical host name into `realhost'. It must be * freed by the caller. */ static const char *raw_init(void *frontend_handle, void **backend_handle, Conf *conf, char *host, int port, char **realhost, int nodelay, int keepalive) { static const struct plug_function_table fn_table = { raw_log, raw_closing, raw_receive, raw_sent }; SockAddr addr; const char *err; Raw raw; int addressfamily; char *loghost; raw = snew(struct raw_backend_data); raw->fn = &fn_table; raw->s = NULL; *backend_handle = raw; raw->sent_console_eof = raw->sent_socket_eof = FALSE; raw->frontend = frontend_handle; addressfamily = conf_get_int(conf, CONF_addressfamily); /* * Try to find host. */ { char *buf; buf = dupprintf("Looking up host \"%s\"%s", host, (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : (addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : ""))); logevent(raw->frontend, buf); sfree(buf); } addr = name_lookup(host, port, realhost, conf, addressfamily); if ((err = sk_addr_error(addr)) != NULL) { sk_addr_free(addr); return err; } if (port < 0) port = 23; /* default telnet port */ /* * Open socket. */ raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive, (Plug) raw, conf); if ((err = sk_socket_error(raw->s)) != NULL) return err; loghost = conf_get_str(conf, CONF_loghost); if (*loghost) { char *colon; sfree(*realhost); *realhost = dupstr(loghost); colon = strrchr(*realhost, ':'); if (colon) { /* * FIXME: if we ever update this aspect of ssh.c for * IPv6 literal management, this should change in line * with it. */ *colon++ = '\0'; } } return NULL; }
/* * Get a name token. */ static token_t token_getname(context_t cxt, term_t *val) { char buf0[TOKEN_MAXLEN+1]; char *buf = buf0; size_t len = 0; if (*cxt->str == '\'') { cxt->str++; buf = token_readstring(cxt, '\''); if (buf == NULL) return TOKEN_ERROR; } else { while (isalnum(*cxt->str) || *cxt->str == '_') { buf[len++] = *cxt->str++; if (len >= TOKEN_MAXLEN) return TOKEN_ERROR; } buf[len] = '\0'; } // Check for anonymous variable: if (buf[0] == '_' && buf[1] == '\0') { var_t v = make_var(NULL); term_t t = term_var(v); *val = t; return TOKEN_VARIABLE; } // Check for special name: name_t entry = name_lookup(buf); if (entry != NULL) { *val = entry->val; return entry->token; } // Check for variable: term_t t; if (varset_search(cxt->vars, buf, &t)) { *val = t; return TOKEN_VARIABLE; } // By default, create a new variable: char *name; if (buf == buf0) { name = (char *)gc_malloc(len+1); strcpy(name, buf); } else name = buf; register_name(name); var_t v = make_var(name); t = term_var(v); cxt->vars = varset_insert(cxt->vars, name, t); *val = t; return TOKEN_VARIABLE; }
/* * Called to set up the adb connection. * * Returns an error message, or NULL on success. * * Also places the canonical host name into `realhost'. It must be * freed by the caller. */ static const char *adb_init(void *frontend_handle, void **backend_handle, Config *cfg, char *host, int port, char **realhost, int nodelay, int keepalive) { static const struct plug_function_table fn_table = { adb_log, adb_closing, adb_receive, adb_sent }; SockAddr addr; const char *err; Adb adb; adb = snew(struct adb_backend_data); adb->fn = &fn_table; adb->s = NULL; adb->state = 0; *backend_handle = adb; adb->frontend = frontend_handle; /* * Try to find host. */ { char *buf; buf = dupprintf("Looking up host \"%s\"%s", "localhost", (cfg->addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" : (cfg->addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" : ""))); logevent(adb->frontend, buf); sfree(buf); } addr = name_lookup("localhost", port, realhost, cfg, cfg->addressfamily); if ((err = sk_addr_error(addr)) != NULL) { sk_addr_free(addr); return err; } if (port < 0) port = 5037; /* default adb port */ /* * Open socket. */ adb->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive, (Plug) adb, cfg); if ((err = sk_socket_error(adb->s)) != NULL) return err; if (*cfg->loghost) { char *colon; sfree(*realhost); *realhost = dupstr(cfg->loghost); colon = strrchr(*realhost, ':'); if (colon) { /* * FIXME: if we ever update this aspect of ssh.c for * IPv6 literal management, this should change in line * with it. */ *colon++ = '\0'; } } /* send initial data to adb server */ #define ADB_SHELL_DEFAULT_STR "0012" "host:transport-usb" #define ADB_SHELL_DEFAULT_STR_LEN (sizeof(ADB_SHELL_DEFAULT_STR)-1) #define ADB_SHELL_SERIAL_PREFIX "host:transport:" #define ADB_SHELL_SERIAL_PREFIX_LEN (sizeof(ADB_SHELL_SERIAL_PREFIX)-1) do { size_t len = strlen(host); if (len == 0) { sk_write(adb->s, ADB_SHELL_DEFAULT_STR, ADB_SHELL_DEFAULT_STR_LEN); } else { char sendbuf[512]; #define ADB_SHELL_HOST_MAX_LEN \ (sizeof(sendbuf)-4-ADB_SHELL_SERIAL_PREFIX_LEN-1) if (len > ADB_SHELL_HOST_MAX_LEN) len = ADB_SHELL_HOST_MAX_LEN; sprintf(sendbuf,"%04x" ADB_SHELL_SERIAL_PREFIX, len+ADB_SHELL_SERIAL_PREFIX_LEN); memcpy(sendbuf+4+ADB_SHELL_SERIAL_PREFIX_LEN, host, len); sk_write(adb->s,sendbuf,len+4+ADB_SHELL_SERIAL_PREFIX_LEN); } } while (0); sk_flush(adb->s); adb->state = 1; return NULL; }
/* * Called to set up the raw connection. * * Returns an error message, or NULL on success. * * Also places the canonical host name into `realhost'. It must be * freed by the caller. */ static const char *raw_init(void *frontend_handle, void **backend_handle, Conf *conf, const char *host, int port, char **realhost, int nodelay, int keepalive) { static const struct plug_function_table fn_table = { raw_log, raw_closing, raw_receive, raw_sent }; SockAddr addr; const char *err; Raw raw; int addressfamily; char *loghost; raw = snew(struct raw_backend_data); raw->fn = &fn_table; raw->s = NULL; raw->closed_on_socket_error = FALSE; *backend_handle = raw; raw->sent_console_eof = raw->sent_socket_eof = FALSE; raw->bufsize = 0; raw->session_started = FALSE; raw->conf = conf_copy(conf); raw->frontend = frontend_handle; addressfamily = conf_get_int(conf, CONF_addressfamily); /* * Try to find host. */ addr = name_lookup(host, port, realhost, conf, addressfamily, raw->frontend, "main connection"); if ((err = sk_addr_error(addr)) != NULL) { sk_addr_free(addr); return err; } if (port < 0) port = 23; /* default telnet port */ /* * Open socket. */ raw->s = new_connection(addr, *realhost, port, 0, 1, nodelay, keepalive, (Plug) raw, conf); if ((err = sk_socket_error(raw->s)) != NULL) return err; loghost = conf_get_str(conf, CONF_loghost); if (*loghost) { char *colon; sfree(*realhost); *realhost = dupstr(loghost); colon = host_strrchr(*realhost, ':'); if (colon) *colon++ = '\0'; } return NULL; }
int main(int argc, char **argv) { char *arg; char *value; char *type; int rc = 1, opt; int timeout = 600; key_serial_t key; char *progname, *keystr = NULL; int clearing = 0, keymask = 0, display = 0, list = 0; /* Set the basename */ if ((progname = strrchr(argv[0], '/')) != NULL) progname++; else progname = argv[0]; xlog_open(progname); while ((opt = getopt(argc, argv, "du:g:r:ct:vl")) != -1) { switch (opt) { case 'd': display++; break; case 'l': list++; break; case 'u': keymask = UIDKEYS; keystr = strdup(optarg); break; case 'g': keymask = GIDKEYS; keystr = strdup(optarg); break; case 'r': keymask = GIDKEYS|UIDKEYS; keystr = strdup(optarg); break; case 'c': clearing++; break; case 'v': verbose++; break; case 't': timeout = atoi(optarg); break; default: xlog_warn(usage, progname); break; } } if ((rc = nfs4_init_name_mapping(PATH_IDMAPDCONF))) { xlog_errno(rc, "Unable to create name to user id mappings."); return EXIT_FAILURE; } if (!verbose) verbose = conf_get_num("General", "Verbosity", 0); if (display) return display_default_domain(); if (list) return list_keyring(DEFAULT_KEYRING); if (keystr) { return key_invalidate(keystr, keymask); } if (clearing) { xlog_syslog(0); return keyring_clear(DEFAULT_KEYRING); } xlog_stderr(0); if ((argc - optind) != 2) { xlog_err("Bad arg count. Check /etc/request-key.conf"); xlog_warn(usage, progname); return EXIT_FAILURE; } if (verbose) nfs4_set_debug(verbose, NULL); key = strtol(argv[optind++], NULL, 10); arg = strdup(argv[optind]); if (arg == NULL) { xlog_err("strdup failed: %m"); return EXIT_FAILURE; } type = strtok(arg, ":"); value = strtok(NULL, ":"); if (value == NULL) { free(arg); xlog_err("Error: Null uid/gid value."); return EXIT_FAILURE; } if (verbose) { xlog_warn("key: 0x%lx type: %s value: %s timeout %ld", key, type, value, timeout); } /* Become a possesor of the to-be-instantiated key to set the key's timeout */ request_key("keyring", DEFAULT_KEYRING, NULL, KEY_SPEC_THREAD_KEYRING); if (strcmp(type, "uid") == 0) rc = id_lookup(value, key, USER); else if (strcmp(type, "gid") == 0) rc = id_lookup(value, key, GROUP); else if (strcmp(type, "user") == 0) rc = name_lookup(value, key, USER); else if (strcmp(type, "group") == 0) rc = name_lookup(value, key, GROUP); /* Set timeout to 10 (600 seconds) minutes */ if (rc == EXIT_SUCCESS) keyctl_set_timeout(key, timeout); free(arg); return rc; }
int platform_make_x11_server(Plug *plug, const char *progname, int mindisp, const char *screen_number_suffix, ptrlen authproto, ptrlen authdata, Socket **sockets, Conf *conf) { char *tmpdir; char *authfilename = NULL; strbuf *authfiledata = NULL; char *unix_path = NULL; SockAddr *a_tcp = NULL, *a_unix = NULL; int authfd; FILE *authfp; int displayno; authfiledata = strbuf_new_nm(); int nsockets = 0; /* * Look for a free TCP port to run our server on. */ for (displayno = mindisp;; displayno++) { const char *err; int tcp_port = displayno + 6000; int addrtype = ADDRTYPE_IPV4; sockets[nsockets] = new_listener( NULL, tcp_port, plug, false, conf, addrtype); err = sk_socket_error(sockets[nsockets]); if (!err) { char *hostname = get_hostname(); if (hostname) { char *canonicalname = NULL; a_tcp = name_lookup(hostname, tcp_port, &canonicalname, conf, addrtype, NULL, ""); sfree(canonicalname); } sfree(hostname); nsockets++; break; /* success! */ } else { sk_close(sockets[nsockets]); } if (!strcmp(err, strerror(EADDRINUSE))) /* yuck! */ goto out; } if (a_tcp) { x11_format_auth_for_authfile( BinarySink_UPCAST(authfiledata), a_tcp, displayno, authproto, authdata); } /* * Try to establish the Unix-domain analogue. That may or may not * work - file permissions in /tmp may prevent it, for example - * but it's worth a try, and we don't consider it a fatal error if * it doesn't work. */ unix_path = dupprintf("/tmp/.X11-unix/X%d", displayno); a_unix = unix_sock_addr(unix_path); sockets[nsockets] = new_unix_listener(a_unix, plug); if (!sk_socket_error(sockets[nsockets])) { x11_format_auth_for_authfile( BinarySink_UPCAST(authfiledata), a_unix, displayno, authproto, authdata); nsockets++; } else { sk_close(sockets[nsockets]); sfree(unix_path); unix_path = NULL; } /* * Decide where the authority data will be written. */ tmpdir = getenv("TMPDIR"); if (!tmpdir || !*tmpdir) tmpdir = "/tmp"; authfilename = dupcat(tmpdir, "/", progname, "-Xauthority-XXXXXX", NULL); { int oldumask = umask(077); authfd = mkstemp(authfilename); umask(oldumask); } if (authfd < 0) { while (nsockets-- > 0) sk_close(sockets[nsockets]); goto out; } /* * Spawn a subprocess which will try to reliably delete our * auth file when we terminate, in case we die unexpectedly. */ { int cleanup_pipe[2]; pid_t pid; /* Don't worry if pipe or fork fails; it's not _that_ critical. */ if (!pipe(cleanup_pipe)) { if ((pid = fork()) == 0) { int buf[1024]; /* * Our parent process holds the writing end of * this pipe, and writes nothing to it. Hence, * we expect read() to return EOF as soon as * that process terminates. */ close(0); close(1); close(2); setpgid(0, 0); close(cleanup_pipe[1]); close(authfd); while (read(cleanup_pipe[0], buf, sizeof(buf)) > 0); unlink(authfilename); if (unix_path) unlink(unix_path); _exit(0); } else if (pid < 0) { close(cleanup_pipe[0]); close(cleanup_pipe[1]); } else { close(cleanup_pipe[0]); cloexec(cleanup_pipe[1]); } } } authfp = fdopen(authfd, "wb"); fwrite(authfiledata->u, 1, authfiledata->len, authfp); fclose(authfp); { char *display = dupprintf(":%d%s", displayno, screen_number_suffix); conf_set_str_str(conf, CONF_environmt, "DISPLAY", display); sfree(display); } conf_set_str_str(conf, CONF_environmt, "XAUTHORITY", authfilename); /* * FIXME: return at least the DISPLAY and XAUTHORITY env settings, * and perhaps also the display number */ out: if (a_tcp) sk_addr_free(a_tcp); /* a_unix doesn't need freeing, because new_unix_listener took it over */ sfree(authfilename); strbuf_free(authfiledata); sfree(unix_path); return nsockets; }