~ubuntu-branches/ubuntu/saucy/dahdi-tools/saucy-proposed

« back to all changes in this revision

Viewing changes to xpp/astribank_allow.c

  • Committer: Package Import Robot
  • Author(s): Jackson Doak
  • Date: 2013-08-25 12:48:37 UTC
  • mfrom: (2.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130825124837-wtefi7f9dsihg8is
Tags: 1:2.7.0-1ubuntu1
* Merge from debian. Remaining changes:
  - debian/control: Added gawk as dependency for dkms build
  - debian/control: Package dahdi Depends on dahdi-dkms | dahdi-source
  - debian/control: Set ubuntu maintainer    
  - added debian/dahdi.postinst
  - debian/control: Removed Uploaders field.
  - added debian/dahdi.postinst
  - added --error-handler=init_failed to debian/rules
  

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <arpa/inet.h>
33
33
#include <ctype.h>
34
34
#include "mpp.h"
35
 
#include "mpp_funcs.h"
36
 
#include "debug.h"
 
35
#include "mpptalk.h"
 
36
#include <debug.h>
 
37
#include "astribank_license.h"
37
38
 
38
 
static const char rcsid[] = "$Id: astribank_allow.c 7908 2010-01-13 16:26:15Z tzafrir $";
 
39
static const char rcsid[] = "$Id$";
39
40
 
40
41
#define DBG_MASK        0x80
41
42
 
49
50
        fprintf(stderr, "\t\t[-d mask]          # Debug mask (0xFF for everything)\n");
50
51
        fprintf(stderr, "\t\t[-w]               # Write capabilities to EEPROM, otherwise read capabilities\n");
51
52
        fprintf(stderr, "\t\t[-f filename]      # License filename (stdin/stdout if not specified)\n\n");
 
53
        fprintf(stderr, "\t\t[-m num]           # Numeric code of License markers to generate\n");
 
54
        license_markers_help("\t", stderr);
52
55
        exit(1);
53
56
}
54
57
 
70
73
        return 0;
71
74
}
72
75
 
73
 
static int bin_to_file(void *buf, int len, FILE *f)
74
 
{
75
 
        static int bytes_on_line;
76
 
        unsigned char *p = buf;
77
 
        if (buf == NULL) {
78
 
                if (bytes_on_line != 0) {
79
 
                        if (fprintf(f, "\n") != 1)
80
 
                                return -1;
81
 
                        bytes_on_line = 0;
82
 
                }
83
 
                return 0;
84
 
        }
85
 
        int i;
86
 
        for (i = 0; i < len; i++) {
87
 
                if (fprintf(f, "%02x", *p++) != 2)
88
 
                        return -1;
89
 
                bytes_on_line++;
90
 
                if (bytes_on_line >= 16) {
91
 
                        if (fprintf(f, "\n") != 1)
92
 
                                return -1;
93
 
                        bytes_on_line = 0;
94
 
                }
95
 
        }
96
 
        return 0;
97
 
}
98
 
 
99
 
static int write_to_file(struct eeprom_table *eeprom_table, struct capabilities *caps, struct capkey *key, FILE *f)
100
 
{
101
 
        fprintf(f, "-----BEGIN XORCOM LICENSE BLOCK-----\n");
102
 
        fprintf(f, "Version: 1.0\n");
103
 
        fprintf(f, "Timestamp: %u\n", caps->timestamp);
104
 
        fprintf(f, "Serial: %.*s\n", LABEL_SIZE, eeprom_table->label);
105
 
        fprintf(f, "Capabilities.Port.FXS: %d\n", caps->ports_fxs);
106
 
        fprintf(f, "Capabilities.Port.FXO: %d\n", caps->ports_fxo);
107
 
        fprintf(f, "Capabilities.Port.BRI: %d\n", caps->ports_bri);
108
 
        fprintf(f, "Capabilities.Port.PRI: %d\n", caps->ports_pri);
109
 
        fprintf(f, "Capabilities.Twinstar: %d\n", CAP_EXTRA_TWINSTAR(caps));
110
 
        fprintf(f, "Data:\n");
111
 
        bin_to_file(eeprom_table, sizeof(*eeprom_table), f);
112
 
        bin_to_file(caps, sizeof(*caps), f);
113
 
        bin_to_file(key, sizeof(*key), f);
114
 
        bin_to_file(NULL, 0, f);
115
 
        fprintf(f, "-----END XORCOM LICENSE BLOCK-----\n");
116
 
        return 0;
117
 
}
118
 
 
119
 
/*
120
 
 * Removes whitespace on both sizes of the string.
121
 
 * Returns a pointer to the first non-space char. The string
122
 
 * is modified in place to trim trailing whitespace.
123
 
 * If the whole string is whitespace, returns NULL.
124
 
 */
125
 
char *trim(char *s)
126
 
{
127
 
        int len = strlen(s);
128
 
        while (len > 0 && isspace(s[len-1])) {
129
 
                len--;
130
 
        }
131
 
        if (len == 0)
132
 
                return NULL;
133
 
        s[len] = '\0';
134
 
        while (isspace(*s))
135
 
                s++;
136
 
        /* *s is not a space, since in this case we'd return NULL above */
137
 
        return s;
138
 
}
139
 
 
140
 
int get_key_value(char *line, char **key, char **value)
141
 
{
142
 
        char *p = strchr(line, ':');
143
 
        if (p == NULL)
144
 
                return -1;
145
 
        *p = '\0';
146
 
        *key = trim(line);
147
 
        *value = trim(p + 1);
148
 
        return 0;
149
 
}
150
 
 
151
 
static int hex_digit_to_int(char c)
152
 
{
153
 
        if (c >= '0' && c <= '9')
154
 
                return c - '0';
155
 
        else if (c >= 'a' && c <= 'f')
156
 
                return c - 'a' + 10;
157
 
        else
158
 
                return -1;
159
 
}
160
 
 
161
 
static int str_to_bin(char *line, void *buf, int maxlen)
162
 
{
163
 
        static int offset;
164
 
        unsigned char *p = buf;
165
 
        if (strlen(line) % 2 != 0)
166
 
                return -1;
167
 
        while (offset < maxlen && *line) {
168
 
                uint8_t value;
169
 
                char c = hex_digit_to_int(*line++);
170
 
                if (c < 0 || *line == '\0')
171
 
                        return -1;
172
 
                value = c << 4;
173
 
                c = hex_digit_to_int(*line++);
174
 
                if (c < 0)
175
 
                        return -1;
176
 
                value |= c;
177
 
                p[offset++] = value;
178
 
        }
179
 
        if (offset == maxlen && *line)
180
 
                return -1;
181
 
        return offset;
182
 
}
183
 
 
184
 
static int read_from_file(struct eeprom_table *eeprom_table, struct capabilities *caps, struct capkey *capkey, FILE *f)
185
 
{
186
 
        char buf[256];
187
 
        char *line, *key, *value;
188
 
        int state = 0;
189
 
        int lineno = 0;
190
 
        struct table {
191
 
                struct eeprom_table eeprom_table;
192
 
                struct capabilities capabilities;
193
 
                struct capkey capkey;
194
 
        } PACKED table;
195
 
 
196
 
        memset(&table, 0, sizeof(struct table));
197
 
        /*
198
 
         * states:
199
 
         * 0: start - before BEGIN_LICENSE_BLOCK line. on BEGIN_LICENSE_BLOCK line goto 1.
200
 
         * 1: read Version, goto 2. if not version line then error.
201
 
         * 2: after BEGIN line. split line into key:value. if line is Data:, goto 3.
202
 
         * 3: read binary data. if line is END_LICENSE_BLOCK goto 4.
203
 
         * 4: END_LICENSE_BLOCK - ignore lines.
204
 
         */
205
 
        while (fgets(buf, 256, f) != NULL) {
206
 
                lineno++;
207
 
                int len = strlen(buf);
208
 
                if (len > 0 && buf[len-1] != '\n') {
209
 
                        ERR("Line %d: Line too long\n", lineno);
210
 
                        return -1;
211
 
                }
212
 
                line = trim(buf);
213
 
                if (line == NULL) {
214
 
                        if (state > 0 && state < 4) {
215
 
                                ERR("Line %d: Empty line\n", lineno);
216
 
                                return -1;
217
 
                        }
218
 
                        else
219
 
                                continue;
220
 
                }
221
 
                switch (state) {
222
 
                        case 0:
223
 
                                if (strcmp(line, "-----BEGIN XORCOM LICENSE BLOCK-----") == 0)
224
 
                                        state = 1;
225
 
                                else {
226
 
                                        ERR("Line %d: Invalid license begin block\n", lineno);
227
 
                                        return -1;
228
 
                                }
229
 
                                break;
230
 
                        case 1:
231
 
                                if (get_key_value(line, &key, &value) < 0) {
232
 
                                        ERR("Line %d: Can't parse line\n", lineno);
233
 
                                        return -1;
234
 
                                }
235
 
                                if (strcmp(key, "Version") == 0) {
236
 
                                        if (strcmp(value, "1.0") == 0) {
237
 
                                                state = 2;
238
 
                                        } else {
239
 
                                                ERR("Line %d: Unknown license file version '%s', need version '1.0'\n", lineno, value);
240
 
                                                return -1;
241
 
                                        }
242
 
                                } else {
243
 
                                        ERR("Line %d: No license file version\n", lineno);
244
 
                                        return -1;
245
 
                                }
246
 
                                break;
247
 
                        case 2:
248
 
                                if (get_key_value(line, &key, &value) < 0) {
249
 
                                        ERR("Line %d: Can't parse line\n", lineno);
250
 
                                        return -1;
251
 
                                }
252
 
                                if (strcmp(key, "Data") == 0) {
253
 
                                        state = 3;
254
 
                                        break;
255
 
                                }
256
 
                                break;
257
 
                        case 3:
258
 
                                if (strcmp(line, "-----END XORCOM LICENSE BLOCK-----") == 0) {
259
 
                                        state = 4;
260
 
                                        break;
261
 
                                }
262
 
                                if (str_to_bin(line, &table, sizeof(table)) < 0) {
263
 
                                        ERR("Line %d: Error in data block\n", lineno);
264
 
                                        return -1;
265
 
                                }
266
 
                                break;
267
 
                        case 4:
268
 
                                break;
269
 
 
270
 
                }
271
 
        }
272
 
        if (state != 4) {
273
 
                ERR("Invalid license file\n");
274
 
                return -1;
275
 
        }
276
 
        memcpy(eeprom_table, &table.eeprom_table, sizeof(*eeprom_table));
277
 
        memcpy(caps, &table.capabilities, sizeof(*caps));
278
 
        memcpy(capkey, &table.capkey, sizeof(*capkey));
279
 
        return 0;
280
 
}
281
 
 
282
76
int main(int argc, char *argv[])
283
77
{
284
78
        char                    *devpath = NULL;
286
80
        struct eeprom_table     eeprom_table;
287
81
        struct capabilities     caps;
288
82
        struct capkey           key;
289
 
        const char              options[] = "vd:D:wf:";
 
83
        const char              options[] = "vd:D:wf:m:";
290
84
        int                     do_write = 0;
 
85
        unsigned int            marker = LICENSE_MARKER_GENERIC;
291
86
        FILE                    *file;
292
87
        char                    *filename = NULL;
293
88
        int                     ret;
316
111
                        case 'f':
317
112
                                filename = optarg;
318
113
                                break;
 
114
                        case 'm':
 
115
                                marker = strtoul(optarg, NULL, 0);
 
116
                                if (!license_marker_valid(marker))
 
117
                                        usage();
 
118
                                break;
319
119
                        case 'h':
320
120
                        default:
321
121
                                ERR("Unknown option '%c'\n", c);
327
127
                usage();
328
128
        }
329
129
        DBG("Startup %s\n", devpath);
330
 
        if((astribank = mpp_init(devpath)) == NULL) {
 
130
        if((astribank = mpp_init(devpath, 1)) == NULL) {
331
131
                ERR("Failed initializing MPP\n");
332
132
                return 1;
333
133
        }
342
142
                return 1;
343
143
        }
344
144
        if (do_write) {
 
145
                unsigned int used_marker;
345
146
                /* update capabilities based on input file */
346
147
                file = stdin;
347
148
                if (filename) {
351
152
                                return 1;
352
153
                        }
353
154
                }
354
 
                ret = read_from_file(&eeprom_table, &caps, &key, file);
 
155
                ret = read_from_file(&eeprom_table, &caps, &key, &used_marker, file);
355
156
                if (ret < 0) {
356
157
                        ERR("Failed to read capabilities from file: %d\n", ret);
357
158
                        return 1;
371
172
                                return 1;
372
173
                        }
373
174
                }
374
 
                ret = write_to_file(&eeprom_table, &caps, &key, file);
 
175
                ret = write_to_file(&eeprom_table, &caps, &key, marker, file);
375
176
                if (ret < 0) {
376
177
                        ERR("Failed to write capabilities to file: %d\n", ret);
377
178
                        return 1;