17 #include <sipwitch-config.h>
18 #include <ucommon/ucommon.h>
19 #include <ucommon/secure.h>
25 using namespace sipwitch;
29 static char *getpass(
const char *prompt)
34 fputs(prompt, stderr);
36 for (i = 0; i <
sizeof(buf) - 1; i++) {
37 buf[i] = fgetc(stdin);
38 if (buf[i] ==
'\r' || buf[i] ==
'\n')
52 const char *realm = NULL, *secret, *verify;
53 const char *mode =
"md5";
61 const char *
control =
"\\\\.\\mailslot\\sipwitch_ctrl";
63 const char *control = DEFAULT_VARPATH
"/run/sipwitch/control";
66 const char *user = *(++argv);
68 if(String::equal(user,
"-version")) {
69 printf(
"sippasswd 0.1\n"
70 "Copyright (C) 2010 David Sugar, Tycho Softworks\n"
71 "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n"
72 "This is free software: you are free to change and redistribute it.\n"
73 "There is NO WARRANTY, to the extent permitted by law.\n");
78 if(user && getuid() != 0)
79 shell::errexit(3,
"*** sippasswd: only root can change other user's digests\n");
82 struct passwd *pwd = getpwuid(getuid());
84 shell::errexit(3,
"*** sippasswd: user id cannot be determined\n");
86 user = strdup(pwd->pw_name);
89 shell::errexit(3,
"*** sippasswd: root privilege required\n");
92 shell::errexit(3,
"*** sippasswd: user id not specified\n");
97 fs.open(DEFAULT_CFGPATH
"/siprealm", fsys::RDONLY);
100 fs.open(DEFAULT_VARPATH
"/lib/sipwitch/uuid", fsys::RDONLY);
103 shell::errexit(4,
"*** sippasswd: no realm active\n");
105 memset(buffer, 0,
sizeof(buffer));
106 fs.read(buffer,
sizeof(buffer) - 1);
109 char *cp = strchr(buffer,
'\n');
113 cp = strchr(buffer,
':');
120 realm = strdup(buffer);
121 secret = getpass(
"Enter new SIP secret: ");
122 if(!secret || !*secret) {
123 printf(
"no password supplied\n");
127 verify = getpass(
"Retype new SIP secret: ");
128 if(!verify || !*verify || !String::equal(secret, verify)) {
129 printf(
"sorry, secrets do not match\n");
133 digest_t digest(mode);
135 if(!digest.puts((string_t)user +
":" + (string_t)realm +
":" + (string_t)secret))
136 shell::errexit(1,
"*** sippasswd: cannot compute");
140 snprintf(replace,
sizeof(replace),
"%s:%s\n", user, *digestbuf);
143 dir::create(DEFAULT_VARPATH
"/lib/sipwitch", fsys::GROUP_PRIVATE);
144 dir::create(DEFAULT_VARPATH
"/lib/sipwitch/digests", fsys::GROUP_PRIVATE);
147 string_t path = str(DEFAULT_VARPATH
"/lib/sipwitch/digests/") + realm;
148 fs.open(*path, fsys::OWNER_PRIVATE, fsys::RDONLY);
151 fp = fopen(*path,
"r+");
153 shell::errexit(1,
"*** sippasswd: cannot access digest");
157 if(NULL == fgets(buffer,
sizeof(buffer), fp) || feof(fp))
160 if(String::equal(buffer, replace)) {
162 printf(
"digest unchanged\n");
166 cp = strchr(buffer,
':');
171 if(String::equal(buffer, user))
181 fp = fopen(control,
"w");
183 fprintf(fp,
"digest %s %s\n", user, *digestbuf);
187 printf(
"digest updated\n");
Server control interfaces and functions.
Top level include directory for GNU Telephony SIP Witch Server.