-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
100 lines (88 loc) · 2.31 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* dsngctl - Control utility for DigSig-ng (digsig-ng.org)
*
* main.c - command line utility
*
* Copyright (c) 2013-2014, The DigSig-ng Authors
*
* This program 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.
*/
#include "interface.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/**
* Checks whether the current process is running as root.
*
* @return 1 if root, 0 otherwise.
*/
static int check_root()
{
return getuid() == 0;
}
/**
* Prints the usage line to standard output.
*/
static void print_usage(char *name)
{
printf("usage: %s [start|status|help]\n", name);
}
/**
* Prints the help documentation to standard output.
*/
static void print_help(char *name)
{
print_usage(name);
printf("\n");
printf(" %s start [pkey]\n", name);
printf(" Loads a private key to the kernel module.\n");
printf(" You can export a key with `gpg --export > pkey`\n");
printf("\n");
printf(" %s status\n", name);
printf(" Determines the current status of the module, whether it is loaded and running.\n");
printf("\n");
printf(" %s help\n", name);
printf(" Prints this message to standard output.\n");
}
int main(int argc, char *argv[])
{
int ret = 0;
if (argc < 2) {
print_usage(argv[0]);
return 1;
}
else if (argc >= 2 && (strcmp(argv[1], "help") == 0)) {
print_help(argv[0]);
return 0;
}
else if (argc >= 2 && (strcmp(argv[1], "start") == 0)) {
if (argc < 3) {
fprintf(stderr, "dsngctl: invalid number of parameters for start command\n");
return 1;
}
if (!check_root()) {
fprintf(stderr, "dsngctl: start command must be run as root\n");
return 1;
}
ret = dsng_start(argv[2]);
}
else if (argc >= 2 && (strcmp(argv[1], "status") == 0)) {
if (!check_root()) {
fprintf(stderr, "dsngctl: status command must be run as root\n");
return 1;
}
printf("dsngctl: digsig-ng status\n\t"
"module loaded:\t\t%s\n\t"
"module initialized:\t%s\n",
digsig_is_loaded() ? "loaded" : "not loaded",
digsig_is_initialized() ? "initialized" : "not initialized");
return 0;
}
else {
fprintf(stderr, "dsngctl: unrecognized argument\n");
}
return ret;
}