using envar to set token

This commit is contained in:
ccppi 2024-09-06 14:57:16 +02:00
parent 29843d6a9a
commit 5726f4f821

144
main.c
View File

@ -9,10 +9,13 @@ int gotify_message(char *,char *,char *);
int systemdCall(DBusConnection *,char *, char *); int systemdCall(DBusConnection *,char *, char *);
int checkIsActive(DBusConnection *connection, char *service_name); int checkIsActive(DBusConnection *connection, char *service_name);
int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *param,char **result); int systemdUnitCall(DBusConnection *connection,char *target,char *method,char *param,char **result);
int GetSendStatus(DBusConnection *connection,char *objectpath);
int checkState(char *ServiceName,char *result); int checkState(char *ServiceName,char *result);
/* /*
*sytemctl show gotify-server | grep PID *sytemctl show gotify-server | grep PID
*sudo busctl tree org.freedesktop.systemd1 > get object paths *sudo busctl tree org.freedesktop.systemd1 > get object paths
//org/freedesktop/systemd1
//gotify_message("https://5ccppi.org:4433/message?token=","anothertitle","anothermessage");
* */ * */
typedef struct service typedef struct service
{ {
@ -23,16 +26,16 @@ typedef struct service
int main(int argc,char *argv[]) int main(int argc,char *argv[])
{ {
unsigned long uid;
int ret,i; int i;
Service serviceSys; Service serviceSys;
char **objects = NULL;
dbus_bool_t allow_user=0;
DBusConnection *connection=NULL; DBusConnection *connection=NULL;
DBusError error; DBusError error;
char *result=NULL; // char *result=NULL;
dbus_error_init(&error); dbus_error_init(&error);
connection = dbus_bus_get(DBUS_BUS_SYSTEM,&error);//DBUS_BUS_SYSTEM DBUS_BUS_SESSION connection = dbus_bus_get(DBUS_BUS_SYSTEM,&error);//DBUS_BUS_SYSTEM DBUS_BUS_SESSION
@ -42,18 +45,78 @@ int main(int argc,char *argv[])
return -1; return -1;
} }
printf("This is my unique dbus name: %s\n",dbus_bus_get_unique_name(connection)); printf("This is my unique dbus name: %s\n",dbus_bus_get_unique_name(connection));
/*
ret = dbus_bus_request_name(connection,"test.method.client",DBUS_NAME_FLAG_REPLACE_EXISTING,&error); if(argc>1)
if(dbus_error_is_set(&error))
{ {
fprintf(stderr,"%s",error.message); if(strcmp(argv[1],"getSendStatus")==0)
{
if(argc>2)
{
if(argv[2][0]=='/')
{
GetSendStatus(connection,argv[2]);
}
else
{
fprintf(stderr,"\n must be in the form of /org.freedesktop.systemd1/...\n");
return 1;
}
}
else
{
fprintf(stderr,"%s needs 1 argument\n",argv[1]);
return 2;
}
}
else if(strcmp(argv[1],"listUnits")==0)
{
systemdCall(connection,"ListUnits",NULL);
}
else
{
fprintf(stderr,"Unknown command");
}
}
else
{
fprintf(stderr,"requires a command\n--\n# getSendStatus [objectpath]\n# listUnits\n#set token with GOTIFY_TOKEN=\"sjdfoiasfd\"\n--\n");
}
return 0;
}
/*interactive
char userinput[128];
while(true)
{
scanf("%s",userinput);
printf("\nuserinput: %s",userinput);
systemdUnitCall(connection,userinput,"Get","SubState",&result);
checkState(userinput,result);
}
*/
int DBusOptions(DBusConnection *connection,DBusError **error) //not tested check pointer of error!!
{
int ret=0,i=0;
dbus_bool_t allow_user=0;
char **objects = NULL;
unsigned long uid;
ret = dbus_bus_request_name(connection,"test.method.client",DBUS_NAME_FLAG_REPLACE_EXISTING,*error);
if(dbus_error_is_set(*error))
{
fprintf(stderr,"%s",(**error).message);
return -1; return -1;
} }
else else
{ {
printf("name requested from dbus: %d\n",ret); printf("name requested from dbus: %d\n",ret);
} }
*/
dbus_connection_set_allow_anonymous(connection,allow_user); dbus_connection_set_allow_anonymous(connection,allow_user);
printf("user auth dbus requ: %d\n",allow_user); printf("user auth dbus requ: %d\n",allow_user);
if(!dbus_connection_list_registered(connection,"/",&objects)) if(!dbus_connection_list_registered(connection,"/",&objects))
@ -75,40 +138,27 @@ int main(int argc,char *argv[])
{ {
printf("NO userID set in dbus session, servers normali do not authenthicate against users/slaves: %d\n",ret); printf("NO userID set in dbus session, servers normali do not authenthicate against users/slaves: %d\n",ret);
} }
//org/freedesktop/systemd1
//systemdCall(connection,"GetUnit","timers.target");
systemdCall(connection,"ListUnits",NULL);
printf("&result:%p",&result);
systemdUnitCall(connection,"/org/freedesktop/systemd1/unit/mullvad_2ddaemon_2eservice","Get","SubState",&result);
checkState("mullvar.service",result);
systemdUnitCall(connection,"/org/freedesktop/systemd1/unit/sysinit_2etarget","Get","SubState",&result);
checkState("sysinit.target",result);
gotify_message("https://5ccppi.org:4433/message?token=ApPFbwayM5zDXf3","anothertitle","anothermessage");
char userinput[128];
while(true)
{
scanf("%s",userinput);
printf("\nuserinput: %s",userinput);
systemdUnitCall(connection,userinput,"Get","SubState",&result);
checkState(userinput,result);
}
return 0; return 0;
} }
int GetSendStatus(DBusConnection *connection,char *objectpath)
{
char *result=0;
int ret = systemdUnitCall(connection,objectpath,"Get","SubState",&result);
checkState(objectpath,result);
return ret;
}
int checkState(char *ServiceName,char *result) int checkState(char *ServiceName,char *result)
{ {
int i=0; int i=0;
char *error_strings[]={"dead","inactive","exited","teeeeeeeeeeeeessssst"}; char *error_strings[]={"dead","inactive","exited"};
printf("\n\nresult systemdUnitCall: %s\n",result); printf("\n\nresult systemdUnitCall: %s\n",result);
for(i=0;i<(sizeof(error_strings)/sizeof(error_strings[0]));i++) for(i=0;i<(sizeof(error_strings)/sizeof(error_strings[0]));i++)
{ {
printf("%s %d\n",error_strings[i],sizeof(error_strings)/sizeof(error_strings[0])); printf("%s %ld\n",error_strings[i],sizeof(error_strings)/sizeof(error_strings[0]));
if(strcmp(result,error_strings[i])==0) if(strcmp(result,error_strings[i])==0)
{ {
gotify_message("https://5ccppi.org:4433/message?token=AnV26OM_KjPdvY9",ServiceName,"dead!"); gotify_message("https://5ccppi.org:4433",ServiceName,"is not running");
printf("!!system unit died!! Alert gotify server!\n"); printf("!!system unit died!! Alert gotify server!\n");
} }
else if(strcmp(result,"active")==0) else if(strcmp(result,"active")==0)
@ -127,9 +177,29 @@ int gotify_message(char *url,char *title,char *message)
CURL *curl; CURL *curl;
CURLcode res; CURLcode res;
char post_buffer[128]; char post_buffer[128];
char url_buffer[128];
curl = curl_easy_init(); curl = curl_easy_init();
const char* token = NULL;
if(NULL==(token = getenv("GOTIFY_TOKEN")))
{
fprintf(stderr,"Pleas set a token using GOTIFY_TOKEN=\"abcde123\"\n");
}
else
{
ret = snprintf(url_buffer,sizeof(url_buffer),"%s/message?token=%s",url,token);
if(ret < sizeof(url_buffer))
{
printf("url:%s",url_buffer);
}
else
{
fprintf(stderr,"error connecting strings for url and token");
}
}
ret = snprintf(post_buffer,sizeof(post_buffer),"title=%s&message=%s&priority=6",title,message); ret = snprintf(post_buffer,sizeof(post_buffer),"title=%s&message=%s&priority=6",title,message);
if(ret < sizeof(post_buffer)) if(ret < sizeof(post_buffer))
{ {
@ -137,20 +207,20 @@ int gotify_message(char *url,char *title,char *message)
if(curl) if(curl)
{ {
curl_easy_setopt(curl,CURLOPT_URL,url); curl_easy_setopt(curl,CURLOPT_URL,url_buffer);
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,post_buffer); curl_easy_setopt(curl,CURLOPT_POSTFIELDS,post_buffer);
curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L); curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,1L);
res = curl_easy_perform(curl); res = curl_easy_perform(curl);
if(res != CURLE_OK) if(res != CURLE_OK)
{ {
fprintf(stderr,"curl_easy_perform failed: %s",curl_easy_strerror(res)); fprintf(stderr,"curl_easy_perform failed: %s\n",curl_easy_strerror(res));
} }
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
} }
} }
else{ else{
printf("Error on string concelblalbla: %d",ret); printf("Error on string concelblalbla: %d\n",ret);
} }
return 0; return 0;