/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 1999-2016. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* %CopyrightEnd%
*/
/*
* setuid_socket_wrap.c
*
* ./a.out [-s [tag,][addr]:[port]]* [-d [tag,][addr]:[port]]*
* [-r [tag,]proto]* -- program args
*
* Where: -s = stream socket, -d datagram socket and -r means raw socket.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifndef EXEC_PROGRAM
# define EXEC_PROGRAM "/bin/echo"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif
struct sock_list {
struct sock_list *next;
int fd;
int type;
int protocol;
struct sockaddr_in addr;
char *arg;
};
int parse_addr(addr, str)
struct sockaddr_in *addr;
char *str;
{
int port = 0;
char *cp;
struct hostent *hp;
struct servent *se;
if ((cp = strrchr(str, (int)':')) != NULL)
*cp++ = '\0';
if (cp) {
if (!isdigit((int)cp[0])) {
if ((se = getservbyname(cp, "tcp")) != NULL) {
port = ntohs(se->s_port);
} else {
fprintf(stderr, "unknown port %s\n", cp);
return -1;
}
} else {
port = atoi(cp);
}
}
if (port < 0 || port > 0xffff) {
fprintf(stderr, "bad port number %d\n", port);
return -1;
}
bzero(addr, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
if (*str == '\000') {
addr->sin_addr.s_addr = INADDR_ANY;
} else {
if ((addr->sin_addr.s_addr = inet_addr(str)) == INADDR_NONE) {
if ((hp = gethostbyname(str)) == NULL) {
fprintf(stderr, "\"%s\" unknown host or address!\n", str);
return -1;
} else {
bcopy(hp->h_addr_list[0], &addr->sin_addr.s_addr,hp->h_length);
}
}
}
return 0;
}
struct sock_list *new_entry(type, argstr)
int type;
char *argstr;
{
struct sock_list *sle;
char *cp;
sle = (struct sock_list *)malloc(sizeof(struct sock_list));
if (!sle)
return NULL;
sle->next = NULL;
sle->fd = -1;
if ((cp = strchr(argstr, (int)',')) != NULL) {
*cp++ = '\0';
sle->arg = argstr;
argstr = cp;
} else {
sle->arg = "-fd";
}
sle->type = type;
switch (type) {
case SOCK_RAW: {
struct protoent *pe;
pe = getprotobyname(argstr);
if (!pe) {
fprintf(stderr, "Unknown protocol: %s\n", argstr);
free(sle);
return NULL;
}
sle->protocol = pe->p_proto;
break;
}
case SOCK_STREAM:
case SOCK_DGRAM:
sle->protocol = 0;
if (parse_addr(&sle->addr, argstr) < 0) {
free(sle);
return NULL;
}
break;
}
return sle;
}
int open_socket(sle)
struct sock_list *sle;
{
sle->fd = socket(AF_INET, sle->type, sle->protocol);
if (sle->fd < 0) {
perror("socket");
return -1;
}
if (sle->type != SOCK_RAW) {
#if 0
printf("binding fd %d to %s:%d\n", sle->fd,
inet_ntoa(sle->addr.sin_addr), ntohs(sle->addr.sin_port));
#endif
if (bind(sle->fd, (struct sockaddr *)&sle->addr, sizeof(sle->addr))<0){
perror("bind");
close(sle->fd);
return -1;
}
}
return sle->fd;
}
int main(argc, argv)
int argc;
char *argv[];
{
struct sock_list *sl = NULL, *sltmp = NULL;
int count = 0;
int c;
while ((c = getopt(argc, argv, "s:d:r:")) != EOF)
switch (c) {
case 's':
sltmp = new_entry(SOCK_STREAM, optarg);
if (!sltmp) {
exit(1);
}
sltmp->next = sl;
sl = sltmp;
count++;
break;
case 'd':
sltmp = new_entry(SOCK_DGRAM, optarg);
if (!sltmp) {
exit(1);
}
sltmp->next = sl;
sl = sltmp;
count++;
break;
case 'r':
sltmp = new_entry(SOCK_RAW, optarg);
if (!sltmp) {
exit(1);
}
sltmp->next = sl;
sl = sltmp;
count++;
break;
default:
exit(1);
}
argc -= optind;
argv += optind;
for(sltmp = sl; sltmp != NULL; sltmp = sltmp->next)
if (open_socket(sltmp) < 0) {
fprintf(stderr, "failed to create socket!\n");
exit(1);
}
setuid(getuid());
{
int i;
char **newargv;
char *run_prog = EXEC_PROGRAM;
char *run_prog_name;
newargv = (char **)malloc((1 + 2*count + argc + 1) * sizeof(char*));
if ((run_prog_name = strrchr(run_prog, (int)'/')) == NULL)
run_prog_name = run_prog;
else
run_prog_name++;
i = 0;
newargv[i++] = run_prog_name;
for (; argc; argc--, argv++, i++)
newargv[i] = *argv;
for(sltmp = sl; sltmp != NULL; ) {
char *fd_str = (char *)malloc(8);
if (!fd_str) exit(1);
sprintf(fd_str, "%d", sltmp->fd);
if (sltmp->arg && *(sltmp->arg))
newargv[i++] = sltmp->arg;
newargv[i++] = fd_str;
sl = sltmp;
sltmp = sltmp->next;
free(sl);
}
newargv[i] = (char *)NULL;
execv(run_prog, newargv);
perror("exec");
exit(1);
}
exit(0);
}
没有合适的资源?快使用搜索试试~ 我知道了~
emqttd-centos6.8-v2.1.2.zip
共1226个文件
beam:826个
hrl:57个
h:54个
需积分: 10 12 下载量 28 浏览量
2017-10-26
10:47:52
上传
评论
收藏 17.54MB ZIP 举报
温馨提示
EMQ Plus = 设备接入 + 消息路由 + 数据存储 + 自动订阅 + 状态监控 + 私有部署 + 快速集成!!!!!!!
资源推荐
资源详情
资源评论
收起资源包目录
emqttd-centos6.8-v2.1.2.zip (1226个子文件)
libethread.a 632KB
liberts_internal.a 151KB
liberts_internal_r.a 151KB
liberts_r.a 95KB
liberts.a 95KB
inets.app 3KB
kernel.app 3KB
stdlib.app 2KB
observer.app 2KB
compiler.app 2KB
emqttd.app 2KB
lager.app 2KB
ssl.app 1KB
sasl.app 1KB
os_mon.app 1KB
runtime_tools.app 1KB
mnesia.app 1KB
crypto.app 934B
esockd.app 823B
xmerl.app 781B
mochiweb.app 752B
mongodb.app 604B
emq_coap.app 604B
gproc.app 581B
recon.app 568B
emq_dashboard.app 553B
epgsql.app 532B
goldrush.app 488B
syntax_tools.app 482B
poolboy.app 478B
public_key.app 470B
ecpool.app 386B
emq_plugin_template.app 356B
emq_stomp.app 353B
emq_auth_pgsql.app 346B
emq_sn.app 341B
emq_auth_redis.app 341B
emq_auth_mysql.app 340B
emq_reloader.app 325B
emq_auth_mongo.app 323B
syslog.app 319B
asn1.app 317B
gen_logger.app 309B
bson.app 304B
emq_auth_http.app 302B
emq_modules.app 300B
emq_auth_clientid.app 292B
emq_auth_username.app 292B
emq_auth_ldap.app 269B
eldap.app 268B
emq_recon.app 268B
mysql.app 267B
emq_retainer.app 261B
pbkdf2.app 259B
lager_syslog.app 242B
eredis.app 235B
cbor.app 234B
kernel.appup 1KB
stdlib.appup 1KB
sasl.appup 1KB
inets.appup 897B
runtime_tools.appup 801B
syntax_tools.appup 793B
eldap.appup 791B
observer.appup 790B
os_mon.appup 789B
crypto.appup 789B
public_key.appup 789B
compiler.appup 787B
xmerl.appup 782B
asn1.appup 779B
mnesia.appup 645B
ssl.appup 280B
beam 2.79MB
OTP-PUB-KEY.beam 503KB
erl_parse.beam 311KB
asn1ct_check.beam 230KB
core_parse.beam 214KB
xmerl_xsd.beam 213KB
xmerl_scan.beam 200KB
erl_lint.beam 175KB
qlc.beam 151KB
mnesia_schema.beam 149KB
qlc_pt.beam 141KB
xmerl_sax_parser_utf8.beam 132KB
xmerl_sax_parser_utf16le.beam 132KB
xmerl_sax_parser_utf16be.beam 132KB
xmerl_sax_parser_latin1.beam 132KB
xmerl_sax_parser_list.beam 127KB
asn1ct_imm.beam 119KB
erl_syntax.beam 119KB
dets.beam 119KB
mochiweb_charref.beam 114KB
v3_core.beam 114KB
dets_v9.beam 111KB
crashdump_viewer.beam 111KB
v3_codegen.beam 110KB
sys_core_fold.beam 108KB
mnesia.beam 104KB
ssl_connection.beam 100KB
共 1226 条
- 1
- 2
- 3
- 4
- 5
- 6
- 13
资源评论
追着光影的朵拉
- 粉丝: 1
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功