parse_privacy.c

Go to the documentation of this file.
00001 /*
00002  * $Id: parse_privacy.c 4720 2008-08-23 10:56:15Z henningw $
00003  *
00004  * Copyright (c) 2006 Juha Heinanen
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License 
00019  * along with this program; if not, write to the Free Software 
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  * History:
00023  * -------
00024  * 2006-12-18 Introduced parsing of Privacy header (RFC 3323)
00025  */
00026 
00027 /*!
00028  * \file
00029  * \brief Privacy value parser
00030  * \ingroup parser
00031  */
00032 
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include "../dprint.h"
00036 #include "../trim.h"
00037 #include "parse_privacy.h"
00038 #include "msg_parser.h"
00039 
00040 
00041 /*!
00042  * Parse a privacy value pointed by start that can be at most max_len long.
00043  * \return length of matched privacy value on success or NULL otherwise
00044  */
00045 unsigned int parse_priv_value(char* start, unsigned int max_len,
00046                unsigned int* value)
00047 {
00048     unsigned int len;
00049 
00050     if (!start || !value) {
00051    LM_ERR("invalid parameter value\n");
00052    return 0;
00053     }
00054 
00055     switch (start[0]) {
00056 
00057     case 'c':
00058     case 'C':
00059    if(max_len < 8)
00060        return 0;
00061    if (strncasecmp(start, "critical", 8) == 0) {
00062        *value = PRIVACY_CRITICAL;
00063        len = 8;
00064        break;
00065    } else {
00066        return 0;
00067    }
00068 
00069     case 'h':
00070     case 'H':
00071    if (max_len < 6)
00072        return 0;
00073    if (strncasecmp(start, "header", 6) == 0) {
00074        *value = PRIVACY_HEADER;
00075        len = 6;
00076        break;
00077    }
00078    if (max_len < 7)
00079        return 0;
00080    if (strncasecmp(start, "history", 7) == 0) {
00081        *value = PRIVACY_HISTORY;
00082        len = 7;
00083        break;
00084    } else {
00085        return 0;
00086    }
00087 
00088     case 'i':
00089     case 'I':
00090    if(max_len < 2)
00091        return 0;
00092    if (start[1] == 'd' || start[1] == 'D') {
00093        *value = PRIVACY_ID;
00094        len = 2;
00095        break;
00096    } else {
00097        return 0;
00098    }
00099 
00100     case 'n':
00101     case 'N':
00102    if(max_len < 4)
00103        return 0;
00104    if (strncasecmp(start, "none", 4) == 0) {
00105        *value = PRIVACY_NONE;
00106        len = 4;
00107        break;
00108    } else {
00109        return 0;
00110    }
00111 
00112     case 's':
00113     case 'S':
00114    if(max_len < 7)
00115        return 0;
00116    if (strncasecmp(start, "session", 7) == 0) {
00117        *value = PRIVACY_SESSION;
00118        len = 7;
00119        break;
00120    } else {
00121        return 0;
00122    }
00123 
00124     case 'u':
00125     case 'U':
00126    if(max_len < 4)
00127        return 0;
00128    if (strncasecmp(start, "user", 4) == 0) {
00129        *value = PRIVACY_USER;
00130        len = 4;
00131        break;
00132    } else {
00133        return 0;
00134    }
00135 
00136     default:
00137    return 0;
00138     }
00139 
00140     if(len < max_len) {
00141    if(start[len] != '\0' && start[len] != ';' && start[len] != ' '
00142       && start[len] != '\t' && start[len] != '\r' && start[len] != '\n')
00143        return 0;
00144     }
00145 
00146     return len;
00147 }
00148 
00149 
00150 /*!
00151  * This method is used to parse Privacy HF body, which consist of
00152  * comma separated list of priv-values.  After parsing, msg->privacy->parsed
00153  * contains enum bits of privacy values defined in parse_privacy.h.
00154  * \return 0 on success and -1 on failure. 
00155  */
00156 int parse_privacy(struct sip_msg *msg)
00157 {
00158     unsigned int val_len, value, values, len;
00159     str next;
00160     char *p, *beyond;
00161 
00162     /* maybe the header is already parsed! */
00163     if (msg->privacy && msg->privacy->parsed)
00164    return 0;
00165 
00166     /* parse Privacy HF (there should be only one) */
00167     if (!msg->privacy &&
00168    (parse_headers(msg, HDR_PRIVACY_F, 0) == -1 || !msg->privacy)) {
00169    return -1;
00170     }
00171 
00172     next.len = msg->privacy->body.len;
00173     next.s = msg->privacy->body.s;
00174 
00175     trim_leading(&next);
00176 
00177     if (next.len == 0) {
00178    LM_ERR("no values\n");
00179    return -1;
00180     }
00181 
00182     values = 0;
00183     p = next.s;
00184     len = next.len;
00185     beyond = p + len;
00186 
00187     while (p < beyond) {
00188    if((val_len = parse_priv_value(p, len, &value)) != 0) {
00189        values |= value;
00190        p = p + val_len;
00191        len = len - val_len;
00192    } else {
00193        LM_ERR("invalid privacy value\n");
00194        return -1;
00195    }
00196 
00197    while(p < beyond && (*p == ' ' || *p == '\t'
00198               || *p == '\r' || *p == '\n'))
00199        p++;
00200 
00201    if(p >= beyond) break;
00202 
00203    if (*p == ';') {
00204        p++;
00205        while(p < beyond && (*p == ' ' || *p == '\t'
00206              || *p == '\r' || *p == '\n'))
00207       p++;
00208        if(p >= beyond) {
00209       LM_ERR("no privacy value after comma\n");
00210       return -1;
00211        }    
00212    } else {
00213        LM_ERR("semicolon expected\n");
00214        return -1;
00215    }
00216     }
00217 
00218     if ((values & PRIVACY_NONE) && (values ^ PRIVACY_NONE)) {
00219    LM_ERR("no other privacy values allowed with 'none'\n");
00220    return -1;
00221     }
00222 
00223     msg->privacy->parsed = (void *)(long)values;
00224 
00225     return 0;
00226 }

Generated on Thu May 24 00:00:28 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6