📄 magicd.c
字号:
/* magicd.c -- Magic C++ telnet server process (Mostly) portable public-domain implementation -- Copyright(C) 2003 Magicunix Infomation Technology Limited This file is part of magicd. magicd is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. For details, see the Magic C++ World-Wide-Web page, `http://www.magicunix.com', or send a mail to the Magic C++ developers <support@magicunix.com>. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/wait.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <tcp.h>#include <errno.h>#include "tools.h"#include "ip.h"#include "fmt.h"#include "login.h"#include "telnet.h"#define _GNU_SOURCE#include <getopt.h>#include "info.h" /*this file is generated by Makefile dynamically,it's include prefix dir*/static char *copyright="@(#) magicd 2.1.0 Copyright (c)2003 MagicUnix infomation Technology, All rights reserved.\n";int getclientip(int fd , struct sockaddr_in *pcliaddr , int * pcliaddrlen , char *ip);int setup(char *argv[],struct passwd *pw);void sync_rw(int sockfd, int streamfd ,char * clientip);void util_byte_copy(register char *to, register unsigned int n,register char *from);int clearuputmpx();int release_licencecnt();extern void outa_here(), daemonize();int tty_getmode(int fd);void relief();void killed( int sig );char *addr2host();int print_usage();extern int errno;int childpid=0, newfd;int netdebug=0, telnet=0; int login=0; /*type of login ,0 for normal login , 1 for express login*/int telnet_col = 300; /*default value for the term columns*/int telnet_row = 300; /*default value for the term rows*/int port = 2004; /*default value for port*/char logname[100]; /*login program name*/char szNoRootUsername[50]; /*user-defined user name*/char szNoRootPasswd[50]; /*user-defined pass word*/int debug = 0;int doutmp = 0;#define MAXKIDS 256 /* The absolute maximum forked processes */int children[MAXKIDS]; /* Forked processes handling tcp connections */static int maxkids=0; /* The maximum number of forked processes */extern char clientip[16];extern int getlicence;char remotehost[64];int fd;int main(int argc,char *argv[]){ extern char *optarg; extern int optind, errno; int streamfd, clilen; int i, found; char *ptr, *args[256]; struct passwd *pw=NULL; struct sockaddr_in cli_addr, serv_addr; char prompt[100]; int express; char newprompt[100]; uid_t uid; int optch; int longindex; static char optstring[] = "p:L:a:f:hvdcr"; static struct option long_opts[] = { {"help" , 0 , 0 , 'h' }, {"version" , 0 , 0 , 'v'}, {"debug" , 0 , 0 , 'd'}, {"col" , 0 , 0 , 'c'}, {"row" , 0 , 0 , 'r'}, {0 , 0 , 0 , 0} }; telnet = 1; memset( logname , 0 , sizeof( logname )); memset( szNoRootUsername , 0 , sizeof(szNoRootUsername )); memset( szNoRootPasswd , 0 , sizeof(szNoRootPasswd )); strcpy(logname , LOGIN); /*default login name, search by configure*/ /*check command line*/ while( (optch = getopt_long( argc , argv , optstring , long_opts , &longindex) ) != -1 ) { char *p1; char szTemp[ 100 ]; char szIniFile[ 200 ]; int nSkipAll = 0; /*-f for skip other arguments...*/ memset(szIniFile , 0 , sizeof(szIniFile)); switch( optch ) { case 'f': /*load arguments from file*/ strcpy(szIniFile , optarg ); util_trim( szIniFile ); if( strlen( szIniFile ) == 0 ) { print_usage(); exit( -3 ); } if( szIniFile[ 0 ] != '/' ) /*it is not absolute path*/ /*using current directory as base directory*/ { char szTemp[ 200 ]; memset( szTemp , 0 , sizeof( szTemp )); if( getcwd( szTemp , sizeof( szTemp ) ) == 0 ) { printf("get current working directory failed!\n"); util_err_log("magicd: get current working directory failed!" , __FILE__ , __LINE__ , errno); exit( -4 ); } if( szTemp[ strlen(szTemp) - 1 ] != '/') strcat( szTemp , "/" ); strcat( szTemp , szIniFile ); memset( szIniFile , 0 , sizeof(szIniFile)); strcpy( szIniFile , szTemp ); } if ( load_inifile(szIniFile) == -1 ) { printf("get infomation from %s failed , please check it!" , szIniFile); exit( -4 ); } nSkipAll = 1; break; case 'p': port = atoi( optarg ); if( port <= 0 ) { print_usage(); exit( -3 ); } break; case 'c': telnet_col = atoi( optarg ); if( telnet_col <= 0 ) { print_usage(); exit(-3 ); } break; case 'r': telnet_row = atoi( optarg ); if( telnet_row <= 0 ) { print_usage(); exit(-3 ); } break; case 'L': strcpy( logname , optarg ); if( logname[ 0 ] != '/' ) { print_usage(); exit(-3 ); } break; case 'a': /*get user name setup by user himself*/ memset(szTemp , 0 , sizeof(szTemp)); memset(szNoRootUsername , 0 , sizeof(szNoRootUsername)); memset(szNoRootPasswd , 0 , sizeof(szNoRootPasswd)); strcpy(szTemp , optarg); printf("optarg=%s\n", optarg); util_trim( szTemp ); printf("username/password=%s\n", szTemp); p1 = strchr( szTemp , ',' ); if( p1 == NULL ) strcpy( szNoRootUsername ,szTemp ); else { strncpy(szNoRootUsername , optarg , p1 - szTemp ); printf("username=%s\n" , szNoRootUsername); strcpy(szNoRootPasswd , p1 + 1 ); printf("password=%s\n" , szNoRootPasswd); } login = 1; /*this means using self-verify*/ break; case 'h': print_usage(); exit( -3 ); case 'v': printf("verion=%s\n" , VERSION); exit( -3 ); case 'd': debug = 1; break; default : print_usage(); exit(-3); } if( nSkipAll ) break; } printf("Starting Magic C++ Server daemon program...\n"); printf("Install Root Directory is :%s \n",szInstallDir ); if( debug ) printf("Login program on your machine is :%s\n", logname ); if( login == 0 && getuid() != 0 ) { printf( "You don't have the superuser privilege!please using the argument '-a' to define user name and password\n"); print_usage(); exit(3); } args[ 0 ] = (char *)malloc(strlen( logname ) + 1 ); if( !args[ 0 ] ) { printf("Malloc error\n"); exit(2); } strcpy(args[0] , logname); args[ 1 ] = NULL; /* Set up tty modes for the pty */ (void) tty_getmode(0); /* * Open a TCP socket (an Internet stream socket). */ if ( (fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("Can't open stream socket\n"); exit(3); } /* * Bind our local address so that the client can send to us. */ d_zero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(port); if (bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) { printf("Can't bind local address...\n"); exit(3); } if ( listen(fd, 3) < 0 ) { printf("listen() error\n"); util_sem_remove(); exit(3); } /* Run as a daemon, if desired. */ printf("Magic C++ Server Daemon Startup successfully on port [%d]...\n",port ); printf("Copyright(c) 2004 MagicUnix Information Technology Limited. All right reserved\n"); daemonize(); util_log("System Startup successfully on port:%d...\n",port); /*init ipcs*/ if( util_ipc_init() == -1 ) { util_err_log("IPC Initialize failed!\n",__FILE__,__LINE__,errno ); exit( 3 ); } util_log("Initialize IPC successfully...\n"); signal(SIGPIPE, SIG_IGN); /* Don't die on broken connections */ /*#if defined(SOLARIS) || defined( FREEBSD )*/ if( signal( SIGCHLD , relief ) == SIG_ERR ) { util_err_log( "Can not signal!\n",__FILE__,__LINE__,errno); exit(3); } /*#else if( signal( SIGCLD , SIG_IGN ) == SIG_ERR ) { util_err_log( "Can not signal!\n",__FILE__,__LINE__,errno); exit(3); }#endif*/ if( signal( SIGTERM , killed ) == SIG_ERR ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -