static OM_uint32 inquire_cred (OM_uint32 *minor_status, gss_opaque_t cred_handle, gss_name_t *name, OM_uint32 *lifetime, int *cred_usage ) { OM_uint32 major, minor; nil_cred *cred; if ((major = check_cred(&minor, cred_handle)) != GSS_S_COMPLETE) return major; cred = (nil_cred *) cred_handle; if (name != NULL) { if ((*name = ilugss_copy_name(cred->name)) == NULL) return GSS_S_FAILURE; } if (lifetime != NULL) { if (cred->endtime != GSS_C_INDEFINITE) *lifetime = cred->endtime - time(NULL); else *lifetime = GSS_C_INDEFINITE; } if (cred_usage != NULL) *cred_usage = cred->usage; return GSS_S_COMPLETE; }
void handle_client( void* addrnew_client) { /* Handles each new client request */ int new_client=(int)addrnew_client; char *buf=(char*)malloc(sizeof(char)*255); int n; /* First we send a welcome message of the server, just for information */ Serv_Msg *s_m = (Serv_Msg *)malloc(sizeof(Serv_Msg)); Cred cr; Cred *cr1, *cr_recv; Cl_Msg *c_m = (Cl_Msg *)malloc(sizeof(Cl_Msg)); ChatRoom *chat_room; strcpy(s_m->wm, "Welcome to AASVAR Chat Server! Enjoy\nIdentify yourself to the server."); send(new_client, s_m, sizeof(Serv_Msg), 0); while(1){ if( (n= recv( new_client, c_m, sizeof(Cl_Msg), 0)) !=-1 && n !=0) { cr = find_cr_by_client_id(bl, new_client); switch(c_m->type){ /* Logon process */ /* Existing user */ case EXIST_USER: if(is_exist_buddy(bl, c_m->cr) && !is_signed_in(bl, c_m->cr) && check_cred(bl, c_m->cr)){ sign_on_buddy(bl, c_m->cr, new_client); strcpy(s_m->m, c_m->cr.u_name); s_m->l_r = OLD_SIGNED_IN; } else if(is_exist_buddy(bl, c_m->cr)){ s_m->l_r = SIGNED_IN_OR_PASS; } else s_m->l_r = DO_NOT_EXIST; s_m->type = LOGIN_RES; send(new_client, s_m, sizeof(Serv_Msg), 0); break; case NEW_USER: if(is_exist_buddy(bl, c_m->cr)) s_m->l_r = OLD_EXIST; else{ add_buddy(bl, c_m->cr, new_client); sign_on_buddy(bl, c_m->cr, new_client); strcpy(s_m->m, c_m->cr.u_name); s_m->l_r = NEW_CREATED; } s_m->type = LOGIN_RES; send(new_client, s_m, sizeof(Serv_Msg), 0); break; case GET_BL: /* Send the current buddy list with all signed on users */ get_buddy_list(bl, s_m); s_m->type = BL_RES; send(new_client, s_m, sizeof(Serv_Msg), 0); break; case IM_REQ_CL: /* Tries to generate an IM b/w two connecting entities */ /* Check again if the buddy exist in the queue */ if(is_exist_buddy(bl, c_m->cr)){ s_m->type = IM_REQ_SERV; cr = find_cr_by_client_id(bl, new_client); /* Send a packet to the receiver to ack the packet */ send(new_client, s_m, sizeof(Serv_Msg), 0); } else printf("Buddy not found!"); break; case IM_ACK_CL: break; case CHAT_ROOM_CREAT_REQ_CL: /* Starts a new chat room */ chat_room = create_chat_room(chat_rooms, cr.u_name); add_cr_to_chat_room(chat_room, cr); s_m->type = CHAT_ROOM_CREAT; sprintf(s_m->m, "Chat room with name %s created!\n", chat_room->name); send(new_client, s_m, sizeof(Serv_Msg), 0); break; case CHAT_ROOM_JOIN_REQ_CL: /* Search the chat room with the specified name */ chat_room = find_chat_room(chat_rooms, c_m->chat_room_name); if(chat_room != NULL){ add_cr_to_chat_room(chat_room, cr); s_m->type = CHAT_ROOM_JOINED; sprintf(s_m->m, "Chat room %s joined!\n", chat_room->name); } else{ s_m->type = ERR; sprintf(s_m->m, "No chat room with name %s found!\n", c_m->chat_room_name); } send(new_client, s_m, sizeof(Serv_Msg), 0); break; case CHAT_ROOM_SEND_MSG: /* Sends a message to all clients in the chat room */ if((chat_room =find_chat_room(chat_rooms, c_m->chat_room_name))!=NULL&& is_user_added_chat_room(chat_room, cr)) broadcast_chat_msg(chat_room, c_m->cm, cr); else if(chat_room == NULL){ s_m->type = ERR; sprintf(s_m->m, "No chat room with name %s found!\n", c_m->chat_room_name); send(new_client, s_m, sizeof(Serv_Msg), 0); } else{ s_m->type = ERR; sprintf(s_m->m, "You are not added!\n", c_m->chat_room_name); send(new_client, s_m, sizeof(Serv_Msg), 0); } break; case IM_START_REQ: /* Asks the other client if it is okay to start IM */ cr1 = find_cr_by_name(bl, c_m->im.cr.u_name); if(cr1){ s_m->type = IM_START_CONF; s_m->im.cr = find_cr_by_client_id(bl, new_client); send(cr1->socket_id, s_m, sizeof(Serv_Msg), 0); } else{ s_m->type = ERR; sprintf(s_m->m, "The user %s does not exist!\n", c_m->im.cr.u_name); send(new_client, s_m, sizeof(Serv_Msg), 0); } break; case SIGN_OUT_CL: /* Locate the user */ cr = find_cr_by_client_id(bl, new_client); sign_out_buddy(bl, cr); s_m->type = SIGN_OUT_CONF; sprintf(s_m->m, "You have been successfully signed out!\n"); send(new_client, s_m, sizeof(Serv_Msg), 0); break; case IM_SEND: /* Locate the user requested */ cr = find_cr_by_client_id(bl, new_client); cr_recv = find_cr_by_name(bl,c_m->im.cr.u_name); if(!cr_recv){ /* User not found! */ s_m->type = ERR; sprintf(s_m->m, "The user %s does not exist\n", c_m->im.cr.u_name); send(new_client, s_m, sizeof(Serv_Msg), 0); } else if(!is_signed_in(bl, *cr_recv)){ s_m->type = ERR; sprintf(s_m->m, "The user %s is offline!\n", c_m->im.cr.u_name); send(new_client, s_m, sizeof(Serv_Msg), 0); } else{ s_m->im = c_m->im; s_m->im.cr = cr; s_m->type = IM_RECV; send(cr_recv->socket_id, s_m, sizeof(Serv_Msg), 0); } break; case FILE_XFER_SEND: /* Locate the user requested */ cr_recv = find_cr_by_name(bl,c_m->cr.u_name); if(!cr_recv){ /* User not found! */ s_m->type = ERR; sprintf(s_m->m, "The user %s does not exist\n", c_m->im.cr.u_name); send(new_client, s_m, sizeof(Serv_Msg), 0); } else if(!is_signed_in(bl, *cr_recv)){ s_m->type = ERR; sprintf(s_m->m, "The user %s is offline!\n", c_m->im.cr.u_name); send(new_client, s_m, sizeof(Serv_Msg), 0); } else{ s_m->fx = c_m->fx; s_m->type = FILE_XFER_RECV; send(cr_recv->socket_id, s_m, sizeof(Serv_Msg), 0); } break; } } else if (n == 0){ cr = find_cr_by_client_id(bl, new_client); printf("Client %s has quit: removing from list\n", cr.u_name); sign_out_buddy(bl, cr); s_m->type = SIGN_OUT_CONF; return; } else { perror("cant get message, exiting\n"); exit(1); } } }
static int dummy_portmap(int sock, FILE *portmap_file) { struct sockaddr_in sin; int pktlen, addrlen; unsigned char pkt[65536]; /* Max UDP packet size */ /* RPC UDP packets do not include TCP fragment size */ struct rpc_call *rpc = (struct rpc_call *) &pkt[-4]; struct rpc_auth *cred; struct rpc_auth *vrf; struct portmap_args *args; struct portmap_reply rply; for (;;) { addrlen = sizeof sin; pktlen = recvfrom(sock, &pkt, sizeof pkt, 0, (struct sockaddr *)&sin, &addrlen); if (pktlen < 0) { if (errno == EINTR) continue; return -1; } /* +4 to skip the TCP fragment header */ if (pktlen + 4 < sizeof(struct portmap_call)) continue; /* Bad packet */ if (rpc->hdr.udp.msg_type != htonl(RPC_CALL)) continue; /* Bad packet */ memset(&rply, 0, sizeof rply); rply.rpc.hdr.udp.xid = rpc->hdr.udp.xid; rply.rpc.hdr.udp.msg_type = htonl(RPC_REPLY); cred = (struct rpc_auth *) &rpc->cred_flavor; if (rpc->rpc_vers != htonl(2)) { rply.rpc.reply_state = htonl(REPLY_DENIED); /* state <- RPC_MISMATCH == 0 */ } else if (rpc->program != htonl(PORTMAP_PROGRAM)) { rply.rpc.reply_state = htonl(PROG_UNAVAIL); } else if (rpc->prog_vers != htonl(2)) { rply.rpc.reply_state = htonl(PROG_MISMATCH); } else if (!(vrf = get_auth(cred)) || (char *)vrf > (char *)pkt + pktlen - 8 - sizeof(*args) || !(args = get_auth(vrf)) || (char *)args > (char *)pkt + pktlen - sizeof(*args) || check_cred(cred) || check_vrf(vrf)) { /* Can't deal with credentials data; the kernel won't send them */ rply.rpc.reply_state = htonl(SYSTEM_ERR); } else { switch (ntohl(rpc->proc)) { case PMAP_PROC_NULL: break; case PMAP_PROC_SET: if (args->proto == htonl(IPPROTO_TCP) || args->proto == htonl(IPPROTO_UDP)) { if (portmap_file) fprintf(portmap_file, "%u %u %s %u\n", ntohl(args->program), ntohl(args->version), protoname(args->proto), ntohl(args->port)); rply.port = htonl(1); /* TRUE = success */ } break; case PMAP_PROC_UNSET: rply.port = htonl(1); /* TRUE = success */ break; case PMAP_PROC_GETPORT: break; case PMAP_PROC_DUMP: break; default: rply.rpc.reply_state = htonl(PROC_UNAVAIL); break; } } sendto(sock, &rply.rpc.hdr.udp, sizeof rply - 4, 0, (struct sockaddr *)&sin, addrlen); } }