Lines Matching +full:ipv4 +full:- +full:simple +full:- +full:service +full:- +full:config
1 /* sane - Scanner Access Now Easy.
3 Copyright (C) 2001 - 2004 Henning Meier-Geinitz
5 AF-independent + IPv6 code, standalone mode
31 #include "../include/sane/config.h"
41 # else /* fallback to the old, IPv4-only code */
121 tv.tv_usec = (timeout - tv.tv_sec * 1000) * 1000; in poll()
128 fdp->revents = 0; in poll()
130 if (fdp->events & POLLIN) in poll()
131 FD_SET (fdp->fd, &rfds); in poll()
133 FD_SET (fdp->fd, &efds); in poll()
135 maxfd = (fdp->fd > maxfd) ? fdp->fd : maxfd; in poll()
147 if (fdp->events & POLLIN) in poll()
148 if (FD_ISSET (fdp->fd, &rfds)) in poll()
149 fdp->revents |= POLLIN; in poll()
151 if (FD_ISSET (fdp->fd, &efds)) in poll()
152 fdp->revents |= POLLERR; in poll()
160 # include <avahi-client/client.h>
161 # include <avahi-client/publish.h>
163 # include <avahi-common/alternative.h>
164 # include <avahi-common/simple-watch.h>
165 # include <avahi-common/malloc.h>
166 # include <avahi-common/error.h>
168 # define SANED_SERVICE_DNS "_sane-port._tcp"
171 pid_t avahi_pid = -1;
181 #include <systemd/sd-daemon.h>
235 #define SANED_SERVICE_NAME "sane-port"
260 static short bind_port = -1;
269 /* The default-user name. This is not used to imply any rights. All
272 static const char *default_username = "saned-user";
364 "auth_callback: called during non-authorizable RPC (resource=%s)\n", in auth_callback()
490 exit (EXIT_SUCCESS); /* This is a nowait-daemon. */ in quit()
497 static int h, last_handle_checked = -1; in get_free_handle()
518 /* we're out of handles---alloc some more: */ in get_free_handle()
519 last_handle_checked = num_handles - 1; in get_free_handle()
526 return -1; in get_free_handle()
549 if (w->status || (unsigned) h >= (unsigned) num_handles || !handle[h].inuse) in decode_handle()
553 "(h=%d, %s)\n", op, h, strerror (w->status)); in decode_handle()
554 return -1; in decode_handle()
561 /* Convert a number of bits to an 8-bit bitmask */
578 cidr = -1; in check_v4_in_range()
589 cidr -= 8; in check_v4_in_range()
592 for (i = 3; cidr >= 0; i--) in check_v4_in_range()
595 cidr -= 8; in check_v4_in_range()
615 base = (struct sockaddr_in *) res->ai_addr; in check_v4_in_range()
621 if ((base->sin_addr.s_addr & mask) == (sin->sin_addr.s_addr & mask)) in check_v4_in_range()
644 cidr = -1; in check_v6_in_range()
655 cidr -= 8; in check_v6_in_range()
661 cidr -= 8; in check_v6_in_range()
679 base = (struct sockaddr_in6 *) res->ai_addr; in check_v6_in_range()
687 if ((base->sin6_addr.s6_addr[i] & mask[i]) != (sin6->sin6_addr.s6_addr[i] & mask[i])) in check_v6_in_range()
709 cidr = -1; in check_v4_in_range()
720 cidr -= 8; in check_v4_in_range()
723 for (i = 3; cidr >= 0; i--) in check_v4_in_range()
726 cidr -= 8; in check_v4_in_range()
738 if ((base->s_addr & mask) == (inaddr->s_addr & mask)) in check_v4_in_range()
764 char *remote_ipv4 = NULL; /* in case we have an IPv4-mapped address (eg ::ffff:127.0.0.1) */ in check_host()
799 if (IN6_IS_ADDR_V4MAPPED ((struct in6_addr *)sin6->sin6_addr.s6_addr)) in check_host()
801 DBG (DBG_DBG, "check_host: detected an IPv4-mapped address\n"); in check_host()
818 sin = (struct sockaddr_in *)res->ai_addr; in check_host()
831 if (IN_LOOPBACK (ntohl (sin->sin_addr.s_addr))) in check_host()
847 if (IN_LOOPBACK (ntohl (sin->sin_addr.s_addr))) in check_host()
856 if (IN6_IS_ADDR_LOOPBACK ((struct in6_addr *)sin6->sin6_addr.s6_addr)) in check_host()
904 for (resp = res; resp != NULL; resp = resp->ai_next) in check_host()
907 resp->ai_canonname); in check_host()
909 err = getnameinfo (resp->ai_addr, resp->ai_addrlen, text_addr, in check_host()
941 DBG (DBG_DBG, "check_host: opening config file: %s\n", in check_host()
950 "check_host: can't open config file: %s (%s)\n", in check_host()
959 DBG (DBG_DBG, "check_host: config file line: `%s'\n", config_line); in check_host()
989 "check_host: malformed IPv6 address in config file, skipping: [%s\n", in check_host()
1015 "check_host: access granted from IP address %s (IPv4-mapped)\n", remote_ip); in check_host()
1036 /* get a sockaddr_in representing the v4-mapped IP address */ in check_host()
1045 sin = (struct sockaddr_in *)res->ai_addr; in check_host()
1106 for (resp = res; resp != NULL; resp = resp->ai_next) in check_host()
1108 err = getnameinfo (resp->ai_addr, resp->ai_addrlen, text_addr, in check_host()
1209 he->h_name); in check_host()
1211 if ((he->h_length == 4) || (he->h_addrtype == AF_INET)) in check_host()
1213 if (!inet_ntop (he->h_addrtype, he->h_addr_list[0], text_addr, in check_host()
1218 if (memcmp (he->h_addr_list[0], &remote_address.s_addr, 4) == 0) in check_host()
1229 "(only IPv4 is supported)\n"); in check_host()
1240 DBG (DBG_DBG, "check_host: opening config file: %s\n", in check_host()
1249 "check_host: can't open config file: %s (%s)\n", in check_host()
1258 DBG (DBG_DBG, "check_host: config file line: `%s'\n", config_line); in check_host()
1317 if (!inet_ntop (he->h_addrtype, he->h_addr_list[0], in check_host()
1324 he->h_addr_list[0], 4) == 0) in check_host()
1348 status = check_host (w->io.fd); in init()
1352 return -1; in init()
1358 if (w->status) in init()
1360 DBG (DBG_ERR, "init: bad status after sanei_w_set_dir: %d\n", w->status); in init()
1361 return -1; in init()
1365 if (w->status || word != SANE_NET_INIT) in init()
1368 w->status, word); in init()
1369 return -1; in init()
1373 if (w->status) in init()
1375 DBG (DBG_ERR, "init: bad status after sanei_w_init_req: %d\n", w->status); in init()
1376 return -1; in init()
1379 w->version = SANEI_NET_PROTOCOL_VERSION; in init()
1384 if (w->status) in init()
1386 DBG (DBG_ERR, "init: bad status after sanei_w_free: %d\n", w->status); in init()
1387 return -1; in init()
1416 if (w->status || status != SANE_STATUS_GOOD) in init()
1417 return -1; in init()
1441 int ret = -1; in start_scan()
1446 if (getsockname (w->io.fd, &data_addr.sa, (socklen_t *) &len) < 0) in start_scan()
1450 reply->status = SANE_STATUS_IO_ERROR; in start_scan()
1451 return -1; in start_scan()
1459 reply->status = SANE_STATUS_IO_ERROR; in start_scan()
1460 return -1; in start_scan()
1483 sin->sin_port = htons(data_port); in start_scan()
1487 sin6->sin6_port = htons(data_port); in start_scan()
1505 reply->status = SANE_STATUS_IO_ERROR; in start_scan()
1506 return -1; in start_scan()
1513 reply->status = SANE_STATUS_IO_ERROR; in start_scan()
1514 return -1; in start_scan()
1521 reply->status = SANE_STATUS_IO_ERROR; in start_scan()
1522 return -1; in start_scan()
1529 reply->port = ntohs (sin->sin_port); in start_scan()
1534 reply->port = ntohs (sin6->sin6_port); in start_scan()
1541 DBG (DBG_MSG, "start_scan: using port %d for data\n", reply->port); in start_scan()
1543 reply->status = sane_start (be_handle); in start_scan()
1544 if (reply->status == SANE_STATUS_GOOD) in start_scan()
1567 if (getsockname (w->io.fd, (struct sockaddr *) &sin, (socklen_t *) &len) < 0) in start_scan()
1571 reply->status = SANE_STATUS_IO_ERROR; in start_scan()
1572 return -1; in start_scan()
1580 reply->status = SANE_STATUS_IO_ERROR; in start_scan()
1581 return -1; in start_scan()
1600 reply->status = SANE_STATUS_IO_ERROR; in start_scan()
1601 return -1; in start_scan()
1608 reply->status = SANE_STATUS_IO_ERROR; in start_scan()
1609 return -1; in start_scan()
1616 reply->status = SANE_STATUS_IO_ERROR; in start_scan()
1617 return -1; in start_scan()
1620 reply->port = ntohs (sin.sin_port); in start_scan()
1622 DBG (DBG_MSG, "start_scan: using port %d for data\n", reply->port); in start_scan()
1624 reply->status = sane_start (be_handle); in start_scan()
1625 if (reply->status == SANE_STATUS_GOOD) in start_scan()
1656 int num_fds, be_fd = -1, reader, bytes_in_buf, status_dirty = 0; in do_scan()
1683 FD_SET(w->io.fd, &rd_mask); in do_scan()
1684 num_fds = w->io.fd + 1; in do_scan()
1718 be_fd = -1; in do_scan()
1723 DBG (DBG_INFO, "do_scan: select_fd was closed --> EOF\n"); in do_scan()
1744 nbytes = buffer_size - writer; in do_scan()
1759 bytes_in_buf -= (size_t) nwritten; in do_scan()
1777 reader -= buffer_size; in do_scan()
1780 nbytes = buffer_size - 4; in do_scan()
1782 nbytes = buffer_size - reader; in do_scan()
1807 if (status_dirty && buffer_size - bytes_in_buf >= 5) in do_scan()
1817 if (FD_ISSET(w->io.fd, &rd_set)) in do_scan()
1820 w->io.fd); in do_scan()
1853 if (w->status)
1856 "process_request: bad status %d\n", w->status);
1857 return -1;
1884 if (w->status)
1888 strerror (w->status));
1930 resource = strdup (device_list[0]->name);
2014 if (w->status || (unsigned) req.handle >= (unsigned) num_handles
2020 , req.handle, strerror (w->status));
2024 /* Addresses CVE-2017-6318 (#315576, Debian BTS #853804) */
2029 if (w->direction == WIRE_DECODE
2040 w->allocated_memory -= (1 + strlen (req.value));
2046 w->status = ENOMEM;
2049 "h=%d (%s)\n", req.handle, strerror (w->status));
2053 w->allocated_memory += req.value_size;
2094 int fd = -1, data_fd = -1;
2122 fds->fd = fd;
2123 fds->events = POLLIN;
2178 data_fd = -1;
2179 return -1;
2191 fds->fd = fd;
2192 fds->events = POLLIN;
2242 data_fd = -1;
2243 return -1;
2259 fcntl (data_fd, F_SETFL, O_NONBLOCK); /* set non-blocking */
2282 return -1;
2291 return -1;
2313 avahi_pid = -1;
2314 numchildren--;
2319 for (c = children; (c != NULL) && (c->next != NULL); p = c, c = c->next)
2321 if (c->pid == ret)
2324 children = c->next;
2326 p->next = c->next;
2330 numchildren--;
2349 return -1;
2352 c->pid = pid;
2353 c->next = children;
2366 int level = -1;
2389 level = p->p_proto;
2392 if (level == -1
2458 wait_child (-1, NULL, 0);
2518 for (fdp = fds; nfds > 0; nfds--, fdp++)
2519 close (fdp->fd);
2528 DBG (DBG_ERR, "saned_avahi: failed to create simple poll object\n");
2575 …DBG (DBG_INFO, "saned_avahi_group_callback: service '%s' successfully established\n", avahi_svc_na…
2579 /* A service name collision with a remote service
2585 …DBG (DBG_WARN, "saned_avahi_group_callback: service name collision, renaming service to '%s'\n", a…
2627 DBG (DBG_INFO, "saned_create_avahi_services: adding service '%s'\n", avahi_svc_name);
2646 …DBG (DBG_WARN, "saned_create_avahi_services: service name collision, renaming service to '%s'\n", …
2655 …DBG (DBG_ERR, "saned_create_avahi_services: failed to add %s service: %s\n", SANED_SERVICE_DNS, av…
2659 /* Tell the server to register the service */
2716 /* Server disappeared - try to reconnect */
2730 /* Another error happened - game over */
2750 DBG (DBG_INFO, "read_config: searching for config file\n");
2788 optval = strchr (endval, '-');
2821 … DBG (DBG_INFO, "read_config: data port range: %d - %d\n", data_port_lo, data_port_hi);
2846 DBG (DBG_INFO, "read_config: done reading config\n");
2849 DBG (DBG_ERR, "read_config: could not open config file (%s): %s\n",
2861 int fd = -1;
2868 for (resp = res, i = 0; resp != NULL; resp = resp->ai_next, i++)
2871 if (resp->ai_family != family)
2874 if (resp->ai_family == AF_INET)
2876 if (sane_port != -1)
2877 ((struct sockaddr_in *) resp->ai_addr)->sin_port = htons(sane_port);
2879 sane_port = ntohs(((struct sockaddr_in *) resp->ai_addr)->sin_port);
2882 else if (resp->ai_family == AF_INET6)
2884 if (sane_port != -1)
2885 ((struct sockaddr_in6 *) resp->ai_addr)->sin6_port = htons(sane_port);
2887 sane_port = ntohs (((struct sockaddr_in6 *) resp->ai_addr)->sin6_port);
2894 if ((fd = socket (resp->ai_family, SOCK_STREAM, 0)) < 0)
2907 if (bind (fd, resp->ai_addr, resp->ai_addrlen) < 0)
2912 * and we're trying to bind to an IPv4 addr now.
2937 socklen_t len = sizeof (*resp->ai_addr);
2938 if (getsockname(fd, resp->ai_addr, &len) != -1)
2940 if (resp->ai_family == AF_INET)
2942 …: [%d] selected ephemeral port: %d\n", i, ntohs(((struct sockaddr_in *) resp->ai_addr)->sin_port));
2946 if (resp->ai_family == AF_INET6)
2948 …[%d] selected ephemeral port: %d\n", i, ntohs(((struct sockaddr_in6 *) resp->ai_addr)->sin6_port));
2956 fdp->fd = fd;
2957 fdp->events = POLLIN;
2975 …DBG (DBG_DBG, "do_bindings: trying to get port for service \"%s\" (getaddrinfo)\n", SANED_SERVICE_…
2986 …DBG (DBG_WARN, "do_bindings: \" %s \" service unknown on your host; you should add\n", SANED_SERVI…
2997 for (resp = res, *nfds = 0; resp != NULL; resp = resp->ai_next, (*nfds)++)
3012 /* bind IPv6 first, IPv4 second */
3036 int fd = -1;
3039 …DBG (DBG_DBG, "do_bindings: trying to get port for service \"%s\" (getservbyname)\n", SANED_SERVIC…
3044 port = serv->s_port;
3050 …DBG (DBG_WARN, "do_bindings: \"%s\" service unknown on your host; you should add\n", SANED_SERVICE…
3094 (*fds)->fd = fd;
3095 (*fds)->events = POLLIN;
3120 runas_uid = pwent->pw_uid;
3121 runas_gid = pwent->pw_gid;
3142 if (grp->gr_gid == runas_gid)
3145 while (grp->gr_mem[i])
3147 if (strcmp(grp->gr_mem[i], user) == 0)
3154 if (grp->gr_gid == grplist[i])
3167 grplist[ngroups++] = grp->gr_gid;
3215 int fd = -1;
3299 while (wait_child (-1, NULL, WNOHANG) > 0)
3308 if (fdp->revents & (POLLERR | POLLHUP | POLLNVAL))
3311 close (fdp->fd);
3315 DBG (DBG_WARN, "run_standalone: invalid fd in set, attempting to re-bind\n");
3322 else if (! (fdp->revents & POLLIN))
3325 fd = accept (fdp->fd, 0, 0);
3343 close (fdp->fd);
3353 int fd = -1;
3373 if (fd == -1)
3390 if (fd == -1)
3423 if (fd == -1)
3436 " -a, --alone[=user] equal to `-l -D -u user'\n"
3437 " -l, --listen run in standalone mode (listen for connection)\n"
3438 " -u, --user=user run as `user'\n"
3439 " -D, --daemonize run in background\n"
3440 " -o, --once exit after first client disconnects\n"
3441 " -d, --debug=level set debug level `level' (default is 2)\n"
3442 " -e, --stderr output to stderr\n"
3443 " -b, --bind=addr bind address `addr' (default all interfaces)\n"
3444 " -p, --port=port bind port `port` (default sane-port or 6566)\n"
3445 " -B, --buffer-size=size set size of read buffer in KB (default: 1024)\n"
3446 " -h, --help show this help message and exit\n", me);
3466 {"buffer-size", required_argument, 0, 'B'},
3492 while((c = getopt_long(argc, argv,"ha::lu:Dod:eb:p:B:", long_options, &long_index )) != -1)
3564 strcat(options, "AF-indep");
3569 strcat(options, "IPv4 only");