+++ /dev/null
-/*
- * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Contact: Bumjin Im <bj.im@samsung.com>
- *
- * 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
- *
- * @file client.c
- * @author Bartlomiej Grzelewski (b.grzelewski@samsung.com)
- * @version 1.0
- * @brief This file is implementation of security-server client.
- *
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <err.h>
-#include <time.h>
-#include <pthread.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <sys/smack.h>
-
-#include <security-server.h>
-
-#define SOCK_PATH "/tmp/security-server-api-echo"
-#define SOCK_DATA_SHARE "/tmp/security-server-api-data-share"
-#define MSG "test"
-
-int send_to_server(const char *interface, const char *snd, size_t snd_size, char *rvc, size_t rvc_size) {
- struct sockaddr_un sockaddr = {.sun_family = AF_UNIX};
- int sock = socket(AF_UNIX, SOCK_STREAM, 0);
- int ret;
-
- strcpy(sockaddr.sun_path, interface);
-
- ret = connect(sock, (struct sockaddr *) &sockaddr, sizeof(struct sockaddr_un));
- if (ret < 0) err(-1, "connect");
-
- ret = write(sock, snd, snd_size);
- if (ret < 0) err(-1, "write");
-
- printf("%d: start read time", getpid());
-
- rvc[0] = 0;
- ret = read(sock, rvc, rvc_size);
- printf("%d: read returned: %d buffer: %s\n", getpid(), ret, rvc);
- close(sock);
-
- return 0;
-}
-
-int echo() {
- struct sockaddr_un sockaddr = {.sun_family = AF_UNIX, .sun_path = SOCK_PATH};
- int sock = socket(AF_UNIX, SOCK_STREAM, 0);
- int ret;
-
- ret = connect(sock, (struct sockaddr *) &sockaddr, sizeof(struct sockaddr_un));
- if (ret < 0) err(-1, "connect");
-
- ret = write(sock, MSG, sizeof(MSG));
- if (ret < 0) err(-1, "write");
-
- char buffer[100];
- ret = read(sock, buffer, 100);
- printf("%d: read %s\n", getpid(), buffer);
- close(sock);
-
- return 0;
-}
-
-int timeout() {
- struct sockaddr_un sockaddr = {.sun_family = AF_UNIX, .sun_path = SOCK_PATH};
- int sock = socket(AF_UNIX, SOCK_STREAM, 0);
- int ret;
- char buffer[100] = {0};
-
- ret = connect(sock, (struct sockaddr *) &sockaddr, sizeof(struct sockaddr_un));
- if (ret < 0) err(-1, "connect");
-
- time_t start;
- printf("%d: start read time: %lld\n", getpid(), (long long)(start=time(NULL)));
- ret = read(sock, buffer, 100);
- printf("%d: read returned: %d timeout: %lld buffer: %s\n", getpid(), ret, (long long)(time(NULL)-start), buffer);
- close(sock);
-
- return 0;
-}
-
-int rule(const char *subject, const char *object) {
- int ret = 0;
- struct smack_accesses *accesses = NULL;
-
- if (0 != smack_accesses_new(&accesses)) {
- printf("%d: Error in smack_accesses_new\n", getpid());
- goto error;
- }
-
- smack_accesses_add(accesses, object, "security-server::api-data-share", "w");
- smack_accesses_apply(accesses);
- smack_set_label_for_self(object);
-
- if (SECURITY_SERVER_API_SUCCESS != security_server_app_give_access(subject, 15)) {
- printf("%d: Error in app_give_access\n", getpid());
- goto error;
- }
-
- goto end;
-error:
- ret = -1;
-end:
- smack_accesses_free(accesses);
- return ret;
-}
-
-int fakerule(const char * subject, const char * object) {
- (void)subject;
- struct smack_accesses *accesses = NULL;
-
- if (0 != smack_accesses_new(&accesses)) {
- printf("%d: Error in smack_accesses_new\n", getpid());
- return -1;
- }
-
- smack_accesses_add(accesses, object, "security-server::api-data-share", "w");
- smack_accesses_apply(accesses);
- smack_set_label_for_self(object);
-
- size_t len = 10;
- char buffer[400];
- memcpy(buffer, &len, sizeof(size_t));
- char buffer2[400];
-
- send_to_server(SOCK_DATA_SHARE, buffer, 8, buffer2, 400);
-
- memcpy(&buffer[4], &len, sizeof(size_t));
-
- send_to_server(SOCK_DATA_SHARE, buffer, 400, buffer2, 400);
-
- memcpy(&buffer[8], &len, sizeof(size_t));
-
- send_to_server(SOCK_DATA_SHARE, buffer, 400, buffer2, 400);
-
- return 0;
-}
-
-int runtest(const char *name, const char *par1, const char *par2, const char *par3) {
- (void)par3;
- if (0 == strcmp(name, "timeout")) {
- return timeout();
- } else if (0 == strcmp(name, "rule")) {
- return rule(par1, par2);
- } else if (0 == strcmp(name, "echo")) {
- return echo();
- }
- printf("%d: Unknown command: %s\n", getpid(), name);
- return -1;
-}
-
-void fclosep(FILE **f) {
- if (*f) fclose(*f);
-}
-
-int filedata(const char *filename) {
- FILE *in __attribute__ ((cleanup(fclosep))) = NULL;
- int size;
- char table[4][100];
-
- if (NULL == (in = fopen(filename, "r"))) {
- printf("Error opening file %s\n", filename);
- return -1;
- }
-
- if (EOF == fscanf(in, "%d", &size)) {
- printf("Error reading configuration file.\n");
- return -1;
- }
-
- while(size--) {
- int i, len;
- if (EOF == fscanf(in , "%d", &len)) {
- printf("Error reading configuration file.\n");
- return -1;
- }
- for (i = 0; i < len; ++i) {
- if (EOF == fscanf(in, "%s", table[i])) {
- printf("Error reading configuration file.\n");
- return -1;
- }
- }
- if(0 == fork()) {
- printf("%d: Process started. Command: %s\n", getpid(), table[0]);
- int ret = runtest(table[0], table[1], table[2], table[3]);
- printf("%d: Process ended.\n", getpid());
- return ret;
- }
- }
- return 0;
-}
-
-int main(int argc, const char **argv)
-{
- if (argc < 2) {
- printf("wrong usage. available commands: echo, rule, timeout, file, fakerule");
- return 0;
- }
-
- if (0 == strcmp(argv[1], "echo")) {
- return echo();
- } else if (0 == strcmp(argv[1], "rule")) {
- return rule(argv[2], argv[3]);
- } else if (0 == strcmp(argv[1], "fakerule")) {
- return fakerule(argv[2], argv[3]);
- } else if (0 == strcmp(argv[1], "timeout")) {
- return timeout();
- } else if (0 == strcmp(argv[1], "file")) {
- return filedata(argv[2]);
- }
-
- return 0;
-}
-