密钥管理页面的实现


密钥管理页面的实现_第1张图片

密钥管理页面的实现_第2张图片

密钥管理页面的实现_第3张图片



实现的功能:可以实现公钥的管理
功能1:可以有保存开启公钥
功能2: 可以单个删除、批量删除
功能3:可以添加公钥



#include "../lib/slas_common.h"
#include "../lib/log_defines.h"
#include "../lib/slas_paths.h"
#include "../lib/slas_defines.h"
#include "../lib/slas_structs.h"
#include "../lib/slas.h"
#include "../lib/utils.h"
#include "../lib/cgi.h"
#include "../lib/libpq-fe.h"
#include "../libso/sessplan.h"
#include "../libso/session.h"
#include "../libso/logs.h"


//#define PULIC_KTY "/storage/etc/ssh_public/"
#define PULIC_KTY "/etc/ssh_public/"

int CheckXm(char *);
int CheckRecord(char *);
int CheckB64(char *);
void changejh(char *);

typedef struct _ManagementList{
	char key[1024];
	char name[256];
	struct _ManagementList *next;	
}ManagementList;

/*
//获取公钥列表
int ManListGet(char *user, ManagementList* head, int public_key){
	FILE *fp = NULL, *fp_name = NULL;
	char temp_key[1024];
	char temp_name[512];	
	char path_key[1024];
	char path_key1[512];
	char path_name[1024];
	char cmd[512];
	int count = 0;
	
	sprintf(path_key, "%s%s_authorized_keys", PULIC_KTY, user);	
	sprintf(path_key1, "%s%s_authorized_keys.un", PULIC_KTY, user);	
	sprintf(path_name, "%s%s_authorized_name", PULIC_KTY, user);
	
	if(public_key){//开启公钥
		if (!access(path_key, F_OK)){		
			while(fgets(temp_key, 1024, fp)){
			
				ManagementList *newnode = (ManagementList *)malloc(sizeof(ManagementList));			
				strcpy(newnode->key, path_key);
				sprintf(newnode->name, "公钥-%d\n", count);
				
				if (!(fp_name = fopen(path_name, "a+"))) 				//写入对应的公钥名称
				{
					return -1;
				}
				fputs(newnode->name, fp_name);
				
				newnode->next = NULL;
				if(head->next == NULL){
					head->next = newnode;
					node = head->next;
				}else {
					node->next = newnode;
					node = node->next;
				}
								
				count++;
			}

		}
	}else{
		if (NULL != access(path_key, F_OK)){
			return -1
		}
			memset(cmd, 0, sizeof(cmd));
			sprintf(cmd, "mv -f %s %s", path_key, path_key1);//读取没有开启的公钥文件
			system(cmd);
			chmod(path_key1, 0600);			
			while(fgets(temp_key, 1024, fp)){
				ManagementList *newnode = (ManagementList *)malloc(sizeof(ManagementList));			
				strcpy(newnode->key, path_key);
				sprintf(newnode->name, "公钥-%d\n", count);
				
				if (NULL == (fp_name = fopen(path_name, "a+"))) return -5; //写入对应的公钥名称
					fputs(newnode->name, fp_name);
				
				newnode->next = NULL;
				if(head->next == NULL){
					head->next = newnode;
					node = head->next;
				}else {
					node->next = newnode;
					node = node->next;
				}
								
				count++;
			}		
	}
	
	fclose(fp);
	return count;
}
*/

int ManListAdd(char *user, char *description,  char *keyname, int public_key) {
	FILE *fp = NULL, *fp1 = NULL;
//	char cmd[256];
	char path[1024];
	char path1[1024];
	char path2[1024];	
	int sgin_key = 0, sgin_key1 = 0;
	
	memset(path, 0, sizeof(path));
	memset(path1, 0, sizeof(path1));
	memset(path2, 0, sizeof(path2));		
	sprintf(path, "%s%s_authorized_keys", PULIC_KTY, user);	
	sprintf(path1, "%s%s_authorized_keys.un", PULIC_KTY, user);	
	sprintf(path2, "%s%s_authorized_name", PULIC_KTY, user);		

	
	if (access(path, F_OK)){
		sgin_key = 1;
	}
	if (access(path1, F_OK)){
		sgin_key1 = 1;
	}	

	if(sgin_key == 1 && sgin_key1 == 1){
		if(public_key==1){
			if (NULL == (fp = fopen(path, "a+"))){	
				printf("open file failed %s:%s\n",path,strerror(errno));
				return -1;
			}
		}else{
			if (NULL == (fp = fopen(path1, "a+"))){	
				printf("open file failed %s:%s\n",path,strerror(errno));
				return -1;
			}		
		}
	}
	else if(!sgin_key){
		if (NULL == (fp = fopen(path, "a+"))){	
			printf("open file failed %s:%s\n",path,strerror(errno));
			return -1;
		}
		fprintf(fp, "%s\n", description);
	}
	else {
		if (NULL == (fp = fopen(path1, "a+"))){	
			printf("open file failed %s:%s\n",path,strerror(errno));
			return -1;
		}
		fprintf(fp, "%s\n", description);
	}
    
	if (NULL == (fp1 = fopen(path2, "a+"))){	
		printf("open file failed %s:%s\n",path2,strerror(errno));
		return -1;
	}
	fprintf(fp1, "%s\n", keyname);
	

	fclose(fp);
	fclose(fp1);
//	memset(cmd, 0, sizeof(cmd));
//	sprintf(cmd, "mv -f %s.swap %s", path, path1);
	//system(cmd);
	chmod(path, 0600);
	chmod(path1, 0600);	
	return 0;
}





int Pub_keyGet(char *user){
	FILE *fp = NULL;
	char temp[10];
	char path[1024];
	sprintf(path, "%s%s_key", PULIC_KTY, user);	
	if (access(path, F_OK)) return 0;
	if (NULL == (fp = fopen(path, "r"))) return -1;


	fgets(temp, 9, fp);
	if(!strncmp(temp, "false",5)){
		return 0;
	}else{
		return 1;
	}
}

int Pub_keySet(int public_key, char *user) {
	FILE *fp = NULL;
	char path[1024];
	memset(path, 0, sizeof(path));

	sprintf(path, "%s%s_key", PULIC_KTY, user);	
	if (NULL == (fp = fopen(path, "w+"))){	
		printf("open file failed %s:%s\n",path,strerror(errno));
		return -1;
	}
	if(public_key == 0)
		fprintf(fp, "false\n");
	else
		fprintf(fp, "true\n");

	fclose(fp);
	chmod(path, 0666);
	return 0;
}


/*
int Delstr(int pubilc_key, char *user, char *delstr){
	FILE *fp = NULL, *fp2 = NULL, *fp_name = NULL, *fp_name1 = NULL;
	char path[1024], path1[1024], path_name[1024], buf[1024], buftemp[1024], path_name1[1024];
	int i = 0,  sgin = 1, sgin_key = 0, sgin_key1 = 0;
	memset(path, 0, sizeof(path));
	memset(path1, 0, sizeof(path1));
	memset(path_name, 0, sizeof(path_name));	


	if (access(path, F_OK)){
		sgin_key = 1;
	}
	if (access(path1, F_OK)){
		sgin_key1 = 1;
	}

	if(pubilc_key){
		sprintf(path, "%s%s_authorized_keys", PULIC_KTY, user);	
		if (NULL == (fp = fopen(path, "r"))){	
			printf("open file failed %s:%s\n",path,strerror(errno));
			return -1;
		}
		
		
		while(fgets(buf, 1024, fp)){
			strcpy(buftemp,buf);
			if(strncmp(buf,delstr,strlen(buf)-1))  //起始key不符合
			{
				fputs(buftemp, fp2);				//写入新文件
				i++;
				continue;	
			}else{
				sgin = i;
			}			
		}

		
		sprintf(path, "%s%s_authorized_name", PULIC_KTY, user);			
		if (NULL == (fp_name = fopen(path_name, "w+"))){	
			printf("open file failed %s:%s\n",path,strerror(errno));
			return -1;
		}		
		while(fgets(buf, 1024, fp_name)){
			strcpy(buftemp,buf);
			if(sgin!= i)  //起始key不符合
			{
				fputs(buftemp, fp2);				//写入新文件
				continue;	
			}			
		}
		
		fclose(fp);
		fclose(fp_name);		
		fclose(fp2);
		if(access(path1,F_OK) == 0) {
				rename(path1,path);
		}
		
	}else{
		sprintf(path, "%s%s_authorized_keys", PULIC_KTY, user);	
		if (NULL == (fp = fopen(path, "w+"))){	
			printf("open file failed %s:%s\n",path,strerror(errno));
			return -2;
		}	
		
		sprintf(path1, "%s%s_authorized_keys.un", PULIC_KTY, user);	
		if (NULL == (fp2 = fopen(path1, "r"))){	
			printf("open file failed %s:%s\n",path,strerror(errno));
			return -2;
		}		
		
		while(fgets(buf, 1024, fp2)){
			strcpy(buftemp,buf);
			if(strncmp(buf,delstr,strlen(buf)-1))   //起始key不符合
			{
				fputs(buftemp, fp);	//写入新文件
				continue;	
			}else{
				sgin = i++;
				trace("sgin=%d\n\n", sgin);
			}	
			i++;			
		}

		
		sprintf(path_name1, "%s%s_authorized_name.un", PULIC_KTY, user);	
		if (NULL == (fp_name1 = fopen(path_name1, "w+"))){	
			printf("open file failed %s:%s\n",path,strerror(errno));
			return -2;
		}	
		
		sprintf(path_name, "%s%s_authorized_name", PULIC_KTY, user);		
		if (NULL == (fp_name = fopen(path_name, "r"))){	
			printf("open file failed %s:%s\n",path,strerror(errno));
			return -3;
		}			
	
		i=0;	
		while(fgets(buf, 1024, fp_name)){
			strcpy(buftemp,buf);

			if(sgin != i)  //起始key不符合
			{
				fputs(buftemp, fp_name1);				//写入新文件
				continue;	
			}
			i++;
		}	

		
		fclose(fp);
		fclose(fp2);		
		fclose(fp_name);
		fclose(fp_name1);
		if(access(path1,F_OK) == 0) {
			rename(path,path1);
		}
		if(access(path1,F_OK) == 0) {
			rename(path_name1,path_name);
		}			
	}
	
	return 0;
}

*/



int Delstr(int pubilc_key, char *user, char *delstr){
	FILE  *fp_name = NULL, *new_fp_name = NULL, *fp_key = NULL, *new_fp_key =NULL;
	char path_key[1024], new_path_key[1024], path_name[1024], new_path_name[1024], buf[1024], *str1 = NULL, *tok1 = NULL;
	int i = 0, ii = 0, iii=0, strvalue[1024], sgin = 0;
	memset(path_key, 0, sizeof(path_key));
	memset(new_path_key, 0, sizeof(new_path_key));
	memset(path_name, 0, sizeof(path_name));
	memset(new_path_name, 0, sizeof(new_path_name));	
	memset(strvalue, 0, sizeof(strvalue));	
	
	for(str1 = strtok_r(delstr, ",", &tok1); NULL != str1; str1 = strtok_r(NULL, ",", &tok1)){
		strvalue[ii] = atoi(str1);
		ii++;
	}
	
	--ii;

	if(pubilc_key){
		sprintf(path_key, "%s%s_authorized_keys", PULIC_KTY, user);	
		if (NULL == (fp_key = fopen(path_key, "r"))){	
			printf("open file failed %s:%s\n",path_key,strerror(errno));
			return -1;
		}

		sprintf(new_path_key, "%s%s_authorized_keys.new", PULIC_KTY, user);	
		if (NULL == (new_fp_key = fopen(new_path_key, "w+"))){	
			printf("open file failed %s:%s\n",new_path_key,strerror(errno));
			return -1;
		}
		
		i = 0;
		for(i=0; fgets(buf, 1024, fp_key); i++){
			for(iii=0; iii<= ii; iii++){
				if(strvalue[iii] == i){
					sgin = 1;				
				}
			}
		
			if(	sgin != 1)   //起始key不符合
			{
				fputs(buf, new_fp_key);	//写入新文件
				continue;	
			}
			sgin = 0;
		}
		
		sprintf(path_name, "%s%s_authorized_name", PULIC_KTY, user);			
		if (NULL == (fp_name = fopen(path_name, "r"))){	
			printf("open file failed %s:%s\n",path_name,strerror(errno));
			return -1;
		}	

		sprintf(new_path_name, "%s%s_authorized_name.new", PULIC_KTY, user);			
		if (NULL == (new_fp_name = fopen(new_path_name, "w+"))){	
			printf("open file failed %s:%s\n",new_path_name,strerror(errno));
			return -1;
		}	
		
		i = 0;
		for(i=0;fgets(buf, 1024, fp_name); i++){
			for(iii=0; iii<= ii; iii++){
				if(strvalue[iii] == i){
					sgin= 1;
				}
			}
			if(	sgin != 1)   //起始key不符合
			{
				fputs(buf, new_fp_name);	//写入新文件
				continue;	
			}
			sgin = 0;
		}	
		
		fclose(fp_key);
		fclose(new_fp_key);
		fclose(fp_name);
		fclose(new_fp_name);
		
		rename(new_path_key,path_key);
		rename(new_path_name,path_name);
		
	}else{
		sprintf(new_path_key, "%s%s_authorized_keys.un.new", PULIC_KTY, user);	
		if (NULL == (new_fp_key = fopen(new_path_key, "w+"))){	
			printf("open file failed %s:%s\n",new_path_key,strerror(errno));
			return -2;
		}	
		
		sprintf(path_key, "%s%s_authorized_keys.un", PULIC_KTY, user);	
		if (NULL == (fp_key = fopen(path_key, "r"))){	
			printf("open file failed %s:%s\n",path_key,strerror(errno));
			return -3;
		}		
		
		for(i=0; fgets(buf, 1024, fp_key); i++){
			for(iii=0; iii<= ii; iii++){
				if(strvalue[iii] == i){
					sgin = 1;				
				}
			}
		
			if(	sgin != 1)   //起始key不符合
			{
				fputs(buf, new_fp_key);	//写入新文件
				continue;	
			}
			sgin = 0;
		}

		
		sprintf(new_path_name, "%s%s_authorized_name.un", PULIC_KTY, user);	
		if (NULL == (new_fp_name = fopen(new_path_name, "w+"))){	
			printf("open file failed %s:%s\n",new_path_name,strerror(errno));
			return -2;
		}	
		
		sprintf(path_name, "%s%s_authorized_name", PULIC_KTY, user);		
		if (NULL == (fp_name = fopen(path_name, "r"))){	
			printf("open file failed %s:%s\n", path_name,strerror(errno));
			return -3;
		}			
	
	
		for(i=0;fgets(buf, 1024, fp_name); i++){
			for(iii=0; iii<= ii; iii++){
				if(strvalue[iii] == i){
					sgin= 1;
				}
			}
			if(	sgin != 1)   //起始key不符合
			{
				fputs(buf, new_fp_name);	//写入新文件
				continue;	
			}
			sgin = 0;
		}	

		
		fclose(fp_key);
		fclose(new_fp_key);		
		fclose(fp_name);
		fclose(new_fp_name);

		rename(new_path_key,path_key);
		rename(new_path_name,path_name);


	/*	memset(cmd, 0, sizeof(cmd));
		sprintf(cmd, "mv -f %s %s", new_path_key, path_key);
		system(cmd);
		chmod(path_key, 0600);

		memset(cmd, 0, sizeof(cmd));
		sprintf(cmd, "mv -f %s %s", new_path_name, path_name);
		system(cmd);
		chmod(path_name, 0600);*/
	
		
	}
	
	return 0;
}







void showhtml(u_int64 session, char *user){
	int count = 0, i = 0, public_key = 0, sgin_key = 0, sgin_key1 = 0; 
	ManagementList *head = NULL;
	ManagementList *node = NULL;
	head = (ManagementList *)malloc(sizeof(ManagementList));
	head->next = NULL;
	memset(head,0,sizeof(ManagementList));

	FILE *fp = NULL, *fp1 = NULL, *fp_name = NULL;	
	char path_key[1024];
	char path_key1[2048];
	char path_name[1024];
	char buf[2048];
	char cmd[512];
	
	sprintf(path_key, "%s%s_authorized_keys", PULIC_KTY, user);	
	sprintf(path_key1, "%s%s_authorized_keys.un", PULIC_KTY, user);	
	sprintf(path_name, "%s%s_authorized_name", PULIC_KTY, user);

	if (access(path_key, F_OK)){
		sgin_key = 1;
	}
	if (access(path_key1, F_OK)){
		sgin_key1 = 1;
	}

	
	public_key = Pub_keyGet(user);
	
	if(public_key == -1){
		printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('公钥开启文件打开失败',function(){history.back();});</script></body></html>");		
		return; 
	}
	
	if(public_key==1){//开启公钥1
		if (sgin_key==1 && sgin_key1 == 1){
			if(!(fp = fopen(path_key, "w+"))){
				printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('公钥备注列表打开失败',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
				return; 
			}
			fclose(fp);
		}else{
			if(!sgin_key1){
				memset(cmd, 0, sizeof(cmd));
				sprintf(cmd, "mv -f %s %s", path_key1, path_key);//读取没有开启的公钥文件	
				system(cmd);
				chmod(path_key, 0666);	
				
				if(!(fp = fopen(path_key, "r"))){
					printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('公钥备注列表打开失败',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
					return; 
				}
				if (!(fp_name = fopen(path_name, "r"))) 				//写入对应的公钥名称
				{
					printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('获取公钥备注列表错误1',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
					return;
				}					
		
				while(fgets(path_key, 1024, fp) && fgets(buf, 1024, fp_name)){
					ManagementList *newnode = (ManagementList *)malloc(sizeof(ManagementList));			
					strncpy(newnode->key,  path_key, strlen(path_key)-1);


					strncpy(newnode->name,  buf, strlen(buf)-1);
					
				
					newnode->next = NULL;
					if(head->next == NULL){
						head->next = newnode;
						node = head->next;
					}else {
						node->next = newnode;
						node = node->next;
					}		
					count++;
				}
						
				fclose(fp);			
				fclose(fp_name);
			}else{
				if (access(path_key, F_OK)){
					if (!(fp = fopen(path_key, "w+"))) 				//写入对应的公钥名称
					{
						printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('获取公钥备注列表错误1',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
						return;
					}
				}else{			
					if(!(fp = fopen(path_key, "r"))){
						printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('公钥备注列表打开失败',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
						return; 
					}					
				}
				
				
				if (access(path_name, F_OK)){
					if (!(fp_name = fopen(path_name, "w+"))) 				//写入对应的公钥名称
					{
						printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('获取公钥备注列表错误1',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
						return;
					}
				}else{			
					if (!(fp_name = fopen(path_name, "r"))) 				//写入对应的公钥名称
					{
						printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('获取公钥备注列表错误1',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
						return;
					}					
				}
									
						
				while(fgets(path_key, 1024, fp) && fgets(buf, 1024, fp_name)){
					ManagementList *newnode = (ManagementList *)malloc(sizeof(ManagementList));			
					strncpy(newnode->key,  path_key, strlen(path_key)-1);


					strncpy(newnode->name,  buf, strlen(buf)-1);
					
				
					newnode->next = NULL;
					if(head->next == NULL){
						head->next = newnode;
						node = head->next;
					}else {
						node->next = newnode;
						node = node->next;
					}		
					count++;
				}
			fclose(fp);			
			fclose(fp_name);	
			}
		}	

	}else{//读取没有开启的公钥文件
		if (sgin_key==1 && sgin_key1 == 1){
			if(!(fp1 = fopen(path_key1, "w+"))){
				printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('公钥备注列表打开失败',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
				return; 
			}
			fclose(fp1);				
		}else{
			if(!sgin_key){
				memset(cmd, 0, sizeof(cmd));
				sprintf(cmd, "mv -f %s %s", path_key, path_key1);//读取没有开启的公钥文件	
				system(cmd);
				chmod(path_key1, 0666);			
				if(!(fp1 = fopen(path_key1, "r"))){
					printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('公钥备注列表打开失败',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
					return; 
				}		
			}else{
				if(!(fp1 = fopen(path_key1, "r"))){
					printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('公钥备注列表打开失败',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
					return; 
				}			
			}

			if (access(path_name, F_OK)){
				if (!(fp_name = fopen(path_name, "w+"))) 				//写入对应的公钥名称
				{
					printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('获取公钥备注列表错误1',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
					return;
				}
			}else{			
				if (!(fp_name = fopen(path_name, "r"))) 				//写入对应的公钥名称
				{
					printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('获取公钥备注列表错误1',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);		
					return;
				}					
			}
			
			while(fgets(path_key, 1024, fp1) && fgets(buf, 1024, fp_name)){
				ManagementList *newnode = (ManagementList *)malloc(sizeof(ManagementList));			
				strncpy(newnode->key,  path_key, strlen(path_key)-1);


				strncpy(newnode->name,  buf, strlen(buf)-1);
				
			
				newnode->next = NULL;
				if(head->next == NULL){
					head->next = newnode;
					node = head->next;
				}else {
					node->next = newnode;
					node = node->next;
				}		
				count++;
			}
			
			fclose(fp1);			
			fclose(fp_name);
		}	
	}

	
	
	node = head->next;


	printf("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
	printf("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
	printf("<head>\n");
	printf("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />\n");
	printf("<link href=\"/bh/style/middle_style.css\" rel=\"stylesheet\" type=\"text/css\"/>\n");
	printf("<script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script>\n");
	printf("<script src=\"/bh/pjs/jquery.wresize.js\" type=\"text/javascript\"></script>\n");
	printf("<script src=\"/bh/pjs/contentboxdivresiez.js\" type=\"text/javascript\"></script>\n");
	printf("<script src=\"/bh/pjs/tab_style.js\" type=\"text/javascript\"></script>\n");
	printf("<script src=\"/bh/pjs/table_litmenu_100.js\" type=\"text/javascript\"></script>\n");
	printf("<script src=\"/bh/pjs/bindpage.js\" type=\"text/javascript\"></script>\n");
	printf("<script src=\"/bh/pjs/common.js\" language=\"javascript\" type=\"text/javascript\"></script>\n");
	printf("<script src=\"/bh/pjs/bh_user.js\" language=\"javascript\" type=\"text/javascript\"></script>");
	printf("<script type=\"text/javascript\"> \n");
	printf("$(function(){\n");
	printf("	tabEvent(\"#tabledivbox\");\n");
	printf("	litmenu(\"#tabledivbox\");\n");
	printf("	wwinsize(0,180);//传入合适高度\n");
	printf("	\n");
	printf("	//阻止冒泡\n");
	printf("	 $(\"#content tbody tr td:first input:checkbox\").click(function(event){\n");
	printf("		event.stopPropagation();\n");
	printf("	});\n");
	if(public_key == 1){
		printf("	$(\"#public_key\").prop(\"checked\",true); \n");	
	}else
		printf("	$(\"#public_key\").prop(\"checked\",false); \n");	
	printf("})\n");
	printf("</script>\n");
	printf("</head>\n");
	printf("<body>\n");
	printf("<form action=\"\" method=\"post\" name=\"frm\">\n");
	printf("<input type=\"hidden\" name=\"tasktype\" id=\"tasktype\" value=\"\">\n");
	printf("<input type=\"hidden\" name=\"session\" id=\"session\" value=\"%llu\">\n",session);
	printf("<input type=\"hidden\" name=\"delstr\" id=\"delstr\" value=\"\">\n");

	printf("<div class=\"head-box\" style=\"width:auto;border-left:1px solid #d8d8d8;border-right:1px solid #d8d8d8\">\n");
	printf("	<h3 class=\"now\">ssh public key</h3>\n");
	printf("	<div class=\"guolvbox divrightbox\">\n");
	printf("		<span _style=\"margin-top:2px\"><label><input type=\"checkbox\" name=\"public_key\" id=\"public_key\" value=\"1\">是否开启公钥</label></span>\n");
	printf("	</div>");
	printf("</div>\n");

	printf("<div id=\"tabledivbox\" >\n");
	printf("<!--此div为小菜单--><div id=\"poslitmenubox\"><div id=\"poslitmenu\"></div></div>\n");
	printf("<!--表头-->\n");
	printf("<div style=\"border:1px solid #ccc;border-top:none;width:100%%;position:absolute;height:29px;z-index:0;margin-left:-1px;\">\n");
	printf("</div>\n");
	printf("<div class=\"faux-head\">\n");
	printf("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"width:100%%;\" id=\"tab-head\">\n");
	printf("<thead>\n");
	printf("<tr>\n");
	printf("<th style=\"width:50px;\"><input type=\"checkbox\" name=\"d_all\" id=\"d_all\"  value=\"CheckAll\" onClick=\"checkall(this,%d,'d');\"/></th>\n", count);
	printf("<th style=\"width:320px\">公钥名称</th>\n");
	printf("<th style=\"background-image:none\">公钥内容</th>\n");
	printf("<th style=\"width:120px\" class=\"mangerlei\">管理</th>\n");
	printf("</tr>\n");
	printf("</thead>\n");
	printf("</table>\n");
	printf("</div>\n");
	printf("<div style=\"height:29px;width:100%%;background:#f2f2f2;\"></div>\n");
	printf("<!--此div包含作用为出滚动条-->\n");
	printf("<div class=\"tableboabox\">\n");
	printf("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"width:100%%;\" id=\"content\">\n");
	printf("<!--此处为表格实际宽度,如果表格不需要出现小菜单则把表格宽度设为100%%-->\n");
	printf("<tbody>\n");

	if(count<0){
//		printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('获取公钥备注列表错误%d',function(){top.location.href='/';});</script></body></html>",count);
		printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('获取公钥备注列表错误%d',function(){history.back();});</script></body></html>",count);
		return;
	}		
	while (node){
		printf(" <tr>\n");
		printf("		<td style=\"width:50px\"><input type=\"checkbox\" name=\"d_%d\" id=\"d_%d\" onclick=\"checkqs(this,%d);\" value=\"%d\"/></td>\n",i,i,i,i);
		printf("        <td style=\"width:320px\">%s</td>\n", node->name);
		printf("        <td><span class=\"kill\" style=\"width:300px\">%s</span></td>\n", node->key);
		printf("		<td class=\"mangerlei\" style=\"width:120px\"><a href=\"#@\" onclick=\"key_del('%d');\">删除</a></td>\n", i);
		printf(" </tr>\n");
		i++;
		node = node->next;	
	}


	printf("</tbody>\n");
	printf("</table>\n");
	printf("</div>\n");
	printf("</div>\n");


	printf("	<div class=\"diag\" id=\"Dialog01\">\n");
	printf("		<div class=\"diag1\">\n");
	printf("			<div class=\"head-box-main diagh\">\n");
	printf("				<h3 class=\"now diagh3\">公钥列表</h3>\n");
	printf("			 <a class=\"dialog-close\" onclick=\"closeDialog('#Dialog01')\" href=\"javascript:void(0)\"></a>\n");
	printf("		   </div>\n");
	printf("			<!-- head-box-main end  -->\n");
	printf("			<div class=\"diag_main\">\n");
	printf("				<div class=\"diag_main_c\">\n");
	printf("					<ul id=\"TypeList\" class=\"TypeList\">\n");
	printf("						<li>\n");
	printf("							<span style=\"float:right;\"></span>公钥名称:<input name=\"keyname\" id=\"keyname\" type=\"text\" maxlength=\"32\" class=\"textbox\" value=\"\" style=\"margin-right:20px;\" > \n");
	printf("						</li>\n");
	printf("						<li>\n");
	printf("							<span style=\"float:right;\"></span>公钥内容:\n");
	printf("						</li>\n");
	printf("						<li>\n");
	printf("							<td><textarea name=\"description\" id=\"description\" cols=\"60\" rows=\"5\" onpropertychange=\"if(document.frm.description.value.length>1023) {value=value.substr(0,1023);alert('输入字符已达到最大值!');}\"></textarea></td> \n");
	printf("						</li>\n");
	printf("					</ul>\n");
	printf("				   <!-- TypeList end  -->\n");
	printf("				   <div class=\"diag_btn\">\n");
	printf("						<a class=\"btn_four_grly\" id=\"save\" onclick=\"Remark_Save();\" href=\"javascript:void(0);\">确定</a>\n");
	printf("						<a class=\"btn_four_grly\"  id=\"cancel\" onclick=\"Cancel();\" href=\"javascript:void(0);\">返回</a>\n");
	printf("				   </div>\n");
	printf("				   <!-- diag_btn  -->\n");
	printf("				</div>\n");
	printf("				<!-- diag_main_c end -->\n");
	printf("			</div>\n");
	printf("			<!-- diag_main end  -->\n");
	printf("			<div class=\"diag_footer\"></div>\n");
	printf("	\n");		
	printf("		</div>\n");
	printf("		 <!-- diag1 end  -->\n");
	printf("	</div>\n");
	
	
	printf("<div class=\"fenyelistbox\">\n");
	printf("<div class=\"ctrl-btn\" style=\"padding:0;margin-left:-10px;_margin-left:-5px\">");
	printf("<a class=\"btn_four_blue\" href=\"#@\" onclick=\"getkeyplanTotalStr('%d');\">批量删除</a>", count);
	printf(" <a href=\"#@\" class=\"btn_four_grly\" id=\"add\" onclick=\"__getDialog('#Dialog01');\">添加</a></div>\n");
	printf(" <a href=\"#@\" class=\"btn_four_grly\" id=\"baocun\" onclick=\"Save();\">保存</a></div>\n");
	printf("	</div>\n");
	printf("</form>\n");
	printf("</body>\n");
	printf("<script>\n");
	printf("function gettype() {\n");
	printf("	var reg_ip = /^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$/;\n");
	printf("	var reg_ip = /^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$/;\n");
	printf("	if(!reg_ip.test(document.frm.ip.value)) document.frm.type.value = 1;\n");
	printf("	else document.frm.type.value = 0;\n");
	printf("}\n");
	
	//保存
	printf("function Save(){\n ");
	printf("	document.frm.tasktype.value = 2;\n");	
	printf("	document.frm.submit();	\n");	
	printf("}\n");
	
	
 	printf("function __getDialog(obj){\n");
  
	printf("	var ww = $(window).width(), wh = $(window).height();\n");
	printf("	var ow = $(obj).width(), oh = $(obj).height();\n");
	printf("	var p = document.documentElement.scrollTop || document.body.scrollTop;\n");

	printf("	$(obj).css({\n");
	printf("		\"top\":(wh - oh)/2+p > 0 ? (wh - oh)/2+p : 0,\n");
	printf("		\"left\":(ww - ow)/2 > 0 ? (ww - ow)/2 :0\n");
	printf("	}).fadeIn(\"fast\");\n");
	
	printf("	$(window).wresize(function(){\n");
	printf("		var ww = $(window).width(), wh = $(window).height();\n");
	printf("	var ow = $(obj).width(), oh = $(obj).height();\n");
	printf("		var p = document.documentElement.scrollTop || document.body.scrollTop;\n");

	printf("		$(obj).css({\n");
	printf("			\"top\":(wh - oh)/2+p > 0 ? (wh - oh)/2+p : 0,\n");
	printf("		\"left\":(ww - ow)/2 > 0 ? (ww - ow)/2 :0\n");
	printf("		});\n");
	printf("	})\n");
	printf("}	\n");
	
	printf("function Cancel(){\n");
	printf("	closeDialog('#Dialog01');\n");
	printf("}\n");


	printf("function key_del(key){\n");
	printf("	confirm(\"确定删除吗?\",function(){\n");
	printf("	document.frm.delstr.value = key;\n");
//	printf("	document.frm.action = 'key_management.cgi';\n");
	printf("	document.frm.tasktype.value = 1;\n");
	printf("	document.frm.submit();\n");
	printf("	});\n");
	printf("}\n");


	printf("function getkeyplanTotalStr(count){\n");
	printf("	var i;\n");
	printf("	var objd;\n");
	printf("	document.frm.delstr.value = '';\n");
	printf("	for(i=0;i<count;i++){\n");
	printf("		objd  = document.getElementById(\"d_\"+i);\n");
	printf("		if(objd.checked == true){\n");
	printf("			document.frm.delstr.value += objd.value + \",\";\n");
	printf("		}\n");
	printf("	}\n");
	printf("	document.frm.tasktype.value = 1;\n");
	printf("	document.frm.submit();\n");
	printf("}\n");
	
	
//备注保存检查
	printf("function Remark_Save(){\n");
	printf("	if($(\"#keyname\").val()==\"\"){\n");
	printf("		alert('请填写公钥名称!');\n");
	printf("		return false;\n");
	printf("	}\n");	
	printf("	if($(\"#description\").val()==\"\"){\n");
	printf("		alert('请填写操作内容!');\n");
	printf("		return false;\n");
	printf("	}\n");
	printf("\n");						
	printf("	if($(\"#description\").val().length>1023) {\n");
	printf("		//value=value.substr(0,255);\n");
	printf("		alert('输入字符已达到最大值!');\n");
	printf("		return false;\n");
	printf("	}\n");
	
	printf("	var Cts = $(\"#description\").val();\n");
	printf("	if(Cts.indexOf(\"\\n\") >= 0 ) {\n");
	printf("		alert('公钥名称('+$(\"#keyname\").val()+')输入字符有特殊符号换行符!');\n");
	printf("		return false;\n");
	printf("	}\n");	

	
	
	printf("	document.frm.tasktype.value = 3;\n");
	printf("	document.frm.description.value=$(\"#description\").val();\n");
	printf("	document.frm.keyname.value=$(\"#keyname\").val();\n");
	printf("	 document.frm.submit();	\n");	
	printf("}\n");	
	
	printf("function closeDialog(obj){\n");
	printf("$(obj).hide();\n");
	printf("}\n");
	
	printf("</script>\n");
	printf("</html>\n");
	printf("<style type='text/css'> \n");
	printf("#ConnectHTML select{margin:4px 0 0 0;}\n");
	printf("span.kill{word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;display:inline-block}\n");

	printf(".fdialog{width:820px;height:280px;overflow:hidden;border-radius:5px;}\n");
	printf(".fdialog .dialog-bg{width:2000px;height:2000px;background:#036;opacity:0.8;filter:alpha(opacity=80);}\n");
	printf(".fdialog .dialog-cont{width:800px;height:auto;overflow:hidden;margin:10px;padding:0;}\n");
	printf(".fdialog .dialog-title,.fdialog .dialog-content{width:100%%;}\n");
	printf(".fdialog .dialog-content{height:auto;border:0;padding:0;}\n");

	printf(".diag{position:absolute; top:50%%; left:50%%; width:585px; margin-left:-29px; margin-top:-145px;z-index:100; display:none;}\n");
	printf(".diag1{background:url(/bh/images/dialog_bg01.gif) 0 0 no-repeat; padding-top:10px;float:left;}\n");
	printf(".diagh{border-left: #d8d8d8 1px solid; width: auto; border-right: #d8d8d8 1px solid; margin:0 12px 0 11px; width:560px; float:left;}\n");
	printf(".diag_main{background:url(/bh/images/dialog_bg01_h.gif) 0 0 repeat-y;overflow:hidden;float:left;width:585px; }\n");
	printf(".diag_main_c{margin:0 12px 0 11px; height:200px;overflow:hidden; overflow-y:auto; float:left; width:560px;}\n");
	printf(".TypeList{overflow-y:auto; padding: 5px 10px;}\n");
	printf(".TypeList li{ line-height:26px;}\n");
	printf(".diag_btn{width:100%%; text-align:center;}\n");
	printf(".diag_footer{background:url(/bh/images/dialog_bg01.gif) 0 -179px no-repeat; height:10px; float:left; width:585px;}\n");
	printf(".baobiaoremove{ margin-top:5px;}\n");
	printf("#zhezhao{position:absolute;top:0;left:0;width:100%%;height:100%%;overflow:hidden;background:#FFF;z-index:98;filter:alpha(opacity=30);-moz-opacity:0.3; -khtml-opacity: 0.3; opacity:0.3; display:none;}\n");
	printf("</style>\n");	
	
	node = head;
	while (node){
		ManagementList *tmp;
		if(node->name) free(node->name);
		tmp = node;
		node = node->next;
		free(tmp);
	}	
}

int main(){
	int ret, tasktype = 0, public_key = 0, d_all = 0;
	u_int64 session;
	char *description = NULL, *keyname = NULL, *delstr = NULL;
	char sql[256] ,cuser[256];	
	
	memset(cuser,0,sizeof(char)*256);
	memset(sql,0,sizeof(char)*256);	
	
	
	printf("Content-type:text/html\n\n");

	if ((ret = CGIArgLoad("Ldssdds","session", &session, "tasktype", &tasktype, "description", &description, "keyname", &keyname, "d_all", &d_all, "public_key", &public_key, "delstr", &delstr))){
		printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('服务器错误(%d)',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", ret, session);
		return 0;
	}


	if (CommandSessionCheck(session, cuser, NULL)){
		printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('系统超时',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);
		goto _END_;
	}

	if (!cuser){
		printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('get user error',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>", session);
		goto _END_;
	}

	
	if(tasktype == 3){//添加
		if((ret = ManListAdd(cuser, description, keyname, public_key))!=0){
			printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('添加失败(%d)',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>\n", ret, session);
			goto _END_;
		}
	}	
	

	if(tasktype == 2){//保存
		if((ret = Pub_keySet(public_key, cuser))!=0){
			printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('保存失败(%d)',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>\n", ret, session);
			goto _END_;
		}else{
			printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('保存成功',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>\n", session);
			goto _END_;
		}
	}


	if(tasktype == 1){
		if(NULL != delstr){
			if((ret = Delstr(public_key, cuser, delstr))!=0){
				printf("<html><head><link href=\"/bh/style/stystem.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><script src=\"/bh/pjs/jquery-1.4.2.min.js\" type=\"text/javascript\"></script><script>alert('删除失败(%d).',function(){location.href='key_management.cgi?session=%llu';});</script></body></html>\n", ret, session);
				goto _END_;
			}
		}
	}
	

	showhtml(session,cuser);

_END_:	
	CGIArgFree();
	return 0;
}

你可能感兴趣的:(管理)